| 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 char *backup_suffix; |
| 25 | extern char *backup_dir; |
| 26 | |
| 27 | |
| 28 | /* simple backup creates a backup with a suffix in the same directory */ |
| 29 | static int make_simple_backup(char *fname) |
| 30 | { |
| 31 | char fnamebak[MAXPATHLEN]; |
| 32 | if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) { |
| 33 | rprintf(FERROR,"backup filename too long\n"); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | slprintf(fnamebak,sizeof(fnamebak),"%s%s",fname,backup_suffix); |
| 38 | if (do_rename(fname,fnamebak) != 0) { |
| 39 | /* cygwin (at least version b19) reports EINVAL */ |
| 40 | if (errno != ENOENT && errno != EINVAL) { |
| 41 | rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); |
| 42 | return 0; |
| 43 | } |
| 44 | } else if (verbose > 1) { |
| 45 | rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak); |
| 46 | } |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* recursively make a directory path */ |
| 52 | static int make_dir(char *name, int mask) |
| 53 | { |
| 54 | char newdir [MAXPATHLEN]; |
| 55 | char *p, *d; |
| 56 | |
| 57 | /* copy pathname over, look for last '/' */ |
| 58 | for (p = d = newdir; *name; *d++ = *name++) |
| 59 | if (*name == '/') |
| 60 | p = d; |
| 61 | if (p == newdir) |
| 62 | return 0; |
| 63 | *p = 0; |
| 64 | |
| 65 | /* make the new directory, if that fails then make its parent */ |
| 66 | while (do_mkdir (newdir, mask) != 0) |
| 67 | if ((errno != ENOENT) || !make_dir (newdir, mask)) |
| 68 | return 0; |
| 69 | |
| 70 | return 1; |
| 71 | } /* make_dir */ |
| 72 | |
| 73 | |
| 74 | /* robustly move a file, creating new directory structures if necessary */ |
| 75 | static int robust_move(char *src, char *dst) |
| 76 | { |
| 77 | int keep_trying = 4; |
| 78 | int keep_path_extfs = 0; |
| 79 | int failed; |
| 80 | |
| 81 | while (keep_trying) { |
| 82 | if (keep_path_extfs) |
| 83 | failed = copy_file (src, dst, 0755); |
| 84 | else |
| 85 | failed = robust_rename (src, dst); |
| 86 | |
| 87 | if (failed) { |
| 88 | if (verbose > 2) |
| 89 | rprintf (FERROR, "robust_move failed: %s(%d)\n", |
| 90 | strerror (errno), errno); |
| 91 | switch (errno) { |
| 92 | /* external filesystem */ |
| 93 | case EXDEV: |
| 94 | keep_path_extfs = 1; |
| 95 | keep_trying--; |
| 96 | break; |
| 97 | /* no directory to write to */ |
| 98 | case ENOENT: |
| 99 | make_dir (dst, 0755); |
| 100 | keep_trying--; |
| 101 | break; |
| 102 | default: |
| 103 | keep_trying = 0; |
| 104 | } /* switch */ |
| 105 | } else |
| 106 | keep_trying = 0; |
| 107 | } /* while */ |
| 108 | return (!failed); |
| 109 | } /* robust_move */ |
| 110 | |
| 111 | |
| 112 | /* if we have a backup_dir, then we get here from make_backup(). |
| 113 | We will move the file to be deleted into a parallel directory tree */ |
| 114 | static int keep_backup(char *fname) |
| 115 | { |
| 116 | static int initialised; |
| 117 | |
| 118 | char keep_name [MAXPATHLEN]; |
| 119 | STRUCT_STAT st; |
| 120 | struct file_struct *file; |
| 121 | |
| 122 | if (!initialised) { |
| 123 | if (backup_dir[strlen(backup_dir) - 1] == '/') |
| 124 | backup_dir[strlen(backup_dir) - 1] = 0; |
| 125 | if (verbose > 0) |
| 126 | rprintf (FINFO, "backup_dir is %s\n", backup_dir); |
| 127 | initialised = 1; |
| 128 | } |
| 129 | |
| 130 | /* return if no file to keep */ |
| 131 | #if SUPPORT_LINKS |
| 132 | if (do_lstat (fname, &st)) return 1; |
| 133 | #else |
| 134 | if (do_stat (fname, &st)) return 1; |
| 135 | #endif |
| 136 | |
| 137 | file = make_file (0, fname); |
| 138 | |
| 139 | /* make a complete pathname for backup file */ |
| 140 | if (strlen(backup_dir) + strlen(fname) > (MAXPATHLEN - 1)) { |
| 141 | rprintf (FERROR, "keep_backup filename too long\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname); |
| 145 | |
| 146 | if (!S_ISDIR(file->mode)) { |
| 147 | /* move to keep tree if a file */ |
| 148 | if (!robust_move (fname, keep_name)) |
| 149 | rprintf(FERROR, "keep_backup failed %s -> %s : %s\n", |
| 150 | fname, keep_name, strerror(errno)); |
| 151 | } else { |
| 152 | /* this bit only used to "keep" empty directories */ |
| 153 | /* make the parent directories */ |
| 154 | make_dir (keep_name, 0755); |
| 155 | /* now make the (empty) directory */ |
| 156 | do_mkdir (keep_name, file->mode); |
| 157 | if (verbose > 1) |
| 158 | rprintf (FINFO, "keep_backup: made empty dir: %s\n", |
| 159 | keep_name); |
| 160 | } |
| 161 | |
| 162 | set_perms (keep_name, file, NULL, 0); |
| 163 | free_file (file); |
| 164 | free (file); |
| 165 | if (verbose > 1) |
| 166 | rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name); |
| 167 | return 1; |
| 168 | } /* keep_backup */ |
| 169 | |
| 170 | |
| 171 | /* main backup switch routine */ |
| 172 | int make_backup(char *fname) |
| 173 | { |
| 174 | if (backup_dir) |
| 175 | return (keep_backup(fname)); |
| 176 | else |
| 177 | return (make_simple_backup(fname)); |
| 178 | } |
| 179 | |