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; |
82ad07c4 WD |
30 | extern int flist_extra_ndx; |
31 | extern int file_struct_len; | |
6b2a3d5d | 32 | extern int backup_dir_len; |
82ad07c4 WD |
33 | extern unsigned int backup_dir_remainder; |
34 | extern char backup_dir_buf[MAXPATHLEN]; | |
35 | extern char *backup_suffix; | |
36 | extern char *backup_dir; | |
378a074c | 37 | |
c94e4afb | 38 | /* make a complete pathname for backup file */ |
45d8bfe0 | 39 | char *get_backup_name(const char *fname) |
c94e4afb | 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 | 55 | /* simple backup creates a backup with a suffix in the same directory */ |
45d8bfe0 | 56 | static int make_simple_backup(const char *fname) |
3d19b4c8 | 57 | { |
ba679d51 | 58 | int rename_errno; |
45d8bfe0 | 59 | const 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 | 135 | do_lchown(fullpath, st.st_uid, st.st_gid); |
1e999f9f | 136 | #ifdef HAVE_CHMOD |
de0e2250 | 137 | do_chmod(fullpath, st.st_mode); |
1e999f9f | 138 | #endif |
47d6a60c S |
139 | } |
140 | } | |
de0e2250 WD |
141 | *p = '/'; |
142 | p += strlen(p); | |
143 | if (p == end) | |
144 | break; | |
a56cdef9 | 145 | if (mkdir_defmode(fullpath) < 0) { |
d62bcc17 WD |
146 | rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed", |
147 | full_fname(fullpath)); | |
de0e2250 WD |
148 | goto failure; |
149 | } | |
47d6a60c S |
150 | } |
151 | return 0; | |
de0e2250 | 152 | |
b2e6caa0 | 153 | failure: |
de0e2250 WD |
154 | while (p != end) { |
155 | *p = '/'; | |
156 | p += strlen(p); | |
157 | } | |
158 | return -1; | |
378a074c AT |
159 | } |
160 | ||
66203a98 | 161 | /* robustly move a file, creating new directory structures if necessary */ |
45d8bfe0 | 162 | static int robust_move(const char *src, char *dst) |
66203a98 | 163 | { |
21955d9c WD |
164 | if (robust_rename(src, dst, NULL, 0755) < 0 |
165 | && (errno != ENOENT || make_bak_dir(dst) < 0 | |
166 | || robust_rename(src, dst, NULL, 0755) < 0)) | |
62c9e6b3 WD |
167 | return -1; |
168 | return 0; | |
de0e2250 | 169 | } |
66203a98 AT |
170 | |
171 | ||
de0e2250 WD |
172 | /* If we have a --backup-dir, then we get here from make_backup(). |
173 | * We will move the file to be deleted into a parallel directory tree. */ | |
45d8bfe0 | 174 | static int keep_backup(const char *fname) |
66203a98 | 175 | { |
66203a98 AT |
176 | STRUCT_STAT st; |
177 | struct file_struct *file; | |
c94e4afb | 178 | char *buf; |
47d6a60c | 179 | int kept = 0; |
378a074c AT |
180 | int ret_code; |
181 | ||
66203a98 | 182 | /* return if no file to keep */ |
018b2832 | 183 | if (do_lstat(fname, &st) < 0) |
9fd62d15 | 184 | return 1; |
66203a98 | 185 | |
3de73827 | 186 | if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) |
c94e4afb | 187 | return 1; /* the file could have disappeared */ |
66203a98 | 188 | |
82ad07c4 WD |
189 | if (!(buf = get_backup_name(fname))) { |
190 | unmake_file(file); | |
47d6a60c | 191 | return 0; |
82ad07c4 | 192 | } |
378a074c | 193 | |
378a074c | 194 | /* Check to see if this is a device file, or link */ |
b5c6a6ae WD |
195 | if ((am_root && preserve_devices && IS_DEVICE(file->mode)) |
196 | || (preserve_specials && IS_SPECIAL(file->mode))) { | |
82ad07c4 | 197 | dev_t rdev = MAKEDEV(F_DMAJOR(file), F_DMINOR(file)); |
93e28fbd | 198 | do_unlink(buf); |
82ad07c4 | 199 | if (do_mknod(buf, file->mode, rdev) < 0 |
93e28fbd | 200 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 201 | || do_mknod(buf, file->mode, rdev) < 0)) { |
93e28fbd WD |
202 | rsyserr(FERROR, errno, "mknod %s failed", |
203 | full_fname(buf)); | |
204 | } else if (verbose > 2) { | |
205 | rprintf(FINFO, "make_backup: DEVICE %s successful.\n", | |
45c49b52 | 206 | fname); |
47d6a60c S |
207 | } |
208 | kept = 1; | |
209 | do_unlink(fname); | |
210 | } | |
378a074c | 211 | |
addf0c4a | 212 | if (!kept && S_ISDIR(file->mode)) { |
378a074c | 213 | /* make an empty directory */ |
c94e4afb WD |
214 | if (do_mkdir(buf, file->mode) < 0 |
215 | && (errno != ENOENT || make_bak_dir(buf) < 0 | |
216 | || do_mkdir(buf, file->mode) < 0)) { | |
d62bcc17 | 217 | rsyserr(FINFO, errno, "mkdir %s failed", |
c94e4afb | 218 | full_fname(buf)); |
377dbd20 | 219 | } |
47d6a60c | 220 | |
377dbd20 | 221 | ret_code = do_rmdir(fname); |
addf0c4a | 222 | if (verbose > 2) { |
ea42541f WD |
223 | rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", |
224 | full_fname(fname), ret_code); | |
47d6a60c S |
225 | } |
226 | kept = 1; | |
227 | } | |
378a074c | 228 | |
4f5b0756 | 229 | #ifdef SUPPORT_LINKS |
addf0c4a | 230 | if (!kept && preserve_links && S_ISLNK(file->mode)) { |
82ad07c4 WD |
231 | const char *sl = F_SYMLINK(file); |
232 | if (safe_symlinks && unsafe_symlink(sl, buf)) { | |
47d6a60c S |
233 | if (verbose) { |
234 | rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", | |
82ad07c4 | 235 | full_fname(buf), sl); |
47d6a60c S |
236 | } |
237 | kept = 1; | |
93e28fbd WD |
238 | } else { |
239 | do_unlink(buf); | |
82ad07c4 | 240 | if (do_symlink(sl, buf) < 0 |
93e28fbd | 241 | && (errno != ENOENT || make_bak_dir(buf) < 0 |
82ad07c4 | 242 | || do_symlink(sl, buf) < 0)) { |
93e28fbd | 243 | rsyserr(FERROR, errno, "link %s -> \"%s\"", |
82ad07c4 | 244 | full_fname(buf), sl); |
93e28fbd WD |
245 | } |
246 | do_unlink(fname); | |
247 | kept = 1; | |
47d6a60c | 248 | } |
47d6a60c | 249 | } |
378a074c | 250 | #endif |
378a074c | 251 | |
addf0c4a | 252 | if (!kept && !S_ISREG(file->mode)) { |
47d6a60c | 253 | rprintf(FINFO, "make_bak: skipping non-regular file %s\n", |
45c49b52 | 254 | fname); |
82ad07c4 | 255 | unmake_file(file); |
f8c8ef9e | 256 | return 1; |
47d6a60c | 257 | } |
66203a98 | 258 | |
66203a98 | 259 | /* move to keep tree if a file */ |
addf0c4a | 260 | if (!kept) { |
c94e4afb | 261 | if (robust_move(fname, buf) != 0) { |
d62bcc17 | 262 | rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", |
45c49b52 | 263 | full_fname(fname), buf); |
394bcdb5 WD |
264 | } else if (st.st_nlink > 1) { |
265 | /* If someone has hard-linked the file into the backup | |
3cb22c20 | 266 | * dir, rename() might return success but do nothing! */ |
394bcdb5 | 267 | robust_unlink(fname); /* Just in case... */ |
47d6a60c S |
268 | } |
269 | } | |
c3ef136d | 270 | set_file_attrs(buf, file, NULL, 0); |
82ad07c4 | 271 | unmake_file(file); |
378a074c | 272 | |
4875d6b6 WD |
273 | if (verbose > 1) { |
274 | rprintf(FINFO, "backed up %s to %s\n", | |
45c49b52 | 275 | fname, buf); |
4875d6b6 | 276 | } |
66203a98 | 277 | return 1; |
de0e2250 | 278 | } |
66203a98 AT |
279 | |
280 | ||
281 | /* main backup switch routine */ | |
45d8bfe0 | 282 | int make_backup(const char *fname) |
66203a98 AT |
283 | { |
284 | if (backup_dir) | |
addf0c4a WD |
285 | return keep_backup(fname); |
286 | return make_simple_backup(fname); | |
66203a98 | 287 | } |