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