2 Copyright (C) Andrew Tridgell 1999
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* backup handling code */
24 extern int backup_suffix_len;
25 extern int backup_dir_len;
26 extern char *backup_suffix;
27 extern char *backup_dir;
28 extern struct file_struct **hlink_list;
31 extern int preserve_devices;
32 extern int preserve_links;
33 extern int preserve_hard_links;
34 extern int orig_umask;
36 /* simple backup creates a backup with a suffix in the same directory */
37 static int make_simple_backup(char *fname)
39 char fnamebak[MAXPATHLEN];
41 if (stringjoin(fnamebak, sizeof fnamebak, fname, backup_suffix, NULL)
43 rprintf(FERROR, "backup filename too long\n");
47 if (do_rename(fname, fnamebak) != 0) {
48 /* cygwin (at least version b19) reports EINVAL */
49 if (errno != ENOENT && errno != EINVAL) {
50 rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak);
53 } else if (verbose > 1) {
54 rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak);
60 /* recursively make a directory path */
61 static int make_dir(char *name, int mask)
63 char newdir [MAXPATHLEN];
66 /* copy pathname over, look for last '/' */
67 for (p = d = newdir; *name; *d++ = *name++)
74 /* make the new directory, if that fails then make its parent */
75 while (do_mkdir(newdir, mask) != 0) {
76 if (errno != ENOENT || !make_dir(newdir, mask))
84 /****************************************************************************
85 Create a directory given an absolute path, perms based upon another directory
87 ****************************************************************************/
88 static int make_bak_dir(char *fname, char *bak_path)
92 char fullpath[MAXPATHLEN];
96 while(strncmp(bak_path, "./", 2) == 0) bak_path += 2;
98 if (pathjoin(fullpath, sizeof fullpath, bak_path, fname)
100 rprintf(FERROR, "backup dirname too long\n");
104 q = fullpath + strlen(bak_path);
106 q++; /* Point past the middle '/' added by pathjoin(). */
108 /* Make the directories */
109 while ((p = strchr(p, '/')) != NULL) {
111 if (do_lstat(fullpath, &st) != 0) {
112 do_mkdir(fullpath, 0777 & ~orig_umask);
114 if (do_lstat(q, &st) != 0) {
115 rprintf(FERROR, "make_bak_dir stat %s failed: %s\n",
116 full_fname(fullpath), strerror(errno));
119 set_modtime(fullpath, st2->st_mtime);
120 if (do_lchown(fullpath, st2->st_uid, st2->st_gid) != 0) {
121 rprintf(FERROR, "make_bak_dir chown %s failed: %s\n",
122 full_fname(fullpath), strerror(errno));
124 if (do_chmod(fullpath, st2->st_mode) != 0) {
125 rprintf(FERROR, "make_bak_dir failed to set permissions on %s: %s\n",
126 full_fname(fullpath), strerror(errno));
136 /* robustly move a file, creating new directory structures if necessary */
137 static int robust_move(char *src, char *dst)
140 int keep_path_extfs = 0;
143 while (keep_trying) {
144 if (keep_path_extfs) {
145 failed = copy_file(src, dst, 0755);
149 failed = robust_rename(src, dst);
153 rprintf(FERROR, "robust_move failed: %s(%d)\n",
154 strerror(errno), errno);
157 case EXDEV: /* external filesystem */
161 case ENOENT: /* no directory to write to */
176 /* if we have a backup_dir, then we get here from make_backup().
177 We will move the file to be deleted into a parallel directory tree */
178 static int keep_backup(char *fname)
180 static int initialised;
181 char keep_name[MAXPATHLEN];
183 struct file_struct *file;
188 if (backup_dir_len && backup_dir[backup_dir_len - 1] == '/')
189 backup_dir[--backup_dir_len] = '\0';
191 rprintf(FINFO, "backup_dir is %s\n", backup_dir);
195 /* return if no file to keep */
197 if (do_lstat(fname, &st)) return 1;
199 if (do_stat(fname, &st)) return 1;
202 file = make_file(fname, NULL, NO_EXCLUDES);
204 /* the file could have disappeared */
207 /* make a complete pathname for backup file */
208 if (stringjoin(keep_name, sizeof keep_name,
209 backup_dir, "/", fname, backup_suffix, NULL)
210 >= sizeof keep_name) {
211 rprintf(FERROR, "keep_backup filename too long\n");
216 /* Check to see if this is a device file, or link */
217 if (IS_DEVICE(file->mode)) {
218 if (am_root && preserve_devices) {
219 make_bak_dir(fname, backup_dir);
220 if (do_mknod(keep_name, file->mode, file->u.rdev) != 0) {
221 rprintf(FERROR, "mknod %s failed: %s\n",
222 full_fname(keep_name), strerror(errno));
223 } else if (verbose > 2) {
225 "make_backup: DEVICE %s successful.\n",
234 if (!kept && S_ISDIR(file->mode)) {
235 /* make an empty directory */
236 make_bak_dir(fname, backup_dir);
237 do_mkdir(keep_name, file->mode);
238 ret_code = do_rmdir(fname);
241 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
242 full_fname(fname), ret_code);
248 if (!kept && preserve_links && S_ISLNK(file->mode)) {
249 extern int safe_symlinks;
250 if (safe_symlinks && unsafe_symlink(file->u.link, keep_name)) {
252 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
253 full_fname(keep_name), file->u.link);
257 make_bak_dir(fname, backup_dir);
258 if (do_symlink(file->u.link, keep_name) != 0) {
259 rprintf(FERROR, "link %s -> %s : %s\n",
260 full_fname(keep_name), file->u.link, strerror(errno));
266 if (!kept && preserve_hard_links && file->link_u.links
267 && hlink_list[file->F_HLINDEX] != file) {
269 rprintf(FINFO, "%s is a hard link\n", f_name(file));
272 if (!kept && !S_ISREG(file->mode)) {
273 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
277 /* move to keep tree if a file */
279 if (!robust_move(fname, keep_name)) {
280 rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n",
281 full_fname(fname), keep_name, strerror(errno));
284 set_perms(keep_name, file, NULL, 0);
289 rprintf(FINFO, "keep_backup %s -> %s\n", fname, keep_name);
294 /* main backup switch routine */
295 int make_backup(char *fname)
298 return keep_backup(fname);
299 return make_simple_backup(fname);