Preparing for release of 3.0.1pre2
[rsync.git] / backup.c
blob39a6e5ae5fd97202b741d0c1679898999eec1aad
1 /*
2 * Backup handling code.
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003-2008 Wayne Davison
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 3 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
21 #include "rsync.h"
23 extern int verbose;
24 extern int am_root;
25 extern int preserve_acls;
26 extern int preserve_xattrs;
27 extern int preserve_devices;
28 extern int preserve_specials;
29 extern int preserve_links;
30 extern int safe_symlinks;
31 extern int backup_dir_len;
32 extern unsigned int backup_dir_remainder;
33 extern char backup_dir_buf[MAXPATHLEN];
34 extern char *backup_suffix;
35 extern char *backup_dir;
37 /* make a complete pathname for backup file */
38 char *get_backup_name(const char *fname)
40 if (backup_dir) {
41 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
42 fname, backup_suffix, NULL) < backup_dir_remainder)
43 return backup_dir_buf;
44 } else {
45 if (stringjoin(backup_dir_buf, MAXPATHLEN,
46 fname, backup_suffix, NULL) < MAXPATHLEN)
47 return backup_dir_buf;
50 rprintf(FERROR, "backup filename too long\n");
51 return NULL;
54 /* simple backup creates a backup with a suffix in the same directory */
55 static int make_simple_backup(const char *fname)
57 int rename_errno;
58 const char *fnamebak = get_backup_name(fname);
60 if (!fnamebak)
61 return 0;
63 while (1) {
64 if (do_rename(fname, fnamebak) == 0) {
65 if (verbose > 1) {
66 rprintf(FINFO, "backed up %s to %s\n",
67 fname, fnamebak);
69 break;
71 /* cygwin (at least version b19) reports EINVAL */
72 if (errno == ENOENT || errno == EINVAL)
73 break;
75 rename_errno = errno;
76 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
77 continue;
78 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
79 continue;
81 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
82 fname, fnamebak);
83 errno = rename_errno;
84 return 0;
87 return 1;
91 /****************************************************************************
92 Create a directory given an absolute path, perms based upon another directory
93 path
94 ****************************************************************************/
95 int make_bak_dir(const char *fullpath)
97 char fbuf[MAXPATHLEN], *rel, *end, *p;
98 struct file_struct *file;
99 int len = backup_dir_len;
100 stat_x sx;
102 while (*fullpath == '.' && fullpath[1] == '/') {
103 fullpath += 2;
104 len -= 2;
107 if (strlcpy(fbuf, fullpath, sizeof fbuf) >= sizeof fbuf)
108 return -1;
110 rel = fbuf + len;
111 end = p = rel + strlen(rel);
113 /* Try to find an existing dir, starting from the deepest dir. */
114 while (1) {
115 if (--p == fbuf)
116 return -1;
117 if (*p == '/') {
118 *p = '\0';
119 if (mkdir_defmode(fbuf) == 0)
120 break;
121 if (errno != ENOENT) {
122 rsyserr(FERROR, errno,
123 "make_bak_dir mkdir %s failed",
124 full_fname(fbuf));
125 return -1;
130 /* Make all the dirs that we didn't find on the way here. */
131 while (1) {
132 if (p >= rel) {
133 /* Try to transfer the directory settings of the
134 * actual dir that the files are coming from. */
135 if (x_stat(rel, &sx.st, NULL) < 0) {
136 rsyserr(FERROR, errno,
137 "make_bak_dir stat %s failed",
138 full_fname(rel));
139 } else {
140 #ifdef SUPPORT_ACLS
141 sx.acc_acl = sx.def_acl = NULL;
142 #endif
143 #ifdef SUPPORT_XATTRS
144 sx.xattr = NULL;
145 #endif
146 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
147 continue;
148 #ifdef SUPPORT_ACLS
149 if (preserve_acls && !S_ISLNK(file->mode)) {
150 get_acl(rel, &sx);
151 cache_acl(file, &sx);
152 free_acl(&sx);
154 #endif
155 #ifdef SUPPORT_XATTRS
156 if (preserve_xattrs) {
157 get_xattr(rel, &sx);
158 cache_xattr(file, &sx);
159 free_xattr(&sx);
161 #endif
162 set_file_attrs(fbuf, file, NULL, NULL, 0);
163 unmake_file(file);
166 *p = '/';
167 p += strlen(p);
168 if (p == end)
169 break;
170 if (mkdir_defmode(fbuf) < 0) {
171 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
172 full_fname(fbuf));
173 return -1;
177 return 0;
180 /* robustly move a file, creating new directory structures if necessary */
181 static int robust_move(const char *src, char *dst)
183 if (robust_rename(src, dst, NULL, 0755) < 0
184 && (errno != ENOENT || make_bak_dir(dst) < 0
185 || robust_rename(src, dst, NULL, 0755) < 0))
186 return -1;
187 return 0;
191 /* If we have a --backup-dir, then we get here from make_backup().
192 * We will move the file to be deleted into a parallel directory tree. */
193 static int keep_backup(const char *fname)
195 stat_x sx;
196 struct file_struct *file;
197 char *buf;
198 int kept = 0;
199 int ret_code;
201 /* return if no file to keep */
202 if (x_lstat(fname, &sx.st, NULL) < 0)
203 return 1;
204 #ifdef SUPPORT_ACLS
205 sx.acc_acl = sx.def_acl = NULL;
206 #endif
207 #ifdef SUPPORT_XATTRS
208 sx.xattr = NULL;
209 #endif
211 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
212 return 1; /* the file could have disappeared */
214 if (!(buf = get_backup_name(fname))) {
215 unmake_file(file);
216 return 0;
219 #ifdef SUPPORT_ACLS
220 if (preserve_acls && !S_ISLNK(file->mode)) {
221 get_acl(fname, &sx);
222 cache_acl(file, &sx);
223 free_acl(&sx);
225 #endif
226 #ifdef SUPPORT_XATTRS
227 if (preserve_xattrs) {
228 get_xattr(fname, &sx);
229 cache_xattr(file, &sx);
230 free_xattr(&sx);
232 #endif
234 /* Check to see if this is a device file, or link */
235 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
236 || (preserve_specials && IS_SPECIAL(file->mode))) {
237 uint32 *devp = F_RDEV_P(file);
238 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
239 do_unlink(buf);
240 if (do_mknod(buf, file->mode, rdev) < 0
241 && (errno != ENOENT || make_bak_dir(buf) < 0
242 || do_mknod(buf, file->mode, rdev) < 0)) {
243 rsyserr(FERROR, errno, "mknod %s failed",
244 full_fname(buf));
245 } else if (verbose > 2) {
246 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
247 fname);
249 kept = 1;
250 do_unlink(fname);
253 if (!kept && S_ISDIR(file->mode)) {
254 /* make an empty directory */
255 if (do_mkdir(buf, file->mode) < 0
256 && (errno != ENOENT || make_bak_dir(buf) < 0
257 || do_mkdir(buf, file->mode) < 0)) {
258 rsyserr(FINFO, errno, "mkdir %s failed",
259 full_fname(buf));
262 ret_code = do_rmdir(fname);
263 if (verbose > 2) {
264 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
265 full_fname(fname), ret_code);
267 kept = 1;
270 #ifdef SUPPORT_LINKS
271 if (!kept && preserve_links && S_ISLNK(file->mode)) {
272 const char *sl = F_SYMLINK(file);
273 if (safe_symlinks && unsafe_symlink(sl, buf)) {
274 if (verbose) {
275 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
276 full_fname(buf), sl);
278 kept = 1;
279 } else {
280 do_unlink(buf);
281 if (do_symlink(sl, buf) < 0
282 && (errno != ENOENT || make_bak_dir(buf) < 0
283 || do_symlink(sl, buf) < 0)) {
284 rsyserr(FERROR, errno, "link %s -> \"%s\"",
285 full_fname(buf), sl);
287 do_unlink(fname);
288 kept = 1;
291 #endif
293 if (!kept && !S_ISREG(file->mode)) {
294 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
295 fname);
296 unmake_file(file);
297 return 1;
300 /* move to keep tree if a file */
301 if (!kept) {
302 if (robust_move(fname, buf) != 0) {
303 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
304 full_fname(fname), buf);
305 } else if (sx.st.st_nlink > 1) {
306 /* If someone has hard-linked the file into the backup
307 * dir, rename() might return success but do nothing! */
308 robust_unlink(fname); /* Just in case... */
311 set_file_attrs(buf, file, NULL, fname, 0);
312 unmake_file(file);
314 if (verbose > 1) {
315 rprintf(FINFO, "backed up %s to %s\n",
316 fname, buf);
318 return 1;
322 /* main backup switch routine */
323 int make_backup(const char *fname)
325 if (backup_dir)
326 return keep_backup(fname);
327 return make_simple_backup(fname);