Switching to GPL 3.
[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
4fd842f9 10 * it under the terms of the GNU General Public License version 3 as
ba2133d6 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 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
31e12522
AT
21
22#include "rsync.h"
23
4f5b0756 24#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
da6eb9d1
WD
25#include <sys/un.h>
26#endif
27
31e12522 28extern int dry_run;
9439c0cb 29extern int am_root;
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;
9439c0cb
WD
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
4f5b0756 90#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
da6eb9d1
WD
91 if (S_ISFIFO(mode))
92 return mkfifo(pathname, mode);
93#endif
4f5b0756 94#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
da6eb9d1
WD
95 if (S_ISSOCK(mode)) {
96 int sock;
97 struct sockaddr_un saddr;
4f375592
WD
98#ifdef HAVE_SOCKADDR_UN_LEN
99 unsigned int len =
100#endif
101 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
4f5b0756 102#ifdef HAVE_SOCKADDR_UN_LEN
da6eb9d1
WD
103 saddr.sun_len = len >= sizeof saddr.sun_path
104 ? sizeof saddr.sun_path : len + 1;
105#endif
4f375592 106 saddr.sun_family = AF_UNIX;
da6eb9d1
WD
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);
d11f5c6e 113#ifdef HAVE_CHMOD
4fd842f9 114 return do_chmod(pathname, mode);
d11f5c6e
WD
115#else
116 return 0;
117#endif
da6eb9d1
WD
118 }
119#endif
4f5b0756 120#ifdef HAVE_MKNOD
31e12522 121 return mknod(pathname, mode, dev);
da6eb9d1
WD
122#else
123 return -1;
f92ef572 124#endif
da6eb9d1 125}
31e12522 126
63cf5ae7 127int do_rmdir(const char *pathname)
31e12522
AT
128{
129 if (dry_run) return 0;
cfeed4da 130 RETURN_ERROR_IF_RO_OR_LO;
31e12522
AT
131 return rmdir(pathname);
132}
133
63cf5ae7 134int do_open(const char *pathname, int flags, mode_t mode)
31e12522 135{
3420c8e6 136 if (flags != O_RDONLY) {
7a27e9b5 137 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 138 RETURN_ERROR_IF_RO_OR_LO;
3420c8e6 139 }
b0f3f578 140
e106de49 141 return open(pathname, flags | O_BINARY, mode);
31e12522 142}
7308bd66 143
4f5b0756 144#ifdef HAVE_CHMOD
7308bd66
AT
145int do_chmod(const char *path, mode_t mode)
146{
f0b4fdaf 147 int code;
7308bd66 148 if (dry_run) return 0;
cfeed4da 149 RETURN_ERROR_IF_RO_OR_LO;
d11f5c6e
WD
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);
cfeed4da 158 if (code != 0 && preserve_perms)
f0b4fdaf
DD
159 return code;
160 return 0;
7308bd66 161}
f92ef572 162#endif
1b2d733a 163
63cf5ae7 164int do_rename(const char *fname1, const char *fname2)
1b2d733a
AT
165{
166 if (dry_run) return 0;
cfeed4da 167 RETURN_ERROR_IF_RO_OR_LO;
1b2d733a
AT
168 return rename(fname1, fname2);
169}
170
bf4e725d
MP
171void trim_trailing_slashes(char *name)
172{
c127e8aa 173 int l;
bf4e725d
MP
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> */
0f78b815 177
c127e8aa
MP
178 /* Don't change empty string; and also we can't improve on
179 * "/" */
0f78b815 180
c127e8aa
MP
181 l = strlen(name);
182 while (l > 1) {
183 if (name[--l] != '/')
184 break;
185 name[l] = '\0';
bf4e725d
MP
186 }
187}
188
1b2d733a
AT
189int do_mkdir(char *fname, mode_t mode)
190{
cfeed4da
WD
191 if (dry_run) return 0;
192 RETURN_ERROR_IF_RO_OR_LO;
0f78b815 193 trim_trailing_slashes(fname);
1b2d733a
AT
194 return mkdir(fname, mode);
195}
196
f62c17e3
AT
197/* like mkstemp but forces permissions */
198int do_mkstemp(char *template, mode_t perms)
199{
7a27e9b5 200 RETURN_ERROR_IF(dry_run, 0);
cfeed4da 201 RETURN_ERROR_IF(read_only, EROFS);
f62c17e3 202
4f5b0756 203#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
f62c17e3
AT
204 {
205 int fd = mkstemp(template);
cfeed4da
WD
206 if (fd == -1)
207 return -1;
208 if (fchmod(fd, perms) != 0 && preserve_perms) {
209 int errno_save = errno;
f62c17e3
AT
210 close(fd);
211 unlink(template);
cfeed4da 212 errno = errno_save;
f62c17e3
AT
213 return -1;
214 }
4f5b0756 215#if defined HAVE_SETMODE && O_BINARY
3267d6a9
WD
216 setmode(fd, O_BINARY);
217#endif
f62c17e3
AT
218 return fd;
219 }
220#else
cfeed4da
WD
221 if (!mktemp(template))
222 return -1;
25f2cb3d 223 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
f62c17e3 224#endif
1b2d733a 225}
bcacc18b
AT
226
227int do_stat(const char *fname, STRUCT_STAT *st)
228{
4f5b0756 229#ifdef USE_STAT64_FUNCS
bcacc18b
AT
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{
4f5b0756
WD
238#ifdef SUPPORT_LINKS
239# ifdef USE_STAT64_FUNCS
bcacc18b 240 return lstat64(fname, st);
0957a746 241# else
bcacc18b 242 return lstat(fname, st);
0957a746
WD
243# endif
244#else
245 return do_stat(fname, st);
bcacc18b
AT
246#endif
247}
248
249int do_fstat(int fd, STRUCT_STAT *st)
250{
4f5b0756 251#ifdef USE_STAT64_FUNCS
bcacc18b
AT
252 return fstat64(fd, st);
253#else
254 return fstat(fd, st);
255#endif
256}
73233f0f
AT
257
258OFF_T do_lseek(int fd, OFF_T offset, int whence)
259{
ebd33e0c 260#ifdef HAVE_LSEEK64
f853b777
WD
261#if !SIZEOF_OFF64_T
262 OFF_T lseek64();
263#else
f28ee65b 264 off64_t lseek64();
f853b777 265#endif
73233f0f
AT
266 return lseek64(fd, offset, whence);
267#else
268 return lseek(fd, offset, whence);
269#endif
270}
d6e6ecbd
AT
271
272char *d_name(struct dirent *di)
273{
4f5b0756 274#ifdef HAVE_BROKEN_READDIR
59503278
AT
275 return (di->d_name - 2);
276#else
d6e6ecbd 277 return di->d_name;
59503278 278#endif
d6e6ecbd 279}