Allow $RSYNC_TEST_TMP to indicate a good tmp dir for our tests.
[rsync/rsync.git] / backup.c
... / ...
CommitLineData
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
24extern int am_root;
25extern int preserve_acls;
26extern int preserve_xattrs;
27extern int preserve_devices;
28extern int preserve_specials;
29extern int preserve_links;
30extern int safe_symlinks;
31extern int backup_dir_len;
32extern unsigned int backup_dir_remainder;
33extern char backup_dir_buf[MAXPATHLEN];
34extern char *backup_suffix;
35extern char *backup_dir;
36
37/* make a complete pathname for backup file */
38char *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/****************************************************************************
55Create a directory given an absolute path, perms based upon another directory
56path
57****************************************************************************/
58int 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_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_xattr(file, &sx);
117 free_xattr(&sx);
118 }
119#endif
120 set_file_attrs(fbuf, file, NULL, NULL, 0);
121 unmake_file(file);
122 }
123 }
124 *p = '/';
125 p += strlen(p);
126 if (p == end)
127 break;
128 if (mkdir_defmode(fbuf) < 0) {
129 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
130 full_fname(fbuf));
131 return -1;
132 }
133 }
134
135 return 0;
136}
137
138/* Has same return codes as make_backup(). */
139static inline int link_or_rename(const char *from, const char *to,
140 BOOL prefer_rename, STRUCT_STAT *stp)
141{
142 if (S_ISLNK(stp->st_mode)) {
143 if (prefer_rename)
144 goto do_rename;
145#ifndef CAN_HARDLINK_SYMLINK
146 return 0; /* Use copy code. */
147#endif
148 }
149 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode)) {
150 if (prefer_rename)
151 goto do_rename;
152#ifndef CAN_HARDLINK_SPECIAL
153 return 0; /* Use copy code. */
154#endif
155 }
156#ifdef SUPPORT_HARD_LINKS
157 if (!S_ISDIR(stp->st_mode)) {
158 if (do_link(from, to) == 0)
159 return 2;
160 return 0;
161 }
162#endif
163 do_rename:
164 if (do_rename(from, to) == 0) {
165 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
166 /* If someone has hard-linked the file into the backup
167 * dir, rename() might return success but do nothing! */
168 robust_unlink(to); /* Just in case... */
169 }
170 return 1;
171 }
172 return 0;
173}
174
175/* Hard-link, rename, or copy an item to the backup name. Returns 2 if item
176 * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
177int make_backup(const char *fname, BOOL prefer_rename)
178{
179 stat_x sx;
180 struct file_struct *file;
181 int save_preserve_xattrs;
182 char *buf = get_backup_name(fname);
183 int ret = 0;
184
185 if (!buf)
186 return 0;
187
188 init_stat_x(&sx);
189 /* Return success if no file to keep. */
190 if (x_lstat(fname, &sx.st, NULL) < 0)
191 return 1;
192
193 /* Try a hard-link or a rename first. Using rename is not atomic, but
194 * is more efficient than forcing a copy for larger files when no hard-
195 * linking is possible. */
196 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
197 goto success;
198 if (errno == EEXIST) {
199 STRUCT_STAT bakst;
200 if (do_lstat(buf, &bakst) == 0) {
201 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
202 if (delete_item(buf, bakst.st_mode, flags) != 0)
203 return 0;
204 }
205 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
206 goto success;
207 } else if (backup_dir && errno == ENOENT) {
208 /* If the backup dir is missing, try again after making it. */
209 if (make_bak_dir(buf) != 0)
210 return 0;
211 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
212 goto success;
213 }
214
215 /* Fall back to making a copy. */
216 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
217 return 1; /* the file could have disappeared */
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 int save_errno;
238 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) {
239 save_errno = errno ? errno : EINVAL; /* 0 paranoia */
240 if (errno == ENOENT && make_bak_dir(buf) == 0) {
241 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
242 save_errno = errno ? errno : save_errno;
243 else
244 save_errno = 0;
245 }
246 if (save_errno) {
247 rsyserr(FERROR, save_errno, "mknod %s failed",
248 full_fname(buf));
249 }
250 } else
251 save_errno = 0;
252 if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) {
253 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
254 fname);
255 }
256 ret = 2;
257 }
258
259 if (!ret && S_ISDIR(file->mode)) {
260 int ret_code;
261 /* make an empty directory */
262 if (do_mkdir(buf, file->mode) < 0) {
263 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
264 if (errno == ENOENT && make_bak_dir(buf) == 0) {
265 if (do_mkdir(buf, file->mode) < 0)
266 save_errno = errno ? errno : save_errno;
267 else
268 save_errno = 0;
269 }
270 if (save_errno) {
271 rsyserr(FINFO, save_errno, "mkdir %s failed",
272 full_fname(buf));
273 }
274 }
275
276 ret_code = do_rmdir(fname);
277 if (DEBUG_GTE(BACKUP, 1)) {
278 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
279 full_fname(fname), ret_code);
280 }
281 ret = 2;
282 }
283
284#ifdef SUPPORT_LINKS
285 if (!ret && preserve_links && S_ISLNK(file->mode)) {
286 const char *sl = F_SYMLINK(file);
287 if (safe_symlinks && unsafe_symlink(sl, fname)) {
288 if (INFO_GTE(SYMSAFE, 1)) {
289 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
290 full_fname(buf), sl);
291 }
292 ret = 2;
293 } else {
294 if (do_symlink(sl, buf) < 0) {
295 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
296 if (errno == ENOENT && make_bak_dir(buf) == 0) {
297 if (do_symlink(sl, buf) < 0)
298 save_errno = errno ? errno : save_errno;
299 else
300 save_errno = 0;
301 }
302 if (save_errno) {
303 rsyserr(FERROR, save_errno, "link %s -> \"%s\"",
304 full_fname(buf), sl);
305 }
306 }
307 ret = 2;
308 }
309 }
310#endif
311
312 if (!ret && !S_ISREG(file->mode)) {
313 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
314 fname);
315 unmake_file(file);
316 return 2;
317 }
318
319 /* Copy to backup tree if a file. */
320 if (!ret) {
321 if (copy_file(fname, buf, -1, file->mode, 1) < 0) {
322 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
323 full_fname(fname), buf);
324 unmake_file(file);
325 return 0;
326 }
327 ret = 2;
328 }
329
330 save_preserve_xattrs = preserve_xattrs;
331 preserve_xattrs = 0;
332 set_file_attrs(buf, file, NULL, fname, 0);
333 preserve_xattrs = save_preserve_xattrs;
334
335 unmake_file(file);
336
337 success:
338 if (INFO_GTE(BACKUP, 1)) {
339 rprintf(FINFO, "backed up %s to %s\n",
340 fname, buf);
341 }
342 return ret;
343}