2 * Backup handling code.
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
26 extern int preserve_devices;
27 extern int preserve_specials;
28 extern int preserve_links;
29 extern int safe_symlinks;
30 extern int backup_dir_len;
31 extern unsigned int backup_dir_remainder;
32 extern char backup_dir_buf[MAXPATHLEN];
33 extern char *backup_suffix;
34 extern char *backup_dir;
36 /* make a complete pathname for backup file */
37 char *get_backup_name(const char *fname)
40 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
41 fname, backup_suffix, NULL) < backup_dir_remainder)
42 return backup_dir_buf;
44 if (stringjoin(backup_dir_buf, MAXPATHLEN,
45 fname, backup_suffix, NULL) < MAXPATHLEN)
46 return backup_dir_buf;
49 rprintf(FERROR, "backup filename too long\n");
53 /* simple backup creates a backup with a suffix in the same directory */
54 static int make_simple_backup(const char *fname)
57 const char *fnamebak = get_backup_name(fname);
63 if (do_rename(fname, fnamebak) == 0) {
65 rprintf(FINFO, "backed up %s to %s\n",
70 /* cygwin (at least version b19) reports EINVAL */
71 if (errno == ENOENT || errno == EINVAL)
75 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
77 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
80 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
90 /****************************************************************************
91 Create a directory given an absolute path, perms based upon another directory
93 ****************************************************************************/
94 static int make_bak_dir(char *fullpath)
97 char *rel = fullpath + backup_dir_len;
98 char *end = rel + strlen(rel);
101 while (strncmp(fullpath, "./", 2) == 0)
104 /* Try to find an existing dir, starting from the deepest dir. */
106 if (--p == fullpath) {
112 if (mkdir_defmode(fullpath) == 0)
114 if (errno != ENOENT) {
115 rsyserr(FERROR, errno,
116 "make_bak_dir mkdir %s failed",
117 full_fname(fullpath));
123 /* Make all the dirs that we didn't find on the way here. */
126 /* Try to transfer the directory settings of the
127 * actual dir that the files are coming from. */
128 if (do_stat(rel, &st) < 0) {
129 rsyserr(FERROR, errno,
130 "make_bak_dir stat %s failed",
133 do_lchown(fullpath, st.st_uid, st.st_gid);
135 do_chmod(fullpath, st.st_mode);
143 if (mkdir_defmode(fullpath) < 0) {
144 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
145 full_fname(fullpath));
159 /* robustly move a file, creating new directory structures if necessary */
160 static int robust_move(const char *src, char *dst)
162 if (robust_rename(src, dst, NULL, 0755) < 0
163 && (errno != ENOENT || make_bak_dir(dst) < 0
164 || robust_rename(src, dst, NULL, 0755) < 0))
170 /* If we have a --backup-dir, then we get here from make_backup().
171 * We will move the file to be deleted into a parallel directory tree. */
172 static int keep_backup(const char *fname)
175 struct file_struct *file;
180 /* return if no file to keep */
181 if (do_lstat(fname, &st) < 0)
184 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
185 return 1; /* the file could have disappeared */
187 if (!(buf = get_backup_name(fname))) {
192 /* Check to see if this is a device file, or link */
193 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
194 || (preserve_specials && IS_SPECIAL(file->mode))) {
195 uint32 *devp = F_RDEV_P(file);
196 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
198 if (do_mknod(buf, file->mode, rdev) < 0
199 && (errno != ENOENT || make_bak_dir(buf) < 0
200 || do_mknod(buf, file->mode, rdev) < 0)) {
201 rsyserr(FERROR, errno, "mknod %s failed",
203 } else if (verbose > 2) {
204 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
211 if (!kept && S_ISDIR(file->mode)) {
212 /* make an empty directory */
213 if (do_mkdir(buf, file->mode) < 0
214 && (errno != ENOENT || make_bak_dir(buf) < 0
215 || do_mkdir(buf, file->mode) < 0)) {
216 rsyserr(FINFO, errno, "mkdir %s failed",
220 ret_code = do_rmdir(fname);
222 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
223 full_fname(fname), ret_code);
229 if (!kept && preserve_links && S_ISLNK(file->mode)) {
230 const char *sl = F_SYMLINK(file);
231 if (safe_symlinks && unsafe_symlink(sl, buf)) {
233 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
234 full_fname(buf), sl);
239 if (do_symlink(sl, buf) < 0
240 && (errno != ENOENT || make_bak_dir(buf) < 0
241 || do_symlink(sl, buf) < 0)) {
242 rsyserr(FERROR, errno, "link %s -> \"%s\"",
243 full_fname(buf), sl);
251 if (!kept && !S_ISREG(file->mode)) {
252 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
258 /* move to keep tree if a file */
260 if (robust_move(fname, buf) != 0) {
261 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
262 full_fname(fname), buf);
263 } else if (st.st_nlink > 1) {
264 /* If someone has hard-linked the file into the backup
265 * dir, rename() might return success but do nothing! */
266 robust_unlink(fname); /* Just in case... */
269 set_file_attrs(buf, file, NULL, 0);
273 rprintf(FINFO, "backed up %s to %s\n",
280 /* main backup switch routine */
281 int make_backup(const char *fname)
284 return keep_backup(fname);
285 return make_simple_backup(fname);