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; | |
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 AT |
34 | extern int preserve_links; |
35 | extern int preserve_hard_links; | |
5d2a7071 | 36 | extern int orig_umask; |
377dbd20 | 37 | extern int safe_symlinks; |
378a074c | 38 | |
c94e4afb WD |
39 | /* make a complete pathname for backup file */ |
40 | char *get_backup_name(char *fname) | |
41 | { | |
c94e4afb WD |
42 | if (backup_dir) { |
43 | if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder, | |
44 | fname, backup_suffix, NULL) < backup_dir_remainder) | |
45 | return backup_dir_buf; | |
46 | } else { | |
fb22c277 WD |
47 | if (stringjoin(backup_dir_buf, MAXPATHLEN, |
48 | fname, backup_suffix, NULL) < MAXPATHLEN) | |
49 | return backup_dir_buf; | |
c94e4afb WD |
50 | } |
51 | ||
52 | rprintf(FERROR, "backup filename too long\n"); | |
53 | return NULL; | |
54 | } | |
55 | ||
66203a98 AT |
56 | /* simple backup creates a backup with a suffix in the same directory */ |
57 | static int make_simple_backup(char *fname) | |
3d19b4c8 | 58 | { |
ba679d51 | 59 | int rename_errno; |
c94e4afb | 60 | char *fnamebak = get_backup_name(fname); |
5d2a7071 | 61 | |
c94e4afb | 62 | if (!fnamebak) |
3d19b4c8 | 63 | return 0; |
3d19b4c8 | 64 | |
ba679d51 WD |
65 | while (1) { |
66 | if (do_rename(fname, fnamebak) == 0) { | |
67 | if (verbose > 1) { | |
68 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 69 | fname, fnamebak); |
ba679d51 WD |
70 | } |
71 | break; | |
3d19b4c8 | 72 | } |
ba679d51 WD |
73 | /* cygwin (at least version b19) reports EINVAL */ |
74 | if (errno == ENOENT || errno == EINVAL) | |
75 | break; | |
76 | ||
77 | rename_errno = errno; | |
78 | if (errno == EISDIR && do_rmdir(fnamebak) == 0) | |
79 | continue; | |
80 | if (errno == ENOTDIR && do_unlink(fnamebak) == 0) | |
81 | continue; | |
82 | ||
83 | rsyserr(FERROR, rename_errno, "rename %s to backup %s", | |
45c49b52 | 84 | fname, fnamebak); |
ba679d51 WD |
85 | errno = rename_errno; |
86 | return 0; | |
3d19b4c8 | 87 | } |
ba679d51 | 88 | |
3d19b4c8 AT |
89 | return 1; |
90 | } | |
66203a98 AT |
91 | |
92 | ||
378a074c AT |
93 | /**************************************************************************** |
94 | Create a directory given an absolute path, perms based upon another directory | |
95 | path | |
96 | ****************************************************************************/ | |
de0e2250 | 97 | static int make_bak_dir(char *fullpath) |
378a074c | 98 | { |
47d6a60c | 99 | STRUCT_STAT st; |
de0e2250 WD |
100 | char *rel = fullpath + backup_dir_len; |
101 | char *end = rel + strlen(rel); | |
102 | char *p = end; | |
103 | ||
104 | while (strncmp(fullpath, "./", 2) == 0) | |
105 | fullpath += 2; | |
106 | ||
107 | /* Try to find an existing dir, starting from the deepest dir. */ | |
108 | while (1) { | |
109 | if (--p == fullpath) { | |
110 | p += strlen(p); | |
111 | goto failure; | |
112 | } | |
113 | if (*p == '/') { | |
114 | *p = '\0'; | |
115 | if (do_mkdir(fullpath, 0777 & ~orig_umask) == 0) | |
116 | break; | |
117 | if (errno != ENOENT) { | |
d62bcc17 WD |
118 | rsyserr(FERROR, errno, |
119 | "make_bak_dir mkdir %s failed", | |
120 | full_fname(fullpath)); | |
de0e2250 WD |
121 | goto failure; |
122 | } | |
123 | } | |
47d6a60c | 124 | } |
de0e2250 WD |
125 | |
126 | /* Make all the dirs that we didn't find on the way here. */ | |
127 | while (1) { | |
128 | if (p >= rel) { | |
129 | /* Try to transfer the directory settings of the | |
130 | * actual dir that the files are coming from. */ | |
9fd62d15 | 131 | if (do_stat(rel, &st) < 0) { |
d62bcc17 WD |
132 | rsyserr(FERROR, errno, |
133 | "make_bak_dir stat %s failed", | |
134 | full_fname(rel)); | |
de0e2250 | 135 | } else { |
de0e2250 WD |
136 | do_lchown(fullpath, st.st_uid, st.st_gid); |
137 | do_chmod(fullpath, st.st_mode); | |
47d6a60c S |
138 | } |
139 | } | |
de0e2250 WD |
140 | *p = '/'; |
141 | p += strlen(p); | |
142 | if (p == end) | |
143 | break; | |
144 | if (do_mkdir(fullpath, 0777 & ~orig_umask) < 0) { | |
d62bcc17 WD |
145 | rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed", |
146 | full_fname(fullpath)); | |
de0e2250 WD |
147 | goto failure; |
148 | } | |
47d6a60c S |
149 | } |
150 | return 0; | |
de0e2250 | 151 | |
b2e6caa0 | 152 | failure: |
de0e2250 WD |
153 | while (p != end) { |
154 | *p = '/'; | |
155 | p += strlen(p); | |
156 | } | |
157 | return -1; | |
378a074c AT |
158 | } |
159 | ||
66203a98 AT |
160 | /* robustly move a file, creating new directory structures if necessary */ |
161 | static int robust_move(char *src, char *dst) | |
162 | { | |
21955d9c WD |
163 | if (robust_rename(src, dst, NULL, 0755) < 0 |
164 | && (errno != ENOENT || make_bak_dir(dst) < 0 | |
165 | || robust_rename(src, dst, NULL, 0755) < 0)) | |
62c9e6b3 WD |
166 | return -1; |
167 | return 0; | |
de0e2250 | 168 | } |
66203a98 AT |
169 | |
170 | ||
de0e2250 WD |
171 | /* If we have a --backup-dir, then we get here from make_backup(). |
172 | * We will move the file to be deleted into a parallel directory tree. */ | |
66203a98 AT |
173 | static int keep_backup(char *fname) |
174 | { | |
66203a98 AT |
175 | STRUCT_STAT st; |
176 | struct file_struct *file; | |
c94e4afb | 177 | char *buf; |
47d6a60c | 178 | int kept = 0; |
378a074c AT |
179 | int ret_code; |
180 | ||
66203a98 | 181 | /* return if no file to keep */ |
018b2832 | 182 | if (do_lstat(fname, &st) < 0) |
9fd62d15 | 183 | return 1; |
66203a98 | 184 | |
3de73827 | 185 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
c94e4afb | 186 | return 1; /* the file could have disappeared */ |
66203a98 | 187 | |
c94e4afb | 188 | if (!(buf = get_backup_name(fname))) |
47d6a60c | 189 | return 0; |
378a074c | 190 | |
378a074c | 191 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
192 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
193 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
93e28fbd WD |
194 | do_unlink(buf); |
195 | if (do_mknod(buf, file->mode, file->u.rdev) < 0 | |
196 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
197 | || do_mknod(buf, file->mode, file->u.rdev) < 0)) { | |
198 | rsyserr(FERROR, errno, "mknod %s failed", | |
199 | full_fname(buf)); | |
200 | } else if (verbose > 2) { | |
201 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 202 | fname); |
47d6a60c S |
203 | } |
204 | kept = 1; | |
205 | do_unlink(fname); | |
206 | } | |
378a074c | 207 | |
addf0c4a | 208 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 209 | /* make an empty directory */ |
c94e4afb WD |
210 | if (do_mkdir(buf, file->mode) < 0 |
211 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
212 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 213 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 214 | full_fname(buf)); |
377dbd20 | 215 | } |
47d6a60c | 216 | |
377dbd20 | 217 | ret_code = do_rmdir(fname); |
addf0c4a | 218 | if (verbose > 2) { |
ea42541f WD |
219 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
220 | full_fname(fname), ret_code); | |
47d6a60c S |
221 | } |
222 | kept = 1; | |
223 | } | |
378a074c | 224 | |
4f5b0756 | 225 | #ifdef SUPPORT_LINKS |
addf0c4a | 226 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
c94e4afb | 227 | if (safe_symlinks && unsafe_symlink(file->u.link, buf)) { |
47d6a60c S |
228 | if (verbose) { |
229 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
c94e4afb | 230 | full_fname(buf), file->u.link); |
47d6a60c S |
231 | } |
232 | kept = 1; | |
93e28fbd WD |
233 | } else { |
234 | do_unlink(buf); | |
235 | if (do_symlink(file->u.link, buf) < 0 | |
236 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
237 | || do_symlink(file->u.link, buf) < 0)) { | |
238 | rsyserr(FERROR, errno, "link %s -> \"%s\"", | |
239 | full_fname(buf), | |
45c49b52 | 240 | file->u.link); |
93e28fbd WD |
241 | } |
242 | do_unlink(fname); | |
243 | kept = 1; | |
47d6a60c | 244 | } |
47d6a60c | 245 | } |
378a074c | 246 | #endif |
378a074c | 247 | |
addf0c4a | 248 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 249 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 250 | fname); |
f8c8ef9e | 251 | return 1; |
47d6a60c | 252 | } |
66203a98 | 253 | |
66203a98 | 254 | /* move to keep tree if a file */ |
addf0c4a | 255 | if (!kept) { |
c94e4afb | 256 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 257 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 258 | full_fname(fname), buf); |
394bcdb5 WD |
259 | } else if (st.st_nlink > 1) { |
260 | /* If someone has hard-linked the file into the backup | |
3cb22c20 | 261 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 262 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
263 | } |
264 | } | |
c3ef136d | 265 | set_file_attrs(buf, file, NULL, 0); |
9935066b | 266 | free(file); |
378a074c | 267 | |
4875d6b6 WD |
268 | if (verbose > 1) { |
269 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 270 | fname, buf); |
4875d6b6 | 271 | } |
66203a98 | 272 | return 1; |
de0e2250 | 273 | } |
66203a98 AT |
274 | |
275 | ||
276 | /* main backup switch routine */ | |
277 | int make_backup(char *fname) | |
278 | { | |
279 | if (backup_dir) | |
addf0c4a WD |
280 | return keep_backup(fname); |
281 | return make_simple_backup(fname); | |
66203a98 | 282 | } |