Mentioned the vanished-file warning enhancement.
[rsync.git] / syscall.c
blobdbde033b3879726c56e5b7e28fcb4e2cb8427d35
1 /*
2 Copyright (C) Andrew Tridgell 1998
3 Copyright (C) 2002 by Martin Pool
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.
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.
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.
20 /**
21 * @file syscall.c
23 * Syscall wrappers to ensure that nothing gets done in dry_run mode
24 * and to handle system peculiarities.
25 **/
27 #include "rsync.h"
29 extern int dry_run;
30 extern int read_only;
31 extern int list_only;
32 extern int preserve_perms;
34 #define CHECK_RO if (read_only || list_only) {errno = EROFS; return -1;}
36 int do_unlink(char *fname)
38 if (dry_run) return 0;
39 CHECK_RO
40 return unlink(fname);
43 int do_symlink(char *fname1, char *fname2)
45 if (dry_run) return 0;
46 CHECK_RO
47 return symlink(fname1, fname2);
50 #if HAVE_LINK
51 int do_link(char *fname1, char *fname2)
53 if (dry_run) return 0;
54 CHECK_RO
55 return link(fname1, fname2);
57 #endif
59 int do_lchown(const char *path, uid_t owner, gid_t group)
61 if (dry_run) return 0;
62 CHECK_RO
63 return lchown(path, owner, group);
66 #if HAVE_MKNOD
67 int do_mknod(char *pathname, mode_t mode, dev_t dev)
69 if (dry_run) return 0;
70 CHECK_RO
71 return mknod(pathname, mode, dev);
73 #endif
75 int do_rmdir(char *pathname)
77 if (dry_run) return 0;
78 CHECK_RO
79 return rmdir(pathname);
82 int do_open(char *pathname, int flags, mode_t mode)
84 if (flags != O_RDONLY) {
85 if (dry_run) return -1;
86 CHECK_RO
88 /* some systems can't handle a double / */
89 if (pathname[0] == '/' && pathname[1] == '/') pathname++;
91 return open(pathname, flags | O_BINARY, mode);
94 #if HAVE_CHMOD
95 int do_chmod(const char *path, mode_t mode)
97 int code;
98 if (dry_run) return 0;
99 CHECK_RO
100 code = chmod(path, mode);
101 if ((code != 0) && preserve_perms)
102 return code;
103 return 0;
105 #endif
107 int do_rename(char *fname1, char *fname2)
109 if (dry_run) return 0;
110 CHECK_RO
111 return rename(fname1, fname2);
115 void trim_trailing_slashes(char *name)
117 int l;
118 /* Some BSD systems cannot make a directory if the name
119 * contains a trailing slash.
120 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
122 /* Don't change empty string; and also we can't improve on
123 * "/" */
125 l = strlen(name);
126 while (l > 1) {
127 if (name[--l] != '/')
128 break;
129 name[l] = '\0';
134 int do_mkdir(char *fname, mode_t mode)
136 if (dry_run)
137 return 0;
138 CHECK_RO;
139 trim_trailing_slashes(fname);
140 return mkdir(fname, mode);
144 /* like mkstemp but forces permissions */
145 int do_mkstemp(char *template, mode_t perms)
147 if (dry_run) return -1;
148 if (read_only) {errno = EROFS; return -1;}
150 #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD)
152 int fd = mkstemp(template);
153 if (fd == -1) return -1;
154 if ((fchmod(fd, perms) != 0) && preserve_perms) {
155 close(fd);
156 unlink(template);
157 return -1;
159 return fd;
161 #else
162 if (!mktemp(template)) return -1;
163 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
164 #endif
167 int do_stat(const char *fname, STRUCT_STAT *st)
169 #if HAVE_OFF64_T
170 return stat64(fname, st);
171 #else
172 return stat(fname, st);
173 #endif
176 #if SUPPORT_LINKS
177 int do_lstat(const char *fname, STRUCT_STAT *st)
179 #if HAVE_OFF64_T
180 return lstat64(fname, st);
181 #else
182 return lstat(fname, st);
183 #endif
185 #endif
187 int do_fstat(int fd, STRUCT_STAT *st)
189 #if HAVE_OFF64_T
190 return fstat64(fd, st);
191 #else
192 return fstat(fd, st);
193 #endif
196 OFF_T do_lseek(int fd, OFF_T offset, int whence)
198 #if HAVE_OFF64_T
199 off64_t lseek64();
200 return lseek64(fd, offset, whence);
201 #else
202 return lseek(fd, offset, whence);
203 #endif
206 #ifdef USE_MMAP
207 void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
209 #if HAVE_OFF64_T
210 return mmap64(start, len, prot, flags, fd, offset);
211 #else
212 return mmap(start, len, prot, flags, fd, offset);
213 #endif
215 #endif
217 char *d_name(struct dirent *di)
219 #if HAVE_BROKEN_READDIR
220 return (di->d_name - 2);
221 #else
222 return di->d_name;
223 #endif