blob: 5cee2326ea1d7736d8b28d6bc3b61ad71a977218 [file] [log] [blame]
Andrew Tridgell31e12521998-03-23 13:25:30 +00001/*
2 Copyright (C) Andrew Tridgell 1998
Martin Poolded83472002-02-18 22:44:23 +00003 Copyright (C) 2002 by Martin Pool
Andrew Tridgell31e12521998-03-23 13:25:30 +00004
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
Martin Poolded83472002-02-18 22:44:23 +000020/**
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 **/
Andrew Tridgell31e12521998-03-23 13:25:30 +000026
27#include "rsync.h"
28
Wayne Davison4f5b0752005-02-14 00:53:43 +000029#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000030#include <sys/un.h>
31#endif
32
Andrew Tridgell31e12521998-03-23 13:25:30 +000033extern int dry_run;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000034extern int read_only;
Andrew Tridgell2b086e01998-12-05 01:56:45 +000035extern int list_only;
David Dykstraf0b4fda2003-01-21 00:58:50 +000036extern int preserve_perms;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000037
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000038#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)
Andrew Tridgell31e12521998-03-23 13:25:30 +000047
Wayne Davison63cf5ae2006-01-29 18:52:53 +000048int do_unlink(const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000049{
50 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000051 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000052 return unlink(fname);
53}
54
Wayne Davison63cf5ae2006-01-29 18:52:53 +000055int do_symlink(const char *fname1, const char *fname2)
Andrew Tridgell31e12521998-03-23 13:25:30 +000056{
57 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000058 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000059 return symlink(fname1, fname2);
60}
61
Wayne Davison4f5b0752005-02-14 00:53:43 +000062#ifdef HAVE_LINK
Wayne Davison63cf5ae2006-01-29 18:52:53 +000063int do_link(const char *fname1, const char *fname2)
Andrew Tridgell31e12521998-03-23 13:25:30 +000064{
65 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000066 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000067 return link(fname1, fname2);
68}
Andrew Tridgellf92ef571998-03-24 06:42:11 +000069#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +000070
Andrew Tridgell7308bd61998-03-24 06:39:16 +000071int do_lchown(const char *path, uid_t owner, gid_t group)
72{
73 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000074 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +000075#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +000076#define lchown chown
77#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +000078 return lchown(path, owner, group);
79}
80
Andrew Tridgell31e12521998-03-23 13:25:30 +000081int do_mknod(char *pathname, mode_t mode, dev_t dev)
82{
83 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000084 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +000085#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000086 if (S_ISFIFO(mode))
87 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +000088#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +000089#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000090 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);
Wayne Davison4f5b0752005-02-14 00:53:43 +000097#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000098 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);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000107#ifdef HAVE_CHMOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000108 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000109#else
110 return 0;
111#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000112 }
113#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000114#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000115 return mknod(pathname, mode, dev);
116#else
117 return -1;
118#endif
119}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000120
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000121int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000122{
123 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000124 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000125 return rmdir(pathname);
126}
127
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000128int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000129{
David Dykstra3420c8e1999-11-04 15:43:38 +0000130 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000131 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000132 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000133 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000134
Wayne Davisone106de42003-03-30 23:00:51 +0000135 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000136}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000137
Wayne Davison4f5b0752005-02-14 00:53:43 +0000138#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000139int do_chmod(const char *path, mode_t mode)
140{
David Dykstraf0b4fda2003-01-21 00:58:50 +0000141 int code;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000142 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000143 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisond11f5c62005-07-27 23:30:53 +0000144 if (S_ISLNK(mode)) {
145#ifdef HAVE_LCHMOD
146 code = lchmod(path, mode & CHMOD_BITS);
147#else
148 code = 1;
149#endif
150 } else
151 code = chmod(path, mode & CHMOD_BITS);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000152 if (code != 0 && preserve_perms)
David Dykstraf0b4fda2003-01-21 00:58:50 +0000153 return code;
154 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000155}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000156#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000157
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000158int do_rename(const char *fname1, const char *fname2)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000159{
160 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000161 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000162 return rename(fname1, fname2);
163}
164
Martin Poolbf4e7252002-03-25 03:29:47 +0000165void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000166{
Martin Poolc127e8a2002-03-25 03:51:17 +0000167 int l;
Martin Poolded83472002-02-18 22:44:23 +0000168 /* Some BSD systems cannot make a directory if the name
169 * contains a trailing slash.
170 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
Martin Poolc127e8a2002-03-25 03:51:17 +0000171
172 /* Don't change empty string; and also we can't improve on
173 * "/" */
174
175 l = strlen(name);
176 while (l > 1) {
177 if (name[--l] != '/')
178 break;
179 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000180 }
181}
182
Martin Poolbf4e7252002-03-25 03:29:47 +0000183int do_mkdir(char *fname, mode_t mode)
184{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000185 if (dry_run) return 0;
186 RETURN_ERROR_IF_RO_OR_LO;
Martin Poolbf4e7252002-03-25 03:29:47 +0000187 trim_trailing_slashes(fname);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000188 return mkdir(fname, mode);
189}
190
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000191/* like mkstemp but forces permissions */
192int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000193{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000194 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000195 RETURN_ERROR_IF(read_only, EROFS);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000196
Wayne Davison4f5b0752005-02-14 00:53:43 +0000197#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000198 {
199 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000200 if (fd == -1)
201 return -1;
202 if (fchmod(fd, perms) != 0 && preserve_perms) {
203 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000204 close(fd);
205 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000206 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000207 return -1;
208 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000209#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000210 setmode(fd, O_BINARY);
211#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000212 return fd;
213 }
214#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000215 if (!mktemp(template))
216 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000217 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000218#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000219}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000220
221int do_stat(const char *fname, STRUCT_STAT *st)
222{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000223#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000224 return stat64(fname, st);
225#else
226 return stat(fname, st);
227#endif
228}
229
230int do_lstat(const char *fname, STRUCT_STAT *st)
231{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000232#ifdef SUPPORT_LINKS
233# ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000234 return lstat64(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000235# else
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000236 return lstat(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000237# endif
238#else
239 return do_stat(fname, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000240#endif
241}
242
243int do_fstat(int fd, STRUCT_STAT *st)
244{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000245#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000246 return fstat64(fd, st);
247#else
248 return fstat(fd, st);
249#endif
250}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000251
252OFF_T do_lseek(int fd, OFF_T offset, int whence)
253{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000254#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000255#if !SIZEOF_OFF64_T
256 OFF_T lseek64();
257#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000258 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000259#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000260 return lseek64(fd, offset, whence);
261#else
262 return lseek(fd, offset, whence);
263#endif
264}
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000265
266char *d_name(struct dirent *di)
267{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000268#ifdef HAVE_BROKEN_READDIR
Andrew Tridgell59503271998-05-06 07:18:06 +0000269 return (di->d_name - 2);
270#else
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000271 return di->d_name;
Andrew Tridgell59503271998-05-06 07:18:06 +0000272#endif
Andrew Tridgelld6e6ecb1998-05-06 07:00:38 +0000273}