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;
30 extern int preserve_devices;
31 extern int preserve_links;
32 extern int preserve_hard_links;
34 /* simple backup creates a backup with a suffix in the same directory */
35 static int make_simple_backup(char *fname)
37 char fnamebak[MAXPATHLEN];
38 if (strlen(fname) + backup_suffix_len > MAXPATHLEN-1) {
39 rprintf(FERROR, "backup filename too long\n");
43 snprintf(fnamebak, sizeof(fnamebak), "%s%s", fname, backup_suffix);
44 if (do_rename(fname, fnamebak) != 0) {
45 /* cygwin (at least version b19) reports EINVAL */
46 if (errno != ENOENT && errno != EINVAL) {
47 rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak);
50 } else if (verbose > 1) {
51 rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak);
57 /* recursively make a directory path */
58 static int make_dir(char *name, int mask)
60 char newdir [MAXPATHLEN];
63 /* copy pathname over, look for last '/' */
64 for (p = d = newdir; *name; *d++ = *name++)
71 /* make the new directory, if that fails then make its parent */
72 while (do_mkdir(newdir, mask) != 0) {
73 if (errno != ENOENT || !make_dir(newdir, mask))
81 /****************************************************************************
82 Create a directory given an absolute path, perms based upon another directory
84 ****************************************************************************/
85 static int make_bak_dir(char *fname, char *bak_path)
89 char fullpath[MAXPATHLEN];
90 extern int orig_umask;
94 while(strncmp(bak_path, "./", 2) == 0) bak_path += 2;
96 if (bak_path[strlen(bak_path)-1] != '/') {
97 snprintf(fullpath, sizeof(fullpath), "%s/", bak_path);
99 snprintf(fullpath, sizeof(fullpath), "%s", bak_path);
102 q = &fullpath[strlen(fullpath)]; /* End of bak_path string */
103 strcat(fullpath, fname);
105 /* Make the directories */
106 while ((p = strchr(p, '/')) != NULL) {
108 if (do_lstat(fullpath, &st) != 0) {
109 do_mkdir(fullpath, 0777 & ~orig_umask);
111 if (do_lstat(q, &st) != 0) {
112 rprintf(FERROR, "make_bak_dir stat %s failed: %s\n",
113 full_fname(fullpath), strerror(errno));
116 set_modtime(fullpath, st2->st_mtime);
117 if (do_lchown(fullpath, st2->st_uid, st2->st_gid) != 0) {
118 rprintf(FERROR, "make_bak_dir chown %s failed: %s\n",
119 full_fname(fullpath), strerror(errno));
121 if (do_chmod(fullpath, st2->st_mode) != 0) {
122 rprintf(FERROR, "make_bak_dir failed to set permissions on %s: %s\n",
123 full_fname(fullpath), strerror(errno));
134 /* robustly move a file, creating new directory structures if necessary */
135 static int robust_move(char *src, char *dst)
138 int keep_path_extfs = 0;
141 while (keep_trying) {
142 if (keep_path_extfs) {
143 failed = copy_file(src, dst, 0755);
147 failed = robust_rename(src, dst);
151 rprintf(FERROR, "robust_move failed: %s(%d)\n",
152 strerror(errno), errno);
155 case EXDEV: /* external filesystem */
159 case ENOENT: /* no directory to write to */
174 /* if we have a backup_dir, then we get here from make_backup().
175 We will move the file to be deleted into a parallel directory tree */
176 static int keep_backup(char *fname)
179 static int initialised;
181 char keep_name [MAXPATHLEN];
183 struct file_struct *file;
189 if (backup_dir_len && backup_dir[backup_dir_len - 1] == '/')
190 backup_dir[--backup_dir_len] = '\0';
192 rprintf(FINFO, "backup_dir is %s\n", backup_dir);
196 /* return if no file to keep */
198 if (do_lstat(fname, &st)) return 1;
200 if (do_stat(fname, &st)) return 1;
203 file = make_file(fname, NULL, NO_EXCLUDES);
205 /* the file could have disappeared */
208 /* make a complete pathname for backup file */
209 if (backup_dir_len+strlen(fname)+backup_suffix_len > MAXPATHLEN-1) {
210 rprintf(FERROR, "keep_backup filename too long\n");
214 snprintf(keep_name, sizeof (keep_name), "%s/%s%s",
215 backup_dir, fname, backup_suffix);
218 /* Check to see if this is a device file, or link */
219 if (IS_DEVICE(file->mode)) {
220 if (am_root && preserve_devices) {
221 make_bak_dir(fname, backup_dir);
222 if (do_mknod(keep_name, file->mode, file->rdev) != 0) {
223 rprintf(FERROR, "mknod %s failed: %s\n",
224 full_fname(keep_name), strerror(errno));
225 } else if (verbose > 2) {
227 "make_backup: DEVICE %s successful.\n",
236 if (!kept && S_ISDIR(file->mode)) {
237 /* make an empty directory */
238 make_bak_dir(fname, backup_dir);
239 do_mkdir(keep_name, file->mode);
240 ret_code = do_rmdir(fname);
243 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
244 full_fname(fname), ret_code);
250 if (!kept && preserve_links && S_ISLNK(file->mode)) {
251 extern int safe_symlinks;
252 if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
254 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
255 full_fname(keep_name), file->link);
259 make_bak_dir(fname, backup_dir);
260 if (do_symlink(file->link, keep_name) != 0) {
261 rprintf(FERROR, "link %s -> %s : %s\n",
262 full_fname(keep_name), file->link, strerror(errno));
268 if (!kept && preserve_hard_links && check_hard_link(file)) {
270 rprintf(FINFO, "%s is a hard link\n", f_name(file));
273 if (!kept && !S_ISREG(file->mode)) {
274 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
278 /* move to keep tree if a file */
280 if (!robust_move(fname, keep_name)) {
281 rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n",
282 full_fname(fname), keep_name, strerror(errno));
285 set_perms(keep_name, file, NULL, 0);
290 rprintf(FINFO, "keep_backup %s -> %s\n", fname, keep_name);
295 /* main backup switch routine */
296 int make_backup(char *fname)
299 return keep_backup(fname);
300 return make_simple_backup(fname);