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;
29 extern int preserve_devices;
30 extern int preserve_links;
31 extern int preserve_hard_links;
33 /* simple backup creates a backup with a suffix in the same directory */
34 static int make_simple_backup(char *fname)
36 char fnamebak[MAXPATHLEN];
37 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
38 rprintf(FERROR,"backup filename too long\n");
42 slprintf(fnamebak,sizeof(fnamebak),"%s%s",fname,backup_suffix);
43 if (do_rename(fname,fnamebak) != 0) {
44 /* cygwin (at least version b19) reports EINVAL */
45 if (errno != ENOENT && errno != EINVAL) {
46 rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
49 } else if (verbose > 1) {
50 rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak);
56 /* recursively make a directory path */
57 static int make_dir(char *name, int mask)
59 char newdir [MAXPATHLEN];
62 /* copy pathname over, look for last '/' */
63 for (p = d = newdir; *name; *d++ = *name++)
70 /* make the new directory, if that fails then make its parent */
71 while (do_mkdir (newdir, mask) != 0)
72 if ((errno != ENOENT) || !make_dir (newdir, mask))
79 /****************************************************************************
80 Create a directory given an absolute path, perms based upon another directory
82 ****************************************************************************/
83 static int make_bak_dir(char *fname,char *bak_path)
87 char fullpath[MAXPATHLEN];
88 extern int orig_umask;
92 while(strncmp(bak_path,"./",2)==0) bak_path += 2;
94 if(bak_path[strlen(bak_path)-1]!='/') {
95 slprintf(fullpath,sizeof(fullpath),"%s/",bak_path);
97 slprintf(fullpath,sizeof(fullpath),"%s",bak_path);
100 q=&fullpath[strlen(fullpath)]; /* End of bak_path string */
101 strcat(fullpath,fname);
103 /* Make the directories */
104 while ((p=strchr(p,'/'))) {
106 if(do_lstat(fullpath,&st)!=0) {
107 do_mkdir(fullpath,0777 & ~orig_umask);
109 if(do_lstat(q,&st)!=0) {
110 rprintf(FERROR,"make_bak_dir stat %s : %s\n",fullpath,strerror(errno));
113 set_modtime(fullpath,st2->st_mtime);
114 if(do_lchown(fullpath,st2->st_uid,st2->st_gid)!=0) {
115 rprintf(FERROR,"make_bak_dir chown %s : %s\n",fullpath,strerror(errno));
117 if(do_chmod(fullpath,st2->st_mode)!=0) {
118 rprintf(FERROR,"make_bak_dir failed to set permissions on %s : %s\n",fullpath,strerror(errno));
129 /* robustly move a file, creating new directory structures if necessary */
130 static int robust_move(char *src, char *dst)
133 int keep_path_extfs = 0;
136 while (keep_trying) {
137 if (keep_path_extfs) {
138 failed = copy_file(src, dst, 0755);
143 failed = robust_rename (src, dst);
148 rprintf (FERROR, "robust_move failed: %s(%d)\n",
149 strerror (errno), errno);
151 /* external filesystem */
156 /* no directory to write to */
158 make_dir (dst, 0755);
171 /* if we have a backup_dir, then we get here from make_backup().
172 We will move the file to be deleted into a parallel directory tree */
173 static int keep_backup(char *fname)
176 static int initialised;
178 char keep_name [MAXPATHLEN];
180 struct file_struct *file;
186 if (backup_dir[strlen(backup_dir) - 1] == '/')
187 backup_dir[strlen(backup_dir) - 1] = 0;
189 rprintf (FINFO, "backup_dir is %s\n", backup_dir);
193 /* return if no file to keep */
195 if (do_lstat (fname, &st)) return 1;
197 if (do_stat (fname, &st)) return 1;
200 file = make_file (-1, fname, 0);
202 /* make a complete pathname for backup file */
203 if (strlen(backup_dir) + strlen(fname) > (MAXPATHLEN - 1)) {
204 rprintf (FERROR, "keep_backup filename too long\n");
208 slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
212 /* Check to see if this is a device file, or link */
213 if(IS_DEVICE(file->mode)) {
214 if(am_root && preserve_devices) {
215 make_bak_dir(fname,backup_dir);
216 if(do_mknod(keep_name,file->mode,file->rdev)!=0) {
217 rprintf(FERROR,"mknod %s : %s\n",keep_name,strerror(errno));
220 rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname);
228 if(!kept && S_ISDIR(file->mode)) {
229 /* make an empty directory */
230 make_bak_dir(fname,backup_dir);
231 do_mkdir(keep_name,file->mode);
232 ret_code=do_rmdir(fname);
234 rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code);
239 if(!kept && preserve_links && S_ISLNK(file->mode)) {
240 extern int safe_symlinks;
241 if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
243 rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
244 keep_name,file->link);
248 make_bak_dir(fname,backup_dir);
249 if(do_symlink(file->link,keep_name) != 0) {
250 rprintf(FERROR,"link %s -> %s : %s\n",keep_name,file->link,strerror(errno));
256 if(!kept && preserve_hard_links && check_hard_link(file)) {
257 if(verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file));
260 if(!kept && !S_ISREG(file->mode)) {
261 rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname);
264 /* move to keep tree if a file */
266 if (!robust_move (fname, keep_name))
267 rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
268 fname, keep_name, strerror(errno));
270 set_perms (keep_name, file, NULL, 0);
275 rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
280 /* main backup switch routine */
281 int make_backup(char *fname)
284 return (keep_backup(fname));
286 return (make_simple_backup(fname));