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