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.
23 * Syscall wrappers to ensure that nothing gets done in dry_run mode
24 * and to handle system peculiarities.
33 #define CHECK_RO if (read_only || list_only) {errno = EROFS; return -1;}
35 int do_unlink(char *fname)
37 if (dry_run) return 0;
42 int do_symlink(char *fname1, char *fname2)
44 if (dry_run) return 0;
46 return symlink(fname1, fname2);
50 int do_link(char *fname1, char *fname2)
52 if (dry_run) return 0;
54 return link(fname1, fname2);
58 int do_lchown(const char *path, uid_t owner, gid_t group)
60 if (dry_run) return 0;
62 return lchown(path, owner, group);
66 int do_mknod(char *pathname, mode_t mode, dev_t dev)
68 if (dry_run) return 0;
70 return mknod(pathname, mode, dev);
74 int do_rmdir(char *pathname)
76 if (dry_run) return 0;
78 return rmdir(pathname);
81 int do_open(char *pathname, int flags, mode_t mode)
83 if (flags != O_RDONLY) {
84 if (dry_run) return -1;
91 /* some systems can't handle a double / */
92 if (pathname[0] == '/' && pathname[1] == '/') pathname++;
94 return open(pathname, flags, mode);
98 int do_chmod(const char *path, mode_t mode)
100 if (dry_run) return 0;
102 return chmod(path, mode);
106 int do_rename(char *fname1, char *fname2)
108 if (dry_run) return 0;
110 return rename(fname1, fname2);
114 void trim_trailing_slashes(char *name)
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> */
121 return; /* empty string */
122 p = strchr(name, '\0') - 1;
129 int do_mkdir(char *fname, mode_t mode)
134 trim_trailing_slashes(fname);
135 return mkdir(fname, mode);
139 /* like mkstemp but forces permissions */
140 int do_mkstemp(char *template, mode_t perms)
142 if (dry_run) return -1;
143 if (read_only) {errno = EROFS; return -1;}
145 #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD)
147 int fd = mkstemp(template);
148 if (fd == -1) return -1;
149 if (fchmod(fd, perms) != 0) {
157 if (!mktemp(template)) return -1;
158 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
162 int do_stat(const char *fname, STRUCT_STAT *st)
165 return stat64(fname, st);
167 return stat(fname, st);
172 int do_lstat(const char *fname, STRUCT_STAT *st)
175 return lstat64(fname, st);
177 return lstat(fname, st);
182 int do_fstat(int fd, STRUCT_STAT *st)
185 return fstat64(fd, st);
187 return fstat(fd, st);
191 OFF_T do_lseek(int fd, OFF_T offset, int whence)
195 return lseek64(fd, offset, whence);
197 return lseek(fd, offset, whence);
202 void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset)
205 return mmap64(start, len, prot, flags, fd, offset);
207 return mmap(start, len, prot, flags, fd, offset);
212 char *d_name(struct dirent *di)
214 #if HAVE_BROKEN_READDIR
215 return (di->d_name - 2);