- Don't call do_chmod() unless HAVE_CHMOD is defined.
[rsync/rsync.git] / syscall.c
1 /* 
2    Copyright (C) Andrew Tridgell 1998
3    Copyright (C) 2002 by Martin Pool
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
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  **/
26
27 #include "rsync.h"
28
29 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
30 #include <sys/un.h>
31 #endif
32
33 extern int dry_run;
34 extern int read_only;
35 extern int list_only;
36 extern int preserve_perms;
37
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)
47
48 int do_unlink(char *fname)
49 {
50         if (dry_run) return 0;
51         RETURN_ERROR_IF_RO_OR_LO;
52         return unlink(fname);
53 }
54
55 int do_symlink(char *fname1, char *fname2)
56 {
57         if (dry_run) return 0;
58         RETURN_ERROR_IF_RO_OR_LO;
59         return symlink(fname1, fname2);
60 }
61
62 #ifdef HAVE_LINK
63 int do_link(char *fname1, char *fname2)
64 {
65         if (dry_run) return 0;
66         RETURN_ERROR_IF_RO_OR_LO;
67         return link(fname1, fname2);
68 }
69 #endif
70
71 int do_lchown(const char *path, uid_t owner, gid_t group)
72 {
73         if (dry_run) return 0;
74         RETURN_ERROR_IF_RO_OR_LO;
75 #ifndef HAVE_LCHOWN
76 #define lchown chown
77 #endif
78         return lchown(path, owner, group);
79 }
80
81 int do_mknod(char *pathname, mode_t mode, dev_t dev)
82 {
83         if (dry_run) return 0;
84         RETURN_ERROR_IF_RO_OR_LO;
85 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
86         if (S_ISFIFO(mode))
87                 return mkfifo(pathname, mode);
88 #endif
89 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
90         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);
97 #ifdef HAVE_SOCKADDR_UN_LEN
98                 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);
107 #ifdef HAVE_CHMOD
108                 return do_chmod(pathname, mode);
109 #else
110                 return 0;
111 #endif
112         }
113 #endif
114 #ifdef HAVE_MKNOD
115         return mknod(pathname, mode, dev);
116 #else
117         return -1;
118 #endif
119 }
120
121 int do_rmdir(char *pathname)
122 {
123         if (dry_run) return 0;
124         RETURN_ERROR_IF_RO_OR_LO;
125         return rmdir(pathname);
126 }
127
128 int do_open(char *pathname, int flags, mode_t mode)
129 {
130         if (flags != O_RDONLY) {
131                 RETURN_ERROR_IF(dry_run, 0);
132                 RETURN_ERROR_IF_RO_OR_LO;
133         }
134
135         return open(pathname, flags | O_BINARY, mode);
136 }
137
138 #ifdef HAVE_CHMOD
139 int do_chmod(const char *path, mode_t mode)
140 {
141         int code;
142         if (dry_run) return 0;
143         RETURN_ERROR_IF_RO_OR_LO;
144         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);
152         if (code != 0 && preserve_perms)
153             return code;
154         return 0;
155 }
156 #endif
157
158 int do_rename(char *fname1, char *fname2)
159 {
160         if (dry_run) return 0;
161         RETURN_ERROR_IF_RO_OR_LO;
162         return rename(fname1, fname2);
163 }
164
165
166 void trim_trailing_slashes(char *name)
167 {
168         int l;
169         /* Some BSD systems cannot make a directory if the name
170          * contains a trailing slash.
171          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
172         
173         /* Don't change empty string; and also we can't improve on
174          * "/" */
175         
176         l = strlen(name);
177         while (l > 1) {
178                 if (name[--l] != '/')
179                         break;
180                 name[l] = '\0';
181         }
182 }
183
184
185 int do_mkdir(char *fname, mode_t mode)
186 {
187         if (dry_run) return 0;
188         RETURN_ERROR_IF_RO_OR_LO;
189         trim_trailing_slashes(fname);   
190         return mkdir(fname, mode);
191 }
192
193
194 /* like mkstemp but forces permissions */
195 int do_mkstemp(char *template, mode_t perms)
196 {
197         RETURN_ERROR_IF(dry_run, 0);
198         RETURN_ERROR_IF(read_only, EROFS);
199
200 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
201         {
202                 int fd = mkstemp(template);
203                 if (fd == -1)
204                         return -1;
205                 if (fchmod(fd, perms) != 0 && preserve_perms) {
206                         int errno_save = errno;
207                         close(fd);
208                         unlink(template);
209                         errno = errno_save;
210                         return -1;
211                 }
212 #if defined HAVE_SETMODE && O_BINARY
213                 setmode(fd, O_BINARY);
214 #endif
215                 return fd;
216         }
217 #else
218         if (!mktemp(template))
219                 return -1;
220         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
221 #endif
222 }
223
224 int do_stat(const char *fname, STRUCT_STAT *st)
225 {
226 #ifdef USE_STAT64_FUNCS
227         return stat64(fname, st);
228 #else
229         return stat(fname, st);
230 #endif
231 }
232
233 int do_lstat(const char *fname, STRUCT_STAT *st)
234 {
235 #ifdef SUPPORT_LINKS
236 # ifdef USE_STAT64_FUNCS
237         return lstat64(fname, st);
238 # else
239         return lstat(fname, st);
240 # endif
241 #else
242         return do_stat(fname, st);
243 #endif
244 }
245
246 int do_fstat(int fd, STRUCT_STAT *st)
247 {
248 #ifdef USE_STAT64_FUNCS
249         return fstat64(fd, st);
250 #else
251         return fstat(fd, st);
252 #endif
253 }
254
255 OFF_T do_lseek(int fd, OFF_T offset, int whence)
256 {
257 #ifdef HAVE_LSEEK64
258         off64_t lseek64();
259         return lseek64(fd, offset, whence);
260 #else
261         return lseek(fd, offset, whence);
262 #endif
263 }
264
265 char *d_name(struct dirent *di)
266 {
267 #ifdef HAVE_BROKEN_READDIR
268         return (di->d_name - 2);
269 #else
270         return di->d_name;
271 #endif
272 }