Allow %i to have a field width.
[rsync.git] / syscall.c
blob5cdf052a9eea0b8fc60b08b46313a4d00c701920
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 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
30 #include <sys/un.h>
31 #endif
33 extern int dry_run;
34 extern int read_only;
35 extern int list_only;
36 extern int preserve_perms;
38 #define RETURN_ERROR_IF(x,e) \
39 do { \
40 if (x) { \
41 errno = (e); \
42 return -1; \
43 } \
44 } while (0)
46 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
48 int do_unlink(char *fname)
50 if (dry_run) return 0;
51 RETURN_ERROR_IF_RO_OR_LO;
52 return unlink(fname);
55 int do_symlink(char *fname1, char *fname2)
57 if (dry_run) return 0;
58 RETURN_ERROR_IF_RO_OR_LO;
59 return symlink(fname1, fname2);
62 #ifdef HAVE_LINK
63 int do_link(char *fname1, char *fname2)
65 if (dry_run) return 0;
66 RETURN_ERROR_IF_RO_OR_LO;
67 return link(fname1, fname2);
69 #endif
71 int do_lchown(const char *path, uid_t owner, gid_t group)
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);
81 int do_mknod(char *pathname, mode_t mode, dev_t dev)
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;
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
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 return do_chmod(pathname, mode);
109 #endif
110 #ifdef HAVE_MKNOD
111 return mknod(pathname, mode, dev);
112 #else
113 return -1;
114 #endif
117 int do_rmdir(char *pathname)
119 if (dry_run) return 0;
120 RETURN_ERROR_IF_RO_OR_LO;
121 return rmdir(pathname);
124 int do_open(char *pathname, int flags, mode_t mode)
126 if (flags != O_RDONLY) {
127 RETURN_ERROR_IF(dry_run, 0);
128 RETURN_ERROR_IF_RO_OR_LO;
131 return open(pathname, flags | O_BINARY, mode);
134 #ifdef HAVE_CHMOD
135 int do_chmod(const char *path, mode_t mode)
137 int code;
138 if (dry_run) return 0;
139 RETURN_ERROR_IF_RO_OR_LO;
140 code = chmod(path, mode);
141 if (code != 0 && preserve_perms)
142 return code;
143 return 0;
145 #endif
147 int do_rename(char *fname1, char *fname2)
149 if (dry_run) return 0;
150 RETURN_ERROR_IF_RO_OR_LO;
151 return rename(fname1, fname2);
155 void trim_trailing_slashes(char *name)
157 int l;
158 /* Some BSD systems cannot make a directory if the name
159 * contains a trailing slash.
160 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
162 /* Don't change empty string; and also we can't improve on
163 * "/" */
165 l = strlen(name);
166 while (l > 1) {
167 if (name[--l] != '/')
168 break;
169 name[l] = '\0';
174 int do_mkdir(char *fname, mode_t mode)
176 if (dry_run) return 0;
177 RETURN_ERROR_IF_RO_OR_LO;
178 trim_trailing_slashes(fname);
179 return mkdir(fname, mode);
183 /* like mkstemp but forces permissions */
184 int do_mkstemp(char *template, mode_t perms)
186 RETURN_ERROR_IF(dry_run, 0);
187 RETURN_ERROR_IF(read_only, EROFS);
189 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
191 int fd = mkstemp(template);
192 if (fd == -1)
193 return -1;
194 if (fchmod(fd, perms) != 0 && preserve_perms) {
195 int errno_save = errno;
196 close(fd);
197 unlink(template);
198 errno = errno_save;
199 return -1;
201 #if defined HAVE_SETMODE && O_BINARY
202 setmode(fd, O_BINARY);
203 #endif
204 return fd;
206 #else
207 if (!mktemp(template))
208 return -1;
209 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
210 #endif
213 int do_stat(const char *fname, STRUCT_STAT *st)
215 #ifdef USE_STAT64_FUNCS
216 return stat64(fname, st);
217 #else
218 return stat(fname, st);
219 #endif
222 int do_lstat(const char *fname, STRUCT_STAT *st)
224 #ifdef SUPPORT_LINKS
225 # ifdef USE_STAT64_FUNCS
226 return lstat64(fname, st);
227 # else
228 return lstat(fname, st);
229 # endif
230 #else
231 return do_stat(fname, st);
232 #endif
235 int do_fstat(int fd, STRUCT_STAT *st)
237 #ifdef USE_STAT64_FUNCS
238 return fstat64(fd, st);
239 #else
240 return fstat(fd, st);
241 #endif
244 OFF_T do_lseek(int fd, OFF_T offset, int whence)
246 #if SIZEOF_OFF64_T
247 off64_t lseek64();
248 return lseek64(fd, offset, whence);
249 #else
250 return lseek(fd, offset, whence);
251 #endif
254 char *d_name(struct dirent *di)
256 #ifdef HAVE_BROKEN_READDIR
257 return (di->d_name - 2);
258 #else
259 return di->d_name;
260 #endif