another hang-at-end fix. It looks like we are more sensiitive to
[rsync/rsync.git] / backup.c
CommitLineData
3d19b4c8
AT
1/*
2 Copyright (C) Andrew Tridgell 1999
3
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.
8
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.
13
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.
17*/
18
19/* backup handling code */
20
21#include "rsync.h"
22
23extern int verbose;
24extern char *backup_suffix;
66203a98 25extern char *backup_dir;
3d19b4c8
AT
26
27
378a074c
AT
28extern int am_root;
29extern int preserve_devices;
30extern int preserve_links;
31extern int preserve_hard_links;
32
66203a98
AT
33/* simple backup creates a backup with a suffix in the same directory */
34static int make_simple_backup(char *fname)
3d19b4c8
AT
35{
36 char fnamebak[MAXPATHLEN];
37 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
38 rprintf(FERROR,"backup filename too long\n");
39 return 0;
40 }
41
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));
47 return 0;
48 }
49 } else if (verbose > 1) {
50 rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak);
51 }
52 return 1;
53}
66203a98
AT
54
55
56/* recursively make a directory path */
57static int make_dir(char *name, int mask)
58{
59 char newdir [MAXPATHLEN];
60 char *p, *d;
61
62 /* copy pathname over, look for last '/' */
63 for (p = d = newdir; *name; *d++ = *name++)
64 if (*name == '/')
65 p = d;
66 if (p == newdir)
67 return 0;
68 *p = 0;
69
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))
73 return 0;
74
75 return 1;
76} /* make_dir */
77
78
378a074c
AT
79/****************************************************************************
80Create a directory given an absolute path, perms based upon another directory
81path
82****************************************************************************/
83static int make_bak_dir(char *fname,char *bak_path)
84{
85 STRUCT_STAT st;
86 STRUCT_STAT *st2;
87 char fullpath[MAXPATHLEN];
88 extern int orig_umask;
89 char *p;
90 char *q;
91
92 while(strncmp(bak_path,"./",2)==0) bak_path += 2;
93
94 if(bak_path[strlen(bak_path)-1]!='/') {
95 slprintf(fullpath,sizeof(fullpath),"%s/",bak_path);
96 } else {
97 slprintf(fullpath,sizeof(fullpath),"%s",bak_path);
98 }
99 p=fullpath;
100 q=&fullpath[strlen(fullpath)]; /* End of bak_path string */
101 strcat(fullpath,fname);
102
103 /* Make the directories */
104 while ((p=strchr(p,'/'))) {
105 *p = 0;
106 if(do_lstat(fullpath,&st)!=0) {
107 do_mkdir(fullpath,0777 & ~orig_umask);
108 if(p>q) {
109 if(do_lstat(q,&st)!=0) {
110 rprintf(FERROR,"make_bak_dir stat %s : %s\n",fullpath,strerror(errno));
111 } else {
112 st2=&st;
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));
116 };
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));
119 };
120 };
121 }
122 };
123 *p = '/';
124 p++;
125 }
126 return 0;
127}
128
66203a98
AT
129/* robustly move a file, creating new directory structures if necessary */
130static int robust_move(char *src, char *dst)
131{
132 int keep_trying = 4;
133 int keep_path_extfs = 0;
134 int failed;
135
136 while (keep_trying) {
137 if (keep_path_extfs)
138 failed = copy_file (src, dst, 0755);
139 else
140 failed = robust_rename (src, dst);
141
142 if (failed) {
143 if (verbose > 2)
144 rprintf (FERROR, "robust_move failed: %s(%d)\n",
145 strerror (errno), errno);
146 switch (errno) {
147 /* external filesystem */
148 case EXDEV:
149 keep_path_extfs = 1;
150 keep_trying--;
151 break;
152 /* no directory to write to */
153 case ENOENT:
154 make_dir (dst, 0755);
155 keep_trying--;
156 break;
157 default:
158 keep_trying = 0;
159 } /* switch */
160 } else
161 keep_trying = 0;
162 } /* while */
163 return (!failed);
164} /* robust_move */
165
166
167/* if we have a backup_dir, then we get here from make_backup().
168 We will move the file to be deleted into a parallel directory tree */
169static int keep_backup(char *fname)
170{
378a074c 171
66203a98
AT
172 static int initialised;
173
174 char keep_name [MAXPATHLEN];
175 STRUCT_STAT st;
176 struct file_struct *file;
177
378a074c
AT
178 int kept=0;
179 int ret_code;
180
66203a98
AT
181 if (!initialised) {
182 if (backup_dir[strlen(backup_dir) - 1] == '/')
183 backup_dir[strlen(backup_dir) - 1] = 0;
184 if (verbose > 0)
185 rprintf (FINFO, "backup_dir is %s\n", backup_dir);
186 initialised = 1;
187 }
188
189 /* return if no file to keep */
190#if SUPPORT_LINKS
191 if (do_lstat (fname, &st)) return 1;
192#else
193 if (do_stat (fname, &st)) return 1;
194#endif
195
196 file = make_file (0, fname);
197
378a074c
AT
198 /* make a complete pathname for backup file */
199 if (strlen(backup_dir) + strlen(fname) > (MAXPATHLEN - 1)) {
200 rprintf (FERROR, "keep_backup filename too long\n");
201 return 0;
202 }
203
204 slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
205
206
207#ifdef HAVE_MKNOD
208 /* Check to see if this is a device file, or link */
209 if(IS_DEVICE(file->mode)) {
210 if(am_root && preserve_devices) {
211 make_bak_dir(fname,backup_dir);
212 if(do_mknod(keep_name,file->mode,file->rdev)!=0) {
213 rprintf(FERROR,"mknod %s : %s\n",keep_name,strerror(errno));
214 } else {
215 if(verbose>2)
216 rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname);
217 };
218 };
219 kept=1;
220 do_unlink(fname);
221 };
222#endif
223
224 if(!kept && S_ISDIR(file->mode)) {
225 /* make an empty directory */
226 make_bak_dir(fname,backup_dir);
227 do_mkdir(keep_name,file->mode);
228 ret_code=do_rmdir(fname);
229 if(verbose>2)
230 rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code);
231 kept=1;
232 };
233
234#if SUPPORT_LINKS
235 if(!kept && preserve_links && S_ISLNK(file->mode)) {
236 extern int safe_symlinks;
237 if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
238 if (verbose) {
239 rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
240 keep_name,file->link);
241 }
242 kept=1;
243 }
244 make_bak_dir(fname,backup_dir);
245 if(do_symlink(file->link,keep_name) != 0) {
246 rprintf(FERROR,"link %s -> %s : %s\n",keep_name,file->link,strerror(errno));
247 };
248 do_unlink(fname);
249 kept=1;
250 };
251#endif
252 if(!kept && preserve_hard_links && check_hard_link(file)) {
253 if(verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file));
254 };
255
256 if(!kept && !S_ISREG(file->mode)) {
257 rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname);
258 }
66203a98 259
66203a98 260 /* move to keep tree if a file */
378a074c 261 if(!kept) {
66203a98
AT
262 if (!robust_move (fname, keep_name))
263 rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
264 fname, keep_name, strerror(errno));
378a074c 265 };
66203a98
AT
266 set_perms (keep_name, file, NULL, 0);
267 free_file (file);
268 free (file);
378a074c 269
66203a98
AT
270 if (verbose > 1)
271 rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
272 return 1;
273} /* keep_backup */
274
275
276/* main backup switch routine */
277int make_backup(char *fname)
278{
279 if (backup_dir)
280 return (keep_backup(fname));
281 else
282 return (make_simple_backup(fname));
283}
284