- If we return an error because of dry_run being set, we now set errno.
[rsync/rsync.git] / backup.c
CommitLineData
addf0c4a 1/*
3d19b4c8 2 Copyright (C) Andrew Tridgell 1999
addf0c4a 3
3d19b4c8
AT
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
addf0c4a 8
3d19b4c8
AT
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
addf0c4a 13
3d19b4c8
AT
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/* backup handling code */
20
21#include "rsync.h"
22
23extern int verbose;
daa8ce83
WD
24extern int backup_suffix_len;
25extern int backup_dir_len;
de0e2250
WD
26extern unsigned int backup_dir_remainder;
27extern char backup_dir_buf[MAXPATHLEN];
3d19b4c8 28extern char *backup_suffix;
66203a98 29extern char *backup_dir;
3d19b4c8 30
378a074c
AT
31extern int am_root;
32extern int preserve_devices;
33extern int preserve_links;
34extern int preserve_hard_links;
5d2a7071 35extern int orig_umask;
378a074c 36
66203a98
AT
37/* simple backup creates a backup with a suffix in the same directory */
38static int make_simple_backup(char *fname)
3d19b4c8
AT
39{
40 char fnamebak[MAXPATHLEN];
5d2a7071
WD
41
42 if (stringjoin(fnamebak, sizeof fnamebak, fname, backup_suffix, NULL)
43 >= sizeof fnamebak) {
47d6a60c 44 rprintf(FERROR, "backup filename too long\n");
3d19b4c8
AT
45 return 0;
46 }
47
47d6a60c 48 if (do_rename(fname, fnamebak) != 0) {
3d19b4c8
AT
49 /* cygwin (at least version b19) reports EINVAL */
50 if (errno != ENOENT && errno != EINVAL) {
a039749b 51 rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak);
3d19b4c8
AT
52 return 0;
53 }
54 } else if (verbose > 1) {
47d6a60c 55 rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak);
3d19b4c8
AT
56 }
57 return 1;
58}
66203a98
AT
59
60
378a074c
AT
61/****************************************************************************
62Create a directory given an absolute path, perms based upon another directory
63path
64****************************************************************************/
de0e2250 65static int make_bak_dir(char *fullpath)
378a074c 66{
47d6a60c 67 STRUCT_STAT st;
de0e2250
WD
68 char *rel = fullpath + backup_dir_len;
69 char *end = rel + strlen(rel);
70 char *p = end;
71
72 while (strncmp(fullpath, "./", 2) == 0)
73 fullpath += 2;
74
75 /* Try to find an existing dir, starting from the deepest dir. */
76 while (1) {
77 if (--p == fullpath) {
78 p += strlen(p);
79 goto failure;
80 }
81 if (*p == '/') {
82 *p = '\0';
83 if (do_mkdir(fullpath, 0777 & ~orig_umask) == 0)
84 break;
85 if (errno != ENOENT) {
86 rprintf(FERROR,
87 "make_bak_dir mkdir %s failed: %s\n",
88 full_fname(fullpath), strerror(errno));
89 goto failure;
90 }
91 }
47d6a60c 92 }
de0e2250
WD
93
94 /* Make all the dirs that we didn't find on the way here. */
95 while (1) {
96 if (p >= rel) {
97 /* Try to transfer the directory settings of the
98 * actual dir that the files are coming from. */
99 if (do_lstat(rel, &st) != 0) {
100 rprintf(FERROR,
101 "make_bak_dir stat %s failed: %s\n",
102 full_fname(rel), strerror(errno));
103 } else {
104 set_modtime(fullpath, st.st_mtime);
105 do_lchown(fullpath, st.st_uid, st.st_gid);
106 do_chmod(fullpath, st.st_mode);
47d6a60c
S
107 }
108 }
de0e2250
WD
109 *p = '/';
110 p += strlen(p);
111 if (p == end)
112 break;
113 if (do_mkdir(fullpath, 0777 & ~orig_umask) < 0) {
114 rprintf(FERROR,
115 "make_bak_dir mkdir %s failed: %s\n",
116 full_fname(fullpath), strerror(errno));
117 goto failure;
118 }
47d6a60c
S
119 }
120 return 0;
de0e2250
WD
121
122failure:
123 while (p != end) {
124 *p = '/';
125 p += strlen(p);
126 }
127 return -1;
378a074c
AT
128}
129
66203a98
AT
130/* robustly move a file, creating new directory structures if necessary */
131static int robust_move(char *src, char *dst)
132{
133 int keep_trying = 4;
134 int keep_path_extfs = 0;
135 int failed;
136
137 while (keep_trying) {
49d6fdc0
AT
138 if (keep_path_extfs) {
139 failed = copy_file(src, dst, 0755);
addf0c4a 140 if (!failed)
49d6fdc0 141 do_unlink(src);
addf0c4a
WD
142 } else
143 failed = robust_rename(src, dst);
66203a98
AT
144
145 if (failed) {
addf0c4a
WD
146 if (verbose > 2) {
147 rprintf(FERROR, "robust_move failed: %s(%d)\n",
148 strerror(errno), errno);
149 }
66203a98 150 switch (errno) {
addf0c4a
WD
151 case EXDEV: /* external filesystem */
152 keep_path_extfs = 1;
153 keep_trying--;
154 break;
155 case ENOENT: /* no directory to write to */
de0e2250
WD
156 if (make_bak_dir(dst) < 0)
157 keep_trying = 0;
158 else
159 keep_trying--;
addf0c4a
WD
160 break;
161 default:
162 keep_trying = 0;
163 break;
164 }
66203a98
AT
165 } else
166 keep_trying = 0;
de0e2250 167 }
addf0c4a 168 return !failed;
de0e2250 169}
66203a98
AT
170
171
de0e2250
WD
172/* If we have a --backup-dir, then we get here from make_backup().
173 * We will move the file to be deleted into a parallel directory tree. */
66203a98
AT
174static int keep_backup(char *fname)
175{
66203a98
AT
176 STRUCT_STAT st;
177 struct file_struct *file;
47d6a60c 178 int kept = 0;
378a074c
AT
179 int ret_code;
180
66203a98
AT
181 /* return if no file to keep */
182#if SUPPORT_LINKS
addf0c4a 183 if (do_lstat(fname, &st)) return 1;
66203a98 184#else
addf0c4a 185 if (do_stat(fname, &st)) return 1;
66203a98
AT
186#endif
187
9935066b 188 file = make_file(fname, NULL, NO_EXCLUDES);
f2cbf44b
AT
189
190 /* the file could have disappeared */
191 if (!file) return 1;
66203a98 192
47d6a60c 193 /* make a complete pathname for backup file */
de0e2250
WD
194 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
195 fname, backup_suffix, NULL) >= backup_dir_remainder) {
addf0c4a 196 rprintf(FERROR, "keep_backup filename too long\n");
47d6a60c
S
197 return 0;
198 }
378a074c 199
378a074c
AT
200#ifdef HAVE_MKNOD
201 /* Check to see if this is a device file, or link */
addf0c4a
WD
202 if (IS_DEVICE(file->mode)) {
203 if (am_root && preserve_devices) {
de0e2250
WD
204 make_bak_dir(backup_dir_buf);
205 if (do_mknod(backup_dir_buf, file->mode, file->u.rdev) != 0) {
ea42541f 206 rprintf(FERROR, "mknod %s failed: %s\n",
de0e2250 207 full_fname(backup_dir_buf), strerror(errno));
addf0c4a
WD
208 } else if (verbose > 2) {
209 rprintf(FINFO,
210 "make_backup: DEVICE %s successful.\n",
211 fname);
47d6a60c
S
212 }
213 }
214 kept = 1;
215 do_unlink(fname);
216 }
378a074c
AT
217#endif
218
addf0c4a 219 if (!kept && S_ISDIR(file->mode)) {
378a074c 220 /* make an empty directory */
de0e2250
WD
221 make_bak_dir(backup_dir_buf);
222 do_mkdir(backup_dir_buf, file->mode);
47d6a60c
S
223 ret_code = do_rmdir(fname);
224
addf0c4a 225 if (verbose > 2) {
ea42541f
WD
226 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
227 full_fname(fname), ret_code);
47d6a60c
S
228 }
229 kept = 1;
230 }
378a074c
AT
231
232#if SUPPORT_LINKS
addf0c4a 233 if (!kept && preserve_links && S_ISLNK(file->mode)) {
47d6a60c 234 extern int safe_symlinks;
de0e2250 235 if (safe_symlinks && unsafe_symlink(file->u.link, backup_dir_buf)) {
47d6a60c
S
236 if (verbose) {
237 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
de0e2250 238 full_fname(backup_dir_buf), file->u.link);
47d6a60c
S
239 }
240 kept = 1;
241 }
de0e2250
WD
242 make_bak_dir(backup_dir_buf);
243 if (do_symlink(file->u.link, backup_dir_buf) != 0) {
47d6a60c 244 rprintf(FERROR, "link %s -> %s : %s\n",
de0e2250 245 full_fname(backup_dir_buf), file->u.link, strerror(errno));
47d6a60c
S
246 }
247 do_unlink(fname);
248 kept = 1;
249 }
378a074c 250#endif
378a074c 251
addf0c4a 252 if (!kept && !S_ISREG(file->mode)) {
47d6a60c 253 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
addf0c4a 254 fname);
47d6a60c 255 }
66203a98 256
66203a98 257 /* move to keep tree if a file */
addf0c4a 258 if (!kept) {
de0e2250 259 if (!robust_move(fname, backup_dir_buf)) {
ea42541f 260 rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n",
de0e2250 261 full_fname(fname), backup_dir_buf, strerror(errno));
47d6a60c
S
262 }
263 }
de0e2250 264 set_perms(backup_dir_buf, file, NULL, 0);
9935066b 265 free(file);
378a074c 266
66203a98 267 if (verbose > 1)
de0e2250 268 rprintf(FINFO, "keep_backup %s -> %s\n", fname, backup_dir_buf);
66203a98 269 return 1;
de0e2250 270}
66203a98
AT
271
272
273/* main backup switch routine */
274int make_backup(char *fname)
275{
276 if (backup_dir)
addf0c4a
WD
277 return keep_backup(fname);
278 return make_simple_backup(fname);
66203a98 279}