| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1999 |
| 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 | /* backup handling code */ |
| 20 | |
| 21 | #include "rsync.h" |
| 22 | |
| 23 | extern int verbose; |
| 24 | extern int backup_suffix_len; |
| 25 | extern int backup_dir_len; |
| 26 | extern char *backup_suffix; |
| 27 | extern char *backup_dir; |
| 28 | |
| 29 | extern int am_root; |
| 30 | extern int preserve_devices; |
| 31 | extern int preserve_links; |
| 32 | extern int preserve_hard_links; |
| 33 | extern int orig_umask; |
| 34 | |
| 35 | /* simple backup creates a backup with a suffix in the same directory */ |
| 36 | static int make_simple_backup(char *fname) |
| 37 | { |
| 38 | char fnamebak[MAXPATHLEN]; |
| 39 | |
| 40 | if (stringjoin(fnamebak, sizeof fnamebak, fname, backup_suffix, NULL) |
| 41 | >= sizeof fnamebak) { |
| 42 | rprintf(FERROR, "backup filename too long\n"); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | if (do_rename(fname, fnamebak) != 0) { |
| 47 | /* cygwin (at least version b19) reports EINVAL */ |
| 48 | if (errno != ENOENT && errno != EINVAL) { |
| 49 | rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak); |
| 50 | return 0; |
| 51 | } |
| 52 | } else if (verbose > 1) { |
| 53 | rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak); |
| 54 | } |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* recursively make a directory path */ |
| 60 | static int make_dir(char *name, int mask) |
| 61 | { |
| 62 | char newdir [MAXPATHLEN]; |
| 63 | char *p, *d; |
| 64 | |
| 65 | /* copy pathname over, look for last '/' */ |
| 66 | for (p = d = newdir; *name; *d++ = *name++) |
| 67 | if (*name == '/') |
| 68 | p = d; |
| 69 | if (p == newdir) |
| 70 | return 0; |
| 71 | *p = 0; |
| 72 | |
| 73 | /* make the new directory, if that fails then make its parent */ |
| 74 | while (do_mkdir(newdir, mask) != 0) { |
| 75 | if (errno != ENOENT || !make_dir(newdir, mask)) |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | return 1; |
| 80 | } /* make_dir */ |
| 81 | |
| 82 | |
| 83 | /**************************************************************************** |
| 84 | Create a directory given an absolute path, perms based upon another directory |
| 85 | path |
| 86 | ****************************************************************************/ |
| 87 | static int make_bak_dir(char *fname, char *bak_path) |
| 88 | { |
| 89 | STRUCT_STAT st; |
| 90 | STRUCT_STAT *st2; |
| 91 | char fullpath[MAXPATHLEN]; |
| 92 | char *p; |
| 93 | char *q; |
| 94 | |
| 95 | while(strncmp(bak_path, "./", 2) == 0) bak_path += 2; |
| 96 | |
| 97 | if (pathjoin(fullpath, sizeof fullpath, bak_path, fname) |
| 98 | >= sizeof fullpath) { |
| 99 | rprintf(FERROR, "backup dirname too long\n"); |
| 100 | return 0; |
| 101 | } |
| 102 | p = fullpath; |
| 103 | q = fullpath + strlen(bak_path); |
| 104 | if (*q == '/') |
| 105 | q++; /* Point past the middle '/' added by pathjoin(). */ |
| 106 | |
| 107 | /* Make the directories */ |
| 108 | while ((p = strchr(p, '/')) != NULL) { |
| 109 | *p = 0; |
| 110 | if (do_lstat(fullpath, &st) != 0) { |
| 111 | do_mkdir(fullpath, 0777 & ~orig_umask); |
| 112 | if (p > q) { |
| 113 | if (do_lstat(q, &st) != 0) { |
| 114 | rprintf(FERROR, "make_bak_dir stat %s failed: %s\n", |
| 115 | full_fname(fullpath), strerror(errno)); |
| 116 | } else { |
| 117 | st2 = &st; |
| 118 | set_modtime(fullpath, st2->st_mtime); |
| 119 | if (do_lchown(fullpath, st2->st_uid, st2->st_gid) != 0) { |
| 120 | rprintf(FERROR, "make_bak_dir chown %s failed: %s\n", |
| 121 | full_fname(fullpath), strerror(errno)); |
| 122 | } |
| 123 | if (do_chmod(fullpath, st2->st_mode) != 0) { |
| 124 | rprintf(FERROR, "make_bak_dir failed to set permissions on %s: %s\n", |
| 125 | full_fname(fullpath), strerror(errno)); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | *p++ = '/'; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* robustly move a file, creating new directory structures if necessary */ |
| 136 | static int robust_move(char *src, char *dst) |
| 137 | { |
| 138 | int keep_trying = 4; |
| 139 | int keep_path_extfs = 0; |
| 140 | int failed; |
| 141 | |
| 142 | while (keep_trying) { |
| 143 | if (keep_path_extfs) { |
| 144 | failed = copy_file(src, dst, 0755); |
| 145 | if (!failed) |
| 146 | do_unlink(src); |
| 147 | } else |
| 148 | failed = robust_rename(src, dst); |
| 149 | |
| 150 | if (failed) { |
| 151 | if (verbose > 2) { |
| 152 | rprintf(FERROR, "robust_move failed: %s(%d)\n", |
| 153 | strerror(errno), errno); |
| 154 | } |
| 155 | switch (errno) { |
| 156 | case EXDEV: /* external filesystem */ |
| 157 | keep_path_extfs = 1; |
| 158 | keep_trying--; |
| 159 | break; |
| 160 | case ENOENT: /* no directory to write to */ |
| 161 | make_dir(dst, 0700); |
| 162 | keep_trying--; |
| 163 | break; |
| 164 | default: |
| 165 | keep_trying = 0; |
| 166 | break; |
| 167 | } |
| 168 | } else |
| 169 | keep_trying = 0; |
| 170 | } /* while */ |
| 171 | return !failed; |
| 172 | } /* robust_move */ |
| 173 | |
| 174 | |
| 175 | /* if we have a backup_dir, then we get here from make_backup(). |
| 176 | We will move the file to be deleted into a parallel directory tree */ |
| 177 | static int keep_backup(char *fname) |
| 178 | { |
| 179 | static int initialised; |
| 180 | char keep_name[MAXPATHLEN]; |
| 181 | STRUCT_STAT st; |
| 182 | struct file_struct *file; |
| 183 | int kept = 0; |
| 184 | int ret_code; |
| 185 | |
| 186 | if (!initialised) { |
| 187 | if (backup_dir_len && backup_dir[backup_dir_len - 1] == '/') |
| 188 | backup_dir[--backup_dir_len] = '\0'; |
| 189 | if (verbose > 0) |
| 190 | rprintf(FINFO, "backup_dir is %s\n", backup_dir); |
| 191 | initialised = 1; |
| 192 | } |
| 193 | |
| 194 | /* return if no file to keep */ |
| 195 | #if SUPPORT_LINKS |
| 196 | if (do_lstat(fname, &st)) return 1; |
| 197 | #else |
| 198 | if (do_stat(fname, &st)) return 1; |
| 199 | #endif |
| 200 | |
| 201 | file = make_file(fname, NULL, NO_EXCLUDES); |
| 202 | |
| 203 | /* the file could have disappeared */ |
| 204 | if (!file) return 1; |
| 205 | |
| 206 | /* make a complete pathname for backup file */ |
| 207 | if (stringjoin(keep_name, sizeof keep_name, |
| 208 | backup_dir, "/", fname, backup_suffix, NULL) |
| 209 | >= sizeof keep_name) { |
| 210 | rprintf(FERROR, "keep_backup filename too long\n"); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #ifdef HAVE_MKNOD |
| 215 | /* Check to see if this is a device file, or link */ |
| 216 | if (IS_DEVICE(file->mode)) { |
| 217 | if (am_root && preserve_devices) { |
| 218 | make_bak_dir(fname, backup_dir); |
| 219 | if (do_mknod(keep_name, file->mode, file->u.rdev) != 0) { |
| 220 | rprintf(FERROR, "mknod %s failed: %s\n", |
| 221 | full_fname(keep_name), strerror(errno)); |
| 222 | } else if (verbose > 2) { |
| 223 | rprintf(FINFO, |
| 224 | "make_backup: DEVICE %s successful.\n", |
| 225 | fname); |
| 226 | } |
| 227 | } |
| 228 | kept = 1; |
| 229 | do_unlink(fname); |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | if (!kept && S_ISDIR(file->mode)) { |
| 234 | /* make an empty directory */ |
| 235 | make_bak_dir(fname, backup_dir); |
| 236 | do_mkdir(keep_name, file->mode); |
| 237 | ret_code = do_rmdir(fname); |
| 238 | |
| 239 | if (verbose > 2) { |
| 240 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
| 241 | full_fname(fname), ret_code); |
| 242 | } |
| 243 | kept = 1; |
| 244 | } |
| 245 | |
| 246 | #if SUPPORT_LINKS |
| 247 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
| 248 | extern int safe_symlinks; |
| 249 | if (safe_symlinks && unsafe_symlink(file->u.link, keep_name)) { |
| 250 | if (verbose) { |
| 251 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", |
| 252 | full_fname(keep_name), file->u.link); |
| 253 | } |
| 254 | kept = 1; |
| 255 | } |
| 256 | make_bak_dir(fname, backup_dir); |
| 257 | if (do_symlink(file->u.link, keep_name) != 0) { |
| 258 | rprintf(FERROR, "link %s -> %s : %s\n", |
| 259 | full_fname(keep_name), file->u.link, strerror(errno)); |
| 260 | } |
| 261 | do_unlink(fname); |
| 262 | kept = 1; |
| 263 | } |
| 264 | #endif |
| 265 | if (!kept && preserve_hard_links && file->link_u.links |
| 266 | && file->F_HEAD != file) { |
| 267 | if (verbose > 1) |
| 268 | rprintf(FINFO, "%s is a hard link\n", f_name(file)); |
| 269 | } |
| 270 | |
| 271 | if (!kept && !S_ISREG(file->mode)) { |
| 272 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
| 273 | fname); |
| 274 | } |
| 275 | |
| 276 | /* move to keep tree if a file */ |
| 277 | if (!kept) { |
| 278 | if (!robust_move(fname, keep_name)) { |
| 279 | rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n", |
| 280 | full_fname(fname), keep_name, strerror(errno)); |
| 281 | } |
| 282 | } |
| 283 | set_perms(keep_name, file, NULL, 0); |
| 284 | free_file(file); |
| 285 | free(file); |
| 286 | |
| 287 | if (verbose > 1) |
| 288 | rprintf(FINFO, "keep_backup %s -> %s\n", fname, keep_name); |
| 289 | return 1; |
| 290 | } /* keep_backup */ |
| 291 | |
| 292 | |
| 293 | /* main backup switch routine */ |
| 294 | int make_backup(char *fname) |
| 295 | { |
| 296 | if (backup_dir) |
| 297 | return keep_backup(fname); |
| 298 | return make_simple_backup(fname); |
| 299 | } |