Commit | Line | Data |
---|---|---|
addf0c4a | 1 | /* |
0f78b815 WD |
2 | * Backup handling code. |
3 | * | |
4 | * Copyright (C) 1999 Andrew Tridgell | |
5 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
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 WD |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 20 | */ |
3d19b4c8 AT |
21 | |
22 | #include "rsync.h" | |
23 | ||
24 | extern int verbose; | |
daa8ce83 | 25 | extern int backup_dir_len; |
de0e2250 WD |
26 | extern unsigned int backup_dir_remainder; |
27 | extern char backup_dir_buf[MAXPATHLEN]; | |
3d19b4c8 | 28 | extern char *backup_suffix; |
66203a98 | 29 | extern char *backup_dir; |
3d19b4c8 | 30 | |
378a074c AT |
31 | extern int am_root; |
32 | extern int preserve_devices; | |
b5c6a6ae | 33 | extern int preserve_specials; |
378a074c | 34 | extern int preserve_links; |
377dbd20 | 35 | extern int safe_symlinks; |
378a074c | 36 | |
c94e4afb WD |
37 | /* make a complete pathname for backup file */ |
38 | char *get_backup_name(char *fname) | |
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 AT |
54 | /* simple backup creates a backup with a suffix in the same directory */ |
55 | static int make_simple_backup(char *fname) | |
3d19b4c8 | 56 | { |
ba679d51 | 57 | int rename_errno; |
c94e4afb | 58 | 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 | { |
47d6a60c | 97 | STRUCT_STAT st; |
de0e2250 WD |
98 | char *rel = fullpath + backup_dir_len; |
99 | char *end = rel + strlen(rel); | |
100 | char *p = end; | |
101 | ||
102 | while (strncmp(fullpath, "./", 2) == 0) | |
103 | fullpath += 2; | |
104 | ||
105 | /* Try to find an existing dir, starting from the deepest dir. */ | |
106 | while (1) { | |
107 | if (--p == fullpath) { | |
108 | p += strlen(p); | |
109 | goto failure; | |
110 | } | |
111 | if (*p == '/') { | |
112 | *p = '\0'; | |
a56cdef9 | 113 | if (mkdir_defmode(fullpath) == 0) |
de0e2250 WD |
114 | break; |
115 | if (errno != ENOENT) { | |
d62bcc17 WD |
116 | rsyserr(FERROR, errno, |
117 | "make_bak_dir mkdir %s failed", | |
118 | full_fname(fullpath)); | |
de0e2250 WD |
119 | goto failure; |
120 | } | |
121 | } | |
47d6a60c | 122 | } |
de0e2250 WD |
123 | |
124 | /* Make all the dirs that we didn't find on the way here. */ | |
125 | while (1) { | |
126 | if (p >= rel) { | |
127 | /* Try to transfer the directory settings of the | |
128 | * actual dir that the files are coming from. */ | |
9fd62d15 | 129 | if (do_stat(rel, &st) < 0) { |
d62bcc17 WD |
130 | rsyserr(FERROR, errno, |
131 | "make_bak_dir stat %s failed", | |
132 | full_fname(rel)); | |
de0e2250 | 133 | } else { |
de0e2250 WD |
134 | do_lchown(fullpath, st.st_uid, st.st_gid); |
135 | do_chmod(fullpath, st.st_mode); | |
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 AT |
158 | /* robustly move a file, creating new directory structures if necessary */ |
159 | static int robust_move(char *src, char *dst) | |
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. */ | |
66203a98 AT |
171 | static int keep_backup(char *fname) |
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 | |
c94e4afb | 186 | if (!(buf = get_backup_name(fname))) |
47d6a60c | 187 | return 0; |
378a074c | 188 | |
378a074c | 189 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
190 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
191 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
93e28fbd WD |
192 | do_unlink(buf); |
193 | if (do_mknod(buf, file->mode, file->u.rdev) < 0 | |
194 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
195 | || do_mknod(buf, file->mode, file->u.rdev) < 0)) { | |
196 | rsyserr(FERROR, errno, "mknod %s failed", | |
197 | full_fname(buf)); | |
198 | } else if (verbose > 2) { | |
199 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 200 | fname); |
47d6a60c S |
201 | } |
202 | kept = 1; | |
203 | do_unlink(fname); | |
204 | } | |
378a074c | 205 | |
addf0c4a | 206 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 207 | /* make an empty directory */ |
c94e4afb WD |
208 | if (do_mkdir(buf, file->mode) < 0 |
209 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
210 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 211 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 212 | full_fname(buf)); |
377dbd20 | 213 | } |
47d6a60c | 214 | |
377dbd20 | 215 | ret_code = do_rmdir(fname); |
addf0c4a | 216 | if (verbose > 2) { |
ea42541f WD |
217 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
218 | full_fname(fname), ret_code); | |
47d6a60c S |
219 | } |
220 | kept = 1; | |
221 | } | |
378a074c | 222 | |
4f5b0756 | 223 | #ifdef SUPPORT_LINKS |
addf0c4a | 224 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
c94e4afb | 225 | if (safe_symlinks && unsafe_symlink(file->u.link, buf)) { |
47d6a60c S |
226 | if (verbose) { |
227 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
c94e4afb | 228 | full_fname(buf), file->u.link); |
47d6a60c S |
229 | } |
230 | kept = 1; | |
93e28fbd WD |
231 | } else { |
232 | do_unlink(buf); | |
233 | if (do_symlink(file->u.link, buf) < 0 | |
234 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
235 | || do_symlink(file->u.link, buf) < 0)) { | |
236 | rsyserr(FERROR, errno, "link %s -> \"%s\"", | |
237 | full_fname(buf), | |
45c49b52 | 238 | file->u.link); |
93e28fbd WD |
239 | } |
240 | do_unlink(fname); | |
241 | kept = 1; | |
47d6a60c | 242 | } |
47d6a60c | 243 | } |
378a074c | 244 | #endif |
378a074c | 245 | |
addf0c4a | 246 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 247 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 248 | fname); |
f8c8ef9e | 249 | return 1; |
47d6a60c | 250 | } |
66203a98 | 251 | |
66203a98 | 252 | /* move to keep tree if a file */ |
addf0c4a | 253 | if (!kept) { |
c94e4afb | 254 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 255 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 256 | full_fname(fname), buf); |
394bcdb5 WD |
257 | } else if (st.st_nlink > 1) { |
258 | /* If someone has hard-linked the file into the backup | |
3cb22c20 | 259 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 260 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
261 | } |
262 | } | |
c3ef136d | 263 | set_file_attrs(buf, file, NULL, 0); |
9935066b | 264 | free(file); |
378a074c | 265 | |
4875d6b6 WD |
266 | if (verbose > 1) { |
267 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 268 | fname, buf); |
4875d6b6 | 269 | } |
66203a98 | 270 | return 1; |
de0e2250 | 271 | } |
66203a98 AT |
272 | |
273 | ||
274 | /* main backup switch routine */ | |
275 | int make_backup(char *fname) | |
276 | { | |
277 | if (backup_dir) | |
addf0c4a WD |
278 | return keep_backup(fname); |
279 | return make_simple_backup(fname); | |
66203a98 | 280 | } |