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 int flist_extra_ndx;
32 extern int file_struct_len;
33 extern unsigned int backup_dir_remainder;
34 extern char backup_dir_buf[MAXPATHLEN];
35 extern char *backup_suffix;
36 extern char *backup_dir;
38 /* make a complete pathname for backup file */
39 char *get_backup_name(const char *fname)
42 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
43 fname, backup_suffix, NULL) < backup_dir_remainder)
44 return backup_dir_buf;
46 if (stringjoin(backup_dir_buf, MAXPATHLEN,
47 fname, backup_suffix, NULL) < MAXPATHLEN)
48 return backup_dir_buf;
51 rprintf(FERROR, "backup filename too long\n");
55 /* simple backup creates a backup with a suffix in the same directory */
56 static int make_simple_backup(const char *fname)
59 const char *fnamebak = get_backup_name(fname);
65 if (do_rename(fname, fnamebak) == 0) {
67 rprintf(FINFO, "backed up %s to %s\n",
72 /* cygwin (at least version b19) reports EINVAL */
73 if (errno == ENOENT || errno == EINVAL)
77 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
79 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
82 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
92 /****************************************************************************
93 Create a directory given an absolute path, perms based upon another directory
95 ****************************************************************************/
96 static int make_bak_dir(char *fullpath)
99 char *rel = fullpath + backup_dir_len;
100 char *end = rel + strlen(rel);
103 while (strncmp(fullpath, "./", 2) == 0)
106 /* Try to find an existing dir, starting from the deepest dir. */
108 if (--p == fullpath) {
114 if (mkdir_defmode(fullpath) == 0)
116 if (errno != ENOENT) {
117 rsyserr(FERROR, errno,
118 "make_bak_dir mkdir %s failed",
119 full_fname(fullpath));
125 /* Make all the dirs that we didn't find on the way here. */
128 /* Try to transfer the directory settings of the
129 * actual dir that the files are coming from. */
130 if (do_stat(rel, &st) < 0) {
131 rsyserr(FERROR, errno,
132 "make_bak_dir stat %s failed",
135 do_lchown(fullpath, st.st_uid, st.st_gid);
137 do_chmod(fullpath, st.st_mode);
145 if (mkdir_defmode(fullpath) < 0) {
146 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
147 full_fname(fullpath));
161 /* robustly move a file, creating new directory structures if necessary */
162 static int robust_move(const char *src, char *dst)
164 if (robust_rename(src, dst, NULL, 0755) < 0
165 && (errno != ENOENT || make_bak_dir(dst) < 0
166 || robust_rename(src, dst, NULL, 0755) < 0))
172 /* If we have a --backup-dir, then we get here from make_backup().
173 * We will move the file to be deleted into a parallel directory tree. */
174 static int keep_backup(const char *fname)
177 struct file_struct *file;
182 /* return if no file to keep */
183 if (do_lstat(fname, &st) < 0)
186 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
187 return 1; /* the file could have disappeared */
189 if (!(buf = get_backup_name(fname))) {
194 /* Check to see if this is a device file, or link */
195 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
196 || (preserve_specials && IS_SPECIAL(file->mode))) {
197 dev_t rdev = MAKEDEV(F_DMAJOR(file), F_DMINOR(file));
199 if (do_mknod(buf, file->mode, rdev) < 0
200 && (errno != ENOENT || make_bak_dir(buf) < 0
201 || do_mknod(buf, file->mode, rdev) < 0)) {
202 rsyserr(FERROR, errno, "mknod %s failed",
204 } else if (verbose > 2) {
205 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
212 if (!kept && S_ISDIR(file->mode)) {
213 /* make an empty directory */
214 if (do_mkdir(buf, file->mode) < 0
215 && (errno != ENOENT || make_bak_dir(buf) < 0
216 || do_mkdir(buf, file->mode) < 0)) {
217 rsyserr(FINFO, errno, "mkdir %s failed",
221 ret_code = do_rmdir(fname);
223 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
224 full_fname(fname), ret_code);
230 if (!kept && preserve_links && S_ISLNK(file->mode)) {
231 const char *sl = F_SYMLINK(file);
232 if (safe_symlinks && unsafe_symlink(sl, buf)) {
234 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
235 full_fname(buf), sl);
240 if (do_symlink(sl, buf) < 0
241 && (errno != ENOENT || make_bak_dir(buf) < 0
242 || do_symlink(sl, buf) < 0)) {
243 rsyserr(FERROR, errno, "link %s -> \"%s\"",
244 full_fname(buf), sl);
252 if (!kept && !S_ISREG(file->mode)) {
253 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
259 /* move to keep tree if a file */
261 if (robust_move(fname, buf) != 0) {
262 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
263 full_fname(fname), buf);
264 } else if (st.st_nlink > 1) {
265 /* If someone has hard-linked the file into the backup
266 * dir, rename() might return success but do nothing! */
267 robust_unlink(fname); /* Just in case... */
270 set_file_attrs(buf, file, NULL, 0);
274 rprintf(FINFO, "backed up %s to %s\n",
281 /* main backup switch routine */
282 int make_backup(const char *fname)
285 return keep_backup(fname);
286 return make_simple_backup(fname);