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 | |
7 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison | |
8 | * | |
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 2 of the License, or | |
12 | * (at your option) any later version. | |
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 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 22 | */ |
31e12522 AT |
23 | |
24 | #include "rsync.h" | |
25 | ||
4f5b0756 | 26 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
da6eb9d1 WD |
27 | #include <sys/un.h> |
28 | #endif | |
29 | ||
31e12522 | 30 | extern int dry_run; |
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; |
4f5b0756 | 82 | #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO |
da6eb9d1 WD |
83 | if (S_ISFIFO(mode)) |
84 | return mkfifo(pathname, mode); | |
85 | #endif | |
4f5b0756 | 86 | #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
da6eb9d1 WD |
87 | if (S_ISSOCK(mode)) { |
88 | int sock; | |
89 | struct sockaddr_un saddr; | |
4f375592 WD |
90 | #ifdef HAVE_SOCKADDR_UN_LEN |
91 | unsigned int len = | |
92 | #endif | |
93 | strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path); | |
4f5b0756 | 94 | #ifdef HAVE_SOCKADDR_UN_LEN |
da6eb9d1 WD |
95 | saddr.sun_len = len >= sizeof saddr.sun_path |
96 | ? sizeof saddr.sun_path : len + 1; | |
97 | #endif | |
4f375592 | 98 | saddr.sun_family = AF_UNIX; |
da6eb9d1 WD |
99 | |
100 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 | |
101 | || (unlink(pathname) < 0 && errno != ENOENT) | |
102 | || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0) | |
103 | return -1; | |
104 | close(sock); | |
d11f5c6e | 105 | #ifdef HAVE_CHMOD |
da6eb9d1 | 106 | return do_chmod(pathname, mode); |
d11f5c6e WD |
107 | #else |
108 | return 0; | |
109 | #endif | |
da6eb9d1 WD |
110 | } |
111 | #endif | |
4f5b0756 | 112 | #ifdef HAVE_MKNOD |
31e12522 | 113 | return mknod(pathname, mode, dev); |
da6eb9d1 WD |
114 | #else |
115 | return -1; | |
f92ef572 | 116 | #endif |
da6eb9d1 | 117 | } |
31e12522 | 118 | |
63cf5ae7 | 119 | int do_rmdir(const char *pathname) |
31e12522 AT |
120 | { |
121 | if (dry_run) return 0; | |
cfeed4da | 122 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
123 | return rmdir(pathname); |
124 | } | |
125 | ||
63cf5ae7 | 126 | int do_open(const char *pathname, int flags, mode_t mode) |
31e12522 | 127 | { |
3420c8e6 | 128 | if (flags != O_RDONLY) { |
7a27e9b5 | 129 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 130 | RETURN_ERROR_IF_RO_OR_LO; |
3420c8e6 | 131 | } |
b0f3f578 | 132 | |
e106de49 | 133 | return open(pathname, flags | O_BINARY, mode); |
31e12522 | 134 | } |
7308bd66 | 135 | |
4f5b0756 | 136 | #ifdef HAVE_CHMOD |
7308bd66 AT |
137 | int do_chmod(const char *path, mode_t mode) |
138 | { | |
f0b4fdaf | 139 | int code; |
7308bd66 | 140 | if (dry_run) return 0; |
cfeed4da | 141 | RETURN_ERROR_IF_RO_OR_LO; |
d11f5c6e WD |
142 | if (S_ISLNK(mode)) { |
143 | #ifdef HAVE_LCHMOD | |
144 | code = lchmod(path, mode & CHMOD_BITS); | |
145 | #else | |
146 | code = 1; | |
147 | #endif | |
148 | } else | |
149 | code = chmod(path, mode & CHMOD_BITS); | |
cfeed4da | 150 | if (code != 0 && preserve_perms) |
f0b4fdaf DD |
151 | return code; |
152 | return 0; | |
7308bd66 | 153 | } |
f92ef572 | 154 | #endif |
1b2d733a | 155 | |
63cf5ae7 | 156 | int do_rename(const char *fname1, const char *fname2) |
1b2d733a AT |
157 | { |
158 | if (dry_run) return 0; | |
cfeed4da | 159 | RETURN_ERROR_IF_RO_OR_LO; |
1b2d733a AT |
160 | return rename(fname1, fname2); |
161 | } | |
162 | ||
bf4e725d MP |
163 | void trim_trailing_slashes(char *name) |
164 | { | |
c127e8aa | 165 | int l; |
bf4e725d MP |
166 | /* Some BSD systems cannot make a directory if the name |
167 | * contains a trailing slash. | |
168 | * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */ | |
0f78b815 | 169 | |
c127e8aa MP |
170 | /* Don't change empty string; and also we can't improve on |
171 | * "/" */ | |
0f78b815 | 172 | |
c127e8aa MP |
173 | l = strlen(name); |
174 | while (l > 1) { | |
175 | if (name[--l] != '/') | |
176 | break; | |
177 | name[l] = '\0'; | |
bf4e725d MP |
178 | } |
179 | } | |
180 | ||
1b2d733a AT |
181 | int do_mkdir(char *fname, mode_t mode) |
182 | { | |
cfeed4da WD |
183 | if (dry_run) return 0; |
184 | RETURN_ERROR_IF_RO_OR_LO; | |
0f78b815 | 185 | trim_trailing_slashes(fname); |
1b2d733a AT |
186 | return mkdir(fname, mode); |
187 | } | |
188 | ||
f62c17e3 AT |
189 | /* like mkstemp but forces permissions */ |
190 | int do_mkstemp(char *template, mode_t perms) | |
191 | { | |
7a27e9b5 | 192 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 193 | RETURN_ERROR_IF(read_only, EROFS); |
f62c17e3 | 194 | |
4f5b0756 | 195 | #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
f62c17e3 AT |
196 | { |
197 | int fd = mkstemp(template); | |
cfeed4da WD |
198 | if (fd == -1) |
199 | return -1; | |
200 | if (fchmod(fd, perms) != 0 && preserve_perms) { | |
201 | int errno_save = errno; | |
f62c17e3 AT |
202 | close(fd); |
203 | unlink(template); | |
cfeed4da | 204 | errno = errno_save; |
f62c17e3 AT |
205 | return -1; |
206 | } | |
4f5b0756 | 207 | #if defined HAVE_SETMODE && O_BINARY |
3267d6a9 WD |
208 | setmode(fd, O_BINARY); |
209 | #endif | |
f62c17e3 AT |
210 | return fd; |
211 | } | |
212 | #else | |
cfeed4da WD |
213 | if (!mktemp(template)) |
214 | return -1; | |
25f2cb3d | 215 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
f62c17e3 | 216 | #endif |
1b2d733a | 217 | } |
bcacc18b AT |
218 | |
219 | int do_stat(const char *fname, STRUCT_STAT *st) | |
220 | { | |
4f5b0756 | 221 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
222 | return stat64(fname, st); |
223 | #else | |
224 | return stat(fname, st); | |
225 | #endif | |
226 | } | |
227 | ||
228 | int do_lstat(const char *fname, STRUCT_STAT *st) | |
229 | { | |
4f5b0756 WD |
230 | #ifdef SUPPORT_LINKS |
231 | # ifdef USE_STAT64_FUNCS | |
bcacc18b | 232 | return lstat64(fname, st); |
0957a746 | 233 | # else |
bcacc18b | 234 | return lstat(fname, st); |
0957a746 WD |
235 | # endif |
236 | #else | |
237 | return do_stat(fname, st); | |
bcacc18b AT |
238 | #endif |
239 | } | |
240 | ||
241 | int do_fstat(int fd, STRUCT_STAT *st) | |
242 | { | |
4f5b0756 | 243 | #ifdef USE_STAT64_FUNCS |
bcacc18b AT |
244 | return fstat64(fd, st); |
245 | #else | |
246 | return fstat(fd, st); | |
247 | #endif | |
248 | } | |
73233f0f AT |
249 | |
250 | OFF_T do_lseek(int fd, OFF_T offset, int whence) | |
251 | { | |
ebd33e0c | 252 | #ifdef HAVE_LSEEK64 |
f853b777 WD |
253 | #if !SIZEOF_OFF64_T |
254 | OFF_T lseek64(); | |
255 | #else | |
f28ee65b | 256 | off64_t lseek64(); |
f853b777 | 257 | #endif |
73233f0f AT |
258 | return lseek64(fd, offset, whence); |
259 | #else | |
260 | return lseek(fd, offset, whence); | |
261 | #endif | |
262 | } | |
d6e6ecbd AT |
263 | |
264 | char *d_name(struct dirent *di) | |
265 | { | |
4f5b0756 | 266 | #ifdef HAVE_BROKEN_READDIR |
59503278 AT |
267 | return (di->d_name - 2); |
268 | #else | |
d6e6ecbd | 269 | return di->d_name; |
59503278 | 270 | #endif |
d6e6ecbd | 271 | } |