Don't try to backup a file being removed from the backup area.
[rsync/rsync.git] / backup.c
1 /*
2  * Backup handling code.
3  *
4  * Copyright (C) 1999 Andrew Tridgell
5  * Copyright (C) 2003-2009 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 as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, visit the http://fsf.org website.
19  */
20
21 #include "rsync.h"
22 #include "ifuncs.h"
23
24 extern int am_root;
25 extern int preserve_acls;
26 extern int preserve_xattrs;
27 extern int preserve_devices;
28 extern int preserve_specials;
29 extern int preserve_links;
30 extern int safe_symlinks;
31 extern int backup_dir_len;
32 extern unsigned int backup_dir_remainder;
33 extern char backup_dir_buf[MAXPATHLEN];
34 extern char *backup_suffix;
35 extern char *backup_dir;
36
37 /* make a complete pathname for backup file */
38 char *get_backup_name(const char *fname)
39 {
40         if (backup_dir) {
41                 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
42                                fname, backup_suffix, NULL) < backup_dir_remainder)
43                         return backup_dir_buf;
44         } else {
45                 if (stringjoin(backup_dir_buf, MAXPATHLEN,
46                                fname, backup_suffix, NULL) < MAXPATHLEN)
47                         return backup_dir_buf;
48         }
49
50         rprintf(FERROR, "backup filename too long\n");
51         return NULL;
52 }
53
54 /****************************************************************************
55 Create a directory given an absolute path, perms based upon another directory
56 path
57 ****************************************************************************/
58 int make_bak_dir(const char *fullpath)
59 {
60         char fbuf[MAXPATHLEN], *rel, *end, *p;
61         struct file_struct *file;
62         int len = backup_dir_len;
63         stat_x sx;
64
65         while (*fullpath == '.' && fullpath[1] == '/') {
66                 fullpath += 2;
67                 len -= 2;
68         }
69
70         if (strlcpy(fbuf, fullpath, sizeof fbuf) >= sizeof fbuf)
71                 return -1;
72
73         rel = fbuf + len;
74         end = p = rel + strlen(rel);
75
76         /* Try to find an existing dir, starting from the deepest dir. */
77         while (1) {
78                 if (--p == fbuf)
79                         return -1;
80                 if (*p == '/') {
81                         *p = '\0';
82                         if (mkdir_defmode(fbuf) == 0)
83                                 break;
84                         if (errno != ENOENT) {
85                                 rsyserr(FERROR, errno,
86                                         "make_bak_dir mkdir %s failed",
87                                         full_fname(fbuf));
88                                 return -1;
89                         }
90                 }
91         }
92
93         /* Make all the dirs that we didn't find on the way here. */
94         while (1) {
95                 if (p >= rel) {
96                         /* Try to transfer the directory settings of the
97                          * actual dir that the files are coming from. */
98                         init_stat_x(&sx);
99                         if (x_stat(rel, &sx.st, NULL) < 0) {
100                                 rsyserr(FERROR, errno,
101                                         "make_bak_dir stat %s failed",
102                                         full_fname(rel));
103                         } else {
104                                 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
105                                         continue;
106 #ifdef SUPPORT_ACLS
107                                 if (preserve_acls && !S_ISLNK(file->mode)) {
108                                         get_acl(rel, &sx);
109                                         cache_tmp_acl(file, &sx);
110                                         free_acl(&sx);
111                                 }
112 #endif
113 #ifdef SUPPORT_XATTRS
114                                 if (preserve_xattrs) {
115                                         get_xattr(rel, &sx);
116                                         cache_tmp_xattr(file, &sx);
117                                         free_xattr(&sx);
118                                 }
119 #endif
120                                 set_file_attrs(fbuf, file, NULL, NULL, 0);
121                                 unmake_file(file);
122 #ifdef SUPPORT_ACLS
123                                 uncache_tmp_acls();
124 #endif
125 #ifdef SUPPORT_XATTRS
126                                 uncache_tmp_xattrs();
127 #endif
128                         }
129                 }
130                 *p = '/';
131                 p += strlen(p);
132                 if (p == end)
133                         break;
134                 if (mkdir_defmode(fbuf) < 0) {
135                         rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
136                                 full_fname(fbuf));
137                         return -1;
138                 }
139         }
140
141         return 0;
142 }
143
144 /* Has same return codes as make_backup(). */
145 static inline int link_or_rename(const char *from, const char *to,
146                                  BOOL prefer_rename, STRUCT_STAT *stp)
147 {
148         if (S_ISLNK(stp->st_mode)) {
149                 if (prefer_rename)
150                         goto do_rename;
151 #ifndef CAN_HARDLINK_SYMLINK
152                 return 0; /* Use copy code. */
153 #endif
154         }
155         if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode)) {
156                 if (prefer_rename)
157                         goto do_rename;
158 #ifndef CAN_HARDLINK_SPECIAL
159                 return 0; /* Use copy code. */
160 #endif
161         }
162 #ifdef SUPPORT_HARD_LINKS
163         if (!S_ISDIR(stp->st_mode)) {
164                 if (do_link(from, to) == 0)
165                         return 2;
166                 return 0;
167         }
168 #endif
169   do_rename:
170         if (do_rename(from, to) == 0) {
171                 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
172                         /* If someone has hard-linked the file into the backup
173                          * dir, rename() might return success but do nothing! */
174                         robust_unlink(to); /* Just in case... */
175                 }
176                 return 1;
177         }
178         return 0;
179 }
180
181 /* Hard-link, rename, or copy an item to the backup name.  Returns 2 if item
182  * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
183 int make_backup(const char *fname, BOOL prefer_rename)
184 {
185         stat_x sx;
186         struct file_struct *file;
187         int save_preserve_xattrs;
188         char *buf = get_backup_name(fname);
189         int ret = 0;
190
191         if (!buf)
192                 return 0;
193
194         init_stat_x(&sx);
195         /* Return success if no file to keep. */
196         if (x_lstat(fname, &sx.st, NULL) < 0)
197                 return 1;
198
199         /* Try a hard-link or a rename first.  Using rename is not atomic, but
200          * is more efficient than forcing a copy for larger files when no hard-
201          * linking is possible. */
202         if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
203                 goto success;
204         if (errno == EEXIST) {
205                 STRUCT_STAT bakst;
206                 if (do_lstat(buf, &bakst) == 0) {
207                         int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
208                         if (delete_item(buf, bakst.st_mode, flags) != 0)
209                                 return 0;
210                 }
211                 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
212                         goto success;
213         } else if (backup_dir && errno == ENOENT) {
214                 /* If the backup dir is missing, try again after making it. */
215                 if (make_bak_dir(buf) != 0)
216                         return 0;
217                 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
218                         goto success;
219         }
220
221         /* Fall back to making a copy. */
222         if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
223                 return 1; /* the file could have disappeared */
224
225 #ifdef SUPPORT_ACLS
226         if (preserve_acls && !S_ISLNK(file->mode)) {
227                 get_acl(fname, &sx);
228                 cache_tmp_acl(file, &sx);
229                 free_acl(&sx);
230         }
231 #endif
232 #ifdef SUPPORT_XATTRS
233         if (preserve_xattrs) {
234                 get_xattr(fname, &sx);
235                 cache_tmp_xattr(file, &sx);
236                 free_xattr(&sx);
237         }
238 #endif
239
240         /* Check to see if this is a device file, or link */
241         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
242          || (preserve_specials && IS_SPECIAL(file->mode))) {
243                 int save_errno;
244                 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) {
245                         save_errno = errno ? errno : EINVAL; /* 0 paranoia */
246                         if (errno == ENOENT && make_bak_dir(buf) == 0) {
247                                 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
248                                         save_errno = errno ? errno : save_errno;
249                                 else
250                                         save_errno = 0;
251                         }
252                         if (save_errno) {
253                                 rsyserr(FERROR, save_errno, "mknod %s failed",
254                                         full_fname(buf));
255                         }
256                 } else
257                         save_errno = 0;
258                 if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) {
259                         rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
260                                 fname);
261                 }
262                 ret = 2;
263         }
264
265         if (!ret && S_ISDIR(file->mode)) {
266                 int ret_code;
267                 /* make an empty directory */
268                 if (do_mkdir(buf, file->mode) < 0) {
269                         int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
270                         if (errno == ENOENT && make_bak_dir(buf) == 0) {
271                                 if (do_mkdir(buf, file->mode) < 0)
272                                         save_errno = errno ? errno : save_errno;
273                                 else
274                                         save_errno = 0;
275                         }
276                         if (save_errno) {
277                                 rsyserr(FINFO, save_errno, "mkdir %s failed",
278                                         full_fname(buf));
279                         }
280                 }
281
282                 ret_code = do_rmdir(fname);
283                 if (DEBUG_GTE(BACKUP, 1)) {
284                         rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
285                                 full_fname(fname), ret_code);
286                 }
287                 ret = 2;
288         }
289
290 #ifdef SUPPORT_LINKS
291         if (!ret && preserve_links && S_ISLNK(file->mode)) {
292                 const char *sl = F_SYMLINK(file);
293                 if (safe_symlinks && unsafe_symlink(sl, fname)) {
294                         if (INFO_GTE(SYMSAFE, 1)) {
295                                 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
296                                         full_fname(buf), sl);
297                         }
298                         ret = 2;
299                 } else {
300                         if (do_symlink(sl, buf) < 0) {
301                                 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
302                                 if (errno == ENOENT && make_bak_dir(buf) == 0) {
303                                         if (do_symlink(sl, buf) < 0)
304                                                 save_errno = errno ? errno : save_errno;
305                                         else
306                                                 save_errno = 0;
307                                 }
308                                 if (save_errno) {
309                                         rsyserr(FERROR, save_errno, "link %s -> \"%s\"",
310                                                 full_fname(buf), sl);
311                                 }
312                         }
313                         ret = 2;
314                 }
315         }
316 #endif
317
318         if (!ret && !S_ISREG(file->mode)) {
319                 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
320                         fname);
321                 unmake_file(file);
322 #ifdef SUPPORT_ACLS
323                 uncache_tmp_acls();
324 #endif
325 #ifdef SUPPORT_XATTRS
326                 uncache_tmp_xattrs();
327 #endif
328                 return 2;
329         }
330
331         /* Copy to backup tree if a file. */
332         if (!ret) {
333                 if (copy_file(fname, buf, -1, file->mode, 1) < 0) {
334                         rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
335                                 full_fname(fname), buf);
336                         unmake_file(file);
337 #ifdef SUPPORT_ACLS
338                         uncache_tmp_acls();
339 #endif
340 #ifdef SUPPORT_XATTRS
341                         uncache_tmp_xattrs();
342 #endif
343                         return 0;
344                 }
345                 ret = 2;
346         }
347
348         save_preserve_xattrs = preserve_xattrs;
349         preserve_xattrs = 0;
350         set_file_attrs(buf, file, NULL, fname, 0);
351         preserve_xattrs = save_preserve_xattrs;
352
353         unmake_file(file);
354 #ifdef SUPPORT_ACLS
355         uncache_tmp_acls();
356 #endif
357 #ifdef SUPPORT_XATTRS
358         uncache_tmp_xattrs();
359 #endif
360
361   success:
362         if (INFO_GTE(BACKUP, 1)) {
363                 rprintf(FINFO, "backed up %s to %s\n",
364                         fname, buf);
365         }
366         return ret;
367 }