Improved a couple sentences.
[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;
377dbd20 36extern int safe_symlinks;
378a074c 37
c94e4afb
WD
38/* make a complete pathname for backup file */
39char *get_backup_name(char *fname)
40{
c94e4afb
WD
41 if (backup_dir) {
42 if (stringjoin(backup_dir_buf + backup_dir_len, backup_dir_remainder,
43 fname, backup_suffix, NULL) < backup_dir_remainder)
44 return backup_dir_buf;
45 } else {
fb22c277
WD
46 if (stringjoin(backup_dir_buf, MAXPATHLEN,
47 fname, backup_suffix, NULL) < MAXPATHLEN)
48 return backup_dir_buf;
c94e4afb
WD
49 }
50
51 rprintf(FERROR, "backup filename too long\n");
52 return NULL;
53}
54
66203a98
AT
55/* simple backup creates a backup with a suffix in the same directory */
56static int make_simple_backup(char *fname)
3d19b4c8 57{
ba679d51 58 int rename_errno;
c94e4afb 59 char *fnamebak = get_backup_name(fname);
5d2a7071 60
c94e4afb 61 if (!fnamebak)
3d19b4c8 62 return 0;
3d19b4c8 63
ba679d51
WD
64 while (1) {
65 if (do_rename(fname, fnamebak) == 0) {
66 if (verbose > 1) {
67 rprintf(FINFO, "backed up %s to %s\n",
68 safe_fname(fname),
69 safe_fname(fnamebak));
70 }
71 break;
3d19b4c8 72 }
ba679d51
WD
73 /* cygwin (at least version b19) reports EINVAL */
74 if (errno == ENOENT || errno == EINVAL)
75 break;
76
77 rename_errno = errno;
78 if (errno == EISDIR && do_rmdir(fnamebak) == 0)
79 continue;
80 if (errno == ENOTDIR && do_unlink(fnamebak) == 0)
81 continue;
82
83 rsyserr(FERROR, rename_errno, "rename %s to backup %s",
4875d6b6 84 safe_fname(fname), safe_fname(fnamebak));
ba679d51
WD
85 errno = rename_errno;
86 return 0;
3d19b4c8 87 }
ba679d51 88
3d19b4c8
AT
89 return 1;
90}
66203a98
AT
91
92
378a074c
AT
93/****************************************************************************
94Create a directory given an absolute path, perms based upon another directory
95path
96****************************************************************************/
de0e2250 97static int make_bak_dir(char *fullpath)
378a074c 98{
47d6a60c 99 STRUCT_STAT st;
de0e2250
WD
100 char *rel = fullpath + backup_dir_len;
101 char *end = rel + strlen(rel);
102 char *p = end;
103
104 while (strncmp(fullpath, "./", 2) == 0)
105 fullpath += 2;
106
107 /* Try to find an existing dir, starting from the deepest dir. */
108 while (1) {
109 if (--p == fullpath) {
110 p += strlen(p);
111 goto failure;
112 }
113 if (*p == '/') {
114 *p = '\0';
115 if (do_mkdir(fullpath, 0777 & ~orig_umask) == 0)
116 break;
117 if (errno != ENOENT) {
d62bcc17
WD
118 rsyserr(FERROR, errno,
119 "make_bak_dir mkdir %s failed",
120 full_fname(fullpath));
de0e2250
WD
121 goto failure;
122 }
123 }
47d6a60c 124 }
de0e2250
WD
125
126 /* Make all the dirs that we didn't find on the way here. */
127 while (1) {
128 if (p >= rel) {
129 /* Try to transfer the directory settings of the
130 * actual dir that the files are coming from. */
9fd62d15 131 if (do_stat(rel, &st) < 0) {
d62bcc17
WD
132 rsyserr(FERROR, errno,
133 "make_bak_dir stat %s failed",
134 full_fname(rel));
de0e2250 135 } else {
de0e2250
WD
136 do_lchown(fullpath, st.st_uid, st.st_gid);
137 do_chmod(fullpath, st.st_mode);
47d6a60c
S
138 }
139 }
de0e2250
WD
140 *p = '/';
141 p += strlen(p);
142 if (p == end)
143 break;
144 if (do_mkdir(fullpath, 0777 & ~orig_umask) < 0) {
d62bcc17
WD
145 rsyserr(FERROR, errno, "make_bak_dir mkdir %s failed",
146 full_fname(fullpath));
de0e2250
WD
147 goto failure;
148 }
47d6a60c
S
149 }
150 return 0;
de0e2250 151
b2e6caa0 152 failure:
de0e2250
WD
153 while (p != end) {
154 *p = '/';
155 p += strlen(p);
156 }
157 return -1;
378a074c
AT
158}
159
66203a98
AT
160/* robustly move a file, creating new directory structures if necessary */
161static int robust_move(char *src, char *dst)
162{
3d061653
WD
163 if (robust_rename(src, dst, 0755) < 0 && (errno != ENOENT
164 || make_bak_dir(dst) < 0 || robust_rename(src, dst, 0755) < 0))
62c9e6b3
WD
165 return -1;
166 return 0;
de0e2250 167}
66203a98
AT
168
169
de0e2250
WD
170/* If we have a --backup-dir, then we get here from make_backup().
171 * We will move the file to be deleted into a parallel directory tree. */
66203a98
AT
172static int keep_backup(char *fname)
173{
66203a98
AT
174 STRUCT_STAT st;
175 struct file_struct *file;
c94e4afb 176 char *buf;
47d6a60c 177 int kept = 0;
378a074c
AT
178 int ret_code;
179
66203a98 180 /* return if no file to keep */
018b2832 181 if (do_lstat(fname, &st) < 0)
9fd62d15 182 return 1;
66203a98 183
7842418b 184 if (!(file = make_file(fname, NULL, NO_FILTERS)))
c94e4afb 185 return 1; /* the file could have disappeared */
66203a98 186
c94e4afb 187 if (!(buf = get_backup_name(fname)))
47d6a60c 188 return 0;
378a074c 189
378a074c 190 /* Check to see if this is a device file, or link */
93e28fbd
WD
191 if (IS_DEVICE(file->mode) && am_root && preserve_devices) {
192 do_unlink(buf);
193 if (do_mknod(buf, file->mode, file->u.rdev) < 0
194 && (errno != ENOENT || make_bak_dir(buf) < 0
195 || do_mknod(buf, file->mode, file->u.rdev) < 0)) {
196 rsyserr(FERROR, errno, "mknod %s failed",
197 full_fname(buf));
198 } else if (verbose > 2) {
199 rprintf(FINFO, "make_backup: DEVICE %s successful.\n",
200 safe_fname(fname));
47d6a60c
S
201 }
202 kept = 1;
203 do_unlink(fname);
204 }
378a074c 205
addf0c4a 206 if (!kept && S_ISDIR(file->mode)) {
378a074c 207 /* make an empty directory */
c94e4afb
WD
208 if (do_mkdir(buf, file->mode) < 0
209 && (errno != ENOENT || make_bak_dir(buf) < 0
210 || do_mkdir(buf, file->mode) < 0)) {
d62bcc17 211 rsyserr(FINFO, errno, "mkdir %s failed",
c94e4afb 212 full_fname(buf));
377dbd20 213 }
47d6a60c 214
377dbd20 215 ret_code = do_rmdir(fname);
addf0c4a 216 if (verbose > 2) {
ea42541f
WD
217 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
218 full_fname(fname), ret_code);
47d6a60c
S
219 }
220 kept = 1;
221 }
378a074c 222
4f5b0756 223#ifdef SUPPORT_LINKS
addf0c4a 224 if (!kept && preserve_links && S_ISLNK(file->mode)) {
c94e4afb 225 if (safe_symlinks && unsafe_symlink(file->u.link, buf)) {
47d6a60c
S
226 if (verbose) {
227 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
c94e4afb 228 full_fname(buf), file->u.link);
47d6a60c
S
229 }
230 kept = 1;
93e28fbd
WD
231 } else {
232 do_unlink(buf);
233 if (do_symlink(file->u.link, buf) < 0
234 && (errno != ENOENT || make_bak_dir(buf) < 0
235 || do_symlink(file->u.link, buf) < 0)) {
236 rsyserr(FERROR, errno, "link %s -> \"%s\"",
237 full_fname(buf),
238 safe_fname(file->u.link));
239 }
240 do_unlink(fname);
241 kept = 1;
47d6a60c 242 }
47d6a60c 243 }
378a074c 244#endif
378a074c 245
addf0c4a 246 if (!kept && !S_ISREG(file->mode)) {
47d6a60c 247 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
4875d6b6 248 safe_fname(fname));
f8c8ef9e 249 return 1;
47d6a60c 250 }
66203a98 251
66203a98 252 /* move to keep tree if a file */
addf0c4a 253 if (!kept) {
c94e4afb 254 if (robust_move(fname, buf) != 0) {
d62bcc17 255 rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"",
4875d6b6 256 full_fname(fname), safe_fname(buf));
394bcdb5
WD
257 } else if (st.st_nlink > 1) {
258 /* If someone has hard-linked the file into the backup
3cb22c20 259 * dir, rename() might return success but do nothing! */
394bcdb5 260 robust_unlink(fname); /* Just in case... */
47d6a60c
S
261 }
262 }
c94e4afb 263 set_perms(buf, file, NULL, 0);
9935066b 264 free(file);
378a074c 265
4875d6b6
WD
266 if (verbose > 1) {
267 rprintf(FINFO, "backed up %s to %s\n",
268 safe_fname(fname), safe_fname(buf));
269 }
66203a98 270 return 1;
de0e2250 271}
66203a98
AT
272
273
274/* main backup switch routine */
275int make_backup(char *fname)
276{
277 if (backup_dir)
addf0c4a
WD
278 return keep_backup(fname);
279 return make_simple_backup(fname);
66203a98 280}