blob: 868e00d33b2ede3cc392fed1b5d94fe0455da098 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
Martin Poolded83472002-02-18 22:44:23 +00002 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
Wayne Davison0f78b812006-04-25 20:23:34 +00004 *
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003, 2004, 2005, 2006 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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Wayne Davisone7c67062006-04-25 23:51:12 +000019 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Wayne Davison0f78b812006-04-25 20:23:34 +000022 */
Andrew Tridgell31e12521998-03-23 13:25:30 +000023
24#include "rsync.h"
25
Wayne Davison4f5b0752005-02-14 00:53:43 +000026#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000027#include <sys/un.h>
28#endif
29
Andrew Tridgell31e12521998-03-23 13:25:30 +000030extern int dry_run;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000031extern int read_only;
Andrew Tridgell2b086e01998-12-05 01:56:45 +000032extern int list_only;
David Dykstraf0b4fda2003-01-21 00:58:50 +000033extern int preserve_perms;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000034
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000035#define RETURN_ERROR_IF(x,e) \
36 do { \
37 if (x) { \
38 errno = (e); \
39 return -1; \
40 } \
41 } while (0)
42
43#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
Andrew Tridgell31e12521998-03-23 13:25:30 +000044
Wayne Davison63cf5ae2006-01-29 18:52:53 +000045int do_unlink(const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000046{
47 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000048 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000049 return unlink(fname);
50}
51
Wayne Davison63cf5ae2006-01-29 18:52:53 +000052int do_symlink(const char *fname1, const char *fname2)
Andrew Tridgell31e12521998-03-23 13:25:30 +000053{
54 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000055 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000056 return symlink(fname1, fname2);
57}
58
Wayne Davison4f5b0752005-02-14 00:53:43 +000059#ifdef HAVE_LINK
Wayne Davison63cf5ae2006-01-29 18:52:53 +000060int do_link(const char *fname1, const char *fname2)
Andrew Tridgell31e12521998-03-23 13:25:30 +000061{
62 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000063 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000064 return link(fname1, fname2);
65}
Andrew Tridgellf92ef571998-03-24 06:42:11 +000066#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +000067
Andrew Tridgell7308bd61998-03-24 06:39:16 +000068int do_lchown(const char *path, uid_t owner, gid_t group)
69{
70 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000071 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +000072#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +000073#define lchown chown
74#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +000075 return lchown(path, owner, group);
76}
77
Andrew Tridgell31e12521998-03-23 13:25:30 +000078int do_mknod(char *pathname, mode_t mode, dev_t dev)
79{
80 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000081 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +000082#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000083 if (S_ISFIFO(mode))
84 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +000085#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +000086#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000087 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);
Wayne Davison4f5b0752005-02-14 00:53:43 +000094#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000095 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);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000104#ifdef HAVE_CHMOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000105 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000106#else
107 return 0;
108#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000109 }
110#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000111#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000112 return mknod(pathname, mode, dev);
113#else
114 return -1;
115#endif
116}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000117
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000118int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000119{
120 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000121 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000122 return rmdir(pathname);
123}
124
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000125int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000126{
David Dykstra3420c8e1999-11-04 15:43:38 +0000127 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000128 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000129 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000130 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000131
Wayne Davisone106de42003-03-30 23:00:51 +0000132 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000133}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000134
Wayne Davison4f5b0752005-02-14 00:53:43 +0000135#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000136int do_chmod(const char *path, mode_t mode)
137{
David Dykstraf0b4fda2003-01-21 00:58:50 +0000138 int code;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000139 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000140 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisond11f5c62005-07-27 23:30:53 +0000141 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);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000149 if (code != 0 && preserve_perms)
David Dykstraf0b4fda2003-01-21 00:58:50 +0000150 return code;
151 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000152}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000153#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000154
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000155int do_rename(const char *fname1, const char *fname2)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000156{
157 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000158 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000159 return rename(fname1, fname2);
160}
161
Martin Poolbf4e7252002-03-25 03:29:47 +0000162void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000163{
Martin Poolc127e8a2002-03-25 03:51:17 +0000164 int l;
Martin Poolded83472002-02-18 22:44:23 +0000165 /* 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> */
Wayne Davison0f78b812006-04-25 20:23:34 +0000168
Martin Poolc127e8a2002-03-25 03:51:17 +0000169 /* Don't change empty string; and also we can't improve on
170 * "/" */
Wayne Davison0f78b812006-04-25 20:23:34 +0000171
Martin Poolc127e8a2002-03-25 03:51:17 +0000172 l = strlen(name);
173 while (l > 1) {
174 if (name[--l] != '/')
175 break;
176 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000177 }
178}
179
Martin Poolbf4e7252002-03-25 03:29:47 +0000180int do_mkdir(char *fname, mode_t mode)
181{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000182 if (dry_run) return 0;
183 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison0f78b812006-04-25 20:23:34 +0000184 trim_trailing_slashes(fname);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000185 return mkdir(fname, mode);
186}
187
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000188/* like mkstemp but forces permissions */
189int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000190{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000191 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000192 RETURN_ERROR_IF(read_only, EROFS);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000193
Wayne Davison4f5b0752005-02-14 00:53:43 +0000194#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000195 {
196 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000197 if (fd == -1)
198 return -1;
199 if (fchmod(fd, perms) != 0 && preserve_perms) {
200 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000201 close(fd);
202 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000203 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000204 return -1;
205 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000206#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000207 setmode(fd, O_BINARY);
208#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000209 return fd;
210 }
211#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000212 if (!mktemp(template))
213 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000214 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000215#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000216}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000217
218int do_stat(const char *fname, STRUCT_STAT *st)
219{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000220#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000221 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{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000229#ifdef SUPPORT_LINKS
230# ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000231 return lstat64(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000232# else
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000233 return lstat(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000234# endif
235#else
236 return do_stat(fname, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000237#endif
238}
239
240int do_fstat(int fd, STRUCT_STAT *st)
241{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000242#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000243 return fstat64(fd, st);
244#else
245 return fstat(fd, st);
246#endif
247}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000248
249OFF_T do_lseek(int fd, OFF_T offset, int whence)
250{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000251#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000252#if !SIZEOF_OFF64_T
253 OFF_T lseek64();
254#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000255 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000256#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000257 return lseek64(fd, offset, whence);
258#else
259 return lseek(fd, offset, whence);
260#endif
261}
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000262
263char *d_name(struct dirent *di)
264{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000265#ifdef HAVE_BROKEN_READDIR
Andrew Tridgell59503271998-05-06 07:18:06 +0000266 return (di->d_name - 2);
267#else
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000268 return di->d_name;
Andrew Tridgell59503271998-05-06 07:18:06 +0000269#endif
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000270}