Commit | Line | Data |
---|---|---|
addf0c4a | 1 | /* |
0f78b815 WD |
2 | * Backup handling code. |
3 | * | |
4 | * Copyright (C) 1999 Andrew Tridgell | |
ba2133d6 | 5 | * Copyright (C) 2003-2007 Wayne Davison |
0f78b815 WD |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
ba2133d6 WD |
8 | * it under the terms of the GNU General Public License version 2 as |
9 | * published by the Free Software Foundation. | |
0f78b815 WD |
10 | * |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
e7c67065 WD |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 19 | */ |
3d19b4c8 AT |
20 | |
21 | #include "rsync.h" | |
22 | ||
23 | extern int verbose; | |
378a074c AT |
24 | extern int am_root; |
25 | extern int preserve_devices; | |
b5c6a6ae | 26 | extern int preserve_specials; |
378a074c | 27 | extern int preserve_links; |
377dbd20 | 28 | extern int safe_symlinks; |
6b2a3d5d | 29 | extern int backup_dir_len; |
82ad07c4 WD |
30 | extern unsigned int backup_dir_remainder; |
31 | extern char backup_dir_buf[MAXPATHLEN]; | |
32 | extern char *backup_suffix; | |
33 | extern char *backup_dir; | |
378a074c | 34 | |
c94e4afb | 35 | /* make a complete pathname for backup file */ |
45d8bfe0 | 36 | char *get_backup_name(const char *fname) |
c94e4afb | 37 | { |
c94e4afb WD |
38 | if (backup_dir) { |
39 | if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder, | |
40 | fname, backup_suffix, NULL) < backup_dir_remainder) | |
41 | return backup_dir_buf; | |
42 | } else { | |
fb22c277 WD |
43 | if (stringjoin(backup_dir_buf, MAXPATHLEN, |
44 | fname, backup_suffix, NULL) < MAXPATHLEN) | |
45 | return backup_dir_buf; | |
c94e4afb WD |
46 | } |
47 | ||
48 | rprintf(FERROR, "backup filename too long\n"); | |
49 | return NULL; | |
50 | } | |
51 | ||
66203a98 | 52 | /* simple backup creates a backup with a suffix in the same directory */ |
45d8bfe0 | 53 | static int make_simple_backup(const char *fname) |
3d19b4c8 | 54 | { |
ba679d51 | 55 | int rename_errno; |
45d8bfe0 | 56 | const char *fnamebak = get_backup_name(fname); |
5d2a7071 | 57 | |
c94e4afb | 58 | if (!fnamebak) |
3d19b4c8 | 59 | return 0; |
3d19b4c8 | 60 | |
ba679d51 WD |
61 | while (1) { |
62 | if (do_rename(fname, fnamebak) == 0) { | |
63 | if (verbose > 1) { | |
64 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 65 | fname, fnamebak); |
ba679d51 WD |
66 | } |
67 | break; | |
3d19b4c8 | 68 | } |
ba679d51 WD |
69 | /* cygwin (at least version b19) reports EINVAL */ |
70 | if (errno == ENOENT || errno == EINVAL) | |
71 | break; | |
72 | ||
73 | rename_errno = errno; | |
74 | if (errno == EISDIR && do_rmdir(fnamebak) == 0) | |
75 | continue; | |
76 | if (errno == ENOTDIR && do_unlink(fnamebak) == 0) | |
77 | continue; | |
78 | ||
79 | rsyserr(FERROR, rename_errno, "rename %s to backup %s", | |
45c49b52 | 80 | fname, fnamebak); |
ba679d51 WD |
81 | errno = rename_errno; |
82 | return 0; | |
3d19b4c8 | 83 | } |
ba679d51 | 84 | |
3d19b4c8 AT |
85 | return 1; |
86 | } | |
66203a98 AT |
87 | |
88 | ||
378a074c AT |
89 | /**************************************************************************** |
90 | Create a directory given an absolute path, perms based upon another directory | |
91 | path | |
92 | ****************************************************************************/ | |
de0e2250 | 93 | static int make_bak_dir(char *fullpath) |
378a074c | 94 | { |
47d6a60c | 95 | STRUCT_STAT st; |
de0e2250 WD |
96 | char *rel = fullpath + backup_dir_len; |
97 | char *end = rel + strlen(rel); | |
98 | char *p = end; | |
99 | ||
100 | while (strncmp(fullpath, "./", 2) == 0) | |
101 | fullpath += 2; | |
102 | ||
103 | /* Try to find an existing dir, starting from the deepest dir. */ | |
104 | while (1) { | |
105 | if (--p == fullpath) { | |
106 | p += strlen(p); | |
107 | goto failure; | |
108 | } | |
109 | if (*p == '/') { | |
110 | *p = '\0'; | |
a56cdef9 | 111 | if (mkdir_defmode(fullpath) == 0) |
de0e2250 WD |
112 | break; |
113 | if (errno != ENOENT) { | |
d62bcc17 WD |
114 | rsyserr(FERROR, errno, |
115 | "make_bak_dir mkdir %s failed", | |
116 | full_fname(fullpath)); | |
de0e2250 WD |
117 | goto failure; |
118 | } | |
119 | } | |
47d6a60c | 120 | } |
de0e2250 WD |
121 | |
122 | /* Make all the dirs that we didn't find on the way here. */ | |
123 | while (1) { | |
124 | if (p >= rel) { | |
125 | /* Try to transfer the directory settings of the | |
126 | * actual dir that the files are coming from. */ | |
9fd62d15 | 127 | if (do_stat(rel, &st) < 0) { |
d62bcc17 WD |
128 | rsyserr(FERROR, errno, |
129 | "make_bak_dir stat %s failed", | |
130 | full_fname(rel)); | |
de0e2250 | 131 | } else { |
de0e2250 | 132 | do_lchown(fullpath, st.st_uid, st.st_gid); |
1e999f9f | 133 | #ifdef HAVE_CHMOD |
de0e2250 | 134 | do_chmod(fullpath, st.st_mode); |
1e999f9f | 135 | #endif |
47d6a60c S |
136 | } |
137 | } | |
de0e2250 WD |
138 | *p = '/'; |
139 | p += strlen(p); | |
140 | if (p == end) | |
141 | break; | |
a56cdef9 | 142 | if (mkdir_defmode(fullpath) < 0) { |
d62bcc17 WD |
143 | rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed", |
144 | full_fname(fullpath)); | |
de0e2250 WD |
145 | goto failure; |
146 | } | |
47d6a60c S |
147 | } |
148 | return 0; | |
de0e2250 | 149 | |
b2e6caa0 | 150 | failure: |
de0e2250 WD |
151 | while (p != end) { |
152 | *p = '/'; | |
153 | p += strlen(p); | |
154 | } | |
155 | return -1; | |
378a074c AT |
156 | } |
157 | ||
66203a98 | 158 | /* robustly move a file, creating new directory structures if necessary */ |
45d8bfe0 | 159 | static int robust_move(const char *src, char *dst) |
66203a98 | 160 | { |
21955d9c WD |
161 | if (robust_rename(src, dst, NULL, 0755) < 0 |
162 | && (errno != ENOENT || make_bak_dir(dst) < 0 | |
163 | || robust_rename(src, dst, NULL, 0755) < 0)) | |
62c9e6b3 WD |
164 | return -1; |
165 | return 0; | |
de0e2250 | 166 | } |
66203a98 AT |
167 | |
168 | ||
de0e2250 WD |
169 | /* If we have a --backup-dir, then we get here from make_backup(). |
170 | * We will move the file to be deleted into a parallel directory tree. */ | |
45d8bfe0 | 171 | static int keep_backup(const char *fname) |
66203a98 | 172 | { |
66203a98 AT |
173 | STRUCT_STAT st; |
174 | struct file_struct *file; | |
c94e4afb | 175 | char *buf; |
47d6a60c | 176 | int kept = 0; |
378a074c AT |
177 | int ret_code; |
178 | ||
66203a98 | 179 | /* return if no file to keep */ |
018b2832 | 180 | if (do_lstat(fname, &st) < 0) |
9fd62d15 | 181 | return 1; |
66203a98 | 182 | |
3de73827 | 183 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
c94e4afb | 184 | return 1; /* the file could have disappeared */ |
66203a98 | 185 | |
82ad07c4 WD |
186 | if (!(buf = get_backup_name(fname))) { |
187 | unmake_file(file); | |
47d6a60c | 188 | return 0; |
82ad07c4 | 189 | } |
378a074c | 190 | |
378a074c | 191 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
192 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
193 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
e238c997 WD |
194 | uint32 *devp = F_RDEV_P(file); |
195 | dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
93e28fbd | 196 | do_unlink(buf); |
82ad07c4 | 197 | if (do_mknod(buf, file->mode, rdev) < 0 |
93e28fbd | 198 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 199 | || do_mknod(buf, file->mode, rdev) < 0)) { |
93e28fbd WD |
200 | rsyserr(FERROR, errno, "mknod %s failed", |
201 | full_fname(buf)); | |
202 | } else if (verbose > 2) { | |
203 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 204 | fname); |
47d6a60c S |
205 | } |
206 | kept = 1; | |
207 | do_unlink(fname); | |
208 | } | |
378a074c | 209 | |
addf0c4a | 210 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 211 | /* make an empty directory */ |
c94e4afb WD |
212 | if (do_mkdir(buf, file->mode) < 0 |
213 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
214 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 215 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 216 | full_fname(buf)); |
377dbd20 | 217 | } |
47d6a60c | 218 | |
377dbd20 | 219 | ret_code = do_rmdir(fname); |
addf0c4a | 220 | if (verbose > 2) { |
ea42541f WD |
221 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
222 | full_fname(fname), ret_code); | |
47d6a60c S |
223 | } |
224 | kept = 1; | |
225 | } | |
378a074c | 226 | |
4f5b0756 | 227 | #ifdef SUPPORT_LINKS |
addf0c4a | 228 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
82ad07c4 WD |
229 | const char *sl = F_SYMLINK(file); |
230 | if (safe_symlinks && unsafe_symlink(sl, buf)) { | |
47d6a60c S |
231 | if (verbose) { |
232 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
82ad07c4 | 233 | full_fname(buf), sl); |
47d6a60c S |
234 | } |
235 | kept = 1; | |
93e28fbd WD |
236 | } else { |
237 | do_unlink(buf); | |
82ad07c4 | 238 | if (do_symlink(sl, buf) < 0 |
93e28fbd | 239 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 240 | || do_symlink(sl, buf) < 0)) { |
93e28fbd | 241 | rsyserr(FERROR, errno, "link %s -> \"%s\"", |
82ad07c4 | 242 | full_fname(buf), sl); |
93e28fbd WD |
243 | } |
244 | do_unlink(fname); | |
245 | kept = 1; | |
47d6a60c | 246 | } |
47d6a60c | 247 | } |
378a074c | 248 | #endif |
378a074c | 249 | |
addf0c4a | 250 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 251 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 252 | fname); |
82ad07c4 | 253 | unmake_file(file); |
f8c8ef9e | 254 | return 1; |
47d6a60c | 255 | } |
66203a98 | 256 | |
66203a98 | 257 | /* move to keep tree if a file */ |
addf0c4a | 258 | if (!kept) { |
c94e4afb | 259 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 260 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 261 | full_fname(fname), buf); |
394bcdb5 WD |
262 | } else if (st.st_nlink > 1) { |
263 | /* If someone has hard-linked the file into the backup | |
3cb22c20 | 264 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 265 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
266 | } |
267 | } | |
c3ef136d | 268 | set_file_attrs(buf, file, NULL, 0); |
82ad07c4 | 269 | unmake_file(file); |
378a074c | 270 | |
4875d6b6 WD |
271 | if (verbose > 1) { |
272 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 273 | fname, buf); |
4875d6b6 | 274 | } |
66203a98 | 275 | return 1; |
de0e2250 | 276 | } |
66203a98 AT |
277 | |
278 | ||
279 | /* main backup switch routine */ | |
45d8bfe0 | 280 | int make_backup(const char *fname) |
66203a98 AT |
281 | { |
282 | if (backup_dir) | |
addf0c4a WD |
283 | return keep_backup(fname); |
284 | return make_simple_backup(fname); | |
66203a98 | 285 | } |