2 * Backup handling code.
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003-2009 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.
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)
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;
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");
54 /****************************************************************************
55 Create a directory given an absolute path, perms based upon another directory
57 ****************************************************************************/
58 int make_bak_dir(const char *fullpath)
60 char fbuf[MAXPATHLEN], *rel, *end, *p;
61 struct file_struct *file;
62 int len = backup_dir_len;
65 while (*fullpath == '.' && fullpath[1] == '/') {
70 if (strlcpy(fbuf, fullpath, sizeof fbuf) >= sizeof fbuf)
74 end = p = rel + strlen(rel);
76 /* Try to find an existing dir, starting from the deepest dir. */
82 if (mkdir_defmode(fbuf) == 0)
84 if (errno != ENOENT) {
85 rsyserr(FERROR, errno,
86 "make_bak_dir mkdir %s failed",
93 /* Make all the dirs that we didn't find on the way here. */
96 /* Try to transfer the directory settings of the
97 * actual dir that the files are coming from. */
99 if (x_stat(rel, &sx.st, NULL) < 0) {
100 rsyserr(FERROR, errno,
101 "make_bak_dir stat %s failed",
104 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
107 if (preserve_acls && !S_ISLNK(file->mode)) {
109 cache_tmp_acl(file, &sx);
113 #ifdef SUPPORT_XATTRS
114 if (preserve_xattrs) {
116 cache_tmp_xattr(file, &sx);
120 set_file_attrs(fbuf, file, NULL, NULL, 0);
125 #ifdef SUPPORT_XATTRS
126 uncache_tmp_xattrs();
134 if (mkdir_defmode(fbuf) < 0) {
135 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
144 /* Has same return codes as make_backup(). */
145 static inline int link_or_rename(const char *from, const char *to,
146 BOOL prefer_rename, STRUCT_STAT *stp)
148 #ifdef SUPPORT_HARD_LINKS
149 if (!prefer_rename) {
150 #ifndef CAN_HARDLINK_SYMLINK
151 if (S_ISLNK(stp->st_mode))
152 return 0; /* Use copy code. */
154 #ifndef CAN_HARDLINK_SPECIAL
155 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode))
156 return 0; /* Use copy code. */
158 if (!S_ISDIR(stp->st_mode)) {
159 if (do_link(from, to) == 0)
165 if (do_rename(from, to) == 0) {
166 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
167 /* If someone has hard-linked the file into the backup
168 * dir, rename() might return success but do nothing! */
169 robust_unlink(to); /* Just in case... */
176 /* Hard-link, rename, or copy an item to the backup name. Returns 2 if item
177 * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
178 int make_backup(const char *fname, BOOL prefer_rename)
181 struct file_struct *file;
182 int save_preserve_xattrs;
183 char *buf = get_backup_name(fname);
190 /* Return success if no file to keep. */
191 if (x_lstat(fname, &sx.st, NULL) < 0)
194 /* Try a hard-link or a rename first. Using rename is not atomic, but
195 * is more efficient than forcing a copy for larger files when no hard-
196 * linking is possible. */
197 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
199 if (errno == EEXIST || errno == EISDIR) {
201 if (do_lstat(buf, &bakst) == 0) {
202 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
203 if (delete_item(buf, bakst.st_mode, flags) != 0)
206 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
208 } else if (backup_dir && errno == ENOENT) {
209 /* If the backup dir is missing, try again after making it. */
210 if (make_bak_dir(buf) != 0)
212 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
216 /* Fall back to making a copy. */
217 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
218 return 1; /* the file could have disappeared */
221 if (preserve_acls && !S_ISLNK(file->mode)) {
223 cache_tmp_acl(file, &sx);
227 #ifdef SUPPORT_XATTRS
228 if (preserve_xattrs) {
229 get_xattr(fname, &sx);
230 cache_tmp_xattr(file, &sx);
235 /* Check to see if this is a device file, or link */
236 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
237 || (preserve_specials && IS_SPECIAL(file->mode))) {
239 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) {
240 save_errno = errno ? errno : EINVAL; /* 0 paranoia */
241 if (errno == ENOENT && make_bak_dir(buf) == 0) {
242 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
243 save_errno = errno ? errno : save_errno;
248 rsyserr(FERROR, save_errno, "mknod %s failed",
253 if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) {
254 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
260 if (!ret && S_ISDIR(file->mode)) {
262 /* make an empty directory */
263 if (do_mkdir(buf, file->mode) < 0) {
264 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
265 if (errno == ENOENT && make_bak_dir(buf) == 0) {
266 if (do_mkdir(buf, file->mode) < 0)
267 save_errno = errno ? errno : save_errno;
272 rsyserr(FINFO, save_errno, "mkdir %s failed",
277 ret_code = do_rmdir(fname);
278 if (DEBUG_GTE(BACKUP, 1)) {
279 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
280 full_fname(fname), ret_code);
286 if (!ret && preserve_links && S_ISLNK(file->mode)) {
287 const char *sl = F_SYMLINK(file);
288 if (safe_symlinks && unsafe_symlink(sl, fname)) {
289 if (INFO_GTE(SYMSAFE, 1)) {
290 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
291 full_fname(buf), sl);
295 if (do_symlink(sl, buf) < 0) {
296 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
297 if (errno == ENOENT && make_bak_dir(buf) == 0) {
298 if (do_symlink(sl, buf) < 0)
299 save_errno = errno ? errno : save_errno;
304 rsyserr(FERROR, save_errno, "link %s -> \"%s\"",
305 full_fname(buf), sl);
313 if (!ret && !S_ISREG(file->mode)) {
314 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
320 #ifdef SUPPORT_XATTRS
321 uncache_tmp_xattrs();
326 /* Copy to backup tree if a file. */
328 if (copy_file(fname, buf, -1, file->mode, 1) < 0) {
329 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
330 full_fname(fname), buf);
335 #ifdef SUPPORT_XATTRS
336 uncache_tmp_xattrs();
343 save_preserve_xattrs = preserve_xattrs;
345 set_file_attrs(buf, file, NULL, fname, 0);
346 preserve_xattrs = save_preserve_xattrs;
352 #ifdef SUPPORT_XATTRS
353 uncache_tmp_xattrs();
357 if (INFO_GTE(BACKUP, 1)) {
358 rprintf(FINFO, "backed up %s to %s\n",