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 /* Create a backup path from the given fname, putting the result into
38 * backup_dir_buf. Any new directories (compared to the prior backup
39 * path) are ensured to exist as directories, replacing anything else
40 * that may be in the way (e.g. a symlink). */
41 static BOOL copy_valid_path(const char *fname)
47 char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
49 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
54 if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
55 rprintf(FERROR, "backup filename too long\n");
60 for ( ; ; name = b + 1) {
61 if ((b = strchr(name, '/')) == NULL)
65 if (do_lstat(backup_dir_buf, &sx.st) < 0) {
68 rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
72 if (!S_ISDIR(sx.st.st_mode)) {
73 flags = get_del_for_flag(sx.st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
74 if (delete_item(backup_dir_buf, sx.st.st_mode, flags) == 0)
85 for ( ; b; name = b + 1, b = strchr(name, '/')) {
88 if (mkdir_defmode(backup_dir_buf) < 0) {
89 rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
95 /* Try to transfer the directory settings of the actual dir
96 * that the files are coming from. */
97 if (x_stat(rel, &sx.st, NULL) < 0)
98 rsyserr(FERROR, errno, "backup stat %s failed", full_fname(rel));
100 struct file_struct *file;
101 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
104 if (preserve_acls && !S_ISLNK(file->mode)) {
106 cache_tmp_acl(file, &sx);
110 #ifdef SUPPORT_XATTRS
111 if (preserve_xattrs) {
113 cache_tmp_xattr(file, &sx);
117 set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
127 #ifdef SUPPORT_XATTRS
128 uncache_tmp_xattrs();
134 /* Make a complete pathname for backup file and verify any new path elements. */
135 char *get_backup_name(const char *fname)
138 /* copy fname into backup_dir_buf while validating the dirs. */
139 if (copy_valid_path(fname))
140 return backup_dir_buf;
143 if (stringjoin(backup_dir_buf, MAXPATHLEN,
144 fname, backup_suffix, NULL) < MAXPATHLEN)
145 return backup_dir_buf;
148 rprintf(FERROR, "backup filename too long\n");
152 /* Has same return codes as make_backup(). */
153 static inline int link_or_rename(const char *from, const char *to,
154 BOOL prefer_rename, STRUCT_STAT *stp)
156 #ifdef SUPPORT_HARD_LINKS
157 if (!prefer_rename) {
158 #ifndef CAN_HARDLINK_SYMLINK
159 if (S_ISLNK(stp->st_mode))
160 return 0; /* Use copy code. */
162 #ifndef CAN_HARDLINK_SPECIAL
163 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode))
164 return 0; /* Use copy code. */
166 if (do_link(from, to) == 0) {
167 if (DEBUG_GTE(BACKUP, 1))
168 rprintf(FINFO, "make_backup: HLINK %s successful.\n", from);
171 /* We prefer to rename a regular file rather than copy it. */
172 if (!S_ISREG(stp->st_mode) || errno == EEXIST || errno == EISDIR)
176 if (do_rename(from, to) == 0) {
177 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
178 /* If someone has hard-linked the file into the backup
179 * dir, rename() might return success but do nothing! */
180 robust_unlink(from); /* Just in case... */
182 if (DEBUG_GTE(BACKUP, 1))
183 rprintf(FINFO, "make_backup: RENAME %s successful.\n", from);
189 /* Hard-link, rename, or copy an item to the backup name. Returns 2 if item
190 * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
191 int make_backup(const char *fname, BOOL prefer_rename)
194 struct file_struct *file;
195 int save_preserve_xattrs;
196 char *buf = get_backup_name(fname);
203 /* Return success if no file to keep. */
204 if (x_lstat(fname, &sx.st, NULL) < 0)
207 /* Try a hard-link or a rename first. Using rename is not atomic, but
208 * is more efficient than forcing a copy for larger files when no hard-
209 * linking is possible. */
210 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
212 if (errno == EEXIST || errno == EISDIR) {
214 if (do_lstat(buf, &bakst) == 0) {
215 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
216 if (delete_item(buf, bakst.st_mode, flags) != 0)
219 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
223 /* Fall back to making a copy. */
224 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
225 return 1; /* the file could have disappeared */
228 if (preserve_acls && !S_ISLNK(file->mode)) {
230 cache_tmp_acl(file, &sx);
234 #ifdef SUPPORT_XATTRS
235 if (preserve_xattrs) {
236 get_xattr(fname, &sx);
237 cache_tmp_xattr(file, &sx);
242 /* Check to see if this is a device file, or link */
243 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
244 || (preserve_specials && IS_SPECIAL(file->mode))) {
245 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
246 rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf));
247 else if (DEBUG_GTE(BACKUP, 1))
248 rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname);
253 if (!ret && preserve_links && S_ISLNK(file->mode)) {
254 const char *sl = F_SYMLINK(file);
255 if (safe_symlinks && unsafe_symlink(sl, fname)) {
256 if (INFO_GTE(SYMSAFE, 1)) {
257 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
258 full_fname(buf), sl);
262 if (do_symlink(sl, buf) < 0)
263 rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl);
264 else if (DEBUG_GTE(BACKUP, 1))
265 rprintf(FINFO, "make_backup: SYMLINK %s successful.\n", fname);
271 if (!ret && !S_ISREG(file->mode)) {
272 rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname);
277 #ifdef SUPPORT_XATTRS
278 uncache_tmp_xattrs();
283 /* Copy to backup tree if a file. */
285 if (copy_file(fname, buf, -1, file->mode) < 0) {
286 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
287 full_fname(fname), buf);
292 #ifdef SUPPORT_XATTRS
293 uncache_tmp_xattrs();
297 if (DEBUG_GTE(BACKUP, 1))
298 rprintf(FINFO, "make_backup: COPY %s successful.\n", fname);
302 save_preserve_xattrs = preserve_xattrs;
304 set_file_attrs(buf, file, NULL, fname, 0);
305 preserve_xattrs = save_preserve_xattrs;
311 #ifdef SUPPORT_XATTRS
312 uncache_tmp_xattrs();
316 if (INFO_GTE(BACKUP, 1))
317 rprintf(FINFO, "backed up %s to %s\n", fname, buf);