Prefer send_msg_int() over send_msg() for better debug output.
[rsync/rsync.git] / backup.c
CommitLineData
addf0c4a 1/*
0f78b815
WD
2 * Backup handling code.
3 *
4 * Copyright (C) 1999 Andrew Tridgell
b3bf9b9d 5 * Copyright (C) 2003-2009 Wayne Davison
0f78b815
WD
6 *
7 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
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.
0f78b815
WD
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 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
3d19b4c8
AT
20
21#include "rsync.h"
09ca0d15 22#include "ifuncs.h"
3d19b4c8 23
378a074c 24extern int am_root;
1c3344a1 25extern int preserve_acls;
16edf865 26extern int preserve_xattrs;
378a074c 27extern int preserve_devices;
b5c6a6ae 28extern int preserve_specials;
378a074c 29extern int preserve_links;
377dbd20 30extern int safe_symlinks;
6b2a3d5d 31extern int backup_dir_len;
82ad07c4
WD
32extern unsigned int backup_dir_remainder;
33extern char backup_dir_buf[MAXPATHLEN];
34extern char *backup_suffix;
35extern char *backup_dir;
378a074c 36
3696674b
WD
37/* Create a backup path from the given fname, putting the result into
38 * backup_dir_buf. Any new directories (compared to the prior backup
39 * path) are ensured to exist as directories, replacing anything else
40 * that may be in the way (e.g. a symlink). */
41static BOOL copy_valid_path(const char *fname)
378a074c 42{
3696674b
WD
43 const char *f;
44 int flags;
45 BOOL ret = True;
13710874 46 stat_x sx;
3696674b 47 char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
de0e2250 48
3696674b
WD
49 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
50 if (*b == '/')
51 name = b + 1;
e1ac7791
WD
52 }
53
3696674b
WD
54 if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
55 rprintf(FERROR, "backup filename too long\n");
56 *name = '\0';
57 return False;
58 }
e1ac7791 59
3696674b
WD
60 for ( ; ; name = b + 1) {
61 if ((b = strchr(name, '/')) == NULL)
62 return True;
63 *b = '\0';
de0e2250 64
3696674b
WD
65 if (do_lstat(backup_dir_buf, &sx.st) < 0) {
66 if (errno == ENOENT)
de0e2250 67 break;
3696674b
WD
68 rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
69 *name = '\0';
70 return False;
71 }
72 if (!S_ISDIR(sx.st.st_mode)) {
73 flags = get_del_for_flag(sx.st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
74 if (delete_item(backup_dir_buf, sx.st.st_mode, flags) == 0)
75 break;
76 *name = '\0';
77 return False;
de0e2250 78 }
3696674b
WD
79
80 *b = '/';
47d6a60c 81 }
de0e2250 82
3696674b
WD
83 init_stat_x(&sx);
84
85 for ( ; b; name = b + 1, b = strchr(name, '/')) {
86 *b = '\0';
87
88 if (mkdir_defmode(backup_dir_buf) < 0) {
89 rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
90 *name = '\0';
91 ret = False;
92 break;
93 }
94
95 /* Try to transfer the directory settings of the actual dir
96 * that the files are coming from. */
97 if (x_stat(rel, &sx.st, NULL) < 0)
98 rsyserr(FERROR, errno, "backup stat %s failed", full_fname(rel));
99 else {
100 struct file_struct *file;
101 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
102 continue;
1c3344a1 103#ifdef SUPPORT_ACLS
3696674b
WD
104 if (preserve_acls && !S_ISLNK(file->mode)) {
105 get_acl(rel, &sx);
106 cache_tmp_acl(file, &sx);
107 free_acl(&sx);
108 }
1e999f9f 109#endif
16edf865 110#ifdef SUPPORT_XATTRS
3696674b
WD
111 if (preserve_xattrs) {
112 get_xattr(rel, &sx);
113 cache_tmp_xattr(file, &sx);
114 free_xattr(&sx);
115 }
16edf865 116#endif
3696674b
WD
117 set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
118 unmake_file(file);
119 }
120
121 *b = '/';
122 }
123
cb197514 124#ifdef SUPPORT_ACLS
3696674b 125 uncache_tmp_acls();
cb197514
WD
126#endif
127#ifdef SUPPORT_XATTRS
3696674b 128 uncache_tmp_xattrs();
cb197514 129#endif
3696674b
WD
130
131 return ret;
132}
133
134/* Make a complete pathname for backup file and verify any new path elements. */
135char *get_backup_name(const char *fname)
136{
137 if (backup_dir) {
138 /* copy fname into backup_dir_buf while validating the dirs. */
139 if (copy_valid_path(fname))
140 return backup_dir_buf;
141 return NULL;
142 } else {
143 if (stringjoin(backup_dir_buf, MAXPATHLEN,
144 fname, backup_suffix, NULL) < MAXPATHLEN)
145 return backup_dir_buf;
47d6a60c 146 }
de0e2250 147
3696674b
WD
148 rprintf(FERROR, "backup filename too long\n");
149 return NULL;
378a074c
AT
150}
151
21cddef2
WD
152/* Has same return codes as make_backup(). */
153static inline int link_or_rename(const char *from, const char *to,
154 BOOL prefer_rename, STRUCT_STAT *stp)
66203a98 155{
d735fe20
WD
156#ifdef SUPPORT_HARD_LINKS
157 if (!prefer_rename) {
21cddef2 158#ifndef CAN_HARDLINK_SYMLINK
d735fe20
WD
159 if (S_ISLNK(stp->st_mode))
160 return 0; /* Use copy code. */
21cddef2 161#endif
21cddef2 162#ifndef CAN_HARDLINK_SPECIAL
d735fe20
WD
163 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode))
164 return 0; /* Use copy code. */
21cddef2 165#endif
3696674b
WD
166 if (do_link(from, to) == 0) {
167 if (DEBUG_GTE(BACKUP, 1))
168 rprintf(FINFO, "make_backup: HLINK %s successful.\n", from);
5e2d51ee 169 return 2;
3696674b 170 }
5e2d51ee
WD
171 /* We prefer to rename a regular file rather than copy it. */
172 if (!S_ISREG(stp->st_mode) || errno == EEXIST || errno == EISDIR)
d735fe20 173 return 0;
21cddef2
WD
174 }
175#endif
21cddef2
WD
176 if (do_rename(from, to) == 0) {
177 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
178 /* If someone has hard-linked the file into the backup
179 * dir, rename() might return success but do nothing! */
5e2d51ee 180 robust_unlink(from); /* Just in case... */
f1ca7c44 181 }
3696674b
WD
182 if (DEBUG_GTE(BACKUP, 1))
183 rprintf(FINFO, "make_backup: RENAME %s successful.\n", from);
21cddef2 184 return 1;
f1ca7c44 185 }
62c9e6b3 186 return 0;
de0e2250 187}
66203a98 188
21cddef2
WD
189/* Hard-link, rename, or copy an item to the backup name. Returns 2 if item
190 * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
191int make_backup(const char *fname, BOOL prefer_rename)
66203a98 192{
13710874 193 stat_x sx;
66203a98 194 struct file_struct *file;
21cddef2
WD
195 int save_preserve_xattrs;
196 char *buf = get_backup_name(fname);
197 int ret = 0;
198
199 if (!buf)
200 return 0;
378a074c 201
09ca0d15
WD
202 init_stat_x(&sx);
203 /* Return success if no file to keep. */
9439c0cb 204 if (x_lstat(fname, &sx.st, NULL) < 0)
9fd62d15 205 return 1;
66203a98 206
21cddef2
WD
207 /* Try a hard-link or a rename first. Using rename is not atomic, but
208 * is more efficient than forcing a copy for larger files when no hard-
209 * linking is possible. */
210 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
211 goto success;
d735fe20 212 if (errno == EEXIST || errno == EISDIR) {
21cddef2
WD
213 STRUCT_STAT bakst;
214 if (do_lstat(buf, &bakst) == 0) {
215 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
216 if (delete_item(buf, bakst.st_mode, flags) != 0)
217 return 0;
218 }
219 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
220 goto success;
82ad07c4 221 }
378a074c 222
21cddef2
WD
223 /* Fall back to making a copy. */
224 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
225 return 1; /* the file could have disappeared */
226
1c3344a1
WD
227#ifdef SUPPORT_ACLS
228 if (preserve_acls && !S_ISLNK(file->mode)) {
229 get_acl(fname, &sx);
cb197514 230 cache_tmp_acl(file, &sx);
1c3344a1
WD
231 free_acl(&sx);
232 }
233#endif
16edf865
WD
234#ifdef SUPPORT_XATTRS
235 if (preserve_xattrs) {
236 get_xattr(fname, &sx);
cb197514 237 cache_tmp_xattr(file, &sx);
16edf865
WD
238 free_xattr(&sx);
239 }
240#endif
1c3344a1 241
378a074c 242 /* Check to see if this is a device file, or link */
b5c6a6ae
WD
243 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
244 || (preserve_specials && IS_SPECIAL(file->mode))) {
3696674b
WD
245 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
246 rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf));
247 else if (DEBUG_GTE(BACKUP, 1))
248 rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname);
21cddef2 249 ret = 2;
47d6a60c 250 }
378a074c 251
4f5b0756 252#ifdef SUPPORT_LINKS
21cddef2 253 if (!ret && preserve_links && S_ISLNK(file->mode)) {
82ad07c4 254 const char *sl = F_SYMLINK(file);
e6f3a33c 255 if (safe_symlinks && unsafe_symlink(sl, fname)) {
951e826b 256 if (INFO_GTE(SYMSAFE, 1)) {
47d6a60c 257 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
82ad07c4 258 full_fname(buf), sl);
47d6a60c 259 }
21cddef2 260 ret = 2;
93e28fbd 261 } else {
3696674b
WD
262 if (do_symlink(sl, buf) < 0)
263 rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl);
264 else if (DEBUG_GTE(BACKUP, 1))
265 rprintf(FINFO, "make_backup: SYMLINK %s successful.\n", fname);
21cddef2 266 ret = 2;
47d6a60c 267 }
47d6a60c 268 }
378a074c 269#endif
378a074c 270
21cddef2 271 if (!ret && !S_ISREG(file->mode)) {
3696674b 272 rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname);
82ad07c4 273 unmake_file(file);
cb197514
WD
274#ifdef SUPPORT_ACLS
275 uncache_tmp_acls();
276#endif
277#ifdef SUPPORT_XATTRS
278 uncache_tmp_xattrs();
279#endif
21cddef2 280 return 2;
47d6a60c 281 }
66203a98 282
21cddef2
WD
283 /* Copy to backup tree if a file. */
284 if (!ret) {
3696674b 285 if (copy_file(fname, buf, -1, file->mode) < 0) {
d62bcc17 286 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
45c49b52 287 full_fname(fname), buf);
21cddef2 288 unmake_file(file);
cb197514
WD
289#ifdef SUPPORT_ACLS
290 uncache_tmp_acls();
291#endif
292#ifdef SUPPORT_XATTRS
293 uncache_tmp_xattrs();
294#endif
21cddef2 295 return 0;
47d6a60c 296 }
3696674b
WD
297 if (DEBUG_GTE(BACKUP, 1))
298 rprintf(FINFO, "make_backup: COPY %s successful.\n", fname);
21cddef2 299 ret = 2;
47d6a60c 300 }
21cddef2
WD
301
302 save_preserve_xattrs = preserve_xattrs;
e9489cd6 303 preserve_xattrs = 0;
16edf865 304 set_file_attrs(buf, file, NULL, fname, 0);
e9489cd6 305 preserve_xattrs = save_preserve_xattrs;
21cddef2 306
82ad07c4 307 unmake_file(file);
cb197514
WD
308#ifdef SUPPORT_ACLS
309 uncache_tmp_acls();
310#endif
311#ifdef SUPPORT_XATTRS
312 uncache_tmp_xattrs();
313#endif
378a074c 314
21cddef2 315 success:
3696674b
WD
316 if (INFO_GTE(BACKUP, 1))
317 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
21cddef2 318 return ret;
66203a98 319}