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 | ||
29 | extern int dry_run; | |
f0fca04e | 30 | extern int read_only; |
2b086e03 | 31 | extern int list_only; |
f0b4fdaf | 32 | extern int preserve_perms; |
f0fca04e | 33 | |
cfeed4da WD |
34 | #define RETURN_ERROR_IF(x,e) \ |
35 | do { \ | |
36 | if (x) { \ | |
37 | errno = (e); \ | |
38 | return -1; \ | |
39 | } \ | |
40 | } while (0) | |
41 | ||
42 | #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS) | |
31e12522 AT |
43 | |
44 | int do_unlink(char *fname) | |
45 | { | |
46 | if (dry_run) return 0; | |
cfeed4da | 47 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
48 | return unlink(fname); |
49 | } | |
50 | ||
51 | int do_symlink(char *fname1, char *fname2) | |
52 | { | |
53 | if (dry_run) return 0; | |
cfeed4da | 54 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
55 | return symlink(fname1, fname2); |
56 | } | |
57 | ||
f92ef572 | 58 | #if HAVE_LINK |
31e12522 AT |
59 | int do_link(char *fname1, char *fname2) |
60 | { | |
61 | if (dry_run) return 0; | |
cfeed4da | 62 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
63 | return link(fname1, fname2); |
64 | } | |
f92ef572 | 65 | #endif |
31e12522 | 66 | |
7308bd66 AT |
67 | int do_lchown(const char *path, uid_t owner, gid_t group) |
68 | { | |
69 | if (dry_run) return 0; | |
cfeed4da | 70 | RETURN_ERROR_IF_RO_OR_LO; |
7308bd66 AT |
71 | return lchown(path, owner, group); |
72 | } | |
73 | ||
f92ef572 | 74 | #if HAVE_MKNOD |
31e12522 AT |
75 | int do_mknod(char *pathname, mode_t mode, dev_t dev) |
76 | { | |
77 | if (dry_run) return 0; | |
cfeed4da | 78 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
79 | return mknod(pathname, mode, dev); |
80 | } | |
f92ef572 | 81 | #endif |
31e12522 AT |
82 | |
83 | int do_rmdir(char *pathname) | |
84 | { | |
85 | if (dry_run) return 0; | |
cfeed4da | 86 | RETURN_ERROR_IF_RO_OR_LO; |
31e12522 AT |
87 | return rmdir(pathname); |
88 | } | |
89 | ||
90 | int do_open(char *pathname, int flags, mode_t mode) | |
91 | { | |
3420c8e6 | 92 | if (flags != O_RDONLY) { |
7a27e9b5 | 93 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 94 | RETURN_ERROR_IF_RO_OR_LO; |
3420c8e6 | 95 | } |
b0f3f578 | 96 | |
e106de49 | 97 | return open(pathname, flags | O_BINARY, mode); |
31e12522 | 98 | } |
7308bd66 | 99 | |
f92ef572 | 100 | #if HAVE_CHMOD |
7308bd66 AT |
101 | int do_chmod(const char *path, mode_t mode) |
102 | { | |
f0b4fdaf | 103 | int code; |
7308bd66 | 104 | if (dry_run) return 0; |
cfeed4da | 105 | RETURN_ERROR_IF_RO_OR_LO; |
f0b4fdaf | 106 | code = chmod(path, mode); |
cfeed4da | 107 | if (code != 0 && preserve_perms) |
f0b4fdaf DD |
108 | return code; |
109 | return 0; | |
7308bd66 | 110 | } |
f92ef572 | 111 | #endif |
1b2d733a AT |
112 | |
113 | int do_rename(char *fname1, char *fname2) | |
114 | { | |
115 | if (dry_run) return 0; | |
cfeed4da | 116 | RETURN_ERROR_IF_RO_OR_LO; |
1b2d733a AT |
117 | return rename(fname1, fname2); |
118 | } | |
119 | ||
ded8347d | 120 | |
bf4e725d MP |
121 | void trim_trailing_slashes(char *name) |
122 | { | |
c127e8aa | 123 | int l; |
bf4e725d MP |
124 | /* Some BSD systems cannot make a directory if the name |
125 | * contains a trailing slash. | |
126 | * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */ | |
c127e8aa MP |
127 | |
128 | /* Don't change empty string; and also we can't improve on | |
129 | * "/" */ | |
130 | ||
131 | l = strlen(name); | |
132 | while (l > 1) { | |
133 | if (name[--l] != '/') | |
134 | break; | |
135 | name[l] = '\0'; | |
bf4e725d MP |
136 | } |
137 | } | |
138 | ||
139 | ||
1b2d733a AT |
140 | int do_mkdir(char *fname, mode_t mode) |
141 | { | |
cfeed4da WD |
142 | if (dry_run) return 0; |
143 | RETURN_ERROR_IF_RO_OR_LO; | |
bf4e725d | 144 | trim_trailing_slashes(fname); |
1b2d733a AT |
145 | return mkdir(fname, mode); |
146 | } | |
147 | ||
ded8347d | 148 | |
f62c17e3 AT |
149 | /* like mkstemp but forces permissions */ |
150 | int do_mkstemp(char *template, mode_t perms) | |
151 | { | |
7a27e9b5 | 152 | RETURN_ERROR_IF(dry_run, 0); |
cfeed4da | 153 | RETURN_ERROR_IF(read_only, EROFS); |
f62c17e3 AT |
154 | |
155 | #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD) | |
156 | { | |
157 | int fd = mkstemp(template); | |
cfeed4da WD |
158 | if (fd == -1) |
159 | return -1; | |
160 | if (fchmod(fd, perms) != 0 && preserve_perms) { | |
161 | int errno_save = errno; | |
f62c17e3 AT |
162 | close(fd); |
163 | unlink(template); | |
cfeed4da | 164 | errno = errno_save; |
f62c17e3 AT |
165 | return -1; |
166 | } | |
167 | return fd; | |
168 | } | |
169 | #else | |
cfeed4da WD |
170 | if (!mktemp(template)) |
171 | return -1; | |
25f2cb3d | 172 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
f62c17e3 | 173 | #endif |
1b2d733a | 174 | } |
bcacc18b AT |
175 | |
176 | int do_stat(const char *fname, STRUCT_STAT *st) | |
177 | { | |
178 | #if HAVE_OFF64_T | |
179 | return stat64(fname, st); | |
180 | #else | |
181 | return stat(fname, st); | |
182 | #endif | |
183 | } | |
184 | ||
efb2f6bf | 185 | #if SUPPORT_LINKS |
bcacc18b AT |
186 | int do_lstat(const char *fname, STRUCT_STAT *st) |
187 | { | |
188 | #if HAVE_OFF64_T | |
189 | return lstat64(fname, st); | |
190 | #else | |
191 | return lstat(fname, st); | |
192 | #endif | |
193 | } | |
efb2f6bf | 194 | #endif |
bcacc18b AT |
195 | |
196 | int do_fstat(int fd, STRUCT_STAT *st) | |
197 | { | |
198 | #if HAVE_OFF64_T | |
199 | return fstat64(fd, st); | |
200 | #else | |
201 | return fstat(fd, st); | |
202 | #endif | |
203 | } | |
73233f0f AT |
204 | |
205 | OFF_T do_lseek(int fd, OFF_T offset, int whence) | |
206 | { | |
207 | #if HAVE_OFF64_T | |
f28ee65b | 208 | off64_t lseek64(); |
73233f0f AT |
209 | return lseek64(fd, offset, whence); |
210 | #else | |
211 | return lseek(fd, offset, whence); | |
212 | #endif | |
213 | } | |
d6e6ecbd | 214 | |
bb0f7089 | 215 | #ifdef USE_MMAP |
754d120c AT |
216 | void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset) |
217 | { | |
218 | #if HAVE_OFF64_T | |
219 | return mmap64(start, len, prot, flags, fd, offset); | |
220 | #else | |
221 | return mmap(start, len, prot, flags, fd, offset); | |
222 | #endif | |
223 | } | |
224 | #endif | |
225 | ||
d6e6ecbd AT |
226 | char *d_name(struct dirent *di) |
227 | { | |
59503278 AT |
228 | #if HAVE_BROKEN_READDIR |
229 | return (di->d_name - 2); | |
230 | #else | |
d6e6ecbd | 231 | return di->d_name; |
59503278 | 232 | #endif |
d6e6ecbd | 233 | } |