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