2 * Backup handling code.
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003-2019 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 /* Returns -1 on error, 0 on missing dir, and 1 on present dir. */
38 static int validate_backup_dir(void)
42 if (do_lstat(backup_dir_buf
, &st
) < 0) {
45 rsyserr(FERROR
, errno
, "backup lstat %s failed", backup_dir_buf
);
48 if (!S_ISDIR(st
.st_mode
)) {
49 int flags
= get_del_for_flag(st
.st_mode
) | DEL_FOR_BACKUP
| DEL_RECURSE
;
50 if (delete_item(backup_dir_buf
, st
.st_mode
, flags
) == 0)
57 /* Create a backup path from the given fname, putting the result into
58 * backup_dir_buf. Any new directories (compared to the prior backup
59 * path) are ensured to exist as directories, replacing anything else
60 * that may be in the way (e.g. a symlink). */
61 static BOOL
copy_valid_path(const char *fname
)
67 char *b
, *rel
= backup_dir_buf
+ backup_dir_len
, *name
= rel
;
69 for (f
= fname
, b
= rel
; *f
&& *f
== *b
; f
++, b
++) {
74 if (stringjoin(rel
, backup_dir_remainder
, fname
, backup_suffix
, NULL
) >= backup_dir_remainder
) {
75 rprintf(FERROR
, "backup filename too long\n");
80 for ( ; ; name
= b
+ 1) {
81 if ((b
= strchr(name
, '/')) == NULL
)
85 val
= validate_backup_dir();
98 for ( ; b
; name
= b
+ 1, b
= strchr(name
, '/')) {
101 while (do_mkdir(backup_dir_buf
, ACCESSPERMS
) < 0) {
102 if (errno
== EEXIST
) {
103 val
= validate_backup_dir();
109 rsyserr(FERROR
, errno
, "backup mkdir %s failed", backup_dir_buf
);
115 /* Try to transfer the directory settings of the actual dir
116 * that the files are coming from. */
117 if (x_stat(rel
, &sx
.st
, NULL
) < 0)
118 rsyserr(FERROR
, errno
, "backup stat %s failed", full_fname(rel
));
120 struct file_struct
*file
;
121 if (!(file
= make_file(rel
, NULL
, NULL
, 0, NO_FILTERS
)))
124 if (preserve_acls
&& !S_ISLNK(file
->mode
)) {
126 cache_tmp_acl(file
, &sx
);
130 #ifdef SUPPORT_XATTRS
131 if (preserve_xattrs
) {
133 cache_tmp_xattr(file
, &sx
);
137 set_file_attrs(backup_dir_buf
, file
, NULL
, NULL
, 0);
149 #ifdef SUPPORT_XATTRS
150 uncache_tmp_xattrs();
156 /* Make a complete pathname for backup file and verify any new path elements. */
157 char *get_backup_name(const char *fname
)
160 static int initialized
= 0;
163 if (backup_dir_len
> 1)
164 backup_dir_buf
[backup_dir_len
-1] = '\0';
165 ret
= make_path(backup_dir_buf
, 0);
166 if (backup_dir_len
> 1)
167 backup_dir_buf
[backup_dir_len
-1] = '/';
172 /* copy fname into backup_dir_buf while validating the dirs. */
173 if (copy_valid_path(fname
))
174 return backup_dir_buf
;
175 /* copy_valid_path() has printed an error message. */
179 if (stringjoin(backup_dir_buf
, MAXPATHLEN
, fname
, backup_suffix
, NULL
) < MAXPATHLEN
)
180 return backup_dir_buf
;
182 rprintf(FERROR
, "backup filename too long\n");
186 /* Has same return codes as make_backup(). */
187 static inline int link_or_rename(const char *from
, const char *to
,
188 BOOL prefer_rename
, STRUCT_STAT
*stp
)
190 #ifdef SUPPORT_HARD_LINKS
191 if (!prefer_rename
) {
192 #ifndef CAN_HARDLINK_SYMLINK
193 if (S_ISLNK(stp
->st_mode
))
194 return 0; /* Use copy code. */
196 #ifndef CAN_HARDLINK_SPECIAL
197 if (IS_SPECIAL(stp
->st_mode
) || IS_DEVICE(stp
->st_mode
))
198 return 0; /* Use copy code. */
200 if (do_link(from
, to
) == 0) {
201 if (DEBUG_GTE(BACKUP
, 1))
202 rprintf(FINFO
, "make_backup: HLINK %s successful.\n", from
);
205 /* We prefer to rename a regular file rather than copy it. */
206 if (!S_ISREG(stp
->st_mode
) || errno
== EEXIST
|| errno
== EISDIR
)
210 if (do_rename(from
, to
) == 0) {
211 if (stp
->st_nlink
> 1 && !S_ISDIR(stp
->st_mode
)) {
212 /* If someone has hard-linked the file into the backup
213 * dir, rename() might return success but do nothing! */
214 robust_unlink(from
); /* Just in case... */
216 if (DEBUG_GTE(BACKUP
, 1))
217 rprintf(FINFO
, "make_backup: RENAME %s successful.\n", from
);
223 /* Hard-link, rename, or copy an item to the backup name. Returns 0 for
224 * failure, 1 if item was moved, 2 if item was duplicated or hard linked
225 * into backup area, or 3 if item doesn't exist or isn't a regular file. */
226 int make_backup(const char *fname
, BOOL prefer_rename
)
229 struct file_struct
*file
;
230 int save_preserve_xattrs
;
235 /* Return success if no file to keep. */
236 if (x_lstat(fname
, &sx
.st
, NULL
) < 0)
239 if (!(buf
= get_backup_name(fname
)))
242 /* Try a hard-link or a rename first. Using rename is not atomic, but
243 * is more efficient than forcing a copy for larger files when no hard-
244 * linking is possible. */
245 if ((ret
= link_or_rename(fname
, buf
, prefer_rename
, &sx
.st
)) != 0)
247 if (errno
== EEXIST
|| errno
== EISDIR
) {
249 if (do_lstat(buf
, &bakst
) == 0) {
250 int flags
= get_del_for_flag(bakst
.st_mode
) | DEL_FOR_BACKUP
| DEL_RECURSE
;
251 if (delete_item(buf
, bakst
.st_mode
, flags
) != 0)
254 if ((ret
= link_or_rename(fname
, buf
, prefer_rename
, &sx
.st
)) != 0)
258 /* Fall back to making a copy. */
259 if (!(file
= make_file(fname
, NULL
, &sx
.st
, 0, NO_FILTERS
)))
260 return 3; /* the file could have disappeared */
263 if (preserve_acls
&& !S_ISLNK(file
->mode
)) {
265 cache_tmp_acl(file
, &sx
);
269 #ifdef SUPPORT_XATTRS
270 if (preserve_xattrs
) {
271 get_xattr(fname
, &sx
);
272 cache_tmp_xattr(file
, &sx
);
277 /* Check to see if this is a device file, or link */
278 if ((am_root
&& preserve_devices
&& IS_DEVICE(file
->mode
))
279 || (preserve_specials
&& IS_SPECIAL(file
->mode
))) {
280 if (do_mknod(buf
, file
->mode
, sx
.st
.st_rdev
) < 0)
281 rsyserr(FERROR
, errno
, "mknod %s failed", full_fname(buf
));
282 else if (DEBUG_GTE(BACKUP
, 1))
283 rprintf(FINFO
, "make_backup: DEVICE %s successful.\n", fname
);
288 if (!ret
&& preserve_links
&& S_ISLNK(file
->mode
)) {
289 const char *sl
= F_SYMLINK(file
);
290 if (safe_symlinks
&& unsafe_symlink(sl
, fname
)) {
291 if (INFO_GTE(SYMSAFE
, 1)) {
292 rprintf(FINFO
, "not backing up unsafe symlink \"%s\" -> \"%s\"\n",
297 if (do_symlink(sl
, buf
) < 0)
298 rsyserr(FERROR
, errno
, "link %s -> \"%s\"", full_fname(buf
), sl
);
299 else if (DEBUG_GTE(BACKUP
, 1))
300 rprintf(FINFO
, "make_backup: SYMLINK %s successful.\n", fname
);
306 if (!ret
&& !S_ISREG(file
->mode
)) {
307 rprintf(FINFO
, "make_bak: skipping non-regular file %s\n", fname
);
312 #ifdef SUPPORT_XATTRS
313 uncache_tmp_xattrs();
318 /* Copy to backup tree if a file. */
320 if (copy_file(fname
, buf
, -1, file
->mode
) < 0) {
321 rsyserr(FERROR
, errno
, "keep_backup failed: %s -> \"%s\"",
322 full_fname(fname
), buf
);
327 #ifdef SUPPORT_XATTRS
328 uncache_tmp_xattrs();
332 if (DEBUG_GTE(BACKUP
, 1))
333 rprintf(FINFO
, "make_backup: COPY %s successful.\n", fname
);
337 save_preserve_xattrs
= preserve_xattrs
;
339 set_file_attrs(buf
, file
, NULL
, fname
, ATTRS_SET_NANO
);
340 preserve_xattrs
= save_preserve_xattrs
;
346 #ifdef SUPPORT_XATTRS
347 uncache_tmp_xattrs();
351 if (INFO_GTE(BACKUP
, 1))
352 rprintf(FINFO
, "backed up %s to %s\n", fname
, buf
);