2 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003-2015 Wayne Davison
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.
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.
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.
25 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
28 #ifdef HAVE_SYS_ATTR_H
32 #if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
33 #include <sys/syscall.h>
42 extern int preallocate_files;
43 extern int preserve_perms;
44 extern int preserve_executability;
47 # if defined hpux || defined __hpux__ || defined __hpux
48 # define S_BLKSIZE 1024
49 # elif defined _AIX && defined _I386
50 # define S_BLKSIZE 4096
52 # define S_BLKSIZE 512
56 #define RETURN_ERROR_IF(x,e) \
64 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
66 int do_unlink(const char *fname)
68 if (dry_run) return 0;
69 RETURN_ERROR_IF_RO_OR_LO;
74 int do_symlink(const char *lnk, const char *fname)
76 if (dry_run) return 0;
77 RETURN_ERROR_IF_RO_OR_LO;
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. */
83 int ok, len = strlen(lnk);
84 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
87 ok = write(fd, lnk, len) == len;
94 return symlink(lnk, fname);
97 #if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
98 ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
100 /* For --fake-super, we read the link from the file. */
102 int fd = do_open_nofollow(path, O_RDONLY);
104 int len = read(fd, buf, bufsiz);
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. */
114 /* Otherwise fall through and let the sender report the real length. */
117 return readlink(path, buf, bufsiz);
123 int do_link(const char *fname1, const char *fname2)
125 if (dry_run) return 0;
126 RETURN_ERROR_IF_RO_OR_LO;
127 return link(fname1, fname2);
131 int do_lchown(const char *path, uid_t owner, gid_t group)
133 if (dry_run) return 0;
134 RETURN_ERROR_IF_RO_OR_LO;
138 return lchown(path, owner, group);
141 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
143 if (dry_run) return 0;
144 RETURN_ERROR_IF_RO_OR_LO;
146 /* For --fake-super, we create a normal file with mode 0600. */
148 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
149 if (fd < 0 || close(fd) < 0)
154 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
156 return mkfifo(pathname, mode);
158 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
159 if (S_ISSOCK(mode)) {
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;
167 #ifdef HAVE_SOCKADDR_UN_LEN
168 saddr.sun_len = len + 1;
170 saddr.sun_family = AF_UNIX;
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)
178 return do_chmod(pathname, mode);
185 return mknod(pathname, mode, dev);
191 int do_rmdir(const char *pathname)
193 if (dry_run) return 0;
194 RETURN_ERROR_IF_RO_OR_LO;
195 return rmdir(pathname);
198 int do_open(const char *pathname, int flags, mode_t mode)
200 if (flags != O_RDONLY) {
201 RETURN_ERROR_IF(dry_run, 0);
202 RETURN_ERROR_IF_RO_OR_LO;
205 return open(pathname, flags | O_BINARY, mode);
209 int do_chmod(const char *path, mode_t mode)
212 if (dry_run) return 0;
213 RETURN_ERROR_IF_RO_OR_LO;
215 code = lchmod(path, mode & CHMOD_BITS);
218 # if defined HAVE_SETATTRLIST
219 struct attrlist attrList;
220 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
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);
230 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
231 #endif /* !HAVE_LCHMOD */
232 if (code != 0 && (preserve_perms || preserve_executability))
238 int do_rename(const char *fname1, const char *fname2)
240 if (dry_run) return 0;
241 RETURN_ERROR_IF_RO_OR_LO;
242 return rename(fname1, fname2);
245 #ifdef HAVE_FTRUNCATE
246 int do_ftruncate(int fd, OFF_T size)
250 if (dry_run) return 0;
251 RETURN_ERROR_IF_RO_OR_LO;
254 ret = ftruncate(fd, size);
255 } while (ret < 0 && errno == EINTR);
261 void trim_trailing_slashes(char *name)
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> */
268 /* Don't change empty string; and also we can't improve on
273 if (name[--l] != '/')
279 int do_mkdir(char *fname, mode_t mode)
281 if (dry_run) return 0;
282 RETURN_ERROR_IF_RO_OR_LO;
283 trim_trailing_slashes(fname);
284 return mkdir(fname, mode);
287 /* like mkstemp but forces permissions */
288 int do_mkstemp(char *template, mode_t perms)
290 RETURN_ERROR_IF(dry_run, 0);
291 RETURN_ERROR_IF(read_only, EROFS);
294 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
296 int fd = mkstemp(template);
299 if (fchmod(fd, perms) != 0 && preserve_perms) {
300 int errno_save = errno;
306 #if defined HAVE_SETMODE && O_BINARY
307 setmode(fd, O_BINARY);
312 if (!mktemp(template))
314 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
318 int do_stat(const char *fname, STRUCT_STAT *st)
320 #ifdef USE_STAT64_FUNCS
321 return stat64(fname, st);
323 return stat(fname, st);
327 int do_lstat(const char *fname, STRUCT_STAT *st)
330 # ifdef USE_STAT64_FUNCS
331 return lstat64(fname, st);
333 return lstat(fname, st);
336 return do_stat(fname, st);
340 int do_fstat(int fd, STRUCT_STAT *st)
342 #ifdef USE_STAT64_FUNCS
343 return fstat64(fd, st);
345 return fstat(fd, st);
349 OFF_T do_lseek(int fd, OFF_T offset, int whence)
357 return lseek64(fd, offset, whence);
359 return lseek(fd, offset, whence);
363 #ifdef HAVE_SETATTRLIST
364 int do_setattrlist_times(const char *fname, time_t modtime, uint32 mod_nsec)
366 struct attrlist attrList;
369 if (dry_run) return 0;
370 RETURN_ERROR_IF_RO_OR_LO;
373 ts.tv_nsec = mod_nsec;
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);
382 #ifdef HAVE_UTIMENSAT
383 int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
385 struct timespec t[2];
387 if (dry_run) return 0;
388 RETURN_ERROR_IF_RO_OR_LO;
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);
399 int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
403 if (dry_run) return 0;
404 RETURN_ERROR_IF_RO_OR_LO;
406 t[0].tv_sec = time(NULL);
408 t[1].tv_sec = modtime;
409 t[1].tv_usec = mod_nsec / 1000;
410 return lutimes(fname, t);
415 int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
419 if (dry_run) return 0;
420 RETURN_ERROR_IF_RO_OR_LO;
422 t[0].tv_sec = time(NULL);
424 t[1].tv_sec = modtime;
425 t[1].tv_usec = mod_nsec / 1000;
426 return utimes(fname, t);
429 #elif defined HAVE_UTIME
430 int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
432 #ifdef HAVE_STRUCT_UTIMBUF
438 if (dry_run) return 0;
439 RETURN_ERROR_IF_RO_OR_LO;
441 # ifdef HAVE_STRUCT_UTIMBUF
442 tbuf.actime = time(NULL);
443 tbuf.modtime = modtime;
444 return utime(fname, &tbuf);
448 return utime(fname, t);
453 #error Need utimes or utime function.
456 #ifdef SUPPORT_PREALLOCATION
457 #ifdef FALLOC_FL_KEEP_SIZE
458 #define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
460 #define DO_FALLOC_OPTIONS 0
463 OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length)
465 int opts = inplace || preallocate_files ? 0 : DO_FALLOC_OPTIONS;
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 */
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);
480 #error Coding error in SUPPORT_PREALLOCATION logic.
486 if (do_fstat(fd, &st) < 0)
488 return st.st_blocks * S_BLKSIZE;
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)
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)
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)
516 memset(zeros, 0, sizeof zeros);
518 int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len;
519 int wrote = write(fd, zeros, chunk);
521 if (wrote < 0 && errno == EINTR)
531 int do_open_nofollow(const char *pathname, int flags)
534 STRUCT_STAT f_st, l_st;
538 if (flags != O_RDONLY) {
539 RETURN_ERROR_IF(dry_run, 0);
540 RETURN_ERROR_IF_RO_OR_LO;
542 /* This function doesn't support write attempts w/o O_NOFOLLOW. */
549 fd = open(pathname, flags|O_NOFOLLOW);
551 if (do_lstat(pathname, &l_st) < 0)
553 if (S_ISLNK(l_st.st_mode)) {
557 if ((fd = open(pathname, flags)) < 0)
559 if (do_fstat(fd, &f_st) < 0) {
560 close_and_return_error:
562 int save_errno = errno;
568 if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) {
570 goto close_and_return_error;