We no longer munge a double-leading slash in do_open() because we
[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 extern int dry_run;
30 extern int read_only;
31 extern int list_only;
32 extern int preserve_perms;
33
34 #define CHECK_RO if (read_only || list_only) {errno = EROFS; return -1;}
35
36 int do_unlink(char *fname)
37 {
38         if (dry_run) return 0;
39         CHECK_RO
40         return unlink(fname);
41 }
42
43 int do_symlink(char *fname1, char *fname2)
44 {
45         if (dry_run) return 0;
46         CHECK_RO
47         return symlink(fname1, fname2);
48 }
49
50 #if HAVE_LINK
51 int do_link(char *fname1, char *fname2)
52 {
53         if (dry_run) return 0;
54         CHECK_RO
55         return link(fname1, fname2);
56 }
57 #endif
58
59 int do_lchown(const char *path, uid_t owner, gid_t group)
60 {
61         if (dry_run) return 0;
62         CHECK_RO
63         return lchown(path, owner, group);
64 }
65
66 #if HAVE_MKNOD
67 int do_mknod(char *pathname, mode_t mode, dev_t dev)
68 {
69         if (dry_run) return 0;
70         CHECK_RO
71         return mknod(pathname, mode, dev);
72 }
73 #endif
74
75 int do_rmdir(char *pathname)
76 {
77         if (dry_run) return 0;
78         CHECK_RO
79         return rmdir(pathname);
80 }
81
82 int do_open(char *pathname, int flags, mode_t mode)
83 {
84         if (flags != O_RDONLY) {
85             if (dry_run) return -1;
86             CHECK_RO
87         }
88
89         return open(pathname, flags | O_BINARY, mode);
90 }
91
92 #if HAVE_CHMOD
93 int do_chmod(const char *path, mode_t mode)
94 {
95         int code;
96         if (dry_run) return 0;
97         CHECK_RO
98         code = chmod(path, mode);
99         if ((code != 0) && preserve_perms)
100             return code;
101         return 0;
102 }
103 #endif
104
105 int do_rename(char *fname1, char *fname2)
106 {
107         if (dry_run) return 0;
108         CHECK_RO
109         return rename(fname1, fname2);
110 }
111
112
113 void trim_trailing_slashes(char *name)
114 {
115         int l;
116         /* Some BSD systems cannot make a directory if the name
117          * contains a trailing slash.
118          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
119         
120         /* Don't change empty string; and also we can't improve on
121          * "/" */
122         
123         l = strlen(name);
124         while (l > 1) {
125                 if (name[--l] != '/')
126                         break;
127                 name[l] = '\0';
128         }
129 }
130
131
132 int do_mkdir(char *fname, mode_t mode)
133 {
134         if (dry_run)
135                 return 0;
136         CHECK_RO;
137         trim_trailing_slashes(fname);   
138         return mkdir(fname, mode);
139 }
140
141
142 /* like mkstemp but forces permissions */
143 int do_mkstemp(char *template, mode_t perms)
144 {
145         if (dry_run) return -1;
146         if (read_only) {errno = EROFS; return -1;}
147
148 #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD)
149         {
150                 int fd = mkstemp(template);
151                 if (fd == -1) return -1;
152                 if ((fchmod(fd, perms) != 0) && preserve_perms) {
153                         close(fd);
154                         unlink(template);
155                         return -1;
156                 }
157                 return fd;
158         }
159 #else
160         if (!mktemp(template)) return -1;
161         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
162 #endif
163 }
164
165 int do_stat(const char *fname, STRUCT_STAT *st)
166 {
167 #if HAVE_OFF64_T
168         return stat64(fname, st);
169 #else
170         return stat(fname, st);
171 #endif
172 }
173
174 #if SUPPORT_LINKS
175 int do_lstat(const char *fname, STRUCT_STAT *st)
176 {
177 #if HAVE_OFF64_T
178         return lstat64(fname, st);
179 #else
180         return lstat(fname, st);
181 #endif
182 }
183 #endif
184
185 int do_fstat(int fd, STRUCT_STAT *st)
186 {
187 #if HAVE_OFF64_T
188         return fstat64(fd, st);
189 #else
190         return fstat(fd, st);
191 #endif
192 }
193
194 OFF_T do_lseek(int fd, OFF_T offset, int whence)
195 {
196 #if HAVE_OFF64_T
197         off64_t lseek64();
198         return lseek64(fd, offset, whence);
199 #else
200         return lseek(fd, offset, whence);
201 #endif
202 }
203
204 #ifdef USE_MMAP
205 void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
206 {
207 #if HAVE_OFF64_T
208         return mmap64(start, len, prot, flags, fd, offset);
209 #else
210         return mmap(start, len, prot, flags, fd, offset);
211 #endif
212 }
213 #endif
214
215 char *d_name(struct dirent *di)
216 {
217 #if HAVE_BROKEN_READDIR
218         return (di->d_name - 2);
219 #else
220         return di->d_name;
221 #endif
222 }