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