Commit | Line | Data |
---|---|---|
0f78b815 | 1 | /* |
ded8347d MP |
2 | * Syscall wrappers to ensure that nothing gets done in dry_run mode |
3 | * and to handle system peculiarities. | |
0f78b815 WD |
4 | * |
5 | * Copyright (C) 1998 Andrew Tridgell | |
6 | * Copyright (C) 2002 Martin Pool | |
b3bf9b9d | 7 | * Copyright (C) 2003-2009 Wayne Davison |
0f78b815 WD |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | * | |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 21 | */ |
31e12522 AT |
22 | |
23 | #include "rsync.h" | |
24 | ||
4f5b0756 | 25 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
da6eb9d1 WD |
26 | #include <sys/un.h> |
27 | #endif | |
11b02d92 WD |
28 | #ifdef HAVE_SYS_ATTR_H |
29 | #include <sys/attr.h> | |
30 | #endif | |
da6eb9d1 | 31 | |
31e12522 | 32 | extern int dry_run; |
9439c0cb | 33 | extern int am_root; |
6e310d38 | 34 | extern int am_sender; |
f0fca04e | 35 | extern int read_only; |
2b086e03 | 36 | extern int list_only; |
f0b4fdaf | 37 | extern int preserve_perms; |
e35ad79b | 38 | extern int preserve_executability; |
f0fca04e | 39 | |
cfeed4da WD |
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) | |
31e12522 | 49 | |
63cf5ae7 | 50 | int do_unlink(const char *fname) |
31e12522 AT |
51 | { |
52 | if (dry_run) return 0; | |
cfeed4da | 53 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
54 | return unlink(fname); |
55 | } | |
56 | ||
8e15bd87 WD |
57 | #ifdef SUPPORT_LINKS |
58 | int do_symlink(const char *lnk, const char *fname) | |
31e12522 AT |
59 | { |
60 | if (dry_run) return 0; | |
cfeed4da | 61 | RETURN_ERROR_IF_RO_OR_LO; |
6e310d38 WD |
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 | ||
8e15bd87 | 78 | return symlink(lnk, fname); |
31e12522 | 79 | } |
6e310d38 WD |
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 | |
8e15bd87 | 104 | #endif |
31e12522 | 105 | |
4f5b0756 | 106 | #ifdef HAVE_LINK |
63cf5ae7 | 107 | int do_link(const char *fname1, const char *fname2) |
31e12522 AT |
108 | { |
109 | if (dry_run) return 0; | |
cfeed4da | 110 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
111 | return link(fname1, fname2); |
112 | } | |
f92ef572 | 113 | #endif |
31e12522 | 114 | |
7308bd66 AT |
115 | int do_lchown(const char *path, uid_t owner, gid_t group) |
116 | { | |
117 | if (dry_run) return 0; | |
cfeed4da | 118 | RETURN_ERROR_IF_RO_OR_LO; |
4f5b0756 | 119 | #ifndef HAVE_LCHOWN |
05154760 WD |
120 | #define lchown chown |
121 | #endif | |
7308bd66 AT |
122 | return lchown(path, owner, group); |
123 | } | |
124 | ||
4a19c3b2 | 125 | int do_mknod(const char *pathname, mode_t mode, dev_t dev) |
31e12522 AT |
126 | { |
127 | if (dry_run) return 0; | |
cfeed4da | 128 | RETURN_ERROR_IF_RO_OR_LO; |
9439c0cb WD |
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 | ||
4f5b0756 | 138 | #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO |
da6eb9d1 WD |
139 | if (S_ISFIFO(mode)) |
140 | return mkfifo(pathname, mode); | |
141 | #endif | |
4f5b0756 | 142 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
da6eb9d1 WD |
143 | if (S_ISSOCK(mode)) { |
144 | int sock; | |
145 | struct sockaddr_un saddr; | |
4f375592 WD |
146 | #ifdef HAVE_SOCKADDR_UN_LEN |
147 | unsigned int len = | |
148 | #endif | |
149 | strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path); | |
4f5b0756 | 150 | #ifdef HAVE_SOCKADDR_UN_LEN |
da6eb9d1 WD |
151 | saddr.sun_len = len >= sizeof saddr.sun_path |
152 | ? sizeof saddr.sun_path : len + 1; | |
153 | #endif | |
4f375592 | 154 | saddr.sun_family = AF_UNIX; |
da6eb9d1 WD |
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); | |
d11f5c6e | 161 | #ifdef HAVE_CHMOD |
4fd842f9 | 162 | return do_chmod(pathname, mode); |
d11f5c6e WD |
163 | #else |
164 | return 0; | |
165 | #endif | |
da6eb9d1 WD |
166 | } |
167 | #endif | |
4f5b0756 | 168 | #ifdef HAVE_MKNOD |
31e12522 | 169 | return mknod(pathname, mode, dev); |
da6eb9d1 WD |
170 | #else |
171 | return -1; | |
f92ef572 | 172 | #endif |
da6eb9d1 | 173 | } |
31e12522 | 174 | |
63cf5ae7 | 175 | int do_rmdir(const char *pathname) |
31e12522 AT |
176 | { |
177 | if (dry_run) return 0; | |
cfeed4da | 178 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
179 | return rmdir(pathname); |
180 | } | |
181 | ||
63cf5ae7 | 182 | int do_open(const char *pathname, int flags, mode_t mode) |
31e12522 | 183 | { |
3420c8e6 | 184 | if (flags != O_RDONLY) { |
7a27e9b5 | 185 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 186 | RETURN_ERROR_IF_RO_OR_LO; |
3420c8e6 | 187 | } |
b0f3f578 | 188 | |
e106de49 | 189 | return open(pathname, flags | O_BINARY, mode); |
31e12522 | 190 | } |
7308bd66 | 191 | |
4f5b0756 | 192 | #ifdef HAVE_CHMOD |
7308bd66 AT |
193 | int do_chmod(const char *path, mode_t mode) |
194 | { | |
f0b4fdaf | 195 | int code; |
7308bd66 | 196 | if (dry_run) return 0; |
cfeed4da | 197 | RETURN_ERROR_IF_RO_OR_LO; |
d11f5c6e | 198 | #ifdef HAVE_LCHMOD |
42562649 WD |
199 | code = lchmod(path, mode & CHMOD_BITS); |
200 | #else | |
201 | if (S_ISLNK(mode)) { | |
202 | # if defined HAVE_SETATTRLIST | |
11b02d92 WD |
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); | |
42562649 | 210 | # else |
d11f5c6e | 211 | code = 1; |
42562649 | 212 | # endif |
d11f5c6e | 213 | } else |
aa0e6b99 | 214 | code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */ |
42562649 | 215 | #endif /* !HAVE_LCHMOD */ |
e35ad79b | 216 | if (code != 0 && (preserve_perms || preserve_executability)) |
ff0e1580 | 217 | return code; |
f0b4fdaf | 218 | return 0; |
7308bd66 | 219 | } |
f92ef572 | 220 | #endif |
1b2d733a | 221 | |
63cf5ae7 | 222 | int do_rename(const char *fname1, const char *fname2) |
1b2d733a AT |
223 | { |
224 | if (dry_run) return 0; | |
cfeed4da | 225 | RETURN_ERROR_IF_RO_OR_LO; |
1b2d733a AT |
226 | return rename(fname1, fname2); |
227 | } | |
228 | ||
bf4e725d MP |
229 | void trim_trailing_slashes(char *name) |
230 | { | |
c127e8aa | 231 | int l; |
bf4e725d MP |
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> */ | |
0f78b815 | 235 | |
c127e8aa MP |
236 | /* Don't change empty string; and also we can't improve on |
237 | * "/" */ | |
0f78b815 | 238 | |
c127e8aa MP |
239 | l = strlen(name); |
240 | while (l > 1) { | |
241 | if (name[--l] != '/') | |
242 | break; | |
243 | name[l] = '\0'; | |
bf4e725d MP |
244 | } |
245 | } | |
246 | ||
1b2d733a AT |
247 | int do_mkdir(char *fname, mode_t mode) |
248 | { | |
cfeed4da WD |
249 | if (dry_run) return 0; |
250 | RETURN_ERROR_IF_RO_OR_LO; | |
0f78b815 | 251 | trim_trailing_slashes(fname); |
1b2d733a AT |
252 | return mkdir(fname, mode); |
253 | } | |
254 | ||
f62c17e3 AT |
255 | /* like mkstemp but forces permissions */ |
256 | int do_mkstemp(char *template, mode_t perms) | |
257 | { | |
7a27e9b5 | 258 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 259 | RETURN_ERROR_IF(read_only, EROFS); |
0379c8ec | 260 | perms |= S_IWUSR; |
f62c17e3 | 261 | |
4f5b0756 | 262 | #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
f62c17e3 AT |
263 | { |
264 | int fd = mkstemp(template); | |
cfeed4da WD |
265 | if (fd == -1) |
266 | return -1; | |
267 | if (fchmod(fd, perms) != 0 && preserve_perms) { | |
268 | int errno_save = errno; | |
f62c17e3 AT |
269 | close(fd); |
270 | unlink(template); | |
cfeed4da | 271 | errno = errno_save; |
f62c17e3 AT |
272 | return -1; |
273 | } | |
4f5b0756 | 274 | #if defined HAVE_SETMODE && O_BINARY |
3267d6a9 WD |
275 | setmode(fd, O_BINARY); |
276 | #endif | |
f62c17e3 AT |
277 | return fd; |
278 | } | |
279 | #else | |
cfeed4da WD |
280 | if (!mktemp(template)) |
281 | return -1; | |
25f2cb3d | 282 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
f62c17e3 | 283 | #endif |
1b2d733a | 284 | } |
bcacc18b AT |
285 | |
286 | int do_stat(const char *fname, STRUCT_STAT *st) | |
287 | { | |
4f5b0756 | 288 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
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 | { | |
4f5b0756 WD |
297 | #ifdef SUPPORT_LINKS |
298 | # ifdef USE_STAT64_FUNCS | |
bcacc18b | 299 | return lstat64(fname, st); |
0957a746 | 300 | # else |
bcacc18b | 301 | return lstat(fname, st); |
0957a746 WD |
302 | # endif |
303 | #else | |
304 | return do_stat(fname, st); | |
bcacc18b AT |
305 | #endif |
306 | } | |
307 | ||
308 | int do_fstat(int fd, STRUCT_STAT *st) | |
309 | { | |
4f5b0756 | 310 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
311 | return fstat64(fd, st); |
312 | #else | |
313 | return fstat(fd, st); | |
314 | #endif | |
315 | } | |
73233f0f AT |
316 | |
317 | OFF_T do_lseek(int fd, OFF_T offset, int whence) | |
318 | { | |
ebd33e0c | 319 | #ifdef HAVE_LSEEK64 |
f853b777 WD |
320 | #if !SIZEOF_OFF64_T |
321 | OFF_T lseek64(); | |
322 | #else | |
f28ee65b | 323 | off64_t lseek64(); |
f853b777 | 324 | #endif |
73233f0f AT |
325 | return lseek64(fd, offset, whence); |
326 | #else | |
327 | return lseek(fd, offset, whence); | |
328 | #endif | |
329 | } |