2 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
7 * Copyright (C) 2003-2007 Wayne Davison
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 3 of the License, or
12 * (at your option) any later version.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
25 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
28 #ifdef HAVE_SYS_ATTR_H
36 extern int preserve_perms
;
38 #define RETURN_ERROR_IF(x,e) \
46 #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
48 int do_unlink(const char *fname
)
50 if (dry_run
) return 0;
51 RETURN_ERROR_IF_RO_OR_LO
;
55 int do_symlink(const char *fname1
, const char *fname2
)
57 if (dry_run
) return 0;
58 RETURN_ERROR_IF_RO_OR_LO
;
59 return symlink(fname1
, fname2
);
63 int do_link(const char *fname1
, const char *fname2
)
65 if (dry_run
) return 0;
66 RETURN_ERROR_IF_RO_OR_LO
;
67 return link(fname1
, fname2
);
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
;
78 return lchown(path
, owner
, group
);
81 int do_mknod(const char *pathname
, mode_t mode
, dev_t dev
)
83 if (dry_run
) return 0;
84 RETURN_ERROR_IF_RO_OR_LO
;
86 /* For --fake-super, we create a normal file with mode 0600. */
88 int fd
= open(pathname
, O_WRONLY
|O_CREAT
|O_TRUNC
, S_IWUSR
|S_IRUSR
);
89 if (fd
< 0 || close(fd
) < 0)
94 #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
96 return mkfifo(pathname
, mode
);
98 #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
101 struct sockaddr_un saddr
;
102 #ifdef HAVE_SOCKADDR_UN_LEN
105 strlcpy(saddr
.sun_path
, pathname
, sizeof saddr
.sun_path
);
106 #ifdef HAVE_SOCKADDR_UN_LEN
107 saddr
.sun_len
= len
>= sizeof saddr
.sun_path
108 ? sizeof saddr
.sun_path
: len
+ 1;
110 saddr
.sun_family
= AF_UNIX
;
112 if ((sock
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0
113 || (unlink(pathname
) < 0 && errno
!= ENOENT
)
114 || (bind(sock
, (struct sockaddr
*)&saddr
, sizeof saddr
)) < 0)
118 return do_chmod(pathname
, mode
);
125 return mknod(pathname
, mode
, dev
);
131 int do_rmdir(const char *pathname
)
133 if (dry_run
) return 0;
134 RETURN_ERROR_IF_RO_OR_LO
;
135 return rmdir(pathname
);
138 int do_open(const char *pathname
, int flags
, mode_t mode
)
140 if (flags
!= O_RDONLY
) {
141 RETURN_ERROR_IF(dry_run
, 0);
142 RETURN_ERROR_IF_RO_OR_LO
;
145 return open(pathname
, flags
| O_BINARY
, mode
);
149 int do_chmod(const char *path
, mode_t mode
)
152 if (dry_run
) return 0;
153 RETURN_ERROR_IF_RO_OR_LO
;
156 code
= lchmod(path
, mode
& CHMOD_BITS
);
157 #elif defined HAVE_SETATTRLIST
158 struct attrlist attrList
;
159 uint32_t m
= mode
& CHMOD_BITS
; /* manpage is wrong: not mode_t! */
161 memset(&attrList
, 0, sizeof attrList
);
162 attrList
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
163 attrList
.commonattr
= ATTR_CMN_ACCESSMASK
;
164 code
= setattrlist(path
, &attrList
, &m
, sizeof m
, FSOPT_NOFOLLOW
);
169 code
= chmod(path
, mode
& CHMOD_BITS
);
170 if (code
!= 0 && preserve_perms
)
176 int do_rename(const char *fname1
, const char *fname2
)
178 if (dry_run
) return 0;
179 RETURN_ERROR_IF_RO_OR_LO
;
180 return rename(fname1
, fname2
);
183 void trim_trailing_slashes(char *name
)
186 /* Some BSD systems cannot make a directory if the name
187 * contains a trailing slash.
188 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
190 /* Don't change empty string; and also we can't improve on
195 if (name
[--l
] != '/')
201 int do_mkdir(char *fname
, mode_t mode
)
203 if (dry_run
) return 0;
204 RETURN_ERROR_IF_RO_OR_LO
;
205 trim_trailing_slashes(fname
);
206 return mkdir(fname
, mode
);
209 /* like mkstemp but forces permissions */
210 int do_mkstemp(char *template, mode_t perms
)
212 RETURN_ERROR_IF(dry_run
, 0);
213 RETURN_ERROR_IF(read_only
, EROFS
);
216 #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
218 int fd
= mkstemp(template);
221 if (fchmod(fd
, perms
) != 0 && preserve_perms
) {
222 int errno_save
= errno
;
228 #if defined HAVE_SETMODE && O_BINARY
229 setmode(fd
, O_BINARY
);
234 if (!mktemp(template))
236 return do_open(template, O_RDWR
|O_EXCL
|O_CREAT
, perms
);
240 int do_stat(const char *fname
, STRUCT_STAT
*st
)
242 #ifdef USE_STAT64_FUNCS
243 return stat64(fname
, st
);
245 return stat(fname
, st
);
249 int do_lstat(const char *fname
, STRUCT_STAT
*st
)
252 # ifdef USE_STAT64_FUNCS
253 return lstat64(fname
, st
);
255 return lstat(fname
, st
);
258 return do_stat(fname
, st
);
262 int do_fstat(int fd
, STRUCT_STAT
*st
)
264 #ifdef USE_STAT64_FUNCS
265 return fstat64(fd
, st
);
267 return fstat(fd
, st
);
271 OFF_T
do_lseek(int fd
, OFF_T offset
, int whence
)
279 return lseek64(fd
, offset
, whence
);
281 return lseek(fd
, offset
, whence
);
285 char *d_name(struct dirent
*di
)
287 #ifdef HAVE_BROKEN_READDIR
288 return (di
->d_name
- 2);