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.
25 extern int backup_dir_len;
26 extern unsigned int backup_dir_remainder;
27 extern char backup_dir_buf[MAXPATHLEN];
28 extern char *backup_suffix;
29 extern char *backup_dir;
32 extern int preserve_devices;
33 extern int preserve_specials;
34 extern int preserve_links;
35 extern int safe_symlinks;
37 /* make a complete pathname for backup file */
38 char *get_backup_name(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(char *fname)
58 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 static int make_bak_dir(char *fullpath)
98 char *rel = fullpath + backup_dir_len;
99 char *end = rel + strlen(rel);
102 while (strncmp(fullpath, "./", 2) == 0)
105 /* Try to find an existing dir, starting from the deepest dir. */
107 if (--p == fullpath) {
113 if (mkdir_defmode(fullpath) == 0)
115 if (errno != ENOENT) {
116 rsyserr(FERROR, errno,
117 "make_bak_dir mkdir %s failed",
118 full_fname(fullpath));
124 /* Make all the dirs that we didn't find on the way here. */
127 /* Try to transfer the directory settings of the
128 * actual dir that the files are coming from. */
129 if (do_stat(rel, &st) < 0) {
130 rsyserr(FERROR, errno,
131 "make_bak_dir stat %s failed",
134 do_lchown(fullpath, st.st_uid, st.st_gid);
135 do_chmod(fullpath, st.st_mode);
142 if (mkdir_defmode(fullpath) < 0) {
143 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
144 full_fname(fullpath));
158 /* robustly move a file, creating new directory structures if necessary */
159 static int robust_move(char *src, char *dst)
161 if (robust_rename(src, dst, NULL, 0755) < 0
162 && (errno != ENOENT || make_bak_dir(dst) < 0
163 || robust_rename(src, dst, NULL, 0755) < 0))
169 /* If we have a --backup-dir, then we get here from make_backup().
170 * We will move the file to be deleted into a parallel directory tree. */
171 static int keep_backup(char *fname)
174 struct file_struct *file;
179 /* return if no file to keep */
180 if (do_lstat(fname, &st) < 0)
183 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
184 return 1; /* the file could have disappeared */
186 if (!(buf = get_backup_name(fname)))
189 /* Check to see if this is a device file, or link */
190 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
191 || (preserve_specials && IS_SPECIAL(file->mode))) {
193 if (do_mknod(buf, file->mode, file->u.rdev) < 0
194 && (errno != ENOENT || make_bak_dir(buf) < 0
195 || do_mknod(buf, file->mode, file->u.rdev) < 0)) {
196 rsyserr(FERROR, errno, "mknod %s failed",
198 } else if (verbose > 2) {
199 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
206 if (!kept && S_ISDIR(file->mode)) {
207 /* make an empty directory */
208 if (do_mkdir(buf, file->mode) < 0
209 && (errno != ENOENT || make_bak_dir(buf) < 0
210 || do_mkdir(buf, file->mode) < 0)) {
211 rsyserr(FINFO, errno, "mkdir %s failed",
215 ret_code = do_rmdir(fname);
217 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
218 full_fname(fname), ret_code);
224 if (!kept && preserve_links && S_ISLNK(file->mode)) {
225 if (safe_symlinks && unsafe_symlink(file->u.link, buf)) {
227 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
228 full_fname(buf), file->u.link);
233 if (do_symlink(file->u.link, buf) < 0
234 && (errno != ENOENT || make_bak_dir(buf) < 0
235 || do_symlink(file->u.link, buf) < 0)) {
236 rsyserr(FERROR, errno, "link %s -> \"%s\"",
246 if (!kept && !S_ISREG(file->mode)) {
247 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
252 /* move to keep tree if a file */
254 if (robust_move(fname, buf) != 0) {
255 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
256 full_fname(fname), buf);
257 } else if (st.st_nlink > 1) {
258 /* If someone has hard-linked the file into the backup
259 * dir, rename() might return success but do nothing! */
260 robust_unlink(fname); /* Just in case... */
263 set_file_attrs(buf, file, NULL, 0);
267 rprintf(FINFO, "backed up %s to %s\n",
274 /* main backup switch routine */
275 int make_backup(char *fname)
278 return keep_backup(fname);
279 return make_simple_backup(fname);