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