A little tidying up to follow my preferred style.
[rsync/rsync.git] / backup.c
CommitLineData
addf0c4a 1/*
0f78b815
WD
2 * Backup handling code.
3 *
4 * Copyright (C) 1999 Andrew Tridgell
d3d07a5e 5 * Copyright (C) 2003-2008 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"
22
378a074c 23extern int am_root;
1c3344a1 24extern int preserve_acls;
16edf865 25extern int preserve_xattrs;
378a074c 26extern int preserve_devices;
b5c6a6ae 27extern int preserve_specials;
378a074c 28extern int preserve_links;
377dbd20 29extern int safe_symlinks;
6b2a3d5d 30extern int backup_dir_len;
82ad07c4
WD
31extern unsigned int backup_dir_remainder;
32extern char backup_dir_buf[MAXPATHLEN];
33extern char *backup_suffix;
34extern char *backup_dir;
378a074c 35
c94e4afb 36/* make a complete pathname for backup file */
45d8bfe0 37char *get_backup_name(const char *fname)
c94e4afb 38{
c94e4afb
WD
39 if (backup_dir) {
40 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
41 fname, backup_suffix, NULL) < backup_dir_remainder)
42 return backup_dir_buf;
43 } else {
fb22c277
WD
44 if (stringjoin(backup_dir_buf, MAXPATHLEN,
45 fname, backup_suffix, NULL) < MAXPATHLEN)
46 return backup_dir_buf;
c94e4afb
WD
47 }
48
49 rprintf(FERROR, "backup filename too long\n");
50 return NULL;
51}
52
66203a98 53/* simple backup creates a backup with a suffix in the same directory */
45d8bfe0 54static int make_simple_backup(const char *fname)
3d19b4c8 55{
ba679d51 56 int rename_errno;
45d8bfe0 57 const char *fnamebak = get_backup_name(fname);
5d2a7071 58
c94e4afb 59 if (!fnamebak)
3d19b4c8 60 return 0;
3d19b4c8 61
ba679d51
WD
62 while (1) {
63 if (do_rename(fname, fnamebak) == 0) {
951e826b 64 if (INFO_GTE(BACKUP, 1)) {
ba679d51 65 rprintf(FINFO, "backed up %s to %s\n",
45c49b52 66 fname, fnamebak);
ba679d51
WD
67 }
68 break;
3d19b4c8 69 }
ba679d51
WD
70 /* cygwin (at least version b19) reports EINVAL */
71 if (errno == ENOENT || errno == EINVAL)
72 break;
73
74 rename_errno = errno;
75 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
76 continue;
77 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
78 continue;
79
80 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
45c49b52 81 fname, fnamebak);
ba679d51
WD
82 errno = rename_errno;
83 return 0;
3d19b4c8 84 }
ba679d51 85
3d19b4c8
AT
86 return 1;
87}
66203a98
AT
88
89
378a074c
AT
90/****************************************************************************
91Create a directory given an absolute path, perms based upon another directory
92path
93****************************************************************************/
e1ac7791 94int make_bak_dir(const char *fullpath)
378a074c 95{
e1ac7791 96 char fbuf[MAXPATHLEN], *rel, *end, *p;
1c3344a1 97 struct file_struct *file;
e1ac7791 98 int len = backup_dir_len;
13710874 99 stat_x sx;
de0e2250 100
e1ac7791 101 while (*fullpath == '.' && fullpath[1] == '/') {
de0e2250 102 fullpath += 2;
e1ac7791
WD
103 len -= 2;
104 }
105
106 if (strlcpy(fbuf, fullpath, sizeof fbuf) >= sizeof fbuf)
107 return -1;
108
109 rel = fbuf + len;
110 end = p = rel + strlen(rel);
de0e2250
WD
111
112 /* Try to find an existing dir, starting from the deepest dir. */
113 while (1) {
e1ac7791
WD
114 if (--p == fbuf)
115 return -1;
de0e2250
WD
116 if (*p == '/') {
117 *p = '\0';
e1ac7791 118 if (mkdir_defmode(fbuf) == 0)
de0e2250
WD
119 break;
120 if (errno != ENOENT) {
d62bcc17
WD
121 rsyserr(FERROR, errno,
122 "make_bak_dir mkdir %s failed",
e1ac7791
WD
123 full_fname(fbuf));
124 return -1;
de0e2250
WD
125 }
126 }
47d6a60c 127 }
de0e2250
WD
128
129 /* Make all the dirs that we didn't find on the way here. */
130 while (1) {
131 if (p >= rel) {
132 /* Try to transfer the directory settings of the
133 * actual dir that the files are coming from. */
9439c0cb 134 if (x_stat(rel, &sx.st, NULL) < 0) {
d62bcc17
WD
135 rsyserr(FERROR, errno,
136 "make_bak_dir stat %s failed",
137 full_fname(rel));
de0e2250 138 } else {
1c3344a1
WD
139#ifdef SUPPORT_ACLS
140 sx.acc_acl = sx.def_acl = NULL;
16edf865
WD
141#endif
142#ifdef SUPPORT_XATTRS
143 sx.xattr = NULL;
1c3344a1
WD
144#endif
145 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
146 continue;
147#ifdef SUPPORT_ACLS
148 if (preserve_acls && !S_ISLNK(file->mode)) {
149 get_acl(rel, &sx);
150 cache_acl(file, &sx);
151 free_acl(&sx);
152 }
1e999f9f 153#endif
16edf865
WD
154#ifdef SUPPORT_XATTRS
155 if (preserve_xattrs) {
156 get_xattr(rel, &sx);
157 cache_xattr(file, &sx);
158 free_xattr(&sx);
159 }
160#endif
e1ac7791 161 set_file_attrs(fbuf, file, NULL, NULL, 0);
b13505da 162 unmake_file(file);
47d6a60c
S
163 }
164 }
de0e2250
WD
165 *p = '/';
166 p += strlen(p);
167 if (p == end)
168 break;
e1ac7791 169 if (mkdir_defmode(fbuf) < 0) {
d62bcc17 170 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
e1ac7791
WD
171 full_fname(fbuf));
172 return -1;
de0e2250 173 }
47d6a60c 174 }
de0e2250 175
e1ac7791 176 return 0;
378a074c
AT
177}
178
66203a98 179/* robustly move a file, creating new directory structures if necessary */
45d8bfe0 180static int robust_move(const char *src, char *dst)
66203a98 181{
f1ca7c44
WD
182 if (robust_rename(src, dst, NULL, 0755) < 0) {
183 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
184 if (errno == ENOENT && make_bak_dir(dst) == 0) {
185 if (robust_rename(src, dst, NULL, 0755) < 0)
186 save_errno = errno ? errno : save_errno;
187 else
188 save_errno = 0;
189 }
190 if (save_errno) {
191 errno = save_errno;
192 return -1;
193 }
194 }
62c9e6b3 195 return 0;
de0e2250 196}
66203a98
AT
197
198
de0e2250
WD
199/* If we have a --backup-dir, then we get here from make_backup().
200 * We will move the file to be deleted into a parallel directory tree. */
45d8bfe0 201static int keep_backup(const char *fname)
66203a98 202{
13710874 203 stat_x sx;
66203a98 204 struct file_struct *file;
c94e4afb 205 char *buf;
e9489cd6 206 int save_preserve_xattrs = preserve_xattrs;
47d6a60c 207 int kept = 0;
378a074c
AT
208 int ret_code;
209
66203a98 210 /* return if no file to keep */
9439c0cb 211 if (x_lstat(fname, &sx.st, NULL) < 0)
9fd62d15 212 return 1;
1c3344a1
WD
213#ifdef SUPPORT_ACLS
214 sx.acc_acl = sx.def_acl = NULL;
215#endif
16edf865
WD
216#ifdef SUPPORT_XATTRS
217 sx.xattr = NULL;
218#endif
66203a98 219
3de73827 220 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
c94e4afb 221 return 1; /* the file could have disappeared */
66203a98 222
82ad07c4
WD
223 if (!(buf = get_backup_name(fname))) {
224 unmake_file(file);
47d6a60c 225 return 0;
82ad07c4 226 }
378a074c 227
1c3344a1
WD
228#ifdef SUPPORT_ACLS
229 if (preserve_acls && !S_ISLNK(file->mode)) {
230 get_acl(fname, &sx);
231 cache_acl(file, &sx);
232 free_acl(&sx);
233 }
234#endif
16edf865
WD
235#ifdef SUPPORT_XATTRS
236 if (preserve_xattrs) {
237 get_xattr(fname, &sx);
238 cache_xattr(file, &sx);
239 free_xattr(&sx);
240 }
241#endif
1c3344a1 242
378a074c 243 /* Check to see if this is a device file, or link */
b5c6a6ae
WD
244 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
245 || (preserve_specials && IS_SPECIAL(file->mode))) {
88e05f84 246 int save_errno;
93e28fbd 247 do_unlink(buf);
a64f19e2 248 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) {
88e05f84 249 save_errno = errno ? errno : EINVAL; /* 0 paranoia */
f1ca7c44 250 if (errno == ENOENT && make_bak_dir(buf) == 0) {
a64f19e2 251 if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0)
f1ca7c44
WD
252 save_errno = errno ? errno : save_errno;
253 else
254 save_errno = 0;
255 }
256 if (save_errno) {
257 rsyserr(FERROR, save_errno, "mknod %s failed",
258 full_fname(buf));
259 }
88e05f84
WD
260 } else
261 save_errno = 0;
951e826b 262 if (DEBUG_GTE(BACKUP, 1) && save_errno == 0) {
93e28fbd 263 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
45c49b52 264 fname);
47d6a60c
S
265 }
266 kept = 1;
267 do_unlink(fname);
268 }
378a074c 269
addf0c4a 270 if (!kept && S_ISDIR(file->mode)) {
378a074c 271 /* make an empty directory */
f1ca7c44
WD
272 if (do_mkdir(buf, file->mode) < 0) {
273 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
274 if (errno == ENOENT && make_bak_dir(buf) == 0) {
275 if (do_mkdir(buf, file->mode) < 0)
276 save_errno = errno ? errno : save_errno;
277 else
278 save_errno = 0;
279 }
280 if (save_errno) {
281 rsyserr(FINFO, save_errno, "mkdir %s failed",
282 full_fname(buf));
283 }
377dbd20 284 }
47d6a60c 285
377dbd20 286 ret_code = do_rmdir(fname);
951e826b 287 if (DEBUG_GTE(BACKUP, 1)) {
ea42541f
WD
288 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
289 full_fname(fname), ret_code);
47d6a60c
S
290 }
291 kept = 1;
292 }
378a074c 293
4f5b0756 294#ifdef SUPPORT_LINKS
addf0c4a 295 if (!kept && preserve_links && S_ISLNK(file->mode)) {
82ad07c4
WD
296 const char *sl = F_SYMLINK(file);
297 if (safe_symlinks && unsafe_symlink(sl, buf)) {
951e826b 298 if (INFO_GTE(SYMSAFE, 1)) {
47d6a60c 299 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
82ad07c4 300 full_fname(buf), sl);
47d6a60c
S
301 }
302 kept = 1;
93e28fbd
WD
303 } else {
304 do_unlink(buf);
f1ca7c44
WD
305 if (do_symlink(sl, buf) < 0) {
306 int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
307 if (errno == ENOENT && make_bak_dir(buf) == 0) {
308 if (do_symlink(sl, buf) < 0)
309 save_errno = errno ? errno : save_errno;
310 else
311 save_errno = 0;
312 }
313 if (save_errno) {
314 rsyserr(FERROR, save_errno, "link %s -> \"%s\"",
315 full_fname(buf), sl);
316 }
93e28fbd
WD
317 }
318 do_unlink(fname);
319 kept = 1;
47d6a60c 320 }
47d6a60c 321 }
378a074c 322#endif
378a074c 323
addf0c4a 324 if (!kept && !S_ISREG(file->mode)) {
47d6a60c 325 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
45c49b52 326 fname);
82ad07c4 327 unmake_file(file);
f8c8ef9e 328 return 1;
47d6a60c 329 }
66203a98 330
66203a98 331 /* move to keep tree if a file */
addf0c4a 332 if (!kept) {
c94e4afb 333 if (robust_move(fname, buf) != 0) {
d62bcc17 334 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
45c49b52 335 full_fname(fname), buf);
1c3344a1 336 } else if (sx.st.st_nlink > 1) {
394bcdb5 337 /* If someone has hard-linked the file into the backup
3cb22c20 338 * dir, rename() might return success but do nothing! */
394bcdb5 339 robust_unlink(fname); /* Just in case... */
47d6a60c
S
340 }
341 }
e9489cd6 342 preserve_xattrs = 0;
16edf865 343 set_file_attrs(buf, file, NULL, fname, 0);
e9489cd6 344 preserve_xattrs = save_preserve_xattrs;
82ad07c4 345 unmake_file(file);
378a074c 346
951e826b 347 if (INFO_GTE(BACKUP, 1)) {
4875d6b6 348 rprintf(FINFO, "backed up %s to %s\n",
45c49b52 349 fname, buf);
4875d6b6 350 }
66203a98 351 return 1;
de0e2250 352}
66203a98
AT
353
354
355/* main backup switch routine */
45d8bfe0 356int make_backup(const char *fname)
66203a98
AT
357{
358 if (backup_dir)
addf0c4a
WD
359 return keep_backup(fname);
360 return make_simple_backup(fname);
66203a98 361}