Backed out changes to send_deflated_token() that surrounded the
[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;
7308bd66
AT
75 return lchown(path, owner, group);
76}
77
31e12522
AT
78int do_mknod(char *pathname, mode_t mode, dev_t dev)
79{
80 if (dry_run) return 0;
cfeed4da 81 RETURN_ERROR_IF_RO_OR_LO;
da6eb9d1
WD
82#if !MKNOD_CREATES_FIFOS && HAVE_MKFIFO
83 if (S_ISFIFO(mode))
84 return mkfifo(pathname, mode);
85#endif
86#if !MKNOD_CREATES_SOCKETS && HAVE_SYS_UN_H
87 if (S_ISSOCK(mode)) {
88 int sock;
89 struct sockaddr_un saddr;
90 unsigned int len;
91
92 saddr.sun_family = AF_UNIX;
93 len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
94#if HAVE_SOCKADDR_UN_LEN
95 saddr.sun_len = len >= sizeof saddr.sun_path
96 ? sizeof saddr.sun_path : len + 1;
97#endif
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);
104 return do_chmod(pathname, mode);
105 }
106#endif
107#if HAVE_MKNOD
31e12522 108 return mknod(pathname, mode, dev);
da6eb9d1
WD
109#else
110 return -1;
f92ef572 111#endif
da6eb9d1 112}
31e12522
AT
113
114int do_rmdir(char *pathname)
115{
116 if (dry_run) return 0;
cfeed4da 117 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
118 return rmdir(pathname);
119}
120
121int do_open(char *pathname, int flags, mode_t mode)
122{
3420c8e6 123 if (flags != O_RDONLY) {
7a27e9b5 124 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 125 RETURN_ERROR_IF_RO_OR_LO;
3420c8e6 126 }
b0f3f578 127
e106de49 128 return open(pathname, flags | O_BINARY, mode);
31e12522 129}
7308bd66 130
f92ef572 131#if HAVE_CHMOD
7308bd66
AT
132int do_chmod(const char *path, mode_t mode)
133{
f0b4fdaf 134 int code;
7308bd66 135 if (dry_run) return 0;
cfeed4da 136 RETURN_ERROR_IF_RO_OR_LO;
f0b4fdaf 137 code = chmod(path, mode);
cfeed4da 138 if (code != 0 && preserve_perms)
f0b4fdaf
DD
139 return code;
140 return 0;
7308bd66 141}
f92ef572 142#endif
1b2d733a
AT
143
144int do_rename(char *fname1, char *fname2)
145{
146 if (dry_run) return 0;
cfeed4da 147 RETURN_ERROR_IF_RO_OR_LO;
1b2d733a
AT
148 return rename(fname1, fname2);
149}
150
ded8347d 151
bf4e725d
MP
152void trim_trailing_slashes(char *name)
153{
c127e8aa 154 int l;
bf4e725d
MP
155 /* Some BSD systems cannot make a directory if the name
156 * contains a trailing slash.
157 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
c127e8aa
MP
158
159 /* Don't change empty string; and also we can't improve on
160 * "/" */
161
162 l = strlen(name);
163 while (l > 1) {
164 if (name[--l] != '/')
165 break;
166 name[l] = '\0';
bf4e725d
MP
167 }
168}
169
170
1b2d733a
AT
171int do_mkdir(char *fname, mode_t mode)
172{
cfeed4da
WD
173 if (dry_run) return 0;
174 RETURN_ERROR_IF_RO_OR_LO;
bf4e725d 175 trim_trailing_slashes(fname);
1b2d733a
AT
176 return mkdir(fname, mode);
177}
178
ded8347d 179
f62c17e3
AT
180/* like mkstemp but forces permissions */
181int do_mkstemp(char *template, mode_t perms)
182{
7a27e9b5 183 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 184 RETURN_ERROR_IF(read_only, EROFS);
f62c17e3 185
6dcb9320 186#if HAVE_SECURE_MKSTEMP && HAVE_FCHMOD && (!HAVE_OPEN64 || HAVE_MKSTEMP64)
f62c17e3
AT
187 {
188 int fd = mkstemp(template);
cfeed4da
WD
189 if (fd == -1)
190 return -1;
191 if (fchmod(fd, perms) != 0 && preserve_perms) {
192 int errno_save = errno;
f62c17e3
AT
193 close(fd);
194 unlink(template);
cfeed4da 195 errno = errno_save;
f62c17e3
AT
196 return -1;
197 }
3267d6a9
WD
198#if HAVE_SETMODE && O_BINARY
199 setmode(fd, O_BINARY);
200#endif
f62c17e3
AT
201 return fd;
202 }
203#else
cfeed4da
WD
204 if (!mktemp(template))
205 return -1;
25f2cb3d 206 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
f62c17e3 207#endif
1b2d733a 208}
bcacc18b
AT
209
210int do_stat(const char *fname, STRUCT_STAT *st)
211{
212#if HAVE_OFF64_T
213 return stat64(fname, st);
214#else
215 return stat(fname, st);
216#endif
217}
218
efb2f6bf 219#if SUPPORT_LINKS
bcacc18b
AT
220int do_lstat(const char *fname, STRUCT_STAT *st)
221{
222#if HAVE_OFF64_T
223 return lstat64(fname, st);
224#else
225 return lstat(fname, st);
226#endif
227}
efb2f6bf 228#endif
bcacc18b
AT
229
230int do_fstat(int fd, STRUCT_STAT *st)
231{
232#if HAVE_OFF64_T
233 return fstat64(fd, st);
234#else
235 return fstat(fd, st);
236#endif
237}
73233f0f
AT
238
239OFF_T do_lseek(int fd, OFF_T offset, int whence)
240{
241#if HAVE_OFF64_T
f28ee65b 242 off64_t lseek64();
73233f0f
AT
243 return lseek64(fd, offset, whence);
244#else
245 return lseek(fd, offset, whence);
246#endif
247}
d6e6ecbd 248
bb0f7089 249#ifdef USE_MMAP
754d120c
AT
250void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
251{
252#if HAVE_OFF64_T
253 return mmap64(start, len, prot, flags, fd, offset);
254#else
255 return mmap(start, len, prot, flags, fd, offset);
256#endif
257}
258#endif
259
d6e6ecbd
AT
260char *d_name(struct dirent *di)
261{
59503278
AT
262#if HAVE_BROKEN_READDIR
263 return (di->d_name - 2);
264#else
d6e6ecbd 265 return di->d_name;
59503278 266#endif
d6e6ecbd 267}