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 char *backup_suffix;
25 extern char *backup_dir;
28 /* simple backup creates a backup with a suffix in the same directory */
29 static int make_simple_backup(char *fname)
31 char fnamebak[MAXPATHLEN];
32 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
33 rprintf(FERROR,"backup filename too long\n");
37 slprintf(fnamebak,sizeof(fnamebak),"%s%s",fname,backup_suffix);
38 if (do_rename(fname,fnamebak) != 0) {
39 /* cygwin (at least version b19) reports EINVAL */
40 if (errno != ENOENT && errno != EINVAL) {
41 rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
44 } else if (verbose > 1) {
45 rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak);
51 /* recursively make a directory path */
52 static int make_dir(char *name, int mask)
54 char newdir [MAXPATHLEN];
57 /* copy pathname over, look for last '/' */
58 for (p = d = newdir; *name; *d++ = *name++)
65 /* make the new directory, if that fails then make its parent */
66 while (do_mkdir (newdir, mask) != 0)
67 if ((errno != ENOENT) || !make_dir (newdir, mask))
74 /* robustly move a file, creating new directory structures if necessary */
75 static int robust_move(char *src, char *dst)
78 int keep_path_extfs = 0;
83 failed = copy_file (src, dst, 0755);
85 failed = robust_rename (src, dst);
89 rprintf (FERROR, "robust_move failed: %s(%d)\n",
90 strerror (errno), errno);
92 /* external filesystem */
97 /* no directory to write to */
112 /* if we have a backup_dir, then we get here from make_backup().
113 We will move the file to be deleted into a parallel directory tree */
114 static int keep_backup(char *fname)
116 static int initialised;
118 char keep_name [MAXPATHLEN];
120 struct file_struct *file;
123 if (backup_dir[strlen(backup_dir) - 1] == '/')
124 backup_dir[strlen(backup_dir) - 1] = 0;
126 rprintf (FINFO, "backup_dir is %s\n", backup_dir);
130 /* return if no file to keep */
132 if (do_lstat (fname, &st)) return 1;
134 if (do_stat (fname, &st)) return 1;
137 file = make_file (0, fname);
139 /* make a complete pathname for backup file */
140 if (strlen(backup_dir) + strlen(fname) > (MAXPATHLEN - 1)) {
141 rprintf (FERROR, "keep_backup filename too long\n");
144 slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
146 if (!S_ISDIR(file->mode)) {
147 /* move to keep tree if a file */
148 if (!robust_move (fname, keep_name))
149 rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
150 fname, keep_name, strerror(errno));
152 /* this bit only used to "keep" empty directories */
153 /* make the parent directories */
154 make_dir (keep_name, 0755);
155 /* now make the (empty) directory */
156 do_mkdir (keep_name, file->mode);
158 rprintf (FINFO, "keep_backup: made empty dir: %s\n",
162 set_perms (keep_name, file, NULL, 0);
166 rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
171 /* main backup switch routine */
172 int make_backup(char *fname)
175 return (keep_backup(fname));
177 return (make_simple_backup(fname));