Added "const" to appropriate char pointers.
[rsync/rsync.git] / syscall.c
1 /*
2  * Syscall wrappers to ensure that nothing gets done in dry_run mode
3  * and to handle system peculiarities.
4  *
5  * Copyright (C) 1998 Andrew Tridgell
6  * Copyright (C) 2002 Martin Pool
7  * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 #include "rsync.h"
25
26 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
27 #include <sys/un.h>
28 #endif
29
30 extern int dry_run;
31 extern int read_only;
32 extern int list_only;
33 extern int preserve_perms;
34
35 #define RETURN_ERROR_IF(x,e) \
36         do { \
37                 if (x) { \
38                         errno = (e); \
39                         return -1; \
40                 } \
41         } while (0)
42
43 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
44
45 int do_unlink(const char *fname)
46 {
47         if (dry_run) return 0;
48         RETURN_ERROR_IF_RO_OR_LO;
49         return unlink(fname);
50 }
51
52 int do_symlink(const char *fname1, const char *fname2)
53 {
54         if (dry_run) return 0;
55         RETURN_ERROR_IF_RO_OR_LO;
56         return symlink(fname1, fname2);
57 }
58
59 #ifdef HAVE_LINK
60 int do_link(const char *fname1, const char *fname2)
61 {
62         if (dry_run) return 0;
63         RETURN_ERROR_IF_RO_OR_LO;
64         return link(fname1, fname2);
65 }
66 #endif
67
68 int do_lchown(const char *path, uid_t owner, gid_t group)
69 {
70         if (dry_run) return 0;
71         RETURN_ERROR_IF_RO_OR_LO;
72 #ifndef HAVE_LCHOWN
73 #define lchown chown
74 #endif
75         return lchown(path, owner, group);
76 }
77
78 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
79 {
80         if (dry_run) return 0;
81         RETURN_ERROR_IF_RO_OR_LO;
82 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
83         if (S_ISFIFO(mode))
84                 return mkfifo(pathname, mode);
85 #endif
86 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
87         if (S_ISSOCK(mode)) {
88                 int sock;
89                 struct sockaddr_un saddr;
90                 unsigned int len;
91
92                 saddr.sun_family = AF_UNIX;
93                 len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
94 #ifdef HAVE_SOCKADDR_UN_LEN
95                 saddr.sun_len = len >= sizeof saddr.sun_path
96                               ? sizeof saddr.sun_path : len + 1;
97 #endif
98
99                 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
100                     || (unlink(pathname) < 0 && errno != ENOENT)
101                     || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
102                         return -1;
103                 close(sock);
104 #ifdef HAVE_CHMOD
105                 return do_chmod(pathname, mode);
106 #else
107                 return 0;
108 #endif
109         }
110 #endif
111 #ifdef HAVE_MKNOD
112         return mknod(pathname, mode, dev);
113 #else
114         return -1;
115 #endif
116 }
117
118 int do_rmdir(const char *pathname)
119 {
120         if (dry_run) return 0;
121         RETURN_ERROR_IF_RO_OR_LO;
122         return rmdir(pathname);
123 }
124
125 int do_open(const char *pathname, int flags, mode_t mode)
126 {
127         if (flags != O_RDONLY) {
128                 RETURN_ERROR_IF(dry_run, 0);
129                 RETURN_ERROR_IF_RO_OR_LO;
130         }
131
132         return open(pathname, flags | O_BINARY, mode);
133 }
134
135 #ifdef HAVE_CHMOD
136 int do_chmod(const char *path, mode_t mode)
137 {
138         int code;
139         if (dry_run) return 0;
140         RETURN_ERROR_IF_RO_OR_LO;
141         if (S_ISLNK(mode)) {
142 #ifdef HAVE_LCHMOD
143                 code = lchmod(path, mode & CHMOD_BITS);
144 #else
145                 code = 1;
146 #endif
147         } else
148                 code = chmod(path, mode & CHMOD_BITS);
149         if (code != 0 && preserve_perms)
150             return code;
151         return 0;
152 }
153 #endif
154
155 int do_rename(const char *fname1, const char *fname2)
156 {
157         if (dry_run) return 0;
158         RETURN_ERROR_IF_RO_OR_LO;
159         return rename(fname1, fname2);
160 }
161
162 void trim_trailing_slashes(char *name)
163 {
164         int l;
165         /* Some BSD systems cannot make a directory if the name
166          * contains a trailing slash.
167          * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
168
169         /* Don't change empty string; and also we can't improve on
170          * "/" */
171
172         l = strlen(name);
173         while (l > 1) {
174                 if (name[--l] != '/')
175                         break;
176                 name[l] = '\0';
177         }
178 }
179
180 int do_mkdir(char *fname, mode_t mode)
181 {
182         if (dry_run) return 0;
183         RETURN_ERROR_IF_RO_OR_LO;
184         trim_trailing_slashes(fname);
185         return mkdir(fname, mode);
186 }
187
188 /* like mkstemp but forces permissions */
189 int do_mkstemp(char *template, mode_t perms)
190 {
191         RETURN_ERROR_IF(dry_run, 0);
192         RETURN_ERROR_IF(read_only, EROFS);
193
194 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
195         {
196                 int fd = mkstemp(template);
197                 if (fd == -1)
198                         return -1;
199                 if (fchmod(fd, perms) != 0 && preserve_perms) {
200                         int errno_save = errno;
201                         close(fd);
202                         unlink(template);
203                         errno = errno_save;
204                         return -1;
205                 }
206 #if defined HAVE_SETMODE && O_BINARY
207                 setmode(fd, O_BINARY);
208 #endif
209                 return fd;
210         }
211 #else
212         if (!mktemp(template))
213                 return -1;
214         return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
215 #endif
216 }
217
218 int do_stat(const char *fname, STRUCT_STAT *st)
219 {
220 #ifdef USE_STAT64_FUNCS
221         return stat64(fname, st);
222 #else
223         return stat(fname, st);
224 #endif
225 }
226
227 int do_lstat(const char *fname, STRUCT_STAT *st)
228 {
229 #ifdef SUPPORT_LINKS
230 # ifdef USE_STAT64_FUNCS
231         return lstat64(fname, st);
232 # else
233         return lstat(fname, st);
234 # endif
235 #else
236         return do_stat(fname, st);
237 #endif
238 }
239
240 int do_fstat(int fd, STRUCT_STAT *st)
241 {
242 #ifdef USE_STAT64_FUNCS
243         return fstat64(fd, st);
244 #else
245         return fstat(fd, st);
246 #endif
247 }
248
249 OFF_T do_lseek(int fd, OFF_T offset, int whence)
250 {
251 #ifdef HAVE_LSEEK64
252 #if !SIZEOF_OFF64_T
253         OFF_T lseek64();
254 #else
255         off64_t lseek64();
256 #endif
257         return lseek64(fd, offset, whence);
258 #else
259         return lseek(fd, offset, whence);
260 #endif
261 }
262
263 char *d_name(struct dirent *di)
264 {
265 #ifdef HAVE_BROKEN_READDIR
266         return (di->d_name - 2);
267 #else
268         return di->d_name;
269 #endif
270 }