| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1998 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | syscall wrappers to ensure that nothing gets done in dry_run mode |
| 21 | */ |
| 22 | |
| 23 | #include "rsync.h" |
| 24 | |
| 25 | extern int dry_run; |
| 26 | extern int read_only; |
| 27 | extern int list_only; |
| 28 | |
| 29 | #define CHECK_RO if (read_only || list_only) {errno = EROFS; return -1;} |
| 30 | |
| 31 | int do_unlink(char *fname) |
| 32 | { |
| 33 | if (dry_run) return 0; |
| 34 | CHECK_RO |
| 35 | return unlink(fname); |
| 36 | } |
| 37 | |
| 38 | int do_symlink(char *fname1, char *fname2) |
| 39 | { |
| 40 | if (dry_run) return 0; |
| 41 | CHECK_RO |
| 42 | return symlink(fname1, fname2); |
| 43 | } |
| 44 | |
| 45 | #if HAVE_LINK |
| 46 | int do_link(char *fname1, char *fname2) |
| 47 | { |
| 48 | if (dry_run) return 0; |
| 49 | CHECK_RO |
| 50 | return link(fname1, fname2); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | int do_lchown(const char *path, uid_t owner, gid_t group) |
| 55 | { |
| 56 | if (dry_run) return 0; |
| 57 | CHECK_RO |
| 58 | return lchown(path, owner, group); |
| 59 | } |
| 60 | |
| 61 | #if HAVE_MKNOD |
| 62 | int do_mknod(char *pathname, mode_t mode, dev_t dev) |
| 63 | { |
| 64 | if (dry_run) return 0; |
| 65 | CHECK_RO |
| 66 | return mknod(pathname, mode, dev); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | int do_rmdir(char *pathname) |
| 71 | { |
| 72 | if (dry_run) return 0; |
| 73 | CHECK_RO |
| 74 | return rmdir(pathname); |
| 75 | } |
| 76 | |
| 77 | int do_open(char *pathname, int flags, mode_t mode) |
| 78 | { |
| 79 | if (flags != O_RDONLY) { |
| 80 | if (dry_run) return -1; |
| 81 | CHECK_RO |
| 82 | } |
| 83 | #ifdef O_BINARY |
| 84 | /* for Windows */ |
| 85 | flags |= O_BINARY; |
| 86 | #endif |
| 87 | /* some systems can't handle a double / */ |
| 88 | if (pathname[0] == '/' && pathname[1] == '/') pathname++; |
| 89 | |
| 90 | return open(pathname, flags, mode); |
| 91 | } |
| 92 | |
| 93 | #if HAVE_CHMOD |
| 94 | int do_chmod(const char *path, mode_t mode) |
| 95 | { |
| 96 | if (dry_run) return 0; |
| 97 | CHECK_RO |
| 98 | return chmod(path, mode); |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | int do_rename(char *fname1, char *fname2) |
| 103 | { |
| 104 | if (dry_run) return 0; |
| 105 | CHECK_RO |
| 106 | return rename(fname1, fname2); |
| 107 | } |
| 108 | |
| 109 | int do_mkdir(char *fname, mode_t mode) |
| 110 | { |
| 111 | if (dry_run) return 0; |
| 112 | CHECK_RO |
| 113 | return mkdir(fname, mode); |
| 114 | } |
| 115 | |
| 116 | /* like mkstemp but forces permissions */ |
| 117 | int do_mkstemp(char *template, mode_t perms) |
| 118 | { |
| 119 | if (dry_run) return -1; |
| 120 | if (read_only) {errno = EROFS; return -1;} |
| 121 | |
| 122 | #if defined(HAVE_SECURE_MKSTEMP) && defined(HAVE_FCHMOD) |
| 123 | { |
| 124 | int fd = mkstemp(template); |
| 125 | if (fd == -1) return -1; |
| 126 | if (fchmod(fd, perms) != 0) { |
| 127 | close(fd); |
| 128 | unlink(template); |
| 129 | return -1; |
| 130 | } |
| 131 | return fd; |
| 132 | } |
| 133 | #else |
| 134 | if (!mktemp(template)) return -1; |
| 135 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | int do_stat(const char *fname, STRUCT_STAT *st) |
| 140 | { |
| 141 | #if HAVE_OFF64_T |
| 142 | return stat64(fname, st); |
| 143 | #else |
| 144 | return stat(fname, st); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | #if SUPPORT_LINKS |
| 149 | int do_lstat(const char *fname, STRUCT_STAT *st) |
| 150 | { |
| 151 | #if HAVE_OFF64_T |
| 152 | return lstat64(fname, st); |
| 153 | #else |
| 154 | return lstat(fname, st); |
| 155 | #endif |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | int do_fstat(int fd, STRUCT_STAT *st) |
| 160 | { |
| 161 | #if HAVE_OFF64_T |
| 162 | return fstat64(fd, st); |
| 163 | #else |
| 164 | return fstat(fd, st); |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | OFF_T do_lseek(int fd, OFF_T offset, int whence) |
| 169 | { |
| 170 | #if HAVE_OFF64_T |
| 171 | off64_t lseek64(); |
| 172 | return lseek64(fd, offset, whence); |
| 173 | #else |
| 174 | return lseek(fd, offset, whence); |
| 175 | #endif |
| 176 | } |
| 177 | |
| 178 | #ifdef USE_MMAP |
| 179 | void *do_mmap(void *start, int len, int prot, int flags, int fd, OFF_T offset) |
| 180 | { |
| 181 | #if HAVE_OFF64_T |
| 182 | return mmap64(start, len, prot, flags, fd, offset); |
| 183 | #else |
| 184 | return mmap(start, len, prot, flags, fd, offset); |
| 185 | #endif |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | char *d_name(struct dirent *di) |
| 190 | { |
| 191 | #if HAVE_BROKEN_READDIR |
| 192 | return (di->d_name - 2); |
| 193 | #else |
| 194 | return di->d_name; |
| 195 | #endif |
| 196 | } |