Mention the latest changes.
[rsync/rsync.git] / syscall.c
CommitLineData
31e12522
AT
1/*
2 Copyright (C) Andrew Tridgell 1998
ded8347d 3 Copyright (C) 2002 by Martin Pool
31e12522
AT
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
ded8347d
MP
20/**
21 * @file syscall.c
22 *
23 * Syscall wrappers to ensure that nothing gets done in dry_run mode
24 * and to handle system peculiarities.
25 **/
31e12522
AT
26
27#include "rsync.h"
28
da6eb9d1
WD
29#if !MKNOD_CREATES_SOCKETS && HAVE_SYS_UN_H
30#include <sys/un.h>
31#endif
32
31e12522 33extern int dry_run;
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
AT
47
48int do_unlink(char *fname)
49{
50 if (dry_run) return 0;
cfeed4da 51 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
52 return unlink(fname);
53}
54
55int do_symlink(char *fname1, char *fname2)
56{
57 if (dry_run) return 0;
cfeed4da 58 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
59 return symlink(fname1, fname2);
60}
61
f92ef572 62#if HAVE_LINK
31e12522
AT
63int do_link(char *fname1, char *fname2)
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;
05154760
WD
75#ifdef HAVE_LCHOWN
76#define lchown chown
77#endif
7308bd66
AT
78 return lchown(path, owner, group);
79}
80
31e12522
AT
81int do_mknod(char *pathname, mode_t mode, dev_t dev)
82{
83 if (dry_run) return 0;
cfeed4da 84 RETURN_ERROR_IF_RO_OR_LO;
da6eb9d1
WD
85#if !MKNOD_CREATES_FIFOS && HAVE_MKFIFO
86 if (S_ISFIFO(mode))
87 return mkfifo(pathname, mode);
88#endif
89#if !MKNOD_CREATES_SOCKETS && HAVE_SYS_UN_H
90 if (S_ISSOCK(mode)) {
91 int sock;
92 struct sockaddr_un saddr;
93 unsigned int len;
94
95 saddr.sun_family = AF_UNIX;
96 len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
97#if HAVE_SOCKADDR_UN_LEN
98 saddr.sun_len = len >= sizeof saddr.sun_path
99 ? sizeof saddr.sun_path : len + 1;
100#endif
101
102 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
103 || (unlink(pathname) < 0 && errno != ENOENT)
104 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
105 return -1;
106 close(sock);
107 return do_chmod(pathname, mode);
108 }
109#endif
110#if HAVE_MKNOD
31e12522 111 return mknod(pathname, mode, dev);
da6eb9d1
WD
112#else
113 return -1;
f92ef572 114#endif
da6eb9d1 115}
31e12522
AT
116
117int do_rmdir(char *pathname)
118{
119 if (dry_run) return 0;
cfeed4da 120 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
121 return rmdir(pathname);
122}
123
124int do_open(char *pathname, int flags, mode_t mode)
125{
3420c8e6 126 if (flags != O_RDONLY) {
7a27e9b5 127 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 128 RETURN_ERROR_IF_RO_OR_LO;
3420c8e6 129 }
b0f3f578 130
e106de49 131 return open(pathname, flags | O_BINARY, mode);
31e12522 132}
7308bd66 133
f92ef572 134#if HAVE_CHMOD
7308bd66
AT
135int do_chmod(const char *path, mode_t mode)
136{
f0b4fdaf 137 int code;
7308bd66 138 if (dry_run) return 0;
cfeed4da 139 RETURN_ERROR_IF_RO_OR_LO;
f0b4fdaf 140 code = chmod(path, mode);
cfeed4da 141 if (code != 0 && preserve_perms)
f0b4fdaf
DD
142 return code;
143 return 0;
7308bd66 144}
f92ef572 145#endif
1b2d733a
AT
146
147int do_rename(char *fname1, char *fname2)
148{
149 if (dry_run) return 0;
cfeed4da 150 RETURN_ERROR_IF_RO_OR_LO;
1b2d733a
AT
151 return rename(fname1, fname2);
152}
153
ded8347d 154
bf4e725d
MP
155void trim_trailing_slashes(char *name)
156{
c127e8aa 157 int l;
bf4e725d
MP
158 /* Some BSD systems cannot make a directory if the name
159 * contains a trailing slash.
160 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
c127e8aa
MP
161
162 /* Don't change empty string; and also we can't improve on
163 * "/" */
164
165 l = strlen(name);
166 while (l > 1) {
167 if (name[--l] != '/')
168 break;
169 name[l] = '\0';
bf4e725d
MP
170 }
171}
172
173
1b2d733a
AT
174int do_mkdir(char *fname, mode_t mode)
175{
cfeed4da
WD
176 if (dry_run) return 0;
177 RETURN_ERROR_IF_RO_OR_LO;
bf4e725d 178 trim_trailing_slashes(fname);
1b2d733a
AT
179 return mkdir(fname, mode);
180}
181
ded8347d 182
f62c17e3
AT
183/* like mkstemp but forces permissions */
184int do_mkstemp(char *template, mode_t perms)
185{
7a27e9b5 186 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 187 RETURN_ERROR_IF(read_only, EROFS);
f62c17e3 188
6dcb9320 189#if HAVE_SECURE_MKSTEMP && HAVE_FCHMOD && (!HAVE_OPEN64 || HAVE_MKSTEMP64)
f62c17e3
AT
190 {
191 int fd = mkstemp(template);
cfeed4da
WD
192 if (fd == -1)
193 return -1;
194 if (fchmod(fd, perms) != 0 && preserve_perms) {
195 int errno_save = errno;
f62c17e3
AT
196 close(fd);
197 unlink(template);
cfeed4da 198 errno = errno_save;
f62c17e3
AT
199 return -1;
200 }
3267d6a9
WD
201#if HAVE_SETMODE && O_BINARY
202 setmode(fd, O_BINARY);
203#endif
f62c17e3
AT
204 return fd;
205 }
206#else
cfeed4da
WD
207 if (!mktemp(template))
208 return -1;
25f2cb3d 209 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
f62c17e3 210#endif
1b2d733a 211}
bcacc18b
AT
212
213int do_stat(const char *fname, STRUCT_STAT *st)
214{
215#if HAVE_OFF64_T
216 return stat64(fname, st);
217#else
218 return stat(fname, st);
219#endif
220}
221
efb2f6bf 222#if SUPPORT_LINKS
bcacc18b
AT
223int do_lstat(const char *fname, STRUCT_STAT *st)
224{
225#if HAVE_OFF64_T
226 return lstat64(fname, st);
227#else
228 return lstat(fname, st);
229#endif
230}
efb2f6bf 231#endif
bcacc18b
AT
232
233int do_fstat(int fd, STRUCT_STAT *st)
234{
235#if HAVE_OFF64_T
236 return fstat64(fd, st);
237#else
238 return fstat(fd, st);
239#endif
240}
73233f0f
AT
241
242OFF_T do_lseek(int fd, OFF_T offset, int whence)
243{
244#if HAVE_OFF64_T
f28ee65b 245 off64_t lseek64();
73233f0f
AT
246 return lseek64(fd, offset, whence);
247#else
248 return lseek(fd, offset, whence);
249#endif
250}
d6e6ecbd 251
bb0f7089 252#ifdef USE_MMAP
754d120c
AT
253void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
254{
255#if HAVE_OFF64_T
256 return mmap64(start, len, prot, flags, fd, offset);
257#else
258 return mmap(start, len, prot, flags, fd, offset);
259#endif
260}
261#endif
262
d6e6ecbd
AT
263char *d_name(struct dirent *di)
264{
59503278
AT
265#if HAVE_BROKEN_READDIR
266 return (di->d_name - 2);
267#else
d6e6ecbd 268 return di->d_name;
59503278 269#endif
d6e6ecbd 270}