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