Make case_N.h more generic.
[rsync/rsync.git] / syscall.c
... / ...
CommitLineData
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-2009 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 as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
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#ifdef HAVE_SYS_ATTR_H
29#include <sys/attr.h>
30#endif
31
32extern int dry_run;
33extern int am_root;
34extern int am_sender;
35extern int read_only;
36extern int list_only;
37extern int preserve_perms;
38extern int preserve_executability;
39
40#define RETURN_ERROR_IF(x,e) \
41 do { \
42 if (x) { \
43 errno = (e); \
44 return -1; \
45 } \
46 } while (0)
47
48#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
49
50int do_unlink(const char *fname)
51{
52 if (dry_run) return 0;
53 RETURN_ERROR_IF_RO_OR_LO;
54 return unlink(fname);
55}
56
57#ifdef SUPPORT_LINKS
58int do_symlink(const char *lnk, const char *fname)
59{
60 if (dry_run) return 0;
61 RETURN_ERROR_IF_RO_OR_LO;
62
63#ifdef NO_SYMLINK_XATTRS
64 /* For --fake-super, we create a normal file with mode 0600
65 * and write the lnk into it. */
66 if (am_root < 0) {
67 int ok, len = strlen(lnk);
68 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
69 if (fd < 0)
70 return -1;
71 ok = write(fd, lnk, len) == len;
72 if (close(fd) < 0)
73 ok = 0;
74 return ok ? 0 : -1;
75 }
76#endif
77
78 return symlink(lnk, fname);
79}
80
81#ifdef NO_SYMLINK_XATTRS
82ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
83{
84 /* For --fake-super, we read the link from the file. */
85 if (am_root < 0) {
86 int fd = open(path, O_RDONLY|O_NOFOLLOW);
87 if (fd >= 0) {
88 int len = read(fd, buf, bufsiz);
89 close(fd);
90 return len;
91 }
92 if (errno != ELOOP)
93 return -1;
94 /* A real symlink needs to be turned into a fake one on the receiving
95 * side, so tell the generator that the link has no length. */
96 if (!am_sender)
97 return 0;
98 /* Otherwise fall through and let the sender report the real length. */
99 }
100
101 return readlink(path, buf, bufsiz);
102}
103#endif
104#endif
105
106#ifdef HAVE_LINK
107int do_link(const char *fname1, const char *fname2)
108{
109 if (dry_run) return 0;
110 RETURN_ERROR_IF_RO_OR_LO;
111 return link(fname1, fname2);
112}
113#endif
114
115int do_lchown(const char *path, uid_t owner, gid_t group)
116{
117 if (dry_run) return 0;
118 RETURN_ERROR_IF_RO_OR_LO;
119#ifndef HAVE_LCHOWN
120#define lchown chown
121#endif
122 return lchown(path, owner, group);
123}
124
125int do_mknod(const char *pathname, mode_t mode, dev_t dev)
126{
127 if (dry_run) return 0;
128 RETURN_ERROR_IF_RO_OR_LO;
129
130 /* For --fake-super, we create a normal file with mode 0600. */
131 if (am_root < 0) {
132 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
133 if (fd < 0 || close(fd) < 0)
134 return -1;
135 return 0;
136 }
137
138#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
139 if (S_ISFIFO(mode))
140 return mkfifo(pathname, mode);
141#endif
142#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
143 if (S_ISSOCK(mode)) {
144 int sock;
145 struct sockaddr_un saddr;
146#ifdef HAVE_SOCKADDR_UN_LEN
147 unsigned int len =
148#endif
149 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
150#ifdef HAVE_SOCKADDR_UN_LEN
151 saddr.sun_len = len >= sizeof saddr.sun_path
152 ? sizeof saddr.sun_path : len + 1;
153#endif
154 saddr.sun_family = AF_UNIX;
155
156 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
157 || (unlink(pathname) < 0 && errno != ENOENT)
158 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
159 return -1;
160 close(sock);
161#ifdef HAVE_CHMOD
162 return do_chmod(pathname, mode);
163#else
164 return 0;
165#endif
166 }
167#endif
168#ifdef HAVE_MKNOD
169 return mknod(pathname, mode, dev);
170#else
171 return -1;
172#endif
173}
174
175int do_rmdir(const char *pathname)
176{
177 if (dry_run) return 0;
178 RETURN_ERROR_IF_RO_OR_LO;
179 return rmdir(pathname);
180}
181
182int do_open(const char *pathname, int flags, mode_t mode)
183{
184 if (flags != O_RDONLY) {
185 RETURN_ERROR_IF(dry_run, 0);
186 RETURN_ERROR_IF_RO_OR_LO;
187 }
188
189 return open(pathname, flags | O_BINARY, mode);
190}
191
192#ifdef HAVE_CHMOD
193int do_chmod(const char *path, mode_t mode)
194{
195 int code;
196 if (dry_run) return 0;
197 RETURN_ERROR_IF_RO_OR_LO;
198#ifdef HAVE_LCHMOD
199 code = lchmod(path, mode & CHMOD_BITS);
200#else
201 if (S_ISLNK(mode)) {
202# if defined HAVE_SETATTRLIST
203 struct attrlist attrList;
204 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
205
206 memset(&attrList, 0, sizeof attrList);
207 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
208 attrList.commonattr = ATTR_CMN_ACCESSMASK;
209 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
210# else
211 code = 1;
212# endif
213 } else
214 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
215#endif /* !HAVE_LCHMOD */
216 if (code != 0 && (preserve_perms || preserve_executability))
217 return code;
218 return 0;
219}
220#endif
221
222int do_rename(const char *fname1, const char *fname2)
223{
224 if (dry_run) return 0;
225 RETURN_ERROR_IF_RO_OR_LO;
226 return rename(fname1, fname2);
227}
228
229void trim_trailing_slashes(char *name)
230{
231 int l;
232 /* Some BSD systems cannot make a directory if the name
233 * contains a trailing slash.
234 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
235
236 /* Don't change empty string; and also we can't improve on
237 * "/" */
238
239 l = strlen(name);
240 while (l > 1) {
241 if (name[--l] != '/')
242 break;
243 name[l] = '\0';
244 }
245}
246
247int do_mkdir(char *fname, mode_t mode)
248{
249 if (dry_run) return 0;
250 RETURN_ERROR_IF_RO_OR_LO;
251 trim_trailing_slashes(fname);
252 return mkdir(fname, mode);
253}
254
255/* like mkstemp but forces permissions */
256int do_mkstemp(char *template, mode_t perms)
257{
258 RETURN_ERROR_IF(dry_run, 0);
259 RETURN_ERROR_IF(read_only, EROFS);
260 perms |= S_IWUSR;
261
262#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
263 {
264 int fd = mkstemp(template);
265 if (fd == -1)
266 return -1;
267 if (fchmod(fd, perms) != 0 && preserve_perms) {
268 int errno_save = errno;
269 close(fd);
270 unlink(template);
271 errno = errno_save;
272 return -1;
273 }
274#if defined HAVE_SETMODE && O_BINARY
275 setmode(fd, O_BINARY);
276#endif
277 return fd;
278 }
279#else
280 if (!mktemp(template))
281 return -1;
282 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
283#endif
284}
285
286int do_stat(const char *fname, STRUCT_STAT *st)
287{
288#ifdef USE_STAT64_FUNCS
289 return stat64(fname, st);
290#else
291 return stat(fname, st);
292#endif
293}
294
295int do_lstat(const char *fname, STRUCT_STAT *st)
296{
297#ifdef SUPPORT_LINKS
298# ifdef USE_STAT64_FUNCS
299 return lstat64(fname, st);
300# else
301 return lstat(fname, st);
302# endif
303#else
304 return do_stat(fname, st);
305#endif
306}
307
308int do_fstat(int fd, STRUCT_STAT *st)
309{
310#ifdef USE_STAT64_FUNCS
311 return fstat64(fd, st);
312#else
313 return fstat(fd, st);
314#endif
315}
316
317OFF_T do_lseek(int fd, OFF_T offset, int whence)
318{
319#ifdef HAVE_LSEEK64
320#if !SIZEOF_OFF64_T
321 OFF_T lseek64();
322#else
323 off64_t lseek64();
324#endif
325 return lseek64(fd, offset, whence);
326#else
327 return lseek(fd, offset, whence);
328#endif
329}