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