Switching to GPL 3.
[rsync/rsync.git] / syscall.c
... / ...
CommitLineData
1/*
2 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
4 *
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003-2007 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 3 as
11 * published by the Free Software Foundation.
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 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23
24#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
25#include <sys/un.h>
26#endif
27
28extern int dry_run;
29extern int am_root;
30extern int read_only;
31extern int list_only;
32extern int preserve_perms;
33
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)
43
44int do_unlink(const char *fname)
45{
46 if (dry_run) return 0;
47 RETURN_ERROR_IF_RO_OR_LO;
48 return unlink(fname);
49}
50
51int do_symlink(const char *fname1, const char *fname2)
52{
53 if (dry_run) return 0;
54 RETURN_ERROR_IF_RO_OR_LO;
55 return symlink(fname1, fname2);
56}
57
58#ifdef HAVE_LINK
59int do_link(const char *fname1, const char *fname2)
60{
61 if (dry_run) return 0;
62 RETURN_ERROR_IF_RO_OR_LO;
63 return link(fname1, fname2);
64}
65#endif
66
67int do_lchown(const char *path, uid_t owner, gid_t group)
68{
69 if (dry_run) return 0;
70 RETURN_ERROR_IF_RO_OR_LO;
71#ifndef HAVE_LCHOWN
72#define lchown chown
73#endif
74 return lchown(path, owner, group);
75}
76
77int do_mknod(const char *pathname, mode_t mode, dev_t dev)
78{
79 if (dry_run) return 0;
80 RETURN_ERROR_IF_RO_OR_LO;
81
82 /* For --fake-super, we create a normal file with mode 0600. */
83 if (am_root < 0) {
84 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
85 if (fd < 0 || close(fd) < 0)
86 return -1;
87 return 0;
88 }
89
90#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
91 if (S_ISFIFO(mode))
92 return mkfifo(pathname, mode);
93#endif
94#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
95 if (S_ISSOCK(mode)) {
96 int sock;
97 struct sockaddr_un saddr;
98#ifdef HAVE_SOCKADDR_UN_LEN
99 unsigned int len =
100#endif
101 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
102#ifdef HAVE_SOCKADDR_UN_LEN
103 saddr.sun_len = len >= sizeof saddr.sun_path
104 ? sizeof saddr.sun_path : len + 1;
105#endif
106 saddr.sun_family = AF_UNIX;
107
108 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
109 || (unlink(pathname) < 0 && errno != ENOENT)
110 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
111 return -1;
112 close(sock);
113#ifdef HAVE_CHMOD
114 return do_chmod(pathname, mode);
115#else
116 return 0;
117#endif
118 }
119#endif
120#ifdef HAVE_MKNOD
121 return mknod(pathname, mode, dev);
122#else
123 return -1;
124#endif
125}
126
127int do_rmdir(const char *pathname)
128{
129 if (dry_run) return 0;
130 RETURN_ERROR_IF_RO_OR_LO;
131 return rmdir(pathname);
132}
133
134int do_open(const char *pathname, int flags, mode_t mode)
135{
136 if (flags != O_RDONLY) {
137 RETURN_ERROR_IF(dry_run, 0);
138 RETURN_ERROR_IF_RO_OR_LO;
139 }
140
141 return open(pathname, flags | O_BINARY, mode);
142}
143
144#ifdef HAVE_CHMOD
145int do_chmod(const char *path, mode_t mode)
146{
147 int code;
148 if (dry_run) return 0;
149 RETURN_ERROR_IF_RO_OR_LO;
150 if (S_ISLNK(mode)) {
151#ifdef HAVE_LCHMOD
152 code = lchmod(path, mode & CHMOD_BITS);
153#else
154 code = 1;
155#endif
156 } else
157 code = chmod(path, mode & CHMOD_BITS);
158 if (code != 0 && preserve_perms)
159 return code;
160 return 0;
161}
162#endif
163
164int do_rename(const char *fname1, const char *fname2)
165{
166 if (dry_run) return 0;
167 RETURN_ERROR_IF_RO_OR_LO;
168 return rename(fname1, fname2);
169}
170
171void trim_trailing_slashes(char *name)
172{
173 int l;
174 /* Some BSD systems cannot make a directory if the name
175 * contains a trailing slash.
176 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
177
178 /* Don't change empty string; and also we can't improve on
179 * "/" */
180
181 l = strlen(name);
182 while (l > 1) {
183 if (name[--l] != '/')
184 break;
185 name[l] = '\0';
186 }
187}
188
189int do_mkdir(char *fname, mode_t mode)
190{
191 if (dry_run) return 0;
192 RETURN_ERROR_IF_RO_OR_LO;
193 trim_trailing_slashes(fname);
194 return mkdir(fname, mode);
195}
196
197/* like mkstemp but forces permissions */
198int do_mkstemp(char *template, mode_t perms)
199{
200 RETURN_ERROR_IF(dry_run, 0);
201 RETURN_ERROR_IF(read_only, EROFS);
202
203#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
204 {
205 int fd = mkstemp(template);
206 if (fd == -1)
207 return -1;
208 if (fchmod(fd, perms) != 0 && preserve_perms) {
209 int errno_save = errno;
210 close(fd);
211 unlink(template);
212 errno = errno_save;
213 return -1;
214 }
215#if defined HAVE_SETMODE && O_BINARY
216 setmode(fd, O_BINARY);
217#endif
218 return fd;
219 }
220#else
221 if (!mktemp(template))
222 return -1;
223 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
224#endif
225}
226
227int do_stat(const char *fname, STRUCT_STAT *st)
228{
229#ifdef USE_STAT64_FUNCS
230 return stat64(fname, st);
231#else
232 return stat(fname, st);
233#endif
234}
235
236int do_lstat(const char *fname, STRUCT_STAT *st)
237{
238#ifdef SUPPORT_LINKS
239# ifdef USE_STAT64_FUNCS
240 return lstat64(fname, st);
241# else
242 return lstat(fname, st);
243# endif
244#else
245 return do_stat(fname, st);
246#endif
247}
248
249int do_fstat(int fd, STRUCT_STAT *st)
250{
251#ifdef USE_STAT64_FUNCS
252 return fstat64(fd, st);
253#else
254 return fstat(fd, st);
255#endif
256}
257
258OFF_T do_lseek(int fd, OFF_T offset, int whence)
259{
260#ifdef HAVE_LSEEK64
261#if !SIZEOF_OFF64_T
262 OFF_T lseek64();
263#else
264 off64_t lseek64();
265#endif
266 return lseek64(fd, offset, whence);
267#else
268 return lseek(fd, offset, whence);
269#endif
270}
271
272char *d_name(struct dirent *di)
273{
274#ifdef HAVE_BROKEN_READDIR
275 return (di->d_name - 2);
276#else
277 return di->d_name;
278#endif
279}