Stop password errors from getting reported as transfer errors.
[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
ba2133d6 7 * Copyright (C) 2003-2007 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
31e12522 32extern int dry_run;
9439c0cb 33extern int am_root;
f0fca04e 34extern int read_only;
2b086e03 35extern int list_only;
f0b4fdaf 36extern int preserve_perms;
f0fca04e 37
cfeed4da
WD
38#define RETURN_ERROR_IF(x,e) \
39 do { \
40 if (x) { \
41 errno = (e); \
42 return -1; \
43 } \
44 } while (0)
45
46#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
31e12522 47
63cf5ae7 48int do_unlink(const char *fname)
31e12522
AT
49{
50 if (dry_run) return 0;
cfeed4da 51 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
52 return unlink(fname);
53}
54
63cf5ae7 55int do_symlink(const char *fname1, const char *fname2)
31e12522
AT
56{
57 if (dry_run) return 0;
cfeed4da 58 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
59 return symlink(fname1, fname2);
60}
61
4f5b0756 62#ifdef HAVE_LINK
63cf5ae7 63int do_link(const char *fname1, const char *fname2)
31e12522
AT
64{
65 if (dry_run) return 0;
cfeed4da 66 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
67 return link(fname1, fname2);
68}
f92ef572 69#endif
31e12522 70
7308bd66
AT
71int do_lchown(const char *path, uid_t owner, gid_t group)
72{
73 if (dry_run) return 0;
cfeed4da 74 RETURN_ERROR_IF_RO_OR_LO;
4f5b0756 75#ifndef HAVE_LCHOWN
05154760
WD
76#define lchown chown
77#endif
7308bd66
AT
78 return lchown(path, owner, group);
79}
80
4a19c3b2 81int do_mknod(const char *pathname, mode_t mode, dev_t dev)
31e12522
AT
82{
83 if (dry_run) return 0;
cfeed4da 84 RETURN_ERROR_IF_RO_OR_LO;
9439c0cb
WD
85
86 /* For --fake-super, we create a normal file with mode 0600. */
87 if (am_root < 0) {
88 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
89 if (fd < 0 || close(fd) < 0)
90 return -1;
91 return 0;
92 }
93
4f5b0756 94#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
da6eb9d1
WD
95 if (S_ISFIFO(mode))
96 return mkfifo(pathname, mode);
97#endif
4f5b0756 98#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
da6eb9d1
WD
99 if (S_ISSOCK(mode)) {
100 int sock;
101 struct sockaddr_un saddr;
4f375592
WD
102#ifdef HAVE_SOCKADDR_UN_LEN
103 unsigned int len =
104#endif
105 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
4f5b0756 106#ifdef HAVE_SOCKADDR_UN_LEN
da6eb9d1
WD
107 saddr.sun_len = len >= sizeof saddr.sun_path
108 ? sizeof saddr.sun_path : len + 1;
109#endif
4f375592 110 saddr.sun_family = AF_UNIX;
da6eb9d1
WD
111
112 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
113 || (unlink(pathname) < 0 && errno != ENOENT)
114 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
115 return -1;
116 close(sock);
d11f5c6e 117#ifdef HAVE_CHMOD
4fd842f9 118 return do_chmod(pathname, mode);
d11f5c6e
WD
119#else
120 return 0;
121#endif
da6eb9d1
WD
122 }
123#endif
4f5b0756 124#ifdef HAVE_MKNOD
31e12522 125 return mknod(pathname, mode, dev);
da6eb9d1
WD
126#else
127 return -1;
f92ef572 128#endif
da6eb9d1 129}
31e12522 130
63cf5ae7 131int do_rmdir(const char *pathname)
31e12522
AT
132{
133 if (dry_run) return 0;
cfeed4da 134 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
135 return rmdir(pathname);
136}
137
63cf5ae7 138int do_open(const char *pathname, int flags, mode_t mode)
31e12522 139{
3420c8e6 140 if (flags != O_RDONLY) {
7a27e9b5 141 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 142 RETURN_ERROR_IF_RO_OR_LO;
3420c8e6 143 }
b0f3f578 144
e106de49 145 return open(pathname, flags | O_BINARY, mode);
31e12522 146}
7308bd66 147
4f5b0756 148#ifdef HAVE_CHMOD
7308bd66
AT
149int do_chmod(const char *path, mode_t mode)
150{
f0b4fdaf 151 int code;
7308bd66 152 if (dry_run) return 0;
cfeed4da 153 RETURN_ERROR_IF_RO_OR_LO;
d11f5c6e
WD
154 if (S_ISLNK(mode)) {
155#ifdef HAVE_LCHMOD
156 code = lchmod(path, mode & CHMOD_BITS);
11b02d92
WD
157#elif defined HAVE_SETATTRLIST
158 struct attrlist attrList;
159 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
160
161 memset(&attrList, 0, sizeof attrList);
162 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
163 attrList.commonattr = ATTR_CMN_ACCESSMASK;
164 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
d11f5c6e
WD
165#else
166 code = 1;
167#endif
168 } else
169 code = chmod(path, mode & CHMOD_BITS);
cfeed4da 170 if (code != 0 && preserve_perms)
f0b4fdaf
DD
171 return code;
172 return 0;
7308bd66 173}
f92ef572 174#endif
1b2d733a 175
63cf5ae7 176int do_rename(const char *fname1, const char *fname2)
1b2d733a
AT
177{
178 if (dry_run) return 0;
cfeed4da 179 RETURN_ERROR_IF_RO_OR_LO;
1b2d733a
AT
180 return rename(fname1, fname2);
181}
182
bf4e725d
MP
183void trim_trailing_slashes(char *name)
184{
c127e8aa 185 int l;
bf4e725d
MP
186 /* Some BSD systems cannot make a directory if the name
187 * contains a trailing slash.
188 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
0f78b815 189
c127e8aa
MP
190 /* Don't change empty string; and also we can't improve on
191 * "/" */
0f78b815 192
c127e8aa
MP
193 l = strlen(name);
194 while (l > 1) {
195 if (name[--l] != '/')
196 break;
197 name[l] = '\0';
bf4e725d
MP
198 }
199}
200
1b2d733a
AT
201int do_mkdir(char *fname, mode_t mode)
202{
cfeed4da
WD
203 if (dry_run) return 0;
204 RETURN_ERROR_IF_RO_OR_LO;
0f78b815 205 trim_trailing_slashes(fname);
1b2d733a
AT
206 return mkdir(fname, mode);
207}
208
f62c17e3
AT
209/* like mkstemp but forces permissions */
210int do_mkstemp(char *template, mode_t perms)
211{
7a27e9b5 212 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 213 RETURN_ERROR_IF(read_only, EROFS);
f62c17e3 214
4f5b0756 215#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
f62c17e3
AT
216 {
217 int fd = mkstemp(template);
cfeed4da
WD
218 if (fd == -1)
219 return -1;
220 if (fchmod(fd, perms) != 0 && preserve_perms) {
221 int errno_save = errno;
f62c17e3
AT
222 close(fd);
223 unlink(template);
cfeed4da 224 errno = errno_save;
f62c17e3
AT
225 return -1;
226 }
4f5b0756 227#if defined HAVE_SETMODE && O_BINARY
3267d6a9
WD
228 setmode(fd, O_BINARY);
229#endif
f62c17e3
AT
230 return fd;
231 }
232#else
cfeed4da
WD
233 if (!mktemp(template))
234 return -1;
25f2cb3d 235 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
f62c17e3 236#endif
1b2d733a 237}
bcacc18b
AT
238
239int do_stat(const char *fname, STRUCT_STAT *st)
240{
4f5b0756 241#ifdef USE_STAT64_FUNCS
bcacc18b
AT
242 return stat64(fname, st);
243#else
244 return stat(fname, st);
245#endif
246}
247
248int do_lstat(const char *fname, STRUCT_STAT *st)
249{
4f5b0756
WD
250#ifdef SUPPORT_LINKS
251# ifdef USE_STAT64_FUNCS
bcacc18b 252 return lstat64(fname, st);
0957a746 253# else
bcacc18b 254 return lstat(fname, st);
0957a746
WD
255# endif
256#else
257 return do_stat(fname, st);
bcacc18b
AT
258#endif
259}
260
261int do_fstat(int fd, STRUCT_STAT *st)
262{
4f5b0756 263#ifdef USE_STAT64_FUNCS
bcacc18b
AT
264 return fstat64(fd, st);
265#else
266 return fstat(fd, st);
267#endif
268}
73233f0f
AT
269
270OFF_T do_lseek(int fd, OFF_T offset, int whence)
271{
ebd33e0c 272#ifdef HAVE_LSEEK64
f853b777
WD
273#if !SIZEOF_OFF64_T
274 OFF_T lseek64();
275#else
f28ee65b 276 off64_t lseek64();
f853b777 277#endif
73233f0f
AT
278 return lseek64(fd, offset, whence);
279#else
280 return lseek(fd, offset, whence);
281#endif
282}
d6e6ecbd
AT
283
284char *d_name(struct dirent *di)
285{
4f5b0756 286#ifdef HAVE_BROKEN_READDIR
59503278
AT
287 return (di->d_name - 2);
288#else
d6e6ecbd 289 return di->d_name;
59503278 290#endif
d6e6ecbd 291}