| 1 | /* |
| 2 | * Backup handling code. |
| 3 | * |
| 4 | * Copyright (C) 1999 Andrew Tridgell |
| 5 | * Copyright (C) 2003-2008 Wayne Davison |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, visit the http://fsf.org website. |
| 19 | */ |
| 20 | |
| 21 | #include "rsync.h" |
| 22 | |
| 23 | extern int am_root; |
| 24 | extern int preserve_acls; |
| 25 | extern int preserve_xattrs; |
| 26 | extern int preserve_devices; |
| 27 | extern int preserve_specials; |
| 28 | extern int preserve_links; |
| 29 | extern int safe_symlinks; |
| 30 | extern int backup_dir_len; |
| 31 | extern unsigned int backup_dir_remainder; |
| 32 | extern char backup_dir_buf[MAXPATHLEN]; |
| 33 | extern char *backup_suffix; |
| 34 | extern char *backup_dir; |
| 35 | |
| 36 | /* make a complete pathname for backup file */ |
| 37 | char *get_backup_name(const char *fname) |
| 38 | { |
| 39 | if (backup_dir) { |
| 40 | if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder, |
| 41 | fname, backup_suffix, NULL) < backup_dir_remainder) |
| 42 | return backup_dir_buf; |
| 43 | } else { |
| 44 | if (stringjoin(backup_dir_buf, MAXPATHLEN, |
| 45 | fname, backup_suffix, NULL) < MAXPATHLEN) |
| 46 | return backup_dir_buf; |
| 47 | } |
| 48 | |
| 49 | rprintf(FERROR, "backup filename too long\n"); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | /* simple backup creates a backup with a suffix in the same directory */ |
| 54 | static int make_simple_backup(const char *fname) |
| 55 | { |
| 56 | int rename_errno; |
| 57 | const char *fnamebak = get_backup_name(fname); |
| 58 | |
| 59 | if (!fnamebak) |
| 60 | return 0; |
| 61 | |
| 62 | while (1) { |
| 63 | if (do_rename(fname, fnamebak) == 0) { |
| 64 | if (INFO_GTE(BACKUP, 1)) { |
| 65 | rprintf(FINFO, "backed up %s to %s\n", |
| 66 | fname, fnamebak); |
| 67 | } |
| 68 | break; |
| 69 | } |
| 70 | /* cygwin (at least version b19) reports EINVAL */ |
| 71 | if (errno == ENOENT || errno == EINVAL) |
| 72 | break; |
| 73 | |
| 74 | rename_errno = errno; |
| 75 | if (errno == EISDIR && do_rmdir(fnamebak) == 0) |
| 76 | continue; |
| 77 | if (errno == ENOTDIR && do_unlink(fnamebak) == 0) |
| 78 | continue; |
| 79 | |
| 80 | rsyserr(FERROR, rename_errno, "rename %s to backup %s", |
| 81 | fname, fnamebak); |
| 82 | errno = rename_errno; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /**************************************************************************** |
| 91 | Create a directory given an absolute path, perms based upon another directory |
| 92 | path |
| 93 | ****************************************************************************/ |
| 94 | int make_bak_dir(const char *fullpath) |
| 95 | { |
| 96 | char fbuf[MAXPATHLEN], *rel, *end, *p; |
| 97 | struct file_struct *file; |
| 98 | int len = backup_dir_len; |
| 99 | stat_x sx; |
| 100 | |
| 101 | while (*fullpath == '.' && fullpath[1] == '/') { |
| 102 | fullpath += 2; |
| 103 | len -= 2; |
| 104 | } |
| 105 | |
| 106 | if (strlcpy(fbuf, fullpath, sizeof fbuf) >= sizeof fbuf) |
| 107 | return -1; |
| 108 | |
| 109 | rel = fbuf + len; |
| 110 | end = p = rel + strlen(rel); |
| 111 | |
| 112 | /* Try to find an existing dir, starting from the deepest dir. */ |
| 113 | while (1) { |
| 114 | if (--p == fbuf) |
| 115 | return -1; |
| 116 | if (*p == '/') { |
| 117 | *p = '\0'; |
| 118 | if (mkdir_defmode(fbuf) == 0) |
| 119 | break; |
| 120 | if (errno != ENOENT) { |
| 121 | rsyserr(FERROR, errno, |
| 122 | "make_bak_dir mkdir %s failed", |
| 123 | full_fname(fbuf)); |
| 124 | return -1; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* Make all the dirs that we didn't find on the way here. */ |
| 130 | while (1) { |
| 131 | if (p >= rel) { |
| 132 | /* Try to transfer the directory settings of the |
| 133 | * actual dir that the files are coming from. */ |
| 134 | if (x_stat(rel, &sx.st, NULL) < 0) { |
| 135 | rsyserr(FERROR, errno, |
| 136 | "make_bak_dir stat %s failed", |
| 137 | full_fname(rel)); |
| 138 | } else { |
| 139 | #ifdef SUPPORT_ACLS |
| 140 | sx.acc_acl = sx.def_acl = NULL; |
| 141 | #endif |
| 142 | #ifdef SUPPORT_XATTRS |
| 143 | sx.xattr = NULL; |
| 144 | #endif |
| 145 | if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS))) |
| 146 | continue; |
| 147 | #ifdef SUPPORT_ACLS |
| 148 | if (preserve_acls && !S_ISLNK(file->mode)) { |
| 149 | get_acl(rel, &sx); |
| 150 | cache_acl(file, &sx); |
| 151 | free_acl(&sx); |
| 152 | } |
| 153 | #endif |
| 154 | #ifdef SUPPORT_XATTRS |
| 155 | if (preserve_xattrs) { |
| 156 | get_xattr(rel, &sx); |
| 157 | cache_xattr(file, &sx); |
| 158 | free_xattr(&sx); |
| 159 | } |
| 160 | #endif |
| 161 | set_file_attrs(fbuf, file, NULL, NULL, 0); |
| 162 | unmake_file(file); |
| 163 | } |
| 164 | } |
| 165 | *p = '/'; |
| 166 | p += strlen(p); |
| 167 | if (p == end) |
| 168 | break; |
| 169 | if (mkdir_defmode(fbuf) < 0) { |
| 170 | rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed", |
| 171 | full_fname(fbuf)); |
| 172 | return -1; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* robustly move a file, creating new directory structures if necessary */ |
| 180 | static int robust_move(const char *src, char *dst) |
| 181 | { |
| 182 | if (robust_rename(src, dst, NULL, 0755) < 0) { |
| 183 | int save_errno = errno ? errno : EINVAL; /* 0 paranoia */ |
| 184 | if (errno == ENOENT && make_bak_dir(dst) == 0) { |
| 185 | if (robust_rename(src, dst, NULL, 0755) < 0) |
| 186 | save_errno = errno ? errno : save_errno; |
| 187 | else |
| 188 | save_errno = 0; |
| 189 | } |
| 190 | if (save_errno) { |
| 191 | errno = save_errno; |
| 192 | return -1; |
| 193 | } |
| 194 | } |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* If we have a --backup-dir, then we get here from make_backup(). |
| 200 | * We will move the file to be deleted into a parallel directory tree. */ |
| 201 | static int keep_backup(const char *fname) |
| 202 | { |
| 203 | stat_x sx; |
| 204 | struct file_struct *file; |
| 205 | char *buf; |
| 206 | int save_preserve_xattrs = preserve_xattrs; |
| 207 | int kept = 0; |
| 208 | int ret_code; |
| 209 | |
| 210 | /* return if no file to keep */ |
| 211 | if (x_lstat(fname, &sx.st, NULL) < 0) |
| 212 | return 1; |
| 213 | #ifdef SUPPORT_ACLS |
| 214 | sx.acc_acl = sx.def_acl = NULL; |
| 215 | #endif |
| 216 | #ifdef SUPPORT_XATTRS |
| 217 | sx.xattr = NULL; |
| 218 | #endif |
| 219 | |
| 220 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
| 221 | return 1; /* the file could have disappeared */ |
| 222 | |
| 223 | if (!(buf = get_backup_name(fname))) { |
| 224 | unmake_file(file); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | #ifdef SUPPORT_ACLS |
| 229 | if (preserve_acls && !S_ISLNK(file->mode)) { |
| 230 | get_acl(fname, &sx); |
| 231 | cache_acl(file, &sx); |
| 232 | free_acl(&sx); |
| 233 | } |
| 234 | #endif |
| 235 | #ifdef SUPPORT_XATTRS |
| 236 | if (preserve_xattrs) { |
| 237 | get_xattr(fname, &sx); |
| 238 | cache_xattr(file, &sx); |
| 239 | free_xattr(&sx); |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | /* Check to see if this is a device file, or link */ |
| 244 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
| 245 | || (preserve_specials && IS_SPECIAL(file->mode))) { |
| 246 | int save_errno; |
| 247 | do_unlink(buf); |
| 248 | if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) { |
| 249 | save_errno = errno ? errno : EINVAL; /* 0 paranoia */ |
| 250 | if (errno == ENOENT && make_bak_dir(buf) == 0) { |
| 251 | if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) |
| 252 | save_errno = errno ? errno : save_errno; |
| 253 | else |
| 254 | save_errno = 0; |
| 255 | } |
| 256 | if (save_errno) { |
| 257 | rsyserr(FERROR, save_errno, "mknod %s failed", |
| 258 | full_fname(buf)); |
| 259 | } |
| 260 | } else |
| 261 | save_errno = 0; |
| 262 | if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) { |
| 263 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", |
| 264 | fname); |
| 265 | } |
| 266 | kept = 1; |
| 267 | do_unlink(fname); |
| 268 | } |
| 269 | |
| 270 | if (!kept && S_ISDIR(file->mode)) { |
| 271 | /* make an empty directory */ |
| 272 | if (do_mkdir(buf, file->mode) < 0) { |
| 273 | int save_errno = errno ? errno : EINVAL; /* 0 paranoia */ |
| 274 | if (errno == ENOENT && make_bak_dir(buf) == 0) { |
| 275 | if (do_mkdir(buf, file->mode) < 0) |
| 276 | save_errno = errno ? errno : save_errno; |
| 277 | else |
| 278 | save_errno = 0; |
| 279 | } |
| 280 | if (save_errno) { |
| 281 | rsyserr(FINFO, save_errno, "mkdir %s failed", |
| 282 | full_fname(buf)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | ret_code = do_rmdir(fname); |
| 287 | if (DEBUG_GTE(BACKUP, 1)) { |
| 288 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
| 289 | full_fname(fname), ret_code); |
| 290 | } |
| 291 | kept = 1; |
| 292 | } |
| 293 | |
| 294 | #ifdef SUPPORT_LINKS |
| 295 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
| 296 | const char *sl = F_SYMLINK(file); |
| 297 | if (safe_symlinks && unsafe_symlink(sl, buf)) { |
| 298 | if (INFO_GTE(SYMSAFE, 1)) { |
| 299 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", |
| 300 | full_fname(buf), sl); |
| 301 | } |
| 302 | kept = 1; |
| 303 | } else { |
| 304 | do_unlink(buf); |
| 305 | if (do_symlink(sl, buf) < 0) { |
| 306 | int save_errno = errno ? errno : EINVAL; /* 0 paranoia */ |
| 307 | if (errno == ENOENT && make_bak_dir(buf) == 0) { |
| 308 | if (do_symlink(sl, buf) < 0) |
| 309 | save_errno = errno ? errno : save_errno; |
| 310 | else |
| 311 | save_errno = 0; |
| 312 | } |
| 313 | if (save_errno) { |
| 314 | rsyserr(FERROR, save_errno, "link %s -> \"%s\"", |
| 315 | full_fname(buf), sl); |
| 316 | } |
| 317 | } |
| 318 | do_unlink(fname); |
| 319 | kept = 1; |
| 320 | } |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | if (!kept && !S_ISREG(file->mode)) { |
| 325 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
| 326 | fname); |
| 327 | unmake_file(file); |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | /* move to keep tree if a file */ |
| 332 | if (!kept) { |
| 333 | if (robust_move(fname, buf) != 0) { |
| 334 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
| 335 | full_fname(fname), buf); |
| 336 | } else if (sx.st.st_nlink > 1) { |
| 337 | /* If someone has hard-linked the file into the backup |
| 338 | * dir, rename() might return success but do nothing! */ |
| 339 | robust_unlink(fname); /* Just in case... */ |
| 340 | } |
| 341 | } |
| 342 | preserve_xattrs = 0; |
| 343 | set_file_attrs(buf, file, NULL, fname, 0); |
| 344 | preserve_xattrs = save_preserve_xattrs; |
| 345 | unmake_file(file); |
| 346 | |
| 347 | if (INFO_GTE(BACKUP, 1)) { |
| 348 | rprintf(FINFO, "backed up %s to %s\n", |
| 349 | fname, buf); |
| 350 | } |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /* main backup switch routine */ |
| 356 | int make_backup(const char *fname) |
| 357 | { |
| 358 | if (backup_dir) |
| 359 | return keep_backup(fname); |
| 360 | return make_simple_backup(fname); |
| 361 | } |