Tweaked an error message and a comment.
[rsync/rsync.git] / backup.c
... / ...
CommitLineData
1/*
2 * Backup handling code.
3 *
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003-2007 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 version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "rsync.h"
22
23extern int verbose;
24extern int am_root;
25extern int preserve_acls;
26extern int preserve_devices;
27extern int preserve_specials;
28extern int preserve_links;
29extern int safe_symlinks;
30extern int backup_dir_len;
31extern unsigned int backup_dir_remainder;
32extern char backup_dir_buf[MAXPATHLEN];
33extern char *backup_suffix;
34extern char *backup_dir;
35
36/* make a complete pathname for backup file */
37char *get_backup_name(const char *fname)
38{
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 {
44 if (stringjoin(backup_dir_buf, MAXPATHLEN,
45 fname, backup_suffix, NULL) < MAXPATHLEN)
46 return backup_dir_buf;
47 }
48
49 rprintf(FERROR, "backup filename too long\n");
50 return NULL;
51}
52
53/* simple backup creates a backup with a suffix in the same directory */
54static int make_simple_backup(const char *fname)
55{
56 int rename_errno;
57 const char *fnamebak = get_backup_name(fname);
58
59 if (!fnamebak)
60 return 0;
61
62 while (1) {
63 if (do_rename(fname, fnamebak) == 0) {
64 if (verbose > 1) {
65 rprintf(FINFO, "backed up %s to %s\n",
66 fname, fnamebak);
67 }
68 break;
69 }
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",
81 fname, fnamebak);
82 errno = rename_errno;
83 return 0;
84 }
85
86 return 1;
87}
88
89
90/****************************************************************************
91Create a directory given an absolute path, perms based upon another directory
92path
93****************************************************************************/
94static int make_bak_dir(char *fullpath)
95{
96 statx sx;
97 struct file_struct *file;
98 char *rel = fullpath + backup_dir_len;
99 char *end = rel + strlen(rel);
100 char *p = end;
101
102 while (strncmp(fullpath, "./", 2) == 0)
103 fullpath += 2;
104
105 /* Try to find an existing dir, starting from the deepest dir. */
106 while (1) {
107 if (--p == fullpath) {
108 p += strlen(p);
109 goto failure;
110 }
111 if (*p == '/') {
112 *p = '\0';
113 if (mkdir_defmode(fullpath) == 0)
114 break;
115 if (errno != ENOENT) {
116 rsyserr(FERROR, errno,
117 "make_bak_dir mkdir %s failed",
118 full_fname(fullpath));
119 goto failure;
120 }
121 }
122 }
123
124 /* Make all the dirs that we didn't find on the way here. */
125 while (1) {
126 if (p >= rel) {
127 /* Try to transfer the directory settings of the
128 * actual dir that the files are coming from. */
129 if (do_stat(rel, &sx.st) < 0) {
130 rsyserr(FERROR, errno,
131 "make_bak_dir stat %s failed",
132 full_fname(rel));
133 } else {
134#ifdef SUPPORT_ACLS
135 sx.acc_acl = sx.def_acl = NULL;
136#endif
137 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
138 continue;
139#ifdef SUPPORT_ACLS
140 if (preserve_acls && !S_ISLNK(file->mode)) {
141 get_acl(rel, &sx);
142 cache_acl(file, &sx);
143 free_acl(&sx);
144 }
145#endif
146 set_file_attrs(fullpath, file, NULL, 0);
147 free(file);
148 }
149 }
150 *p = '/';
151 p += strlen(p);
152 if (p == end)
153 break;
154 if (mkdir_defmode(fullpath) < 0) {
155 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
156 full_fname(fullpath));
157 goto failure;
158 }
159 }
160 return 0;
161
162 failure:
163 while (p != end) {
164 *p = '/';
165 p += strlen(p);
166 }
167 return -1;
168}
169
170/* robustly move a file, creating new directory structures if necessary */
171static int robust_move(const char *src, char *dst)
172{
173 if (robust_rename(src, dst, NULL, 0755) < 0
174 && (errno != ENOENT || make_bak_dir(dst) < 0
175 || robust_rename(src, dst, NULL, 0755) < 0))
176 return -1;
177 return 0;
178}
179
180
181/* If we have a --backup-dir, then we get here from make_backup().
182 * We will move the file to be deleted into a parallel directory tree. */
183static int keep_backup(const char *fname)
184{
185 statx sx;
186 struct file_struct *file;
187 char *buf;
188 int kept = 0;
189 int ret_code;
190
191 /* return if no file to keep */
192 if (do_lstat(fname, &sx.st) < 0)
193 return 1;
194#ifdef SUPPORT_ACLS
195 sx.acc_acl = sx.def_acl = NULL;
196#endif
197
198 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
199 return 1; /* the file could have disappeared */
200
201 if (!(buf = get_backup_name(fname))) {
202 unmake_file(file);
203 return 0;
204 }
205
206#ifdef SUPPORT_ACLS
207 if (preserve_acls && !S_ISLNK(file->mode)) {
208 get_acl(fname, &sx);
209 cache_acl(file, &sx);
210 free_acl(&sx);
211 }
212#endif
213
214 /* Check to see if this is a device file, or link */
215 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
216 || (preserve_specials && IS_SPECIAL(file->mode))) {
217 uint32 *devp = F_RDEV_P(file);
218 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
219 do_unlink(buf);
220 if (do_mknod(buf, file->mode, rdev) < 0
221 && (errno != ENOENT || make_bak_dir(buf) < 0
222 || do_mknod(buf, file->mode, rdev) < 0)) {
223 rsyserr(FERROR, errno, "mknod %s failed",
224 full_fname(buf));
225 } else if (verbose > 2) {
226 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
227 fname);
228 }
229 kept = 1;
230 do_unlink(fname);
231 }
232
233 if (!kept && S_ISDIR(file->mode)) {
234 /* make an empty directory */
235 if (do_mkdir(buf, file->mode) < 0
236 && (errno != ENOENT || make_bak_dir(buf) < 0
237 || do_mkdir(buf, file->mode) < 0)) {
238 rsyserr(FINFO, errno, "mkdir %s failed",
239 full_fname(buf));
240 }
241
242 ret_code = do_rmdir(fname);
243 if (verbose > 2) {
244 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
245 full_fname(fname), ret_code);
246 }
247 kept = 1;
248 }
249
250#ifdef SUPPORT_LINKS
251 if (!kept && preserve_links && S_ISLNK(file->mode)) {
252 const char *sl = F_SYMLINK(file);
253 if (safe_symlinks && unsafe_symlink(sl, buf)) {
254 if (verbose) {
255 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
256 full_fname(buf), sl);
257 }
258 kept = 1;
259 } else {
260 do_unlink(buf);
261 if (do_symlink(sl, buf) < 0
262 && (errno != ENOENT || make_bak_dir(buf) < 0
263 || do_symlink(sl, buf) < 0)) {
264 rsyserr(FERROR, errno, "link %s -> \"%s\"",
265 full_fname(buf), sl);
266 }
267 do_unlink(fname);
268 kept = 1;
269 }
270 }
271#endif
272
273 if (!kept && !S_ISREG(file->mode)) {
274 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
275 fname);
276 unmake_file(file);
277 return 1;
278 }
279
280 /* move to keep tree if a file */
281 if (!kept) {
282 if (robust_move(fname, buf) != 0) {
283 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
284 full_fname(fname), buf);
285 } else if (sx.st.st_nlink > 1) {
286 /* If someone has hard-linked the file into the backup
287 * dir, rename() might return success but do nothing! */
288 robust_unlink(fname); /* Just in case... */
289 }
290 }
291 set_file_attrs(buf, file, NULL, 0);
292 unmake_file(file);
293
294 if (verbose > 1) {
295 rprintf(FINFO, "backed up %s to %s\n",
296 fname, buf);
297 }
298 return 1;
299}
300
301
302/* main backup switch routine */
303int make_backup(const char *fname)
304{
305 if (backup_dir)
306 return keep_backup(fname);
307 return make_simple_backup(fname);
308}