aacac8c398de662446d0760dd25179573da2598c
[rsync/rsync.git] / backup.c
1 /*
2  * Backup handling code.
3  *
4  * Copyright (C) 1999 Andrew Tridgell
5  * Copyright (C) 2003-2007 Wayne Davison
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 3 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, visit the http://fsf.org website.
18  */
19
20 #include "rsync.h"
21
22 extern int verbose;
23 extern int am_root;
24 extern int preserve_acls;
25 extern int preserve_xattrs;
26 extern int preserve_devices;
27 extern int preserve_specials;
28 extern int preserve_links;
29 extern int safe_symlinks;
30 extern int backup_dir_len;
31 extern unsigned int backup_dir_remainder;
32 extern char backup_dir_buf[MAXPATHLEN];
33 extern char *backup_suffix;
34 extern char *backup_dir;
35
36 /* make a complete pathname for backup file */
37 char *get_backup_name(const char *fname)
38 {
39         if (backup_dir) {
40                 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
41                                fname, backup_suffix, NULL) < backup_dir_remainder)
42                         return backup_dir_buf;
43         } else {
44                 if (stringjoin(backup_dir_buf, MAXPATHLEN,
45                                fname, backup_suffix, NULL) < MAXPATHLEN)
46                         return backup_dir_buf;
47         }
48
49         rprintf(FERROR, "backup filename too long\n");
50         return NULL;
51 }
52
53 /* simple backup creates a backup with a suffix in the same directory */
54 static int make_simple_backup(const char *fname)
55 {
56         int rename_errno;
57         const char *fnamebak = get_backup_name(fname);
58
59         if (!fnamebak)
60                 return 0;
61
62         while (1) {
63                 if (do_rename(fname, fnamebak) == 0) {
64                         if (verbose > 1) {
65                                 rprintf(FINFO, "backed up %s to %s\n",
66                                         fname, fnamebak);
67                         }
68                         break;
69                 }
70                 /* cygwin (at least version b19) reports EINVAL */
71                 if (errno == ENOENT || errno == EINVAL)
72                         break;
73
74                 rename_errno = errno;
75                 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
76                         continue;
77                 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
78                         continue;
79
80                 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
81                         fname, fnamebak);
82                 errno = rename_errno;
83                 return 0;
84         }
85
86         return 1;
87 }
88
89
90 /****************************************************************************
91 Create a directory given an absolute path, perms based upon another directory
92 path
93 ****************************************************************************/
94 static int make_bak_dir(char *fullpath)
95 {
96         statx sx;
97         struct file_struct *file;
98         char *rel = fullpath + backup_dir_len;
99         char *end = rel + strlen(rel);
100         char *p = end;
101
102         while (strncmp(fullpath, "./", 2) == 0)
103                 fullpath += 2;
104
105         /* Try to find an existing dir, starting from the deepest dir. */
106         while (1) {
107                 if (--p == fullpath) {
108                         p += strlen(p);
109                         goto failure;
110                 }
111                 if (*p == '/') {
112                         *p = '\0';
113                         if (mkdir_defmode(fullpath) == 0)
114                                 break;
115                         if (errno != ENOENT) {
116                                 rsyserr(FERROR, errno,
117                                         "make_bak_dir mkdir %s failed",
118                                         full_fname(fullpath));
119                                 goto failure;
120                         }
121                 }
122         }
123
124         /* Make all the dirs that we didn't find on the way here. */
125         while (1) {
126                 if (p >= rel) {
127                         /* Try to transfer the directory settings of the
128                          * actual dir that the files are coming from. */
129                         if (x_stat(rel, &sx.st, NULL) < 0) {
130                                 rsyserr(FERROR, errno,
131                                         "make_bak_dir stat %s failed",
132                                         full_fname(rel));
133                         } else {
134 #ifdef SUPPORT_ACLS
135                                 sx.acc_acl = sx.def_acl = NULL;
136 #endif
137 #ifdef SUPPORT_XATTRS
138                                 sx.xattr = NULL;
139 #endif
140                                 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
141                                         continue;
142 #ifdef SUPPORT_ACLS
143                                 if (preserve_acls && !S_ISLNK(file->mode)) {
144                                         get_acl(rel, &sx);
145                                         cache_acl(file, &sx);
146                                         free_acl(&sx);
147                                 }
148 #endif
149 #ifdef SUPPORT_XATTRS
150                                 if (preserve_xattrs) {
151                                         get_xattr(rel, &sx);
152                                         cache_xattr(file, &sx);
153                                         free_xattr(&sx);
154                                 }
155 #endif
156                                 set_file_attrs(fullpath, file, NULL, NULL, 0);
157                                 unmake_file(file);
158                         }
159                 }
160                 *p = '/';
161                 p += strlen(p);
162                 if (p == end)
163                         break;
164                 if (mkdir_defmode(fullpath) < 0) {
165                         rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
166                                 full_fname(fullpath));
167                         goto failure;
168                 }
169         }
170         return 0;
171
172   failure:
173         while (p != end) {
174                 *p = '/';
175                 p += strlen(p);
176         }
177         return -1;
178 }
179
180 /* robustly move a file, creating new directory structures if necessary */
181 static int robust_move(const char *src, char *dst)
182 {
183         if (robust_rename(src, dst, NULL, 0755) < 0
184          && (errno != ENOENT || make_bak_dir(dst) < 0
185           || robust_rename(src, dst, NULL, 0755) < 0))
186                 return -1;
187         return 0;
188 }
189
190
191 /* If we have a --backup-dir, then we get here from make_backup().
192  * We will move the file to be deleted into a parallel directory tree. */
193 static int keep_backup(const char *fname)
194 {
195         statx sx;
196         struct file_struct *file;
197         char *buf;
198         int kept = 0;
199         int ret_code;
200
201         /* return if no file to keep */
202         if (x_lstat(fname, &sx.st, NULL) < 0)
203                 return 1;
204 #ifdef SUPPORT_ACLS
205         sx.acc_acl = sx.def_acl = NULL;
206 #endif
207 #ifdef SUPPORT_XATTRS
208         sx.xattr = NULL;
209 #endif
210
211         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
212                 return 1; /* the file could have disappeared */
213
214         if (!(buf = get_backup_name(fname))) {
215                 unmake_file(file);
216                 return 0;
217         }
218
219 #ifdef SUPPORT_ACLS
220         if (preserve_acls && !S_ISLNK(file->mode)) {
221                 get_acl(fname, &sx);
222                 cache_acl(file, &sx);
223                 free_acl(&sx);
224         }
225 #endif
226 #ifdef SUPPORT_XATTRS
227         if (preserve_xattrs) {
228                 get_xattr(fname, &sx);
229                 cache_xattr(file, &sx);
230                 free_xattr(&sx);
231         }
232 #endif
233
234         /* Check to see if this is a device file, or link */
235         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
236          || (preserve_specials && IS_SPECIAL(file->mode))) {
237                 uint32 *devp = F_RDEV_P(file);
238                 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
239                 do_unlink(buf);
240                 if (do_mknod(buf, file->mode, rdev) < 0
241                     && (errno != ENOENT || make_bak_dir(buf) < 0
242                      || do_mknod(buf, file->mode, rdev) < 0)) {
243                         rsyserr(FERROR, errno, "mknod %s failed",
244                                 full_fname(buf));
245                 } else if (verbose > 2) {
246                         rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
247                                 fname);
248                 }
249                 kept = 1;
250                 do_unlink(fname);
251         }
252
253         if (!kept && S_ISDIR(file->mode)) {
254                 /* make an empty directory */
255                 if (do_mkdir(buf, file->mode) < 0
256                     && (errno != ENOENT || make_bak_dir(buf) < 0
257                      || do_mkdir(buf, file->mode) < 0)) {
258                         rsyserr(FINFO, errno, "mkdir %s failed",
259                                 full_fname(buf));
260                 }
261
262                 ret_code = do_rmdir(fname);
263                 if (verbose > 2) {
264                         rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
265                                 full_fname(fname), ret_code);
266                 }
267                 kept = 1;
268         }
269
270 #ifdef SUPPORT_LINKS
271         if (!kept && preserve_links && S_ISLNK(file->mode)) {
272                 const char *sl = F_SYMLINK(file);
273                 if (safe_symlinks && unsafe_symlink(sl, buf)) {
274                         if (verbose) {
275                                 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
276                                         full_fname(buf), sl);
277                         }
278                         kept = 1;
279                 } else {
280                         do_unlink(buf);
281                         if (do_symlink(sl, buf) < 0
282                             && (errno != ENOENT || make_bak_dir(buf) < 0
283                              || do_symlink(sl, buf) < 0)) {
284                                 rsyserr(FERROR, errno, "link %s -> \"%s\"",
285                                         full_fname(buf), sl);
286                         }
287                         do_unlink(fname);
288                         kept = 1;
289                 }
290         }
291 #endif
292
293         if (!kept && !S_ISREG(file->mode)) {
294                 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
295                         fname);
296                 unmake_file(file);
297                 return 1;
298         }
299
300         /* move to keep tree if a file */
301         if (!kept) {
302                 if (robust_move(fname, buf) != 0) {
303                         rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
304                                 full_fname(fname), buf);
305                 } else if (sx.st.st_nlink > 1) {
306                         /* If someone has hard-linked the file into the backup
307                          * dir, rename() might return success but do nothing! */
308                         robust_unlink(fname); /* Just in case... */
309                 }
310         }
311         set_file_attrs(buf, file, NULL, fname, 0);
312         unmake_file(file);
313
314         if (verbose > 1) {
315                 rprintf(FINFO, "backed up %s to %s\n",
316                         fname, buf);
317         }
318         return 1;
319 }
320
321
322 /* main backup switch routine */
323 int make_backup(const char *fname)
324 {
325         if (backup_dir)
326                 return keep_backup(fname);
327         return make_simple_backup(fname);
328 }