689bfad505556782491c828c16a98c64f9383c3c
[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 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;
30
31 extern int am_root;
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;
37
38 /* make a complete pathname for backup file */
39 char *get_backup_name(char *fname)
40 {
41         if (backup_dir) {
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;
45         } else {
46                 if (stringjoin(backup_dir_buf, MAXPATHLEN,
47                                fname, backup_suffix, NULL) < MAXPATHLEN)
48                         return backup_dir_buf;
49         }
50
51         rprintf(FERROR, "backup filename too long\n");
52         return NULL;
53 }
54
55 /* simple backup creates a backup with a suffix in the same directory */
56 static int make_simple_backup(char *fname)
57 {
58         char *fnamebak = get_backup_name(fname);
59
60         if (!fnamebak)
61                 return 0;
62
63         if (do_rename(fname, fnamebak) != 0) {
64                 /* cygwin (at least version b19) reports EINVAL */
65                 if (errno != ENOENT && errno != EINVAL) {
66                         rsyserr(FERROR, errno,
67                                 "rename %s to backup %s", fname, fnamebak);
68                         return 0;
69                 }
70         } else if (verbose > 1) {
71                 rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak);
72         }
73         return 1;
74 }
75
76
77 /****************************************************************************
78 Create a directory given an absolute path, perms based upon another directory
79 path
80 ****************************************************************************/
81 static int make_bak_dir(char *fullpath)
82 {
83         STRUCT_STAT st;
84         char *rel = fullpath + backup_dir_len;
85         char *end = rel + strlen(rel);
86         char *p = end;
87
88         while (strncmp(fullpath, "./", 2) == 0)
89                 fullpath += 2;
90
91         /* Try to find an existing dir, starting from the deepest dir. */
92         while (1) {
93                 if (--p == fullpath) {
94                         p += strlen(p);
95                         goto failure;
96                 }
97                 if (*p == '/') {
98                         *p = '\0';
99                         if (do_mkdir(fullpath, 0777 & ~orig_umask) == 0)
100                                 break;
101                         if (errno != ENOENT) {
102                                 rsyserr(FERROR, errno,
103                                         "make_bak_dir mkdir %s failed",
104                                         full_fname(fullpath));
105                                 goto failure;
106                         }
107                 }
108         }
109
110         /* Make all the dirs that we didn't find on the way here. */
111         while (1) {
112                 if (p >= rel) {
113                         /* Try to transfer the directory settings of the
114                          * actual dir that the files are coming from. */
115                         if (do_stat(rel, &st) < 0) {
116                                 rsyserr(FERROR, errno,
117                                         "make_bak_dir stat %s failed",
118                                         full_fname(rel));
119                         } else {
120                                 do_lchown(fullpath, st.st_uid, st.st_gid);
121                                 do_chmod(fullpath, st.st_mode);
122                         }
123                 }
124                 *p = '/';
125                 p += strlen(p);
126                 if (p == end)
127                         break;
128                 if (do_mkdir(fullpath, 0777 & ~orig_umask) < 0) {
129                         rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
130                                 full_fname(fullpath));
131                         goto failure;
132                 }
133         }
134         return 0;
135
136 failure:
137         while (p != end) {
138                 *p = '/';
139                 p += strlen(p);
140         }
141         return -1;
142 }
143
144 /* robustly move a file, creating new directory structures if necessary */
145 static int robust_move(char *src, char *dst)
146 {
147         if (robust_rename(src, dst, 0755) < 0 && (errno != ENOENT
148             || make_bak_dir(dst) < 0 || robust_rename(src, dst, 0755) < 0))
149                 return -1;
150         return 0;
151 }
152
153
154 /* If we have a --backup-dir, then we get here from make_backup().
155  * We will move the file to be deleted into a parallel directory tree. */
156 static int keep_backup(char *fname)
157 {
158         STRUCT_STAT st;
159         struct file_struct *file;
160         char *buf;
161         int kept = 0;
162         int ret_code;
163
164         /* return if no file to keep */
165         if (do_lstat(fname, &st) < 0)
166                 return 1;
167
168         if (!(file = make_file(fname, NULL, NO_FILTERS)))
169                 return 1; /* the file could have disappeared */
170
171         if (!(buf = get_backup_name(fname)))
172                 return 0;
173
174         /* Check to see if this is a device file, or link */
175         if (IS_DEVICE(file->mode)) {
176                 if (am_root && preserve_devices) {
177                         if (do_mknod(buf, file->mode, file->u.rdev) < 0
178                             && (errno != ENOENT || make_bak_dir(buf) < 0
179                              || do_mknod(buf, file->mode, file->u.rdev) < 0)) {
180                                 rsyserr(FERROR, errno, "mknod %s failed",
181                                         full_fname(buf));
182                         } else if (verbose > 2) {
183                                 rprintf(FINFO,
184                                         "make_backup: DEVICE %s successful.\n",
185                                         fname);
186                         }
187                 }
188                 kept = 1;
189                 do_unlink(fname);
190         }
191
192         if (!kept && S_ISDIR(file->mode)) {
193                 /* make an empty directory */
194                 if (do_mkdir(buf, file->mode) < 0
195                     && (errno != ENOENT || make_bak_dir(buf) < 0
196                      || do_mkdir(buf, file->mode) < 0)) {
197                         rsyserr(FINFO, errno, "mkdir %s failed",
198                                 full_fname(buf));
199                 }
200
201                 ret_code = do_rmdir(fname);
202                 if (verbose > 2) {
203                         rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
204                                 full_fname(fname), ret_code);
205                 }
206                 kept = 1;
207         }
208
209 #if SUPPORT_LINKS
210         if (!kept && preserve_links && S_ISLNK(file->mode)) {
211                 if (safe_symlinks && unsafe_symlink(file->u.link, buf)) {
212                         if (verbose) {
213                                 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
214                                         full_fname(buf), file->u.link);
215                         }
216                         kept = 1;
217                 }
218                 if (do_symlink(file->u.link, buf) < 0
219                     && (errno != ENOENT || make_bak_dir(buf) < 0
220                      || do_symlink(file->u.link, buf) < 0)) {
221                         rsyserr(FERROR, errno, "link %s -> \"%s\"",
222                                 full_fname(buf), file->u.link);
223                 }
224                 do_unlink(fname);
225                 kept = 1;
226         }
227 #endif
228
229         if (!kept && !S_ISREG(file->mode)) {
230                 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
231                         fname);
232                 return 1;
233         }
234
235         /* move to keep tree if a file */
236         if (!kept) {
237                 if (robust_move(fname, buf) != 0) {
238                         rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
239                                 full_fname(fname), buf);
240                 } else if (st.st_nlink > 1) {
241                         /* If someone has hard-linked the file into the backup
242                          * dir, rename() might return success but do nothing! */
243                         robust_unlink(fname); /* Just in case... */
244                 }
245         }
246         set_perms(buf, file, NULL, 0);
247         free(file);
248
249         if (verbose > 1)
250                 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
251         return 1;
252 }
253
254
255 /* main backup switch routine */
256 int make_backup(char *fname)
257 {
258         if (backup_dir)
259                 return keep_backup(fname);
260         return make_simple_backup(fname);
261 }