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 | |
d3d07a5e | 7 | * Copyright (C) 2003-2008 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; |
f0fca04e | 34 | extern int read_only; |
2b086e03 | 35 | extern int list_only; |
f0b4fdaf | 36 | extern int preserve_perms; |
e35ad79b | 37 | extern int preserve_executability; |
f0fca04e | 38 | |
cfeed4da WD |
39 | #define RETURN_ERROR_IF(x,e) \ |
40 | do { \ | |
41 | if (x) { \ | |
42 | errno = (e); \ | |
43 | return -1; \ | |
44 | } \ | |
45 | } while (0) | |
46 | ||
47 | #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS) | |
31e12522 | 48 | |
63cf5ae7 | 49 | int do_unlink(const char *fname) |
31e12522 AT |
50 | { |
51 | if (dry_run) return 0; | |
cfeed4da | 52 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
53 | return unlink(fname); |
54 | } | |
55 | ||
63cf5ae7 | 56 | int do_symlink(const char *fname1, const char *fname2) |
31e12522 AT |
57 | { |
58 | if (dry_run) return 0; | |
cfeed4da | 59 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
60 | return symlink(fname1, fname2); |
61 | } | |
62 | ||
4f5b0756 | 63 | #ifdef HAVE_LINK |
63cf5ae7 | 64 | int do_link(const char *fname1, const char *fname2) |
31e12522 AT |
65 | { |
66 | if (dry_run) return 0; | |
cfeed4da | 67 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
68 | return link(fname1, fname2); |
69 | } | |
f92ef572 | 70 | #endif |
31e12522 | 71 | |
7308bd66 AT |
72 | int do_lchown(const char *path, uid_t owner, gid_t group) |
73 | { | |
74 | if (dry_run) return 0; | |
cfeed4da | 75 | RETURN_ERROR_IF_RO_OR_LO; |
4f5b0756 | 76 | #ifndef HAVE_LCHOWN |
05154760 WD |
77 | #define lchown chown |
78 | #endif | |
7308bd66 AT |
79 | return lchown(path, owner, group); |
80 | } | |
81 | ||
4a19c3b2 | 82 | int do_mknod(const char *pathname, mode_t mode, dev_t dev) |
31e12522 AT |
83 | { |
84 | if (dry_run) return 0; | |
cfeed4da | 85 | RETURN_ERROR_IF_RO_OR_LO; |
9439c0cb WD |
86 | |
87 | /* For --fake-super, we create a normal file with mode 0600. */ | |
88 | if (am_root < 0) { | |
89 | int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); | |
90 | if (fd < 0 || close(fd) < 0) | |
91 | return -1; | |
92 | return 0; | |
93 | } | |
94 | ||
4f5b0756 | 95 | #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO |
da6eb9d1 WD |
96 | if (S_ISFIFO(mode)) |
97 | return mkfifo(pathname, mode); | |
98 | #endif | |
4f5b0756 | 99 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
da6eb9d1 WD |
100 | if (S_ISSOCK(mode)) { |
101 | int sock; | |
102 | struct sockaddr_un saddr; | |
4f375592 WD |
103 | #ifdef HAVE_SOCKADDR_UN_LEN |
104 | unsigned int len = | |
105 | #endif | |
106 | strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path); | |
4f5b0756 | 107 | #ifdef HAVE_SOCKADDR_UN_LEN |
da6eb9d1 WD |
108 | saddr.sun_len = len >= sizeof saddr.sun_path |
109 | ? sizeof saddr.sun_path : len + 1; | |
110 | #endif | |
4f375592 | 111 | saddr.sun_family = AF_UNIX; |
da6eb9d1 WD |
112 | |
113 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 | |
114 | || (unlink(pathname) < 0 && errno != ENOENT) | |
115 | || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0) | |
116 | return -1; | |
117 | close(sock); | |
d11f5c6e | 118 | #ifdef HAVE_CHMOD |
4fd842f9 | 119 | return do_chmod(pathname, mode); |
d11f5c6e WD |
120 | #else |
121 | return 0; | |
122 | #endif | |
da6eb9d1 WD |
123 | } |
124 | #endif | |
4f5b0756 | 125 | #ifdef HAVE_MKNOD |
31e12522 | 126 | return mknod(pathname, mode, dev); |
da6eb9d1 WD |
127 | #else |
128 | return -1; | |
f92ef572 | 129 | #endif |
da6eb9d1 | 130 | } |
31e12522 | 131 | |
63cf5ae7 | 132 | int do_rmdir(const char *pathname) |
31e12522 AT |
133 | { |
134 | if (dry_run) return 0; | |
cfeed4da | 135 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
136 | return rmdir(pathname); |
137 | } | |
138 | ||
63cf5ae7 | 139 | int do_open(const char *pathname, int flags, mode_t mode) |
31e12522 | 140 | { |
3420c8e6 | 141 | if (flags != O_RDONLY) { |
7a27e9b5 | 142 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 143 | RETURN_ERROR_IF_RO_OR_LO; |
3420c8e6 | 144 | } |
b0f3f578 | 145 | |
e106de49 | 146 | return open(pathname, flags | O_BINARY, mode); |
31e12522 | 147 | } |
7308bd66 | 148 | |
4f5b0756 | 149 | #ifdef HAVE_CHMOD |
7308bd66 AT |
150 | int do_chmod(const char *path, mode_t mode) |
151 | { | |
f0b4fdaf | 152 | int code; |
7308bd66 | 153 | if (dry_run) return 0; |
cfeed4da | 154 | RETURN_ERROR_IF_RO_OR_LO; |
d11f5c6e WD |
155 | if (S_ISLNK(mode)) { |
156 | #ifdef HAVE_LCHMOD | |
157 | code = lchmod(path, mode & CHMOD_BITS); | |
11b02d92 WD |
158 | #elif defined HAVE_SETATTRLIST |
159 | struct attrlist attrList; | |
160 | uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */ | |
161 | ||
162 | memset(&attrList, 0, sizeof attrList); | |
163 | attrList.bitmapcount = ATTR_BIT_MAP_COUNT; | |
164 | attrList.commonattr = ATTR_CMN_ACCESSMASK; | |
165 | code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW); | |
d11f5c6e WD |
166 | #else |
167 | code = 1; | |
168 | #endif | |
169 | } else | |
170 | code = chmod(path, mode & CHMOD_BITS); | |
e35ad79b | 171 | if (code != 0 && (preserve_perms || preserve_executability)) |
ff0e1580 | 172 | return code; |
f0b4fdaf | 173 | return 0; |
7308bd66 | 174 | } |
f92ef572 | 175 | #endif |
1b2d733a | 176 | |
63cf5ae7 | 177 | int do_rename(const char *fname1, const char *fname2) |
1b2d733a AT |
178 | { |
179 | if (dry_run) return 0; | |
cfeed4da | 180 | RETURN_ERROR_IF_RO_OR_LO; |
1b2d733a AT |
181 | return rename(fname1, fname2); |
182 | } | |
183 | ||
bf4e725d MP |
184 | void trim_trailing_slashes(char *name) |
185 | { | |
c127e8aa | 186 | int l; |
bf4e725d MP |
187 | /* Some BSD systems cannot make a directory if the name |
188 | * contains a trailing slash. | |
189 | * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */ | |
0f78b815 | 190 | |
c127e8aa MP |
191 | /* Don't change empty string; and also we can't improve on |
192 | * "/" */ | |
0f78b815 | 193 | |
c127e8aa MP |
194 | l = strlen(name); |
195 | while (l > 1) { | |
196 | if (name[--l] != '/') | |
197 | break; | |
198 | name[l] = '\0'; | |
bf4e725d MP |
199 | } |
200 | } | |
201 | ||
1b2d733a AT |
202 | int do_mkdir(char *fname, mode_t mode) |
203 | { | |
cfeed4da WD |
204 | if (dry_run) return 0; |
205 | RETURN_ERROR_IF_RO_OR_LO; | |
0f78b815 | 206 | trim_trailing_slashes(fname); |
1b2d733a AT |
207 | return mkdir(fname, mode); |
208 | } | |
209 | ||
f62c17e3 AT |
210 | /* like mkstemp but forces permissions */ |
211 | int do_mkstemp(char *template, mode_t perms) | |
212 | { | |
7a27e9b5 | 213 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 214 | RETURN_ERROR_IF(read_only, EROFS); |
0379c8ec | 215 | perms |= S_IWUSR; |
f62c17e3 | 216 | |
4f5b0756 | 217 | #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
f62c17e3 AT |
218 | { |
219 | int fd = mkstemp(template); | |
cfeed4da WD |
220 | if (fd == -1) |
221 | return -1; | |
222 | if (fchmod(fd, perms) != 0 && preserve_perms) { | |
223 | int errno_save = errno; | |
f62c17e3 AT |
224 | close(fd); |
225 | unlink(template); | |
cfeed4da | 226 | errno = errno_save; |
f62c17e3 AT |
227 | return -1; |
228 | } | |
4f5b0756 | 229 | #if defined HAVE_SETMODE && O_BINARY |
3267d6a9 WD |
230 | setmode(fd, O_BINARY); |
231 | #endif | |
f62c17e3 AT |
232 | return fd; |
233 | } | |
234 | #else | |
cfeed4da WD |
235 | if (!mktemp(template)) |
236 | return -1; | |
25f2cb3d | 237 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
f62c17e3 | 238 | #endif |
1b2d733a | 239 | } |
bcacc18b AT |
240 | |
241 | int do_stat(const char *fname, STRUCT_STAT *st) | |
242 | { | |
4f5b0756 | 243 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
244 | return stat64(fname, st); |
245 | #else | |
246 | return stat(fname, st); | |
247 | #endif | |
248 | } | |
249 | ||
250 | int do_lstat(const char *fname, STRUCT_STAT *st) | |
251 | { | |
4f5b0756 WD |
252 | #ifdef SUPPORT_LINKS |
253 | # ifdef USE_STAT64_FUNCS | |
bcacc18b | 254 | return lstat64(fname, st); |
0957a746 | 255 | # else |
bcacc18b | 256 | return lstat(fname, st); |
0957a746 WD |
257 | # endif |
258 | #else | |
259 | return do_stat(fname, st); | |
bcacc18b AT |
260 | #endif |
261 | } | |
262 | ||
263 | int do_fstat(int fd, STRUCT_STAT *st) | |
264 | { | |
4f5b0756 | 265 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
266 | return fstat64(fd, st); |
267 | #else | |
268 | return fstat(fd, st); | |
269 | #endif | |
270 | } | |
73233f0f AT |
271 | |
272 | OFF_T do_lseek(int fd, OFF_T offset, int whence) | |
273 | { | |
ebd33e0c | 274 | #ifdef HAVE_LSEEK64 |
f853b777 WD |
275 | #if !SIZEOF_OFF64_T |
276 | OFF_T lseek64(); | |
277 | #else | |
f28ee65b | 278 | off64_t lseek64(); |
f853b777 | 279 | #endif |
73233f0f AT |
280 | return lseek64(fd, offset, whence); |
281 | #else | |
282 | return lseek(fd, offset, whence); | |
283 | #endif | |
284 | } | |
d6e6ecbd AT |
285 | |
286 | char *d_name(struct dirent *di) | |
287 | { | |
4f5b0756 | 288 | #ifdef HAVE_BROKEN_READDIR |
59503278 AT |
289 | return (di->d_name - 2); |
290 | #else | |
d6e6ecbd | 291 | return di->d_name; |
59503278 | 292 | #endif |
d6e6ecbd | 293 | } |