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