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 | 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 | ****************************************************************************/ | |
de0e2250 | 95 | static int make_bak_dir(char *fullpath) |
378a074c | 96 | { |
1c3344a1 WD |
97 | statx sx; |
98 | struct file_struct *file; | |
de0e2250 WD |
99 | char *rel = fullpath + backup_dir_len; |
100 | char *end = rel + strlen(rel); | |
101 | char *p = end; | |
102 | ||
103 | while (strncmp(fullpath, "./", 2) == 0) | |
104 | fullpath += 2; | |
105 | ||
106 | /* Try to find an existing dir, starting from the deepest dir. */ | |
107 | while (1) { | |
108 | if (--p == fullpath) { | |
109 | p += strlen(p); | |
110 | goto failure; | |
111 | } | |
112 | if (*p == '/') { | |
113 | *p = '\0'; | |
a56cdef9 | 114 | if (mkdir_defmode(fullpath) == 0) |
de0e2250 WD |
115 | break; |
116 | if (errno != ENOENT) { | |
d62bcc17 WD |
117 | rsyserr(FERROR, errno, |
118 | "make_bak_dir mkdir %s failed", | |
119 | full_fname(fullpath)); | |
de0e2250 WD |
120 | goto failure; |
121 | } | |
122 | } | |
47d6a60c | 123 | } |
de0e2250 WD |
124 | |
125 | /* Make all the dirs that we didn't find on the way here. */ | |
126 | while (1) { | |
127 | if (p >= rel) { | |
128 | /* Try to transfer the directory settings of the | |
129 | * actual dir that the files are coming from. */ | |
9439c0cb | 130 | if (x_stat(rel, &sx.st, NULL) < 0) { |
d62bcc17 WD |
131 | rsyserr(FERROR, errno, |
132 | "make_bak_dir stat %s failed", | |
133 | full_fname(rel)); | |
de0e2250 | 134 | } else { |
1c3344a1 WD |
135 | #ifdef SUPPORT_ACLS |
136 | sx.acc_acl = sx.def_acl = NULL; | |
16edf865 WD |
137 | #endif |
138 | #ifdef SUPPORT_XATTRS | |
139 | sx.xattr = NULL; | |
1c3344a1 WD |
140 | #endif |
141 | if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS))) | |
142 | continue; | |
143 | #ifdef SUPPORT_ACLS | |
144 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
145 | get_acl(rel, &sx); | |
146 | cache_acl(file, &sx); | |
147 | free_acl(&sx); | |
148 | } | |
1e999f9f | 149 | #endif |
16edf865 WD |
150 | #ifdef SUPPORT_XATTRS |
151 | if (preserve_xattrs) { | |
152 | get_xattr(rel, &sx); | |
153 | cache_xattr(file, &sx); | |
154 | free_xattr(&sx); | |
155 | } | |
156 | #endif | |
157 | set_file_attrs(fullpath, file, NULL, NULL, 0); | |
b13505da | 158 | unmake_file(file); |
47d6a60c S |
159 | } |
160 | } | |
de0e2250 WD |
161 | *p = '/'; |
162 | p += strlen(p); | |
163 | if (p == end) | |
164 | break; | |
a56cdef9 | 165 | if (mkdir_defmode(fullpath) < 0) { |
d62bcc17 WD |
166 | rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed", |
167 | full_fname(fullpath)); | |
de0e2250 WD |
168 | goto failure; |
169 | } | |
47d6a60c S |
170 | } |
171 | return 0; | |
de0e2250 | 172 | |
b2e6caa0 | 173 | failure: |
de0e2250 WD |
174 | while (p != end) { |
175 | *p = '/'; | |
176 | p += strlen(p); | |
177 | } | |
178 | return -1; | |
378a074c AT |
179 | } |
180 | ||
66203a98 | 181 | /* robustly move a file, creating new directory structures if necessary */ |
45d8bfe0 | 182 | static int robust_move(const char *src, char *dst) |
66203a98 | 183 | { |
21955d9c WD |
184 | if (robust_rename(src, dst, NULL, 0755) < 0 |
185 | && (errno != ENOENT || make_bak_dir(dst) < 0 | |
186 | || robust_rename(src, dst, NULL, 0755) < 0)) | |
62c9e6b3 WD |
187 | return -1; |
188 | return 0; | |
de0e2250 | 189 | } |
66203a98 AT |
190 | |
191 | ||
de0e2250 WD |
192 | /* If we have a --backup-dir, then we get here from make_backup(). |
193 | * We will move the file to be deleted into a parallel directory tree. */ | |
45d8bfe0 | 194 | static int keep_backup(const char *fname) |
66203a98 | 195 | { |
1c3344a1 | 196 | statx sx; |
66203a98 | 197 | struct file_struct *file; |
c94e4afb | 198 | char *buf; |
47d6a60c | 199 | int kept = 0; |
378a074c AT |
200 | int ret_code; |
201 | ||
66203a98 | 202 | /* return if no file to keep */ |
9439c0cb | 203 | if (x_lstat(fname, &sx.st, NULL) < 0) |
9fd62d15 | 204 | return 1; |
1c3344a1 WD |
205 | #ifdef SUPPORT_ACLS |
206 | sx.acc_acl = sx.def_acl = NULL; | |
207 | #endif | |
16edf865 WD |
208 | #ifdef SUPPORT_XATTRS |
209 | sx.xattr = NULL; | |
210 | #endif | |
66203a98 | 211 | |
3de73827 | 212 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
c94e4afb | 213 | return 1; /* the file could have disappeared */ |
66203a98 | 214 | |
82ad07c4 WD |
215 | if (!(buf = get_backup_name(fname))) { |
216 | unmake_file(file); | |
47d6a60c | 217 | return 0; |
82ad07c4 | 218 | } |
378a074c | 219 | |
1c3344a1 WD |
220 | #ifdef SUPPORT_ACLS |
221 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
222 | get_acl(fname, &sx); | |
223 | cache_acl(file, &sx); | |
224 | free_acl(&sx); | |
225 | } | |
226 | #endif | |
16edf865 WD |
227 | #ifdef SUPPORT_XATTRS |
228 | if (preserve_xattrs) { | |
229 | get_xattr(fname, &sx); | |
230 | cache_xattr(file, &sx); | |
231 | free_xattr(&sx); | |
232 | } | |
233 | #endif | |
1c3344a1 | 234 | |
378a074c | 235 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
236 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
237 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
e238c997 WD |
238 | uint32 *devp = F_RDEV_P(file); |
239 | dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
93e28fbd | 240 | do_unlink(buf); |
82ad07c4 | 241 | if (do_mknod(buf, file->mode, rdev) < 0 |
93e28fbd | 242 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 243 | || do_mknod(buf, file->mode, rdev) < 0)) { |
93e28fbd WD |
244 | rsyserr(FERROR, errno, "mknod %s failed", |
245 | full_fname(buf)); | |
246 | } else if (verbose > 2) { | |
247 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 248 | fname); |
47d6a60c S |
249 | } |
250 | kept = 1; | |
251 | do_unlink(fname); | |
252 | } | |
378a074c | 253 | |
addf0c4a | 254 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 255 | /* make an empty directory */ |
c94e4afb WD |
256 | if (do_mkdir(buf, file->mode) < 0 |
257 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
258 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 259 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 260 | full_fname(buf)); |
377dbd20 | 261 | } |
47d6a60c | 262 | |
377dbd20 | 263 | ret_code = do_rmdir(fname); |
addf0c4a | 264 | if (verbose > 2) { |
ea42541f WD |
265 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
266 | full_fname(fname), ret_code); | |
47d6a60c S |
267 | } |
268 | kept = 1; | |
269 | } | |
378a074c | 270 | |
4f5b0756 | 271 | #ifdef SUPPORT_LINKS |
addf0c4a | 272 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
82ad07c4 WD |
273 | const char *sl = F_SYMLINK(file); |
274 | if (safe_symlinks && unsafe_symlink(sl, buf)) { | |
47d6a60c S |
275 | if (verbose) { |
276 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
82ad07c4 | 277 | full_fname(buf), sl); |
47d6a60c S |
278 | } |
279 | kept = 1; | |
93e28fbd WD |
280 | } else { |
281 | do_unlink(buf); | |
82ad07c4 | 282 | if (do_symlink(sl, buf) < 0 |
93e28fbd | 283 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 284 | || do_symlink(sl, buf) < 0)) { |
93e28fbd | 285 | rsyserr(FERROR, errno, "link %s -> \"%s\"", |
82ad07c4 | 286 | full_fname(buf), sl); |
93e28fbd WD |
287 | } |
288 | do_unlink(fname); | |
289 | kept = 1; | |
47d6a60c | 290 | } |
47d6a60c | 291 | } |
378a074c | 292 | #endif |
378a074c | 293 | |
addf0c4a | 294 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 295 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 296 | fname); |
82ad07c4 | 297 | unmake_file(file); |
f8c8ef9e | 298 | return 1; |
47d6a60c | 299 | } |
66203a98 | 300 | |
66203a98 | 301 | /* move to keep tree if a file */ |
addf0c4a | 302 | if (!kept) { |
c94e4afb | 303 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 304 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 305 | full_fname(fname), buf); |
1c3344a1 | 306 | } else if (sx.st.st_nlink > 1) { |
394bcdb5 | 307 | /* If someone has hard-linked the file into the backup |
3cb22c20 | 308 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 309 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
310 | } |
311 | } | |
16edf865 | 312 | set_file_attrs(buf, file, NULL, fname, 0); |
82ad07c4 | 313 | unmake_file(file); |
378a074c | 314 | |
4875d6b6 WD |
315 | if (verbose > 1) { |
316 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 317 | fname, buf); |
4875d6b6 | 318 | } |
66203a98 | 319 | return 1; |
de0e2250 | 320 | } |
66203a98 AT |
321 | |
322 | ||
323 | /* main backup switch routine */ | |
45d8bfe0 | 324 | int make_backup(const char *fname) |
66203a98 AT |
325 | { |
326 | if (backup_dir) | |
addf0c4a WD |
327 | return keep_backup(fname); |
328 | return make_simple_backup(fname); | |
66203a98 | 329 | } |