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