Commit | Line | Data |
---|---|---|
addf0c4a | 1 | /* |
3d19b4c8 | 2 | Copyright (C) Andrew Tridgell 1999 |
addf0c4a | 3 | |
3d19b4c8 AT |
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. | |
addf0c4a | 8 | |
3d19b4c8 AT |
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. | |
addf0c4a | 13 | |
3d19b4c8 AT |
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; | |
daa8ce83 WD |
24 | extern int backup_suffix_len; |
25 | extern int backup_dir_len; | |
3d19b4c8 | 26 | extern char *backup_suffix; |
66203a98 | 27 | extern char *backup_dir; |
3d19b4c8 | 28 | |
378a074c AT |
29 | extern int am_root; |
30 | extern int preserve_devices; | |
31 | extern int preserve_links; | |
32 | extern int preserve_hard_links; | |
33 | ||
66203a98 AT |
34 | /* simple backup creates a backup with a suffix in the same directory */ |
35 | static int make_simple_backup(char *fname) | |
3d19b4c8 AT |
36 | { |
37 | char fnamebak[MAXPATHLEN]; | |
daa8ce83 | 38 | if (strlen(fname) + backup_suffix_len > MAXPATHLEN-1) { |
47d6a60c | 39 | rprintf(FERROR, "backup filename too long\n"); |
3d19b4c8 AT |
40 | return 0; |
41 | } | |
42 | ||
47d6a60c S |
43 | snprintf(fnamebak, sizeof(fnamebak), "%s%s", fname, backup_suffix); |
44 | if (do_rename(fname, fnamebak) != 0) { | |
3d19b4c8 AT |
45 | /* cygwin (at least version b19) reports EINVAL */ |
46 | if (errno != ENOENT && errno != EINVAL) { | |
a039749b | 47 | rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak); |
3d19b4c8 AT |
48 | return 0; |
49 | } | |
50 | } else if (verbose > 1) { | |
47d6a60c | 51 | rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak); |
3d19b4c8 AT |
52 | } |
53 | return 1; | |
54 | } | |
66203a98 AT |
55 | |
56 | ||
57 | /* recursively make a directory path */ | |
58 | static int make_dir(char *name, int mask) | |
59 | { | |
60 | char newdir [MAXPATHLEN]; | |
61 | char *p, *d; | |
62 | ||
63 | /* copy pathname over, look for last '/' */ | |
64 | for (p = d = newdir; *name; *d++ = *name++) | |
65 | if (*name == '/') | |
66 | p = d; | |
67 | if (p == newdir) | |
68 | return 0; | |
69 | *p = 0; | |
70 | ||
71 | /* make the new directory, if that fails then make its parent */ | |
addf0c4a WD |
72 | while (do_mkdir(newdir, mask) != 0) { |
73 | if (errno != ENOENT || !make_dir(newdir, mask)) | |
66203a98 | 74 | return 0; |
addf0c4a | 75 | } |
66203a98 AT |
76 | |
77 | return 1; | |
78 | } /* make_dir */ | |
79 | ||
80 | ||
378a074c AT |
81 | /**************************************************************************** |
82 | Create a directory given an absolute path, perms based upon another directory | |
83 | path | |
84 | ****************************************************************************/ | |
47d6a60c | 85 | static int make_bak_dir(char *fname, char *bak_path) |
378a074c | 86 | { |
47d6a60c S |
87 | STRUCT_STAT st; |
88 | STRUCT_STAT *st2; | |
89 | char fullpath[MAXPATHLEN]; | |
90 | extern int orig_umask; | |
91 | char *p; | |
92 | char *q; | |
93 | ||
94 | while(strncmp(bak_path, "./", 2) == 0) bak_path += 2; | |
95 | ||
addf0c4a | 96 | if (bak_path[strlen(bak_path)-1] != '/') { |
47d6a60c S |
97 | snprintf(fullpath, sizeof(fullpath), "%s/", bak_path); |
98 | } else { | |
99 | snprintf(fullpath, sizeof(fullpath), "%s", bak_path); | |
100 | } | |
101 | p = fullpath; | |
102 | q = &fullpath[strlen(fullpath)]; /* End of bak_path string */ | |
103 | strcat(fullpath, fname); | |
104 | ||
105 | /* Make the directories */ | |
addf0c4a | 106 | while ((p = strchr(p, '/')) != NULL) { |
47d6a60c | 107 | *p = 0; |
addf0c4a | 108 | if (do_lstat(fullpath, &st) != 0) { |
47d6a60c | 109 | do_mkdir(fullpath, 0777 & ~orig_umask); |
addf0c4a WD |
110 | if (p > q) { |
111 | if (do_lstat(q, &st) != 0) { | |
ea42541f WD |
112 | rprintf(FERROR, "make_bak_dir stat %s failed: %s\n", |
113 | full_fname(fullpath), strerror(errno)); | |
47d6a60c S |
114 | } else { |
115 | st2 = &st; | |
116 | set_modtime(fullpath, st2->st_mtime); | |
addf0c4a | 117 | if (do_lchown(fullpath, st2->st_uid, st2->st_gid) != 0) { |
ea42541f WD |
118 | rprintf(FERROR, "make_bak_dir chown %s failed: %s\n", |
119 | full_fname(fullpath), strerror(errno)); | |
47d6a60c | 120 | } |
addf0c4a | 121 | if (do_chmod(fullpath, st2->st_mode) != 0) { |
ea42541f WD |
122 | rprintf(FERROR, "make_bak_dir failed to set permissions on %s: %s\n", |
123 | full_fname(fullpath), strerror(errno)); | |
47d6a60c S |
124 | } |
125 | } | |
126 | } | |
127 | } | |
128 | *p = '/'; | |
129 | p++; | |
130 | } | |
131 | return 0; | |
378a074c AT |
132 | } |
133 | ||
66203a98 AT |
134 | /* robustly move a file, creating new directory structures if necessary */ |
135 | static int robust_move(char *src, char *dst) | |
136 | { | |
137 | int keep_trying = 4; | |
138 | int keep_path_extfs = 0; | |
139 | int failed; | |
140 | ||
141 | while (keep_trying) { | |
49d6fdc0 AT |
142 | if (keep_path_extfs) { |
143 | failed = copy_file(src, dst, 0755); | |
addf0c4a | 144 | if (!failed) |
49d6fdc0 | 145 | do_unlink(src); |
addf0c4a WD |
146 | } else |
147 | failed = robust_rename(src, dst); | |
66203a98 AT |
148 | |
149 | if (failed) { | |
addf0c4a WD |
150 | if (verbose > 2) { |
151 | rprintf(FERROR, "robust_move failed: %s(%d)\n", | |
152 | strerror(errno), errno); | |
153 | } | |
66203a98 | 154 | switch (errno) { |
addf0c4a WD |
155 | case EXDEV: /* external filesystem */ |
156 | keep_path_extfs = 1; | |
157 | keep_trying--; | |
158 | break; | |
159 | case ENOENT: /* no directory to write to */ | |
160 | make_dir(dst, 0700); | |
161 | keep_trying--; | |
162 | break; | |
163 | default: | |
164 | keep_trying = 0; | |
165 | break; | |
166 | } | |
66203a98 AT |
167 | } else |
168 | keep_trying = 0; | |
169 | } /* while */ | |
addf0c4a | 170 | return !failed; |
66203a98 AT |
171 | } /* robust_move */ |
172 | ||
173 | ||
174 | /* if we have a backup_dir, then we get here from make_backup(). | |
175 | We will move the file to be deleted into a parallel directory tree */ | |
176 | static int keep_backup(char *fname) | |
177 | { | |
378a074c | 178 | |
66203a98 AT |
179 | static int initialised; |
180 | ||
181 | char keep_name [MAXPATHLEN]; | |
182 | STRUCT_STAT st; | |
183 | struct file_struct *file; | |
184 | ||
47d6a60c | 185 | int kept = 0; |
378a074c AT |
186 | int ret_code; |
187 | ||
66203a98 | 188 | if (!initialised) { |
daa8ce83 WD |
189 | if (backup_dir_len && backup_dir[backup_dir_len - 1] == '/') |
190 | backup_dir[--backup_dir_len] = '\0'; | |
66203a98 | 191 | if (verbose > 0) |
addf0c4a | 192 | rprintf(FINFO, "backup_dir is %s\n", backup_dir); |
66203a98 AT |
193 | initialised = 1; |
194 | } | |
195 | ||
196 | /* return if no file to keep */ | |
197 | #if SUPPORT_LINKS | |
addf0c4a | 198 | if (do_lstat(fname, &st)) return 1; |
66203a98 | 199 | #else |
addf0c4a | 200 | if (do_stat(fname, &st)) return 1; |
66203a98 AT |
201 | #endif |
202 | ||
af1d91c5 | 203 | file = make_file(fname, NULL, NO_EXCLUDES); |
f2cbf44b AT |
204 | |
205 | /* the file could have disappeared */ | |
206 | if (!file) return 1; | |
66203a98 | 207 | |
47d6a60c | 208 | /* make a complete pathname for backup file */ |
daa8ce83 | 209 | if (backup_dir_len+strlen(fname)+backup_suffix_len > MAXPATHLEN-1) { |
addf0c4a | 210 | rprintf(FERROR, "keep_backup filename too long\n"); |
47d6a60c S |
211 | return 0; |
212 | } | |
378a074c | 213 | |
daa8ce83 WD |
214 | snprintf(keep_name, sizeof (keep_name), "%s/%s%s", |
215 | backup_dir, fname, backup_suffix); | |
378a074c AT |
216 | |
217 | #ifdef HAVE_MKNOD | |
218 | /* Check to see if this is a device file, or link */ | |
addf0c4a WD |
219 | if (IS_DEVICE(file->mode)) { |
220 | if (am_root && preserve_devices) { | |
47d6a60c | 221 | make_bak_dir(fname, backup_dir); |
728d0922 | 222 | if (do_mknod(keep_name, file->mode, file->u.rdev) != 0) { |
ea42541f WD |
223 | rprintf(FERROR, "mknod %s failed: %s\n", |
224 | full_fname(keep_name), strerror(errno)); | |
addf0c4a WD |
225 | } else if (verbose > 2) { |
226 | rprintf(FINFO, | |
227 | "make_backup: DEVICE %s successful.\n", | |
228 | fname); | |
47d6a60c S |
229 | } |
230 | } | |
231 | kept = 1; | |
232 | do_unlink(fname); | |
233 | } | |
378a074c AT |
234 | #endif |
235 | ||
addf0c4a | 236 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 237 | /* make an empty directory */ |
47d6a60c S |
238 | make_bak_dir(fname, backup_dir); |
239 | do_mkdir(keep_name, file->mode); | |
240 | ret_code = do_rmdir(fname); | |
241 | ||
addf0c4a | 242 | if (verbose > 2) { |
ea42541f WD |
243 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
244 | full_fname(fname), ret_code); | |
47d6a60c S |
245 | } |
246 | kept = 1; | |
247 | } | |
378a074c AT |
248 | |
249 | #if SUPPORT_LINKS | |
addf0c4a | 250 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
47d6a60c | 251 | extern int safe_symlinks; |
728d0922 | 252 | if (safe_symlinks && unsafe_symlink(file->u.link, keep_name)) { |
47d6a60c S |
253 | if (verbose) { |
254 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
728d0922 | 255 | full_fname(keep_name), file->u.link); |
47d6a60c S |
256 | } |
257 | kept = 1; | |
258 | } | |
259 | make_bak_dir(fname, backup_dir); | |
728d0922 | 260 | if (do_symlink(file->u.link, keep_name) != 0) { |
47d6a60c | 261 | rprintf(FERROR, "link %s -> %s : %s\n", |
728d0922 | 262 | full_fname(keep_name), file->u.link, strerror(errno)); |
47d6a60c S |
263 | } |
264 | do_unlink(fname); | |
265 | kept = 1; | |
266 | } | |
378a074c | 267 | #endif |
aa23c220 WD |
268 | if (!kept && preserve_hard_links && file->link_u.links |
269 | && file->F_HEAD != file) { | |
addf0c4a | 270 | if (verbose > 1) |
47d6a60c S |
271 | rprintf(FINFO, "%s is a hard link\n", f_name(file)); |
272 | } | |
378a074c | 273 | |
addf0c4a | 274 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 275 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
addf0c4a | 276 | fname); |
47d6a60c | 277 | } |
66203a98 | 278 | |
66203a98 | 279 | /* move to keep tree if a file */ |
addf0c4a WD |
280 | if (!kept) { |
281 | if (!robust_move(fname, keep_name)) { | |
ea42541f WD |
282 | rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n", |
283 | full_fname(fname), keep_name, strerror(errno)); | |
47d6a60c S |
284 | } |
285 | } | |
addf0c4a WD |
286 | set_perms(keep_name, file, NULL, 0); |
287 | free_file(file); | |
288 | free(file); | |
378a074c | 289 | |
66203a98 | 290 | if (verbose > 1) |
addf0c4a | 291 | rprintf(FINFO, "keep_backup %s -> %s\n", fname, keep_name); |
66203a98 AT |
292 | return 1; |
293 | } /* keep_backup */ | |
294 | ||
295 | ||
296 | /* main backup switch routine */ | |
297 | int make_backup(char *fname) | |
298 | { | |
299 | if (backup_dir) | |
addf0c4a WD |
300 | return keep_backup(fname); |
301 | return make_simple_backup(fname); | |
66203a98 | 302 | } |