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