s3-includes: only include system/filesys.h when needed.
[ira/wip.git] / source3 / modules / vfs_crossrename.c
1 /*
2  * Copyright (c) Björn Jacke 2010
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "includes.h"
19 #include "system/filesys.h"
20
21
22 #define MODULE "crossrename"
23 static SMB_OFF_T module_sizelimit;
24
25 static int crossrename_connect(
26                 struct vfs_handle_struct *  handle,
27                 const char *                service,
28                 const char *                user)
29 {
30         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
31
32         if (ret < 0) {
33                 return ret;
34         }
35
36         module_sizelimit = (SMB_OFF_T) lp_parm_int(SNUM(handle->conn),
37                                         MODULE, "sizelimit", 20);
38         /* convert from MiB to byte: */
39         module_sizelimit *= 1048576;
40
41         return 0;
42 }
43
44 /*********************************************************
45  For rename across filesystems initial Patch from Warren Birnbaum
46  <warrenb@hpcvscdp.cv.hp.com>
47 **********************************************************/
48
49 static int copy_reg(const char *source, const char *dest)
50 {
51         SMB_STRUCT_STAT source_stats;
52         int saved_errno;
53         int ifd = -1;
54         int ofd = -1;
55
56         if (sys_lstat(source, &source_stats, false) == -1)
57                 return -1;
58
59         if (!S_ISREG (source_stats.st_ex_mode))
60                 return -1;
61
62         if (source_stats.st_ex_size > module_sizelimit) {
63                 DEBUG(5,
64                         ("%s: size of %s larger than sizelimit (%lld > %lld), rename prohititted\n",
65                         MODULE, source,
66                         (long long)source_stats.st_ex_size,
67                         (long long)module_sizelimit));
68                 return -1;
69         }
70
71         if((ifd = sys_open (source, O_RDONLY, 0)) < 0)
72                 return -1;
73
74         if (unlink (dest) && errno != ENOENT)
75                 return -1;
76
77 #ifdef O_NOFOLLOW
78         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 )
79 #else
80         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 )
81 #endif
82                 goto err;
83
84         if (transfer_file(ifd, ofd, source_stats.st_ex_size) == -1)
85                 goto err;
86
87         /*
88          * Try to preserve ownership.  For non-root it might fail, but that's ok.
89          * But root probably wants to know, e.g. if NFS disallows it.
90          */
91
92 #ifdef HAVE_FCHOWN
93         if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
94 #else
95         if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
96 #endif
97                 goto err;
98
99         /*
100          * fchown turns off set[ug]id bits for non-root,
101          * so do the chmod last.
102          */
103
104 #if defined(HAVE_FCHMOD)
105         if (fchmod (ofd, source_stats.st_ex_mode & 07777))
106 #else
107         if (chmod (dest, source_stats.st_ex_mode & 07777))
108 #endif
109                 goto err;
110
111         if (close (ifd) == -1)
112                 goto err;
113
114         if (close (ofd) == -1)
115                 return -1;
116
117         /* Try to copy the old file's modtime and access time.  */
118 #if defined(HAVE_UTIMENSAT)
119         {
120                 struct timespec ts[2];
121
122                 ts[0] = source_stats.st_ex_atime;
123                 ts[1] = source_stats.st_ex_mtime;
124                 utimensat(AT_FDCWD, dest, ts, AT_SYMLINK_NOFOLLOW);
125         }
126 #elif defined(HAVE_UTIMES)
127         {
128                 struct timeval tv[2];
129
130                 tv[0] = convert_timespec_to_timeval(source_stats.st_ex_atime);
131                 tv[1] = convert_timespec_to_timeval(source_stats.st_ex_mtime);
132 #ifdef HAVE_LUTIMES
133                 lutimes(dest, tv);
134 #else
135                 utimes(dest, tv);
136 #endif
137         }
138 #elif defined(HAVE_UTIME)
139         {
140                 struct utimbuf tv;
141
142                 tv.actime = convert_timespec_to_time_t(source_stats.st_ex_atime);
143                 tv.modtime = convert_timespec_to_time_t(source_stats.st_ex_mtime);
144                 utime(dest, &tv);
145         }
146 #endif
147
148         if (unlink (source) == -1)
149                 return -1;
150
151         return 0;
152
153   err:
154
155         saved_errno = errno;
156         if (ifd != -1)
157                 close(ifd);
158         if (ofd != -1)
159                 close(ofd);
160         errno = saved_errno;
161         return -1;
162 }
163
164
165 static int crossrename_rename(vfs_handle_struct *handle,
166                           const struct smb_filename *smb_fname_src,
167                           const struct smb_filename *smb_fname_dst)
168 {
169         int result = -1;
170
171         START_PROFILE(syscall_rename);
172
173         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
174                 errno = ENOENT;
175                 goto out;
176         }
177
178         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
179         if ((result == -1) && (errno == EXDEV)) {
180                 /* Rename across filesystems needed. */
181                 result = copy_reg(smb_fname_src->base_name,
182                                   smb_fname_dst->base_name);
183         }
184
185  out:
186         END_PROFILE(syscall_rename);
187         return result;
188 }
189
190 static struct vfs_fn_pointers vfs_crossrename_fns = {
191         .connect_fn = crossrename_connect,
192         .rename = crossrename_rename
193 };
194
195 NTSTATUS vfs_crossrename_init(void);
196 NTSTATUS vfs_crossrename_init(void)
197 {
198         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
199                                 &vfs_crossrename_fns);
200 }
201