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