Restored write_ndx_and_attrs() and made it public.
[rsync/rsync.git] / backup.c
... / ...
CommitLineData
1/*
2 * Backup handling code.
3 *
4 * Copyright (C) 1999 Andrew Tridgell
5 * Copyright (C) 2003, 2004, 2005, 2006 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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "rsync.h"
23
24extern int verbose;
25extern int backup_dir_len;
26extern unsigned int backup_dir_remainder;
27extern char backup_dir_buf[MAXPATHLEN];
28extern char *backup_suffix;
29extern char *backup_dir;
30
31extern int am_root;
32extern int preserve_devices;
33extern int preserve_specials;
34extern int preserve_links;
35extern int safe_symlinks;
36
37/* make a complete pathname for backup file */
38char *get_backup_name(const char *fname)
39{
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 {
45 if (stringjoin(backup_dir_buf, MAXPATHLEN,
46 fname, backup_suffix, NULL) < MAXPATHLEN)
47 return backup_dir_buf;
48 }
49
50 rprintf(FERROR, "backup filename too long\n");
51 return NULL;
52}
53
54/* simple backup creates a backup with a suffix in the same directory */
55static int make_simple_backup(const char *fname)
56{
57 int rename_errno;
58 const char *fnamebak = get_backup_name(fname);
59
60 if (!fnamebak)
61 return 0;
62
63 while (1) {
64 if (do_rename(fname, fnamebak) == 0) {
65 if (verbose > 1) {
66 rprintf(FINFO, "backed up %s to %s\n",
67 fname, fnamebak);
68 }
69 break;
70 }
71 /* cygwin (at least version b19) reports EINVAL */
72 if (errno == ENOENT || errno == EINVAL)
73 break;
74
75 rename_errno = errno;
76 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
77 continue;
78 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
79 continue;
80
81 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
82 fname, fnamebak);
83 errno = rename_errno;
84 return 0;
85 }
86
87 return 1;
88}
89
90
91/****************************************************************************
92Create a directory given an absolute path, perms based upon another directory
93path
94****************************************************************************/
95static int make_bak_dir(char *fullpath)
96{
97 STRUCT_STAT st;
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, &st) < 0) {
130 rsyserr(FERROR, errno,
131 "make_bak_dir stat %s failed",
132 full_fname(rel));
133 } else {
134 do_lchown(fullpath, st.st_uid, st.st_gid);
135#ifdef HAVE_CHMOD
136 do_chmod(fullpath, st.st_mode);
137#endif
138 }
139 }
140 *p = '/';
141 p += strlen(p);
142 if (p == end)
143 break;
144 if (mkdir_defmode(fullpath) < 0) {
145 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
146 full_fname(fullpath));
147 goto failure;
148 }
149 }
150 return 0;
151
152 failure:
153 while (p != end) {
154 *p = '/';
155 p += strlen(p);
156 }
157 return -1;
158}
159
160/* robustly move a file, creating new directory structures if necessary */
161static int robust_move(const char *src, char *dst)
162{
163 if (robust_rename(src, dst, NULL, 0755) < 0
164 && (errno != ENOENT || make_bak_dir(dst) < 0
165 || robust_rename(src, dst, NULL, 0755) < 0))
166 return -1;
167 return 0;
168}
169
170
171/* If we have a --backup-dir, then we get here from make_backup().
172 * We will move the file to be deleted into a parallel directory tree. */
173static int keep_backup(const char *fname)
174{
175 STRUCT_STAT st;
176 struct file_struct *file;
177 char *buf;
178 int kept = 0;
179 int ret_code;
180
181 /* return if no file to keep */
182 if (do_lstat(fname, &st) < 0)
183 return 1;
184
185 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
186 return 1; /* the file could have disappeared */
187
188 if (!(buf = get_backup_name(fname)))
189 return 0;
190
191 /* Check to see if this is a device file, or link */
192 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
193 || (preserve_specials && IS_SPECIAL(file->mode))) {
194 do_unlink(buf);
195 if (do_mknod(buf, file->mode, file->u.rdev) < 0
196 && (errno != ENOENT || make_bak_dir(buf) < 0
197 || do_mknod(buf, file->mode, file->u.rdev) < 0)) {
198 rsyserr(FERROR, errno, "mknod %s failed",
199 full_fname(buf));
200 } else if (verbose > 2) {
201 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
202 fname);
203 }
204 kept = 1;
205 do_unlink(fname);
206 }
207
208 if (!kept && S_ISDIR(file->mode)) {
209 /* make an empty directory */
210 if (do_mkdir(buf, file->mode) < 0
211 && (errno != ENOENT || make_bak_dir(buf) < 0
212 || do_mkdir(buf, file->mode) < 0)) {
213 rsyserr(FINFO, errno, "mkdir %s failed",
214 full_fname(buf));
215 }
216
217 ret_code = do_rmdir(fname);
218 if (verbose > 2) {
219 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
220 full_fname(fname), ret_code);
221 }
222 kept = 1;
223 }
224
225#ifdef SUPPORT_LINKS
226 if (!kept && preserve_links && S_ISLNK(file->mode)) {
227 if (safe_symlinks && unsafe_symlink(file->u.link, buf)) {
228 if (verbose) {
229 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
230 full_fname(buf), file->u.link);
231 }
232 kept = 1;
233 } else {
234 do_unlink(buf);
235 if (do_symlink(file->u.link, buf) < 0
236 && (errno != ENOENT || make_bak_dir(buf) < 0
237 || do_symlink(file->u.link, buf) < 0)) {
238 rsyserr(FERROR, errno, "link %s -> \"%s\"",
239 full_fname(buf),
240 file->u.link);
241 }
242 do_unlink(fname);
243 kept = 1;
244 }
245 }
246#endif
247
248 if (!kept && !S_ISREG(file->mode)) {
249 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
250 fname);
251 return 1;
252 }
253
254 /* move to keep tree if a file */
255 if (!kept) {
256 if (robust_move(fname, buf) != 0) {
257 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
258 full_fname(fname), buf);
259 } else if (st.st_nlink > 1) {
260 /* If someone has hard-linked the file into the backup
261 * dir, rename() might return success but do nothing! */
262 robust_unlink(fname); /* Just in case... */
263 }
264 }
265 set_file_attrs(buf, file, NULL, 0);
266 free(file);
267
268 if (verbose > 1) {
269 rprintf(FINFO, "backed up %s to %s\n",
270 fname, buf);
271 }
272 return 1;
273}
274
275
276/* main backup switch routine */
277int make_backup(const char *fname)
278{
279 if (backup_dir)
280 return keep_backup(fname);
281 return make_simple_backup(fname);
282}