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