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