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