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