Fix --remove-source-files sanity check w/--copy-links the right way.
[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-2018 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 #if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
33 #include <sys/syscall.h>
34 #endif
35
36 extern int dry_run;
37 extern int am_root;
38 extern int am_sender;
39 extern int read_only;
40 extern int list_only;
41 extern int inplace;
42 extern int preallocate_files;
43 extern int preserve_perms;
44 extern int preserve_executability;
45
46 #ifndef S_BLKSIZE
47 # if defined hpux || defined __hpux__ || defined __hpux
48 #  define S_BLKSIZE 1024
49 # elif defined _AIX && defined _I386
50 #  define S_BLKSIZE 4096
51 # else
52 #  define S_BLKSIZE 512
53 # endif
54 #endif
55
56 #define RETURN_ERROR_IF(x,e) \
57         do { \
58                 if (x) { \
59                         errno = (e); \
60                         return -1; \
61                 } \
62         } while (0)
63
64 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
65
66 int do_unlink(const char *fname)
67 {
68         if (dry_run) return 0;
69         RETURN_ERROR_IF_RO_OR_LO;
70         return unlink(fname);
71 }
72
73 #ifdef SUPPORT_LINKS
74 int do_symlink(const char *lnk, const char *fname)
75 {
76         if (dry_run) return 0;
77         RETURN_ERROR_IF_RO_OR_LO;
78
79 #if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
80         /* For --fake-super, we create a normal file with mode 0600
81          * and write the lnk into it. */
82         if (am_root < 0) {
83                 int ok, len = strlen(lnk);
84                 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
85                 if (fd < 0)
86                         return -1;
87                 ok = write(fd, lnk, len) == len;
88                 if (close(fd) < 0)
89                         ok = 0;
90                 return ok ? 0 : -1;
91         }
92 #endif
93
94         return symlink(lnk, fname);
95 }
96
97 #if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
98 ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
99 {
100         /* For --fake-super, we read the link from the file. */
101         if (am_root < 0) {
102                 int fd = do_open_nofollow(path, O_RDONLY);
103                 if (fd >= 0) {
104                         int len = read(fd, buf, bufsiz);
105                         close(fd);
106                         return len;
107                 }
108                 if (errno != ELOOP)
109                         return -1;
110                 /* A real symlink needs to be turned into a fake one on the receiving
111                  * side, so tell the generator that the link has no length. */
112                 if (!am_sender)
113                         return 0;
114                 /* Otherwise fall through and let the sender report the real length. */
115         }
116
117         return readlink(path, buf, bufsiz);
118 }
119 #endif
120 #endif
121
122 #ifdef HAVE_LINK
123 int do_link(const char *fname1, const char *fname2)
124 {
125         if (dry_run) return 0;
126         RETURN_ERROR_IF_RO_OR_LO;
127         return link(fname1, fname2);
128 }
129 #endif
130
131 int do_lchown(const char *path, uid_t owner, gid_t group)
132 {
133         if (dry_run) return 0;
134         RETURN_ERROR_IF_RO_OR_LO;
135 #ifndef HAVE_LCHOWN
136 #define lchown chown
137 #endif
138         return lchown(path, owner, group);
139 }
140
141 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
142 {
143         if (dry_run) return 0;
144         RETURN_ERROR_IF_RO_OR_LO;
145
146         /* For --fake-super, we create a normal file with mode 0600. */
147         if (am_root < 0) {
148                 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
149                 if (fd < 0 || close(fd) < 0)
150                         return -1;
151                 return 0;
152         }
153
154 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
155         if (S_ISFIFO(mode))
156                 return mkfifo(pathname, mode);
157 #endif
158 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
159         if (S_ISSOCK(mode)) {
160                 int sock;
161                 struct sockaddr_un saddr;
162                 unsigned int len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
163                 if (len >= sizeof saddr.sun_path) {
164                         errno = ENAMETOOLONG;
165                         return -1;
166                 }
167 #ifdef HAVE_SOCKADDR_UN_LEN
168                 saddr.sun_len = len + 1;
169 #endif
170                 saddr.sun_family = AF_UNIX;
171
172                 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
173                     || (unlink(pathname) < 0 && errno != ENOENT)
174                     || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
175                         return -1;
176                 close(sock);
177 #ifdef HAVE_CHMOD
178                 return do_chmod(pathname, mode);
179 #else
180                 return 0;
181 #endif
182         }
183 #endif
184 #ifdef HAVE_MKNOD
185         return mknod(pathname, mode, dev);
186 #else
187         return -1;
188 #endif
189 }
190
191 int do_rmdir(const char *pathname)
192 {
193         if (dry_run) return 0;
194         RETURN_ERROR_IF_RO_OR_LO;
195         return rmdir(pathname);
196 }
197
198 int do_open(const char *pathname, int flags, mode_t mode)
199 {
200         if (flags != O_RDONLY) {
201                 RETURN_ERROR_IF(dry_run, 0);
202                 RETURN_ERROR_IF_RO_OR_LO;
203         }
204
205         return open(pathname, flags | O_BINARY, mode);
206 }
207
208 #ifdef HAVE_CHMOD
209 int do_chmod(const char *path, mode_t mode)
210 {
211         int code;
212         if (dry_run) return 0;
213         RETURN_ERROR_IF_RO_OR_LO;
214 #ifdef HAVE_LCHMOD
215         code = lchmod(path, mode & CHMOD_BITS);
216 #else
217         if (S_ISLNK(mode)) {
218 # if defined HAVE_SETATTRLIST
219                 struct attrlist attrList;
220                 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
221
222                 memset(&attrList, 0, sizeof attrList);
223                 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
224                 attrList.commonattr = ATTR_CMN_ACCESSMASK;
225                 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
226 # else
227                 code = 1;
228 # endif
229         } else
230                 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
231 #endif /* !HAVE_LCHMOD */
232         if (code != 0 && (preserve_perms || preserve_executability))
233                 return code;
234         return 0;
235 }
236 #endif
237
238 int do_rename(const char *fname1, const char *fname2)
239 {
240         if (dry_run) return 0;
241         RETURN_ERROR_IF_RO_OR_LO;
242         return rename(fname1, fname2);
243 }
244
245 #ifdef HAVE_FTRUNCATE
246 int do_ftruncate(int fd, OFF_T size)
247 {
248         int ret;
249
250         if (dry_run) return 0;
251         RETURN_ERROR_IF_RO_OR_LO;
252
253         do {
254                 ret = ftruncate(fd, size);
255         } while (ret < 0 && errno == EINTR);
256
257         return ret;
258 }
259 #endif
260
261 void trim_trailing_slashes(char *name)
262 {
263         int l;
264         /* Some BSD systems cannot make a directory if the name
265          * contains a trailing slash.
266          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
267
268         /* Don't change empty string; and also we can't improve on
269          * "/" */
270
271         l = strlen(name);
272         while (l > 1) {
273                 if (name[--l] != '/')
274                         break;
275                 name[l] = '\0';
276         }
277 }
278
279 int do_mkdir(char *fname, mode_t mode)
280 {
281         if (dry_run) return 0;
282         RETURN_ERROR_IF_RO_OR_LO;
283         trim_trailing_slashes(fname);
284         return mkdir(fname, mode);
285 }
286
287 /* like mkstemp but forces permissions */
288 int do_mkstemp(char *template, mode_t perms)
289 {
290         RETURN_ERROR_IF(dry_run, 0);
291         RETURN_ERROR_IF(read_only, EROFS);
292         perms |= S_IWUSR;
293
294 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
295         {
296                 int fd = mkstemp(template);
297                 if (fd == -1)
298                         return -1;
299                 if (fchmod(fd, perms) != 0 && preserve_perms) {
300                         int errno_save = errno;
301                         close(fd);
302                         unlink(template);
303                         errno = errno_save;
304                         return -1;
305                 }
306 #if defined HAVE_SETMODE && O_BINARY
307                 setmode(fd, O_BINARY);
308 #endif
309                 return fd;
310         }
311 #else
312         if (!mktemp(template))
313                 return -1;
314         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
315 #endif
316 }
317
318 int do_stat(const char *fname, STRUCT_STAT *st)
319 {
320 #ifdef USE_STAT64_FUNCS
321         return stat64(fname, st);
322 #else
323         return stat(fname, st);
324 #endif
325 }
326
327 int do_lstat(const char *fname, STRUCT_STAT *st)
328 {
329 #ifdef SUPPORT_LINKS
330 # ifdef USE_STAT64_FUNCS
331         return lstat64(fname, st);
332 # else
333         return lstat(fname, st);
334 # endif
335 #else
336         return do_stat(fname, st);
337 #endif
338 }
339
340 int do_fstat(int fd, STRUCT_STAT *st)
341 {
342 #ifdef USE_STAT64_FUNCS
343         return fstat64(fd, st);
344 #else
345         return fstat(fd, st);
346 #endif
347 }
348
349 OFF_T do_lseek(int fd, OFF_T offset, int whence)
350 {
351 #ifdef HAVE_LSEEK64
352 #if !SIZEOF_OFF64_T
353         OFF_T lseek64();
354 #else
355         off64_t lseek64();
356 #endif
357         return lseek64(fd, offset, whence);
358 #else
359         return lseek(fd, offset, whence);
360 #endif
361 }
362
363 #ifdef HAVE_SETATTRLIST
364 int do_setattrlist_times(const char *fname, time_t modtime, uint32 mod_nsec)
365 {
366         struct attrlist attrList;
367         struct timespec ts;
368
369         if (dry_run) return 0;
370         RETURN_ERROR_IF_RO_OR_LO;
371
372         ts.tv_sec = modtime;
373         ts.tv_nsec = mod_nsec;
374
375         memset(&attrList, 0, sizeof attrList);
376         attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
377         attrList.commonattr = ATTR_CMN_MODTIME;
378         return setattrlist(fname, &attrList, &ts, sizeof ts, FSOPT_NOFOLLOW);
379 }
380 #endif
381
382 #ifdef HAVE_UTIMENSAT
383 int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
384 {
385         struct timespec t[2];
386
387         if (dry_run) return 0;
388         RETURN_ERROR_IF_RO_OR_LO;
389
390         t[0].tv_sec = 0;
391         t[0].tv_nsec = UTIME_NOW;
392         t[1].tv_sec = modtime;
393         t[1].tv_nsec = mod_nsec;
394         return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
395 }
396 #endif
397
398 #ifdef HAVE_LUTIMES
399 int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
400 {
401         struct timeval t[2];
402
403         if (dry_run) return 0;
404         RETURN_ERROR_IF_RO_OR_LO;
405
406         t[0].tv_sec = time(NULL);
407         t[0].tv_usec = 0;
408         t[1].tv_sec = modtime;
409         t[1].tv_usec = mod_nsec / 1000;
410         return lutimes(fname, t);
411 }
412 #endif
413
414 #ifdef HAVE_UTIMES
415 int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
416 {
417         struct timeval t[2];
418
419         if (dry_run) return 0;
420         RETURN_ERROR_IF_RO_OR_LO;
421
422         t[0].tv_sec = time(NULL);
423         t[0].tv_usec = 0;
424         t[1].tv_sec = modtime;
425         t[1].tv_usec = mod_nsec / 1000;
426         return utimes(fname, t);
427 }
428
429 #elif defined HAVE_UTIME
430 int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
431 {
432 #ifdef HAVE_STRUCT_UTIMBUF
433         struct utimbuf tbuf;
434 #else
435         time_t t[2];
436 #endif
437
438         if (dry_run) return 0;
439         RETURN_ERROR_IF_RO_OR_LO;
440
441 # ifdef HAVE_STRUCT_UTIMBUF
442         tbuf.actime = time(NULL);
443         tbuf.modtime = modtime;
444         return utime(fname, &tbuf);
445 # else
446         t[0] = time(NULL);
447         t[1] = modtime;
448         return utime(fname, t);
449 # endif
450 }
451
452 #else
453 #error Need utimes or utime function.
454 #endif
455
456 #ifdef SUPPORT_PREALLOCATION
457 #ifdef FALLOC_FL_KEEP_SIZE
458 #define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
459 #else
460 #define DO_FALLOC_OPTIONS 0
461 #endif
462
463 OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length)
464 {
465         int opts = inplace || preallocate_files ? DO_FALLOC_OPTIONS : 0;
466         int ret;
467         RETURN_ERROR_IF(dry_run, 0);
468         RETURN_ERROR_IF_RO_OR_LO;
469         if (length & 1) /* make the length not match the desired length */
470                 length++;
471         else
472                 length--;
473 #if defined HAVE_FALLOCATE
474         ret = fallocate(fd, opts, offset, length);
475 #elif defined HAVE_SYS_FALLOCATE
476         ret = syscall(SYS_fallocate, fd, opts, (loff_t)offset, (loff_t)length);
477 #elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
478         ret = posix_fallocate(fd, offset, length);
479 #else
480 #error Coding error in SUPPORT_PREALLOCATION logic.
481 #endif
482         if (ret < 0)
483                 return ret;
484         if (opts == 0) {
485                 STRUCT_STAT st;
486                 if (do_fstat(fd, &st) < 0)
487                         return length;
488                 return st.st_blocks * S_BLKSIZE;
489         }
490         return 0;
491 }
492 #endif
493
494 /* Punch a hole at pos for len bytes. The current file position must be at pos and will be
495  * changed to be at pos + len. */
496 int do_punch_hole(int fd, UNUSED(OFF_T pos), int len)
497 {
498 #ifdef HAVE_FALLOCATE
499 # ifdef HAVE_FALLOC_FL_PUNCH_HOLE
500         if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos, len) == 0) {
501                 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
502                         return -1;
503                 return 0;
504         }
505 # endif
506 # ifdef HAVE_FALLOC_FL_ZERO_RANGE
507         if (fallocate(fd, FALLOC_FL_ZERO_RANGE, pos, len) == 0) {
508                 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
509                         return -1;
510                 return 0;
511         }
512 # endif
513 #endif
514         {
515                 char zeros[4096];
516                 memset(zeros, 0, sizeof zeros);
517                 while (len > 0) {
518                         int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len;
519                         int wrote = write(fd, zeros, chunk);
520                         if (wrote <= 0) {
521                                 if (wrote < 0 && errno == EINTR)
522                                         continue;
523                                 return -1;
524                         }
525                         len -= wrote;
526                 }
527         }
528         return 0;
529 }
530
531 int do_open_nofollow(const char *pathname, int flags)
532 {
533 #ifndef O_NOFOLLOW
534         STRUCT_STAT f_st, l_st;
535 #endif
536         int fd;
537
538         if (flags != O_RDONLY) {
539                 RETURN_ERROR_IF(dry_run, 0);
540                 RETURN_ERROR_IF_RO_OR_LO;
541 #ifndef O_NOFOLLOW
542                 /* This function doesn't support write attempts w/o O_NOFOLLOW. */
543                 errno = EINVAL;
544                 return -1;
545 #endif
546         }
547
548 #ifdef O_NOFOLLOW
549         fd = open(pathname, flags|O_NOFOLLOW);
550 #else
551         if (do_lstat(pathname, &l_st) < 0)
552                 return -1;
553         if (S_ISLNK(l_st.st_mode)) {
554                 errno = ELOOP;
555                 return -1;
556         }
557         if ((fd = open(pathname, flags)) < 0)
558                 return fd;
559         if (do_fstat(fd, &f_st) < 0) {
560           close_and_return_error:
561                 {
562                         int save_errno = errno;
563                         close(fd);
564                         errno = save_errno;
565                 }
566                 return -1;
567         }
568         if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) {
569                 errno = EINVAL;
570                 goto close_and_return_error;
571         }
572 #endif
573
574         return fd;
575 }