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; | |
378a074c AT |
25 | extern int am_root; |
26 | extern int preserve_devices; | |
b5c6a6ae | 27 | extern int preserve_specials; |
378a074c | 28 | extern int preserve_links; |
377dbd20 | 29 | extern int safe_symlinks; |
6b2a3d5d | 30 | extern int backup_dir_len; |
82ad07c4 WD |
31 | extern unsigned int backup_dir_remainder; |
32 | extern char backup_dir_buf[MAXPATHLEN]; | |
33 | extern char *backup_suffix; | |
34 | extern char *backup_dir; | |
378a074c | 35 | |
c94e4afb | 36 | /* make a complete pathname for backup file */ |
45d8bfe0 | 37 | char *get_backup_name(const char *fname) |
c94e4afb | 38 | { |
c94e4afb WD |
39 | if (backup_dir) { |
40 | if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder, | |
41 | fname, backup_suffix, NULL) < backup_dir_remainder) | |
42 | return backup_dir_buf; | |
43 | } else { | |
fb22c277 WD |
44 | if (stringjoin(backup_dir_buf, MAXPATHLEN, |
45 | fname, backup_suffix, NULL) < MAXPATHLEN) | |
46 | return backup_dir_buf; | |
c94e4afb WD |
47 | } |
48 | ||
49 | rprintf(FERROR, "backup filename too long\n"); | |
50 | return NULL; | |
51 | } | |
52 | ||
66203a98 | 53 | /* simple backup creates a backup with a suffix in the same directory */ |
45d8bfe0 | 54 | static int make_simple_backup(const char *fname) |
3d19b4c8 | 55 | { |
ba679d51 | 56 | int rename_errno; |
45d8bfe0 | 57 | const char *fnamebak = get_backup_name(fname); |
5d2a7071 | 58 | |
c94e4afb | 59 | if (!fnamebak) |
3d19b4c8 | 60 | return 0; |
3d19b4c8 | 61 | |
ba679d51 WD |
62 | while (1) { |
63 | if (do_rename(fname, fnamebak) == 0) { | |
64 | if (verbose > 1) { | |
65 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 66 | fname, fnamebak); |
ba679d51 WD |
67 | } |
68 | break; | |
3d19b4c8 | 69 | } |
ba679d51 WD |
70 | /* cygwin (at least version b19) reports EINVAL */ |
71 | if (errno == ENOENT || errno == EINVAL) | |
72 | break; | |
73 | ||
74 | rename_errno = errno; | |
75 | if (errno == EISDIR && do_rmdir(fnamebak) == 0) | |
76 | continue; | |
77 | if (errno == ENOTDIR && do_unlink(fnamebak) == 0) | |
78 | continue; | |
79 | ||
80 | rsyserr(FERROR, rename_errno, "rename %s to backup %s", | |
45c49b52 | 81 | fname, fnamebak); |
ba679d51 WD |
82 | errno = rename_errno; |
83 | return 0; | |
3d19b4c8 | 84 | } |
ba679d51 | 85 | |
3d19b4c8 AT |
86 | return 1; |
87 | } | |
66203a98 AT |
88 | |
89 | ||
378a074c AT |
90 | /**************************************************************************** |
91 | Create a directory given an absolute path, perms based upon another directory | |
92 | path | |
93 | ****************************************************************************/ | |
de0e2250 | 94 | static int make_bak_dir(char *fullpath) |
378a074c | 95 | { |
47d6a60c | 96 | STRUCT_STAT st; |
de0e2250 WD |
97 | char *rel = fullpath + backup_dir_len; |
98 | char *end = rel + strlen(rel); | |
99 | char *p = end; | |
100 | ||
101 | while (strncmp(fullpath, "./", 2) == 0) | |
102 | fullpath += 2; | |
103 | ||
104 | /* Try to find an existing dir, starting from the deepest dir. */ | |
105 | while (1) { | |
106 | if (--p == fullpath) { | |
107 | p += strlen(p); | |
108 | goto failure; | |
109 | } | |
110 | if (*p == '/') { | |
111 | *p = '\0'; | |
a56cdef9 | 112 | if (mkdir_defmode(fullpath) == 0) |
de0e2250 WD |
113 | break; |
114 | if (errno != ENOENT) { | |
d62bcc17 WD |
115 | rsyserr(FERROR, errno, |
116 | "make_bak_dir mkdir %s failed", | |
117 | full_fname(fullpath)); | |
de0e2250 WD |
118 | goto failure; |
119 | } | |
120 | } | |
47d6a60c | 121 | } |
de0e2250 WD |
122 | |
123 | /* Make all the dirs that we didn't find on the way here. */ | |
124 | while (1) { | |
125 | if (p >= rel) { | |
126 | /* Try to transfer the directory settings of the | |
127 | * actual dir that the files are coming from. */ | |
9fd62d15 | 128 | if (do_stat(rel, &st) < 0) { |
d62bcc17 WD |
129 | rsyserr(FERROR, errno, |
130 | "make_bak_dir stat %s failed", | |
131 | full_fname(rel)); | |
de0e2250 | 132 | } else { |
de0e2250 | 133 | do_lchown(fullpath, st.st_uid, st.st_gid); |
1e999f9f | 134 | #ifdef HAVE_CHMOD |
de0e2250 | 135 | do_chmod(fullpath, st.st_mode); |
1e999f9f | 136 | #endif |
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 | 159 | /* robustly move a file, creating new directory structures if necessary */ |
45d8bfe0 | 160 | static int robust_move(const char *src, char *dst) |
66203a98 | 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. */ | |
45d8bfe0 | 172 | static int keep_backup(const char *fname) |
66203a98 | 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 | |
82ad07c4 WD |
187 | if (!(buf = get_backup_name(fname))) { |
188 | unmake_file(file); | |
47d6a60c | 189 | return 0; |
82ad07c4 | 190 | } |
378a074c | 191 | |
378a074c | 192 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
193 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
194 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
e238c997 WD |
195 | uint32 *devp = F_RDEV_P(file); |
196 | dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); | |
93e28fbd | 197 | do_unlink(buf); |
82ad07c4 | 198 | if (do_mknod(buf, file->mode, rdev) < 0 |
93e28fbd | 199 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 200 | || do_mknod(buf, file->mode, rdev) < 0)) { |
93e28fbd WD |
201 | rsyserr(FERROR, errno, "mknod %s failed", |
202 | full_fname(buf)); | |
203 | } else if (verbose > 2) { | |
204 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 205 | fname); |
47d6a60c S |
206 | } |
207 | kept = 1; | |
208 | do_unlink(fname); | |
209 | } | |
378a074c | 210 | |
addf0c4a | 211 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 212 | /* make an empty directory */ |
c94e4afb WD |
213 | if (do_mkdir(buf, file->mode) < 0 |
214 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
215 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 216 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 217 | full_fname(buf)); |
377dbd20 | 218 | } |
47d6a60c | 219 | |
377dbd20 | 220 | ret_code = do_rmdir(fname); |
addf0c4a | 221 | if (verbose > 2) { |
ea42541f WD |
222 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
223 | full_fname(fname), ret_code); | |
47d6a60c S |
224 | } |
225 | kept = 1; | |
226 | } | |
378a074c | 227 | |
4f5b0756 | 228 | #ifdef SUPPORT_LINKS |
addf0c4a | 229 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
82ad07c4 WD |
230 | const char *sl = F_SYMLINK(file); |
231 | if (safe_symlinks && unsafe_symlink(sl, buf)) { | |
47d6a60c S |
232 | if (verbose) { |
233 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
82ad07c4 | 234 | full_fname(buf), sl); |
47d6a60c S |
235 | } |
236 | kept = 1; | |
93e28fbd WD |
237 | } else { |
238 | do_unlink(buf); | |
82ad07c4 | 239 | if (do_symlink(sl, buf) < 0 |
93e28fbd | 240 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 241 | || do_symlink(sl, buf) < 0)) { |
93e28fbd | 242 | rsyserr(FERROR, errno, "link %s -> \"%s\"", |
82ad07c4 | 243 | full_fname(buf), sl); |
93e28fbd WD |
244 | } |
245 | do_unlink(fname); | |
246 | kept = 1; | |
47d6a60c | 247 | } |
47d6a60c | 248 | } |
378a074c | 249 | #endif |
378a074c | 250 | |
addf0c4a | 251 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 252 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 253 | fname); |
82ad07c4 | 254 | unmake_file(file); |
f8c8ef9e | 255 | return 1; |
47d6a60c | 256 | } |
66203a98 | 257 | |
66203a98 | 258 | /* move to keep tree if a file */ |
addf0c4a | 259 | if (!kept) { |
c94e4afb | 260 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 261 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 262 | full_fname(fname), buf); |
394bcdb5 WD |
263 | } else if (st.st_nlink > 1) { |
264 | /* If someone has hard-linked the file into the backup | |
3cb22c20 | 265 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 266 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
267 | } |
268 | } | |
c3ef136d | 269 | set_file_attrs(buf, file, NULL, 0); |
82ad07c4 | 270 | unmake_file(file); |
378a074c | 271 | |
4875d6b6 WD |
272 | if (verbose > 1) { |
273 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 274 | fname, buf); |
4875d6b6 | 275 | } |
66203a98 | 276 | return 1; |
de0e2250 | 277 | } |
66203a98 AT |
278 | |
279 | ||
280 | /* main backup switch routine */ | |
45d8bfe0 | 281 | int make_backup(const char *fname) |
66203a98 AT |
282 | { |
283 | if (backup_dir) | |
addf0c4a WD |
284 | return keep_backup(fname); |
285 | return make_simple_backup(fname); | |
66203a98 | 286 | } |