X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/62c9e6b3a54f147629497a2bc791acac60b5668b..377dbd207556fe8e0bca2387205b4e06d9ed23e1:/backup.c diff --git a/backup.c b/backup.c index 2078861b..db21fb1a 100644 --- a/backup.c +++ b/backup.c @@ -33,6 +33,7 @@ extern int preserve_devices; extern int preserve_links; extern int preserve_hard_links; extern int orig_umask; +extern int safe_symlinks; /* simple backup creates a backup with a suffix in the same directory */ static int make_simple_backup(char *fname) @@ -130,8 +131,8 @@ failure: /* robustly move a file, creating new directory structures if necessary */ static int robust_move(char *src, char *dst) { - if (robust_rename(src, dst, 0755) != 0 || errno != ENOENT - || make_bak_dir(dst) < 0 || robust_rename(src, dst, 0755) != 0) + if (robust_rename(src, dst, 0755) < 0 && (errno != ENOENT + || make_bak_dir(dst) < 0 || robust_rename(src, dst, 0755) < 0)) return -1; return 0; } @@ -169,8 +170,9 @@ static int keep_backup(char *fname) /* Check to see if this is a device file, or link */ if (IS_DEVICE(file->mode)) { if (am_root && preserve_devices) { - make_bak_dir(backup_dir_buf); - if (do_mknod(backup_dir_buf, file->mode, file->u.rdev) != 0) { + if (do_mknod(backup_dir_buf, file->mode, file->u.rdev) < 0 + && (errno != ENOENT || make_bak_dir(backup_dir_buf) < 0 + || do_mknod(backup_dir_buf, file->mode, file->u.rdev) < 0)) { rprintf(FERROR, "mknod %s failed: %s\n", full_fname(backup_dir_buf), strerror(errno)); } else if (verbose > 2) { @@ -186,10 +188,14 @@ static int keep_backup(char *fname) if (!kept && S_ISDIR(file->mode)) { /* make an empty directory */ - make_bak_dir(backup_dir_buf); - do_mkdir(backup_dir_buf, file->mode); - ret_code = do_rmdir(fname); + if (do_mkdir(backup_dir_buf, file->mode) < 0 + && (errno != ENOENT || make_bak_dir(backup_dir_buf) < 0 + || do_mkdir(backup_dir_buf, file->mode) < 0)) { + rprintf(FINFO, "mkdir %s failed: %s\n", + full_fname(backup_dir_buf), strerror(errno)); + } + ret_code = do_rmdir(fname); if (verbose > 2) { rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", full_fname(fname), ret_code); @@ -199,7 +205,6 @@ static int keep_backup(char *fname) #if SUPPORT_LINKS if (!kept && preserve_links && S_ISLNK(file->mode)) { - extern int safe_symlinks; if (safe_symlinks && unsafe_symlink(file->u.link, backup_dir_buf)) { if (verbose) { rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", @@ -207,8 +212,9 @@ static int keep_backup(char *fname) } kept = 1; } - make_bak_dir(backup_dir_buf); - if (do_symlink(file->u.link, backup_dir_buf) != 0) { + if (do_symlink(file->u.link, backup_dir_buf) < 0 + && (errno != ENOENT || make_bak_dir(backup_dir_buf) < 0 + || do_symlink(file->u.link, backup_dir_buf) < 0)) { rprintf(FERROR, "link %s -> %s : %s\n", full_fname(backup_dir_buf), file->u.link, strerror(errno)); } @@ -224,7 +230,7 @@ static int keep_backup(char *fname) /* move to keep tree if a file */ if (!kept) { - if (!robust_move(fname, backup_dir_buf)) { + if (robust_move(fname, backup_dir_buf) != 0) { rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n", full_fname(fname), backup_dir_buf, strerror(errno)); }