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