Mention seek effect of an unmoved --inplace chunk.
[rsync/rsync.git] / syscall.c
1 /*
2  * Syscall wrappers to ensure that nothing gets done in dry_run mode
3  * and to handle system peculiarities.
4  *
5  * Copyright (C) 1998 Andrew Tridgell
6  * Copyright (C) 2002 Martin Pool
7  * Copyright (C) 2003-2009 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, visit the http://fsf.org website.
21  */
22
23 #include "rsync.h"
24
25 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
26 #include <sys/un.h>
27 #endif
28 #ifdef HAVE_SYS_ATTR_H
29 #include <sys/attr.h>
30 #endif
31
32 extern int dry_run;
33 extern int am_root;
34 extern int am_sender;
35 extern int read_only;
36 extern int list_only;
37 extern int preserve_perms;
38 extern int preserve_executability;
39
40 #define RETURN_ERROR_IF(x,e) \
41         do { \
42                 if (x) { \
43                         errno = (e); \
44                         return -1; \
45                 } \
46         } while (0)
47
48 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
49
50 int do_unlink(const char *fname)
51 {
52         if (dry_run) return 0;
53         RETURN_ERROR_IF_RO_OR_LO;
54         return unlink(fname);
55 }
56
57 #ifdef SUPPORT_LINKS
58 int do_symlink(const char *lnk, const char *fname)
59 {
60         if (dry_run) return 0;
61         RETURN_ERROR_IF_RO_OR_LO;
62
63 #ifdef NO_SYMLINK_XATTRS
64         /* For --fake-super, we create a normal file with mode 0600
65          * and write the lnk into it. */
66         if (am_root < 0) {
67                 int ok, len = strlen(lnk);
68                 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
69                 if (fd < 0)
70                         return -1;
71                 ok = write(fd, lnk, len) == len;
72                 if (close(fd) < 0)
73                         ok = 0;
74                 return ok ? 0 : -1;
75         }
76 #endif
77
78         return symlink(lnk, fname);
79 }
80
81 #ifdef NO_SYMLINK_XATTRS
82 ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
83 {
84         /* For --fake-super, we read the link from the file. */
85         if (am_root < 0) {
86                 int fd = open(path, O_RDONLY|O_NOFOLLOW);
87                 if (fd >= 0) {
88                         int len = read(fd, buf, bufsiz);
89                         close(fd);
90                         return len;
91                 }
92                 if (errno != ELOOP)
93                         return -1;
94                 /* A real symlink needs to be turned into a fake one on the receiving
95                  * side, so tell the generator that the link has no length. */
96                 if (!am_sender)
97                         return 0;
98                 /* Otherwise fall through and let the sender report the real length. */
99         }
100
101         return readlink(path, buf, bufsiz);
102 }
103 #endif
104 #endif
105
106 #ifdef HAVE_LINK
107 int do_link(const char *fname1, const char *fname2)
108 {
109         if (dry_run) return 0;
110         RETURN_ERROR_IF_RO_OR_LO;
111         return link(fname1, fname2);
112 }
113 #endif
114
115 int do_lchown(const char *path, uid_t owner, gid_t group)
116 {
117         if (dry_run) return 0;
118         RETURN_ERROR_IF_RO_OR_LO;
119 #ifndef HAVE_LCHOWN
120 #define lchown chown
121 #endif
122         return lchown(path, owner, group);
123 }
124
125 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
126 {
127         if (dry_run) return 0;
128         RETURN_ERROR_IF_RO_OR_LO;
129
130         /* For --fake-super, we create a normal file with mode 0600. */
131         if (am_root < 0) {
132                 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
133                 if (fd < 0 || close(fd) < 0)
134                         return -1;
135                 return 0;
136         }
137
138 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
139         if (S_ISFIFO(mode))
140                 return mkfifo(pathname, mode);
141 #endif
142 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
143         if (S_ISSOCK(mode)) {
144                 int sock;
145                 struct sockaddr_un saddr;
146 #ifdef HAVE_SOCKADDR_UN_LEN
147                 unsigned int len =
148 #endif
149                     strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
150 #ifdef HAVE_SOCKADDR_UN_LEN
151                 saddr.sun_len = len >= sizeof saddr.sun_path
152                               ? sizeof saddr.sun_path : len + 1;
153 #endif
154                 saddr.sun_family = AF_UNIX;
155
156                 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
157                     || (unlink(pathname) < 0 && errno != ENOENT)
158                     || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
159                         return -1;
160                 close(sock);
161 #ifdef HAVE_CHMOD
162                 return do_chmod(pathname, mode);
163 #else
164                 return 0;
165 #endif
166         }
167 #endif
168 #ifdef HAVE_MKNOD
169         return mknod(pathname, mode, dev);
170 #else
171         return -1;
172 #endif
173 }
174
175 int do_rmdir(const char *pathname)
176 {
177         if (dry_run) return 0;
178         RETURN_ERROR_IF_RO_OR_LO;
179         return rmdir(pathname);
180 }
181
182 int do_open(const char *pathname, int flags, mode_t mode)
183 {
184         if (flags != O_RDONLY) {
185                 RETURN_ERROR_IF(dry_run, 0);
186                 RETURN_ERROR_IF_RO_OR_LO;
187         }
188
189         return open(pathname, flags | O_BINARY, mode);
190 }
191
192 #ifdef HAVE_CHMOD
193 int do_chmod(const char *path, mode_t mode)
194 {
195         int code;
196         if (dry_run) return 0;
197         RETURN_ERROR_IF_RO_OR_LO;
198 #ifdef HAVE_LCHMOD
199         code = lchmod(path, mode & CHMOD_BITS);
200 #else
201         if (S_ISLNK(mode)) {
202 # if defined HAVE_SETATTRLIST
203                 struct attrlist attrList;
204                 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
205
206                 memset(&attrList, 0, sizeof attrList);
207                 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
208                 attrList.commonattr = ATTR_CMN_ACCESSMASK;
209                 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
210 # else
211                 code = 1;
212 # endif
213         } else
214                 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
215 #endif /* !HAVE_LCHMOD */
216         if (code != 0 && (preserve_perms || preserve_executability))
217                 return code;
218         return 0;
219 }
220 #endif
221
222 int do_rename(const char *fname1, const char *fname2)
223 {
224         if (dry_run) return 0;
225         RETURN_ERROR_IF_RO_OR_LO;
226         return rename(fname1, fname2);
227 }
228
229 void trim_trailing_slashes(char *name)
230 {
231         int l;
232         /* Some BSD systems cannot make a directory if the name
233          * contains a trailing slash.
234          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
235
236         /* Don't change empty string; and also we can't improve on
237          * "/" */
238
239         l = strlen(name);
240         while (l > 1) {
241                 if (name[--l] != '/')
242                         break;
243                 name[l] = '\0';
244         }
245 }
246
247 int do_mkdir(char *fname, mode_t mode)
248 {
249         if (dry_run) return 0;
250         RETURN_ERROR_IF_RO_OR_LO;
251         trim_trailing_slashes(fname);
252         return mkdir(fname, mode);
253 }
254
255 /* like mkstemp but forces permissions */
256 int do_mkstemp(char *template, mode_t perms)
257 {
258         RETURN_ERROR_IF(dry_run, 0);
259         RETURN_ERROR_IF(read_only, EROFS);
260         perms |= S_IWUSR;
261
262 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
263         {
264                 int fd = mkstemp(template);
265                 if (fd == -1)
266                         return -1;
267                 if (fchmod(fd, perms) != 0 && preserve_perms) {
268                         int errno_save = errno;
269                         close(fd);
270                         unlink(template);
271                         errno = errno_save;
272                         return -1;
273                 }
274 #if defined HAVE_SETMODE && O_BINARY
275                 setmode(fd, O_BINARY);
276 #endif
277                 return fd;
278         }
279 #else
280         if (!mktemp(template))
281                 return -1;
282         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
283 #endif
284 }
285
286 int do_stat(const char *fname, STRUCT_STAT *st)
287 {
288 #ifdef USE_STAT64_FUNCS
289         return stat64(fname, st);
290 #else
291         return stat(fname, st);
292 #endif
293 }
294
295 int do_lstat(const char *fname, STRUCT_STAT *st)
296 {
297 #ifdef SUPPORT_LINKS
298 # ifdef USE_STAT64_FUNCS
299         return lstat64(fname, st);
300 # else
301         return lstat(fname, st);
302 # endif
303 #else
304         return do_stat(fname, st);
305 #endif
306 }
307
308 int do_fstat(int fd, STRUCT_STAT *st)
309 {
310 #ifdef USE_STAT64_FUNCS
311         return fstat64(fd, st);
312 #else
313         return fstat(fd, st);
314 #endif
315 }
316
317 OFF_T do_lseek(int fd, OFF_T offset, int whence)
318 {
319 #ifdef HAVE_LSEEK64
320 #if !SIZEOF_OFF64_T
321         OFF_T lseek64();
322 #else
323         off64_t lseek64();
324 #endif
325         return lseek64(fd, offset, whence);
326 #else
327         return lseek(fd, offset, whence);
328 #endif
329 }
330
331 #ifdef HAVE_UTIMENSAT
332 int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
333 {
334         struct timespec t[2];
335
336         if (dry_run) return 0;
337         RETURN_ERROR_IF_RO_OR_LO;
338
339         t[0].tv_sec = 0;
340         t[0].tv_nsec = UTIME_NOW;
341         t[1].tv_sec = modtime;
342         t[1].tv_nsec = mod_nsec;
343         return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
344 }
345 #endif
346
347 #ifdef HAVE_LUTIMES
348 int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
349 {
350         struct timeval t[2];
351
352         if (dry_run) return 0;
353         RETURN_ERROR_IF_RO_OR_LO;
354
355         t[0].tv_sec = time(NULL);
356         t[0].tv_usec = 0;
357         t[1].tv_sec = modtime;
358         t[1].tv_usec = mod_nsec / 1000;
359         return lutimes(fname, t);
360 }
361 #endif
362
363 #ifdef HAVE_UTIMES
364 int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
365 {
366         struct timeval t[2];
367
368         if (dry_run) return 0;
369         RETURN_ERROR_IF_RO_OR_LO;
370
371         t[0].tv_sec = time(NULL);
372         t[0].tv_usec = 0;
373         t[1].tv_sec = modtime;
374         t[1].tv_usec = mod_nsec / 1000;
375         return utimes(fname, t);
376 }
377
378 #elif defined HAVE_UTIME
379 int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
380 {
381 #ifdef HAVE_STRUCT_UTIMBUF
382         struct utimbuf tbuf;
383 #else
384         time_t t[2];
385 #endif
386
387         if (dry_run) return 0;
388         RETURN_ERROR_IF_RO_OR_LO;
389
390 # ifdef HAVE_STRUCT_UTIMBUF
391         tbuf.actime = time(NULL);
392         tbuf.modtime = modtime;
393         return utime(fname, &tbuf);
394 # else
395         t[0] = time(NULL);
396         t[1] = modtime;
397         return utime(fname, t);
398 # endif
399 }
400
401 #else
402 #error Need utimes or utime function.
403 #endif