Make --files-from allow a missing trailing arg w/--server.
[rsync/rsync.git] / syscall.c
CommitLineData
0f78b815 1/*
ded8347d
MP
2 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
0f78b815
WD
4 *
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
b3bf9b9d 7 * Copyright (C) 2003-2009 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
0f78b815
WD
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 *
e7c67065 19 * You should have received a copy of the GNU General Public License along
4fd842f9 20 * with this program; if not, visit the http://fsf.org website.
0f78b815 21 */
31e12522
AT
22
23#include "rsync.h"
24
4f5b0756 25#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
da6eb9d1
WD
26#include <sys/un.h>
27#endif
11b02d92
WD
28#ifdef HAVE_SYS_ATTR_H
29#include <sys/attr.h>
30#endif
da6eb9d1 31
28b519c9
WD
32#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
33#include <sys/syscall.h>
34#endif
35
31e12522 36extern int dry_run;
9439c0cb 37extern int am_root;
6e310d38 38extern int am_sender;
f0fca04e 39extern int read_only;
2b086e03 40extern int list_only;
f0b4fdaf 41extern int preserve_perms;
e35ad79b 42extern int preserve_executability;
f0fca04e 43
cfeed4da
WD
44#define RETURN_ERROR_IF(x,e) \
45 do { \
46 if (x) { \
47 errno = (e); \
48 return -1; \
49 } \
50 } while (0)
51
52#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
31e12522 53
63cf5ae7 54int do_unlink(const char *fname)
31e12522
AT
55{
56 if (dry_run) return 0;
cfeed4da 57 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
58 return unlink(fname);
59}
60
8e15bd87
WD
61#ifdef SUPPORT_LINKS
62int do_symlink(const char *lnk, const char *fname)
31e12522
AT
63{
64 if (dry_run) return 0;
cfeed4da 65 RETURN_ERROR_IF_RO_OR_LO;
6e310d38
WD
66
67#ifdef NO_SYMLINK_XATTRS
68 /* For --fake-super, we create a normal file with mode 0600
69 * and write the lnk into it. */
70 if (am_root < 0) {
71 int ok, len = strlen(lnk);
72 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
73 if (fd < 0)
74 return -1;
75 ok = write(fd, lnk, len) == len;
76 if (close(fd) < 0)
77 ok = 0;
78 return ok ? 0 : -1;
79 }
80#endif
81
8e15bd87 82 return symlink(lnk, fname);
31e12522 83}
6e310d38
WD
84
85#ifdef NO_SYMLINK_XATTRS
86ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
87{
88 /* For --fake-super, we read the link from the file. */
89 if (am_root < 0) {
90 int fd = open(path, O_RDONLY|O_NOFOLLOW);
91 if (fd >= 0) {
92 int len = read(fd, buf, bufsiz);
93 close(fd);
94 return len;
95 }
96 if (errno != ELOOP)
97 return -1;
98 /* A real symlink needs to be turned into a fake one on the receiving
99 * side, so tell the generator that the link has no length. */
100 if (!am_sender)
101 return 0;
102 /* Otherwise fall through and let the sender report the real length. */
103 }
104
105 return readlink(path, buf, bufsiz);
106}
107#endif
8e15bd87 108#endif
31e12522 109
4f5b0756 110#ifdef HAVE_LINK
63cf5ae7 111int do_link(const char *fname1, const char *fname2)
31e12522
AT
112{
113 if (dry_run) return 0;
cfeed4da 114 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
115 return link(fname1, fname2);
116}
f92ef572 117#endif
31e12522 118
7308bd66
AT
119int do_lchown(const char *path, uid_t owner, gid_t group)
120{
121 if (dry_run) return 0;
cfeed4da 122 RETURN_ERROR_IF_RO_OR_LO;
4f5b0756 123#ifndef HAVE_LCHOWN
05154760
WD
124#define lchown chown
125#endif
7308bd66
AT
126 return lchown(path, owner, group);
127}
128
4a19c3b2 129int do_mknod(const char *pathname, mode_t mode, dev_t dev)
31e12522
AT
130{
131 if (dry_run) return 0;
cfeed4da 132 RETURN_ERROR_IF_RO_OR_LO;
9439c0cb
WD
133
134 /* For --fake-super, we create a normal file with mode 0600. */
135 if (am_root < 0) {
136 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
137 if (fd < 0 || close(fd) < 0)
138 return -1;
139 return 0;
140 }
141
4f5b0756 142#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
da6eb9d1
WD
143 if (S_ISFIFO(mode))
144 return mkfifo(pathname, mode);
145#endif
4f5b0756 146#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
da6eb9d1
WD
147 if (S_ISSOCK(mode)) {
148 int sock;
149 struct sockaddr_un saddr;
4f375592
WD
150#ifdef HAVE_SOCKADDR_UN_LEN
151 unsigned int len =
152#endif
153 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
4f5b0756 154#ifdef HAVE_SOCKADDR_UN_LEN
da6eb9d1
WD
155 saddr.sun_len = len >= sizeof saddr.sun_path
156 ? sizeof saddr.sun_path : len + 1;
157#endif
4f375592 158 saddr.sun_family = AF_UNIX;
da6eb9d1
WD
159
160 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
161 || (unlink(pathname) < 0 && errno != ENOENT)
162 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
163 return -1;
164 close(sock);
d11f5c6e 165#ifdef HAVE_CHMOD
4fd842f9 166 return do_chmod(pathname, mode);
d11f5c6e
WD
167#else
168 return 0;
169#endif
da6eb9d1
WD
170 }
171#endif
4f5b0756 172#ifdef HAVE_MKNOD
31e12522 173 return mknod(pathname, mode, dev);
da6eb9d1
WD
174#else
175 return -1;
f92ef572 176#endif
da6eb9d1 177}
31e12522 178
63cf5ae7 179int do_rmdir(const char *pathname)
31e12522
AT
180{
181 if (dry_run) return 0;
cfeed4da 182 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
183 return rmdir(pathname);
184}
185
63cf5ae7 186int do_open(const char *pathname, int flags, mode_t mode)
31e12522 187{
3420c8e6 188 if (flags != O_RDONLY) {
7a27e9b5 189 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 190 RETURN_ERROR_IF_RO_OR_LO;
3420c8e6 191 }
b0f3f578 192
e106de49 193 return open(pathname, flags | O_BINARY, mode);
31e12522 194}
7308bd66 195
4f5b0756 196#ifdef HAVE_CHMOD
7308bd66
AT
197int do_chmod(const char *path, mode_t mode)
198{
f0b4fdaf 199 int code;
7308bd66 200 if (dry_run) return 0;
cfeed4da 201 RETURN_ERROR_IF_RO_OR_LO;
d11f5c6e 202#ifdef HAVE_LCHMOD
42562649
WD
203 code = lchmod(path, mode & CHMOD_BITS);
204#else
205 if (S_ISLNK(mode)) {
206# if defined HAVE_SETATTRLIST
11b02d92
WD
207 struct attrlist attrList;
208 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
209
210 memset(&attrList, 0, sizeof attrList);
211 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
212 attrList.commonattr = ATTR_CMN_ACCESSMASK;
213 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
42562649 214# else
d11f5c6e 215 code = 1;
42562649 216# endif
d11f5c6e 217 } else
aa0e6b99 218 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
42562649 219#endif /* !HAVE_LCHMOD */
e35ad79b 220 if (code != 0 && (preserve_perms || preserve_executability))
ff0e1580 221 return code;
f0b4fdaf 222 return 0;
7308bd66 223}
f92ef572 224#endif
1b2d733a 225
63cf5ae7 226int do_rename(const char *fname1, const char *fname2)
1b2d733a
AT
227{
228 if (dry_run) return 0;
cfeed4da 229 RETURN_ERROR_IF_RO_OR_LO;
1b2d733a
AT
230 return rename(fname1, fname2);
231}
232
96e051c8
WD
233#ifdef HAVE_FTRUNCATE
234int do_ftruncate(int fd, OFF_T size)
235{
236 int ret;
237
238 if (dry_run) return 0;
239 RETURN_ERROR_IF_RO_OR_LO;
240
241 do {
242 ret = ftruncate(fd, size);
243 } while (ret < 0 && errno == EINTR);
244
245 return ret;
246}
247#endif
248
bf4e725d
MP
249void trim_trailing_slashes(char *name)
250{
c127e8aa 251 int l;
bf4e725d
MP
252 /* Some BSD systems cannot make a directory if the name
253 * contains a trailing slash.
254 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
0f78b815 255
c127e8aa
MP
256 /* Don't change empty string; and also we can't improve on
257 * "/" */
0f78b815 258
c127e8aa
MP
259 l = strlen(name);
260 while (l > 1) {
261 if (name[--l] != '/')
262 break;
263 name[l] = '\0';
bf4e725d
MP
264 }
265}
266
1b2d733a
AT
267int do_mkdir(char *fname, mode_t mode)
268{
cfeed4da
WD
269 if (dry_run) return 0;
270 RETURN_ERROR_IF_RO_OR_LO;
0f78b815 271 trim_trailing_slashes(fname);
1b2d733a
AT
272 return mkdir(fname, mode);
273}
274
f62c17e3
AT
275/* like mkstemp but forces permissions */
276int do_mkstemp(char *template, mode_t perms)
277{
7a27e9b5 278 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 279 RETURN_ERROR_IF(read_only, EROFS);
0379c8ec 280 perms |= S_IWUSR;
f62c17e3 281
4f5b0756 282#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
f62c17e3
AT
283 {
284 int fd = mkstemp(template);
cfeed4da
WD
285 if (fd == -1)
286 return -1;
287 if (fchmod(fd, perms) != 0 && preserve_perms) {
288 int errno_save = errno;
f62c17e3
AT
289 close(fd);
290 unlink(template);
cfeed4da 291 errno = errno_save;
f62c17e3
AT
292 return -1;
293 }
4f5b0756 294#if defined HAVE_SETMODE && O_BINARY
3267d6a9
WD
295 setmode(fd, O_BINARY);
296#endif
f62c17e3
AT
297 return fd;
298 }
299#else
cfeed4da
WD
300 if (!mktemp(template))
301 return -1;
25f2cb3d 302 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
f62c17e3 303#endif
1b2d733a 304}
bcacc18b
AT
305
306int do_stat(const char *fname, STRUCT_STAT *st)
307{
4f5b0756 308#ifdef USE_STAT64_FUNCS
bcacc18b
AT
309 return stat64(fname, st);
310#else
311 return stat(fname, st);
312#endif
313}
314
315int do_lstat(const char *fname, STRUCT_STAT *st)
316{
4f5b0756
WD
317#ifdef SUPPORT_LINKS
318# ifdef USE_STAT64_FUNCS
bcacc18b 319 return lstat64(fname, st);
0957a746 320# else
bcacc18b 321 return lstat(fname, st);
0957a746
WD
322# endif
323#else
324 return do_stat(fname, st);
bcacc18b
AT
325#endif
326}
327
328int do_fstat(int fd, STRUCT_STAT *st)
329{
4f5b0756 330#ifdef USE_STAT64_FUNCS
bcacc18b
AT
331 return fstat64(fd, st);
332#else
333 return fstat(fd, st);
334#endif
335}
73233f0f
AT
336
337OFF_T do_lseek(int fd, OFF_T offset, int whence)
338{
ebd33e0c 339#ifdef HAVE_LSEEK64
f853b777
WD
340#if !SIZEOF_OFF64_T
341 OFF_T lseek64();
342#else
f28ee65b 343 off64_t lseek64();
f853b777 344#endif
73233f0f
AT
345 return lseek64(fd, offset, whence);
346#else
347 return lseek(fd, offset, whence);
348#endif
349}
2a189350
WD
350
351#ifdef HAVE_UTIMENSAT
352int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
353{
354 struct timespec t[2];
355
356 if (dry_run) return 0;
357 RETURN_ERROR_IF_RO_OR_LO;
358
359 t[0].tv_sec = 0;
360 t[0].tv_nsec = UTIME_NOW;
361 t[1].tv_sec = modtime;
362 t[1].tv_nsec = mod_nsec;
363 return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
364}
365#endif
366
367#ifdef HAVE_LUTIMES
368int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
369{
370 struct timeval t[2];
371
372 if (dry_run) return 0;
373 RETURN_ERROR_IF_RO_OR_LO;
374
375 t[0].tv_sec = time(NULL);
376 t[0].tv_usec = 0;
377 t[1].tv_sec = modtime;
378 t[1].tv_usec = mod_nsec / 1000;
379 return lutimes(fname, t);
380}
381#endif
382
383#ifdef HAVE_UTIMES
384int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
385{
386 struct timeval t[2];
387
388 if (dry_run) return 0;
389 RETURN_ERROR_IF_RO_OR_LO;
390
391 t[0].tv_sec = time(NULL);
392 t[0].tv_usec = 0;
393 t[1].tv_sec = modtime;
394 t[1].tv_usec = mod_nsec / 1000;
395 return utimes(fname, t);
396}
397
398#elif defined HAVE_UTIME
399int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
400{
401#ifdef HAVE_STRUCT_UTIMBUF
402 struct utimbuf tbuf;
403#else
404 time_t t[2];
405#endif
406
407 if (dry_run) return 0;
408 RETURN_ERROR_IF_RO_OR_LO;
409
410# ifdef HAVE_STRUCT_UTIMBUF
411 tbuf.actime = time(NULL);
412 tbuf.modtime = modtime;
413 return utime(fname, &tbuf);
414# else
415 t[0] = time(NULL);
416 t[1] = modtime;
417 return utime(fname, t);
418# endif
419}
420
421#else
422#error Need utimes or utime function.
423#endif
28b519c9
WD
424
425#ifdef SUPPORT_PREALLOCATION
426int do_fallocate(int fd, OFF_T offset, OFF_T length)
427{
428#ifdef FALLOC_FL_KEEP_SIZE
429#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
430#else
431#define DO_FALLOC_OPTIONS 0
432#endif
433 RETURN_ERROR_IF(dry_run, 0);
434 RETURN_ERROR_IF_RO_OR_LO;
435#if defined HAVE_FALLOCATE
436 return fallocate(fd, DO_FALLOC_OPTIONS, offset, length);
437#elif defined HAVE_SYS_FALLOCATE
438 return syscall(SYS_fallocate, fd, DO_FALLOC_OPTIONS, (loff_t)offset, (loff_t)length);
439#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
440 return posix_fallocate(fd, offset, length);
441#else
442#error Coding error in SUPPORT_PREALLOCATION logic.
443#endif
444}
445#endif