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-2009 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
37 extern int preserve_perms;
38 extern int preserve_executability;
40 #define RETURN_ERROR_IF(x,e) \
48 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
50 int do_unlink(const char *fname)
52 if (dry_run) return 0;
53 RETURN_ERROR_IF_RO_OR_LO;
58 int do_symlink(const char *lnk, const char *fname)
60 if (dry_run) return 0;
61 RETURN_ERROR_IF_RO_OR_LO;
63 #ifdef NO_SYMLINK_XATTRS
64 /* For --fake-super, we create a normal file with mode 0600
65 * and write the lnk into it. */
67 int ok, len = strlen(lnk);
68 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
71 ok = write(fd, lnk, len) == len;
78 return symlink(lnk, fname);
81 #ifdef NO_SYMLINK_XATTRS
82 ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
84 /* For --fake-super, we read the link from the file. */
86 int fd = open(path, O_RDONLY|O_NOFOLLOW);
88 int len = read(fd, buf, bufsiz);
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. */
98 /* Otherwise fall through and let the sender report the real length. */
101 return readlink(path, buf, bufsiz);
107 int do_link(const char *fname1, const char *fname2)
109 if (dry_run) return 0;
110 RETURN_ERROR_IF_RO_OR_LO;
111 return link(fname1, fname2);
115 int do_lchown(const char *path, uid_t owner, gid_t group)
117 if (dry_run) return 0;
118 RETURN_ERROR_IF_RO_OR_LO;
122 return lchown(path, owner, group);
125 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
127 if (dry_run) return 0;
128 RETURN_ERROR_IF_RO_OR_LO;
130 /* For --fake-super, we create a normal file with mode 0600. */
132 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
133 if (fd < 0 || close(fd) < 0)
138 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
140 return mkfifo(pathname, mode);
142 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
143 if (S_ISSOCK(mode)) {
145 struct sockaddr_un saddr;
146 #ifdef HAVE_SOCKADDR_UN_LEN
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;
154 saddr.sun_family = AF_UNIX;
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)
162 return do_chmod(pathname, mode);
169 return mknod(pathname, mode, dev);
175 int do_rmdir(const char *pathname)
177 if (dry_run) return 0;
178 RETURN_ERROR_IF_RO_OR_LO;
179 return rmdir(pathname);
182 int do_open(const char *pathname, int flags, mode_t mode)
184 if (flags != O_RDONLY) {
185 RETURN_ERROR_IF(dry_run, 0);
186 RETURN_ERROR_IF_RO_OR_LO;
189 return open(pathname, flags | O_BINARY, mode);
193 int do_chmod(const char *path, mode_t mode)
196 if (dry_run) return 0;
197 RETURN_ERROR_IF_RO_OR_LO;
200 code = lchmod(path, mode & CHMOD_BITS);
201 #elif defined HAVE_SETATTRLIST
202 struct attrlist attrList;
203 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
205 memset(&attrList, 0, sizeof attrList);
206 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
207 attrList.commonattr = ATTR_CMN_ACCESSMASK;
208 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
213 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
214 if (code != 0 && (preserve_perms || preserve_executability))
220 int do_rename(const char *fname1, const char *fname2)
222 if (dry_run) return 0;
223 RETURN_ERROR_IF_RO_OR_LO;
224 return rename(fname1, fname2);
227 void trim_trailing_slashes(char *name)
230 /* Some BSD systems cannot make a directory if the name
231 * contains a trailing slash.
232 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
234 /* Don't change empty string; and also we can't improve on
239 if (name[--l] != '/')
245 int do_mkdir(char *fname, mode_t mode)
247 if (dry_run) return 0;
248 RETURN_ERROR_IF_RO_OR_LO;
249 trim_trailing_slashes(fname);
250 return mkdir(fname, mode);
253 /* like mkstemp but forces permissions */
254 int do_mkstemp(char *template, mode_t perms)
256 RETURN_ERROR_IF(dry_run, 0);
257 RETURN_ERROR_IF(read_only, EROFS);
260 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
262 int fd = mkstemp(template);
265 if (fchmod(fd, perms) != 0 && preserve_perms) {
266 int errno_save = errno;
272 #if defined HAVE_SETMODE && O_BINARY
273 setmode(fd, O_BINARY);
278 if (!mktemp(template))
280 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
284 int do_stat(const char *fname, STRUCT_STAT *st)
286 #ifdef USE_STAT64_FUNCS
287 return stat64(fname, st);
289 return stat(fname, st);
293 int do_lstat(const char *fname, STRUCT_STAT *st)
296 # ifdef USE_STAT64_FUNCS
297 return lstat64(fname, st);
299 return lstat(fname, st);
302 return do_stat(fname, st);
306 int do_fstat(int fd, STRUCT_STAT *st)
308 #ifdef USE_STAT64_FUNCS
309 return fstat64(fd, st);
311 return fstat(fd, st);
315 OFF_T do_lseek(int fd, OFF_T offset, int whence)
323 return lseek64(fd, offset, whence);
325 return lseek(fd, offset, whence);