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.
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 /* simple backup creates a backup with a suffix in the same directory */
55 static int make_simple_backup(const char *fname
)
58 const char *fnamebak
= get_backup_name(fname
);
64 if (do_rename(fname
, fnamebak
) == 0) {
66 rprintf(FINFO
, "backed up %s to %s\n",
71 /* cygwin (at least version b19) reports EINVAL */
72 if (errno
== ENOENT
|| errno
== EINVAL
)
76 if (errno
== EISDIR
&& do_rmdir(fnamebak
) == 0)
78 if (errno
== ENOTDIR
&& do_unlink(fnamebak
) == 0)
81 rsyserr(FERROR
, rename_errno
, "rename %s to backup %s",
91 /****************************************************************************
92 Create a directory given an absolute path, perms based upon another directory
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
;
102 while (*fullpath
== '.' && fullpath
[1] == '/') {
107 if (strlcpy(fbuf
, fullpath
, sizeof fbuf
) >= sizeof fbuf
)
111 end
= p
= rel
+ strlen(rel
);
113 /* Try to find an existing dir, starting from the deepest dir. */
119 if (mkdir_defmode(fbuf
) == 0)
121 if (errno
!= ENOENT
) {
122 rsyserr(FERROR
, errno
,
123 "make_bak_dir mkdir %s failed",
130 /* Make all the dirs that we didn't find on the way here. */
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",
141 sx
.acc_acl
= sx
.def_acl
= NULL
;
143 #ifdef SUPPORT_XATTRS
146 if (!(file
= make_file(rel
, NULL
, NULL
, 0, NO_FILTERS
)))
149 if (preserve_acls
&& !S_ISLNK(file
->mode
)) {
151 cache_acl(file
, &sx
);
155 #ifdef SUPPORT_XATTRS
156 if (preserve_xattrs
) {
158 cache_xattr(file
, &sx
);
162 set_file_attrs(fbuf
, file
, NULL
, NULL
, 0);
170 if (mkdir_defmode(fbuf
) < 0) {
171 rsyserr(FERROR
, errno
, "make_bak_dir mkdir %s failed",
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 int save_errno
= errno
? errno
: EINVAL
; /* 0 paranoia */
185 if (errno
== ENOENT
&& make_bak_dir(dst
) == 0) {
186 if (robust_rename(src
, dst
, NULL
, 0755) < 0)
187 save_errno
= errno
? errno
: save_errno
;
200 /* If we have a --backup-dir, then we get here from make_backup().
201 * We will move the file to be deleted into a parallel directory tree. */
202 static int keep_backup(const char *fname
)
205 struct file_struct
*file
;
207 int save_preserve_xattrs
= preserve_xattrs
;
211 /* return if no file to keep */
212 if (x_lstat(fname
, &sx
.st
, NULL
) < 0)
215 sx
.acc_acl
= sx
.def_acl
= NULL
;
217 #ifdef SUPPORT_XATTRS
221 if (!(file
= make_file(fname
, NULL
, NULL
, 0, NO_FILTERS
)))
222 return 1; /* the file could have disappeared */
224 if (!(buf
= get_backup_name(fname
))) {
230 if (preserve_acls
&& !S_ISLNK(file
->mode
)) {
232 cache_acl(file
, &sx
);
236 #ifdef SUPPORT_XATTRS
237 if (preserve_xattrs
) {
238 get_xattr(fname
, &sx
);
239 cache_xattr(file
, &sx
);
244 /* Check to see if this is a device file, or link */
245 if ((am_root
&& preserve_devices
&& IS_DEVICE(file
->mode
))
246 || (preserve_specials
&& IS_SPECIAL(file
->mode
))) {
249 if (do_mknod(buf
, file
->mode
, sx
.st
.st_rdev
) < 0) {
250 save_errno
= errno
? errno
: EINVAL
; /* 0 paranoia */
251 if (errno
== ENOENT
&& make_bak_dir(buf
) == 0) {
252 if (do_mknod(buf
, file
->mode
, sx
.st
.st_rdev
) < 0)
253 save_errno
= errno
? errno
: save_errno
;
258 rsyserr(FERROR
, save_errno
, "mknod %s failed",
263 if (verbose
> 2 && save_errno
== 0) {
264 rprintf(FINFO
, "make_backup: DEVICE %s successful.\n",
271 if (!kept
&& S_ISDIR(file
->mode
)) {
272 /* make an empty directory */
273 if (do_mkdir(buf
, file
->mode
) < 0) {
274 int save_errno
= errno
? errno
: EINVAL
; /* 0 paranoia */
275 if (errno
== ENOENT
&& make_bak_dir(buf
) == 0) {
276 if (do_mkdir(buf
, file
->mode
) < 0)
277 save_errno
= errno
? errno
: save_errno
;
282 rsyserr(FINFO
, save_errno
, "mkdir %s failed",
287 ret_code
= do_rmdir(fname
);
289 rprintf(FINFO
, "make_backup: RMDIR %s returns %i\n",
290 full_fname(fname
), ret_code
);
296 if (!kept
&& preserve_links
&& S_ISLNK(file
->mode
)) {
297 const char *sl
= F_SYMLINK(file
);
298 if (safe_symlinks
&& unsafe_symlink(sl
, buf
)) {
300 rprintf(FINFO
, "ignoring unsafe symlink %s -> %s\n",
301 full_fname(buf
), sl
);
306 if (do_symlink(sl
, buf
) < 0) {
307 int save_errno
= errno
? errno
: EINVAL
; /* 0 paranoia */
308 if (errno
== ENOENT
&& make_bak_dir(buf
) == 0) {
309 if (do_symlink(sl
, buf
) < 0)
310 save_errno
= errno
? errno
: save_errno
;
315 rsyserr(FERROR
, save_errno
, "link %s -> \"%s\"",
316 full_fname(buf
), sl
);
325 if (!kept
&& !S_ISREG(file
->mode
)) {
326 rprintf(FINFO
, "make_bak: skipping non-regular file %s\n",
332 /* move to keep tree if a file */
334 if (robust_move(fname
, buf
) != 0) {
335 rsyserr(FERROR
, errno
, "keep_backup failed: %s -> \"%s\"",
336 full_fname(fname
), buf
);
337 } else if (sx
.st
.st_nlink
> 1) {
338 /* If someone has hard-linked the file into the backup
339 * dir, rename() might return success but do nothing! */
340 robust_unlink(fname
); /* Just in case... */
344 set_file_attrs(buf
, file
, NULL
, fname
, 0);
345 preserve_xattrs
= save_preserve_xattrs
;
349 rprintf(FINFO
, "backed up %s to %s\n",
356 /* main backup switch routine */
357 int make_backup(const char *fname
)
360 return keep_backup(fname
);
361 return make_simple_backup(fname
);