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