Improved link_or_rename() to handle prefer_rename better.
[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
c94e4afb 37/* make a complete pathname for backup file */
45d8bfe0 38char *get_backup_name(const char *fname)
c94e4afb 39{
c94e4afb
WD
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 {
fb22c277
WD
45 if (stringjoin(backup_dir_buf, MAXPATHLEN,
46 fname, backup_suffix, NULL) < MAXPATHLEN)
47 return backup_dir_buf;
c94e4afb
WD
48 }
49
50 rprintf(FERROR, "backup filename too long\n");
51 return NULL;
52}
53
378a074c
AT
54/****************************************************************************
55Create a directory given an absolute path, perms based upon another directory
56path
57****************************************************************************/
e1ac7791 58int make_bak_dir(const char *fullpath)
378a074c 59{
e1ac7791 60 char fbuf[MAXPATHLEN], *rel, *end, *p;
1c3344a1 61 struct file_struct *file;
e1ac7791 62 int len = backup_dir_len;
13710874 63 stat_x sx;
de0e2250 64
e1ac7791 65 while (*fullpath == '.' && fullpath[1] == '/') {
de0e2250 66 fullpath += 2;
e1ac7791
WD
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);
de0e2250
WD
75
76 /* Try to find an existing dir, starting from the deepest dir. */
77 while (1) {
e1ac7791
WD
78 if (--p == fbuf)
79 return -1;
de0e2250
WD
80 if (*p == '/') {
81 *p = '\0';
e1ac7791 82 if (mkdir_defmode(fbuf) == 0)
de0e2250
WD
83 break;
84 if (errno != ENOENT) {
d62bcc17
WD
85 rsyserr(FERROR, errno,
86 "make_bak_dir mkdir %s failed",
e1ac7791
WD
87 full_fname(fbuf));
88 return -1;
de0e2250
WD
89 }
90 }
47d6a60c 91 }
de0e2250
WD
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. */
09ca0d15 98 init_stat_x(&sx);
9439c0cb 99 if (x_stat(rel, &sx.st, NULL) < 0) {
d62bcc17
WD
100 rsyserr(FERROR, errno,
101 "make_bak_dir stat %s failed",
102 full_fname(rel));
de0e2250 103 } else {
1c3344a1
WD
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);
cb197514 109 cache_tmp_acl(file, &sx);
1c3344a1
WD
110 free_acl(&sx);
111 }
1e999f9f 112#endif
16edf865
WD
113#ifdef SUPPORT_XATTRS
114 if (preserve_xattrs) {
115 get_xattr(rel, &sx);
cb197514 116 cache_tmp_xattr(file, &sx);
16edf865
WD
117 free_xattr(&sx);
118 }
119#endif
e1ac7791 120 set_file_attrs(fbuf, file, NULL, NULL, 0);
b13505da 121 unmake_file(file);
cb197514
WD
122#ifdef SUPPORT_ACLS
123 uncache_tmp_acls();
124#endif
125#ifdef SUPPORT_XATTRS
126 uncache_tmp_xattrs();
127#endif
47d6a60c
S
128 }
129 }
de0e2250
WD
130 *p = '/';
131 p += strlen(p);
132 if (p == end)
133 break;
e1ac7791 134 if (mkdir_defmode(fbuf) < 0) {
d62bcc17 135 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
e1ac7791
WD
136 full_fname(fbuf));
137 return -1;
de0e2250 138 }
47d6a60c 139 }
de0e2250 140
e1ac7791 141 return 0;
378a074c
AT
142}
143
21cddef2
WD
144/* Has same return codes as make_backup(). */
145static inline int link_or_rename(const char *from, const char *to,
146 BOOL prefer_rename, STRUCT_STAT *stp)
66203a98 147{
d735fe20
WD
148#ifdef SUPPORT_HARD_LINKS
149 if (!prefer_rename) {
21cddef2 150#ifndef CAN_HARDLINK_SYMLINK
d735fe20
WD
151 if (S_ISLNK(stp->st_mode))
152 return 0; /* Use copy code. */
21cddef2 153#endif
21cddef2 154#ifndef CAN_HARDLINK_SPECIAL
d735fe20
WD
155 if (IS_SPECIAL(stp->st_mode) || IS_DEVICE(stp->st_mode))
156 return 0; /* Use copy code. */
21cddef2 157#endif
d735fe20
WD
158 if (!S_ISDIR(stp->st_mode)) {
159 if (do_link(from, to) == 0)
160 return 2;
161 return 0;
162 }
21cddef2
WD
163 }
164#endif
21cddef2
WD
165 if (do_rename(from, to) == 0) {
166 if (stp->st_nlink > 1 && !S_ISDIR(stp->st_mode)) {
167 /* If someone has hard-linked the file into the backup
168 * dir, rename() might return success but do nothing! */
169 robust_unlink(to); /* Just in case... */
f1ca7c44 170 }
21cddef2 171 return 1;
f1ca7c44 172 }
62c9e6b3 173 return 0;
de0e2250 174}
66203a98 175
21cddef2
WD
176/* Hard-link, rename, or copy an item to the backup name. Returns 2 if item
177 * was duplicated into backup area, 1 if item was moved, or 0 for failure.*/
178int make_backup(const char *fname, BOOL prefer_rename)
66203a98 179{
13710874 180 stat_x sx;
66203a98 181 struct file_struct *file;
21cddef2
WD
182 int save_preserve_xattrs;
183 char *buf = get_backup_name(fname);
184 int ret = 0;
185
186 if (!buf)
187 return 0;
378a074c 188
09ca0d15
WD
189 init_stat_x(&sx);
190 /* Return success if no file to keep. */
9439c0cb 191 if (x_lstat(fname, &sx.st, NULL) < 0)
9fd62d15 192 return 1;
66203a98 193
21cddef2
WD
194 /* Try a hard-link or a rename first. Using rename is not atomic, but
195 * is more efficient than forcing a copy for larger files when no hard-
196 * linking is possible. */
197 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
198 goto success;
d735fe20 199 if (errno == EEXIST || errno == EISDIR) {
21cddef2
WD
200 STRUCT_STAT bakst;
201 if (do_lstat(buf, &bakst) == 0) {
202 int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
203 if (delete_item(buf, bakst.st_mode, flags) != 0)
204 return 0;
205 }
206 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
207 goto success;
208 } else if (backup_dir && errno == ENOENT) {
209 /* If the backup dir is missing, try again after making it. */
210 if (make_bak_dir(buf) != 0)
211 return 0;
212 if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0)
213 goto success;
82ad07c4 214 }
378a074c 215
21cddef2
WD
216 /* Fall back to making a copy. */
217 if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS)))
218 return 1; /* the file could have disappeared */
219
1c3344a1
WD
220#ifdef SUPPORT_ACLS
221 if (preserve_acls && !S_ISLNK(file->mode)) {
222 get_acl(fname, &sx);
cb197514 223 cache_tmp_acl(file, &sx);
1c3344a1
WD
224 free_acl(&sx);
225 }
226#endif
16edf865
WD
227#ifdef SUPPORT_XATTRS
228 if (preserve_xattrs) {
229 get_xattr(fname, &sx);
cb197514 230 cache_tmp_xattr(file, &sx);
16edf865
WD
231 free_xattr(&sx);
232 }
233#endif
1c3344a1 234
378a074c 235 /* Check to see if this is a device file, or link */
b5c6a6ae
WD
236 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
237 || (preserve_specials && IS_SPECIAL(file->mode))) {
88e05f84 238 int save_errno;
a64f19e2 239 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) {
88e05f84 240 save_errno = errno ? errno : EINVAL; /* 0 paranoia */
f1ca7c44 241 if (errno == ENOENT && make_bak_dir(buf) == 0) {
a64f19e2 242 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
f1ca7c44
WD
243 save_errno = errno ? errno : save_errno;
244 else
245 save_errno = 0;
246 }
247 if (save_errno) {
248 rsyserr(FERROR, save_errno, "mknod %s failed",
249 full_fname(buf));
250 }
88e05f84
WD
251 } else
252 save_errno = 0;
951e826b 253 if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) {
93e28fbd 254 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
45c49b52 255 fname);
47d6a60c 256 }
21cddef2 257 ret = 2;
47d6a60c 258 }
378a074c 259
21cddef2
WD
260 if (!ret && S_ISDIR(file->mode)) {
261 int ret_code;
378a074c 262 /* make an empty directory */
f1ca7c44
WD
263 if (do_mkdir(buf, file->mode) < 0) {
264 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
265 if (errno == ENOENT && make_bak_dir(buf) == 0) {
266 if (do_mkdir(buf, file->mode) < 0)
267 save_errno = errno ? errno : save_errno;
268 else
269 save_errno = 0;
270 }
271 if (save_errno) {
272 rsyserr(FINFO, save_errno, "mkdir %s failed",
273 full_fname(buf));
274 }
377dbd20 275 }
47d6a60c 276
377dbd20 277 ret_code = do_rmdir(fname);
951e826b 278 if (DEBUG_GTE(BACKUP, 1)) {
ea42541f
WD
279 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
280 full_fname(fname), ret_code);
47d6a60c 281 }
21cddef2 282 ret = 2;
47d6a60c 283 }
378a074c 284
4f5b0756 285#ifdef SUPPORT_LINKS
21cddef2 286 if (!ret && preserve_links && S_ISLNK(file->mode)) {
82ad07c4 287 const char *sl = F_SYMLINK(file);
e6f3a33c 288 if (safe_symlinks && unsafe_symlink(sl, fname)) {
951e826b 289 if (INFO_GTE(SYMSAFE, 1)) {
47d6a60c 290 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
82ad07c4 291 full_fname(buf), sl);
47d6a60c 292 }
21cddef2 293 ret = 2;
93e28fbd 294 } else {
f1ca7c44
WD
295 if (do_symlink(sl, buf) < 0) {
296 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
297 if (errno == ENOENT && make_bak_dir(buf) == 0) {
298 if (do_symlink(sl, buf) < 0)
299 save_errno = errno ? errno : save_errno;
300 else
301 save_errno = 0;
302 }
303 if (save_errno) {
304 rsyserr(FERROR, save_errno, "link %s -> \"%s\"",
305 full_fname(buf), sl);
306 }
93e28fbd 307 }
21cddef2 308 ret = 2;
47d6a60c 309 }
47d6a60c 310 }
378a074c 311#endif
378a074c 312
21cddef2 313 if (!ret && !S_ISREG(file->mode)) {
47d6a60c 314 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
45c49b52 315 fname);
82ad07c4 316 unmake_file(file);
cb197514
WD
317#ifdef SUPPORT_ACLS
318 uncache_tmp_acls();
319#endif
320#ifdef SUPPORT_XATTRS
321 uncache_tmp_xattrs();
322#endif
21cddef2 323 return 2;
47d6a60c 324 }
66203a98 325
21cddef2
WD
326 /* Copy to backup tree if a file. */
327 if (!ret) {
328 if (copy_file(fname, buf, -1, file->mode, 1) < 0) {
d62bcc17 329 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
45c49b52 330 full_fname(fname), buf);
21cddef2 331 unmake_file(file);
cb197514
WD
332#ifdef SUPPORT_ACLS
333 uncache_tmp_acls();
334#endif
335#ifdef SUPPORT_XATTRS
336 uncache_tmp_xattrs();
337#endif
21cddef2 338 return 0;
47d6a60c 339 }
21cddef2 340 ret = 2;
47d6a60c 341 }
21cddef2
WD
342
343 save_preserve_xattrs = preserve_xattrs;
e9489cd6 344 preserve_xattrs = 0;
16edf865 345 set_file_attrs(buf, file, NULL, fname, 0);
e9489cd6 346 preserve_xattrs = save_preserve_xattrs;
21cddef2 347
82ad07c4 348 unmake_file(file);
cb197514
WD
349#ifdef SUPPORT_ACLS
350 uncache_tmp_acls();
351#endif
352#ifdef SUPPORT_XATTRS
353 uncache_tmp_xattrs();
354#endif
378a074c 355
21cddef2 356 success:
951e826b 357 if (INFO_GTE(BACKUP, 1)) {
4875d6b6 358 rprintf(FINFO, "backed up %s to %s\n",
45c49b52 359 fname, buf);
4875d6b6 360 }
21cddef2 361 return ret;
66203a98 362}