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