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 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_links
;
34 extern int preserve_hard_links
;
35 extern int orig_umask
;
36 extern int safe_symlinks
;
38 /* make a complete pathname for backup file */
39 char *get_backup_name(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(char *fname
)
59 char *fnamebak
= get_backup_name(fname
);
65 if (do_rename(fname
, fnamebak
) == 0) {
67 rprintf(FINFO
, "backed up %s to %s\n",
69 safe_fname(fnamebak
));
73 /* cygwin (at least version b19) reports EINVAL */
74 if (errno
== ENOENT
|| errno
== EINVAL
)
78 if (errno
== EISDIR
&& do_rmdir(fnamebak
) == 0)
80 if (errno
== ENOTDIR
&& do_unlink(fnamebak
) == 0)
83 rsyserr(FERROR
, rename_errno
, "rename %s to backup %s",
84 safe_fname(fname
), safe_fname(fnamebak
));
93 /****************************************************************************
94 Create a directory given an absolute path, perms based upon another directory
96 ****************************************************************************/
97 static int make_bak_dir(char *fullpath
)
100 char *rel
= fullpath
+ backup_dir_len
;
101 char *end
= rel
+ strlen(rel
);
104 while (strncmp(fullpath
, "./", 2) == 0)
107 /* Try to find an existing dir, starting from the deepest dir. */
109 if (--p
== fullpath
) {
115 if (do_mkdir(fullpath
, 0777 & ~orig_umask
) == 0)
117 if (errno
!= ENOENT
) {
118 rsyserr(FERROR
, errno
,
119 "make_bak_dir mkdir %s failed",
120 full_fname(fullpath
));
126 /* Make all the dirs that we didn't find on the way here. */
129 /* Try to transfer the directory settings of the
130 * actual dir that the files are coming from. */
131 if (do_stat(rel
, &st
) < 0) {
132 rsyserr(FERROR
, errno
,
133 "make_bak_dir stat %s failed",
136 do_lchown(fullpath
, st
.st_uid
, st
.st_gid
);
137 do_chmod(fullpath
, st
.st_mode
);
144 if (do_mkdir(fullpath
, 0777 & ~orig_umask
) < 0) {
145 rsyserr(FERROR
, errno
, "make_bak_dir mkdir %s failed",
146 full_fname(fullpath
));
160 /* robustly move a file, creating new directory structures if necessary */
161 static int robust_move(char *src
, char *dst
)
163 if (robust_rename(src
, dst
, 0755) < 0 && (errno
!= ENOENT
164 || make_bak_dir(dst
) < 0 || robust_rename(src
, dst
, 0755) < 0))
170 /* If we have a --backup-dir, then we get here from make_backup().
171 * We will move the file to be deleted into a parallel directory tree. */
172 static int keep_backup(char *fname
)
175 struct file_struct
*file
;
180 /* return if no file to keep */
181 if (do_lstat(fname
, &st
) < 0)
184 if (!(file
= make_file(fname
, NULL
, NO_FILTERS
)))
185 return 1; /* the file could have disappeared */
187 if (!(buf
= get_backup_name(fname
)))
190 /* Check to see if this is a device file, or link */
191 if (IS_DEVICE(file
->mode
)) {
192 if (am_root
&& preserve_devices
) {
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) {
200 "make_backup: DEVICE %s successful.\n",
208 if (!kept
&& S_ISDIR(file
->mode
)) {
209 /* make an empty directory */
210 if (do_mkdir(buf
, file
->mode
) < 0
211 && (errno
!= ENOENT
|| make_bak_dir(buf
) < 0
212 || do_mkdir(buf
, file
->mode
) < 0)) {
213 rsyserr(FINFO
, errno
, "mkdir %s failed",
217 ret_code
= do_rmdir(fname
);
219 rprintf(FINFO
, "make_backup: RMDIR %s returns %i\n",
220 full_fname(fname
), ret_code
);
226 if (!kept
&& preserve_links
&& S_ISLNK(file
->mode
)) {
227 if (safe_symlinks
&& unsafe_symlink(file
->u
.link
, buf
)) {
229 rprintf(FINFO
, "ignoring unsafe symlink %s -> %s\n",
230 full_fname(buf
), file
->u
.link
);
234 if (do_symlink(file
->u
.link
, buf
) < 0
235 && (errno
!= ENOENT
|| make_bak_dir(buf
) < 0
236 || do_symlink(file
->u
.link
, buf
) < 0)) {
237 rsyserr(FERROR
, errno
, "link %s -> \"%s\"",
238 full_fname(buf
), safe_fname(file
->u
.link
));
245 if (!kept
&& !S_ISREG(file
->mode
)) {
246 rprintf(FINFO
, "make_bak: skipping non-regular file %s\n",
251 /* move to keep tree if a file */
253 if (robust_move(fname
, buf
) != 0) {
254 rsyserr(FERROR
, errno
, "keep_backup failed: %s -> \"%s\"",
255 full_fname(fname
), safe_fname(buf
));
256 } else if (st
.st_nlink
> 1) {
257 /* If someone has hard-linked the file into the backup
258 * dir, rename() might return success but do nothing! */
259 robust_unlink(fname
); /* Just in case... */
262 set_perms(buf
, file
, NULL
, 0);
266 rprintf(FINFO
, "backed up %s to %s\n",
267 safe_fname(fname
), safe_fname(buf
));
273 /* main backup switch routine */
274 int make_backup(char *fname
)
277 return keep_backup(fname
);
278 return make_simple_backup(fname
);