I don't like automatic header dependencies
[rsync/rsync.git] / backup.c
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
23 extern int verbose;
24 extern char *backup_suffix;
25 extern char *backup_dir;
26
27
28 extern int am_root;
29 extern int preserve_devices;
30 extern int preserve_links;
31 extern int preserve_hard_links;
32
33 /* simple backup creates a backup with a suffix in the same directory */
34 static int make_simple_backup(char *fname)
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 }
54
55
56 /* recursively make a directory path */
57 static 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
79 /****************************************************************************
80 Create a directory given an absolute path, perms based upon another directory
81 path
82 ****************************************************************************/
83 static 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
129 /* robustly move a file, creating new directory structures if necessary */
130 static 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                         if (!failed) {
140                                 do_unlink(src);
141                         }
142                 } else {
143                         failed = robust_rename (src, dst);
144                 }
145
146                 if (failed) {
147                         if (verbose > 2)
148                                 rprintf (FERROR, "robust_move failed: %s(%d)\n",
149                                         strerror (errno), errno);
150                         switch (errno) {
151                                 /* external filesystem */
152                                 case EXDEV:
153                                         keep_path_extfs = 1;
154                                         keep_trying--;
155                                         break;
156                                 /* no directory to write to */
157                                 case ENOENT:
158                                         make_dir (dst, 0755);
159                                         keep_trying--;
160                                         break;
161                                 default:
162                                         keep_trying = 0;
163                         } /* switch */
164                 } else
165                         keep_trying = 0;
166         } /* while */
167         return (!failed);
168 } /* robust_move */
169
170
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)
174 {
175
176         static int initialised;
177
178         char keep_name [MAXPATHLEN];
179         STRUCT_STAT st;
180         struct file_struct *file;
181
182         int kept=0;
183         int ret_code;
184
185         if (!initialised) {
186                 if (backup_dir[strlen(backup_dir) - 1] == '/')
187                         backup_dir[strlen(backup_dir) - 1] = 0;
188                 if (verbose > 0)
189                         rprintf (FINFO, "backup_dir is %s\n", backup_dir);
190                 initialised = 1;
191         }
192
193         /* return if no file to keep */
194 #if SUPPORT_LINKS
195         if (do_lstat (fname, &st)) return 1;
196 #else
197         if (do_stat (fname, &st)) return 1;
198 #endif
199
200         file = make_file (-1, fname, 0);
201
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");
205                 return 0;
206         }
207
208         slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
209
210
211 #ifdef HAVE_MKNOD
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));
218                         } else {
219                                 if(verbose>2)
220                                         rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname);
221                         };
222                 };
223                 kept=1;
224                 do_unlink(fname);
225         };
226 #endif
227
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);
233                 if(verbose>2)
234                         rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code);
235                 kept=1;
236         };
237
238 #if SUPPORT_LINKS
239         if(!kept && preserve_links && S_ISLNK(file->mode)) {
240                 extern int safe_symlinks;
241                 if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
242                         if (verbose) {
243                                 rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
244                                         keep_name,file->link);
245                         }
246                         kept=1;
247                 }
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));
251                 };
252                 do_unlink(fname);
253                 kept=1;
254         };
255 #endif
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));
258         };
259
260         if(!kept && !S_ISREG(file->mode)) {
261                 rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname);
262         }
263
264         /* move to keep tree if a file */
265         if(!kept) {
266                 if (!robust_move (fname, keep_name))
267                         rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
268                                 fname, keep_name, strerror(errno));
269         };
270         set_perms (keep_name, file, NULL, 0);
271         free_file (file);
272         free (file);
273
274         if (verbose > 1)
275                 rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
276         return 1;
277 } /* keep_backup */
278
279
280 /* main backup switch routine */
281 int make_backup(char *fname)
282 {
283         if (backup_dir)
284                 return (keep_backup(fname));
285         else
286                 return (make_simple_backup(fname));
287 }
288