Added backup_dir_buf and backup_dir_remainder.
[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;
3d19b4c8 26extern char *backup_suffix;
66203a98 27extern char *backup_dir;
3d19b4c8 28
378a074c
AT
29extern int am_root;
30extern int preserve_devices;
31extern int preserve_links;
32extern int preserve_hard_links;
5d2a7071 33extern int orig_umask;
378a074c 34
66203a98
AT
35/* simple backup creates a backup with a suffix in the same directory */
36static int make_simple_backup(char *fname)
3d19b4c8
AT
37{
38 char fnamebak[MAXPATHLEN];
5d2a7071
WD
39
40 if (stringjoin(fnamebak, sizeof fnamebak, fname, backup_suffix, NULL)
41 >= sizeof fnamebak) {
47d6a60c 42 rprintf(FERROR, "backup filename too long\n");
3d19b4c8
AT
43 return 0;
44 }
45
47d6a60c 46 if (do_rename(fname, fnamebak) != 0) {
3d19b4c8
AT
47 /* cygwin (at least version b19) reports EINVAL */
48 if (errno != ENOENT && errno != EINVAL) {
a039749b 49 rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak);
3d19b4c8
AT
50 return 0;
51 }
52 } else if (verbose > 1) {
47d6a60c 53 rprintf(FINFO, "backed up %s to %s\n", fname, fnamebak);
3d19b4c8
AT
54 }
55 return 1;
56}
66203a98
AT
57
58
59/* recursively make a directory path */
60static int make_dir(char *name, int mask)
61{
62 char newdir [MAXPATHLEN];
63 char *p, *d;
64
65 /* copy pathname over, look for last '/' */
66 for (p = d = newdir; *name; *d++ = *name++)
67 if (*name == '/')
68 p = d;
69 if (p == newdir)
70 return 0;
71 *p = 0;
72
73 /* make the new directory, if that fails then make its parent */
addf0c4a
WD
74 while (do_mkdir(newdir, mask) != 0) {
75 if (errno != ENOENT || !make_dir(newdir, mask))
66203a98 76 return 0;
addf0c4a 77 }
66203a98
AT
78
79 return 1;
80} /* make_dir */
81
82
378a074c
AT
83/****************************************************************************
84Create a directory given an absolute path, perms based upon another directory
85path
86****************************************************************************/
47d6a60c 87static int make_bak_dir(char *fname, char *bak_path)
378a074c 88{
47d6a60c
S
89 STRUCT_STAT st;
90 STRUCT_STAT *st2;
91 char fullpath[MAXPATHLEN];
47d6a60c
S
92 char *p;
93 char *q;
94
95 while(strncmp(bak_path, "./", 2) == 0) bak_path += 2;
96
5d2a7071
WD
97 if (pathjoin(fullpath, sizeof fullpath, bak_path, fname)
98 >= sizeof fullpath) {
99 rprintf(FERROR, "backup dirname too long\n");
100 return 0;
47d6a60c
S
101 }
102 p = fullpath;
5d2a7071
WD
103 q = fullpath + strlen(bak_path);
104 if (*q == '/')
105 q++; /* Point past the middle '/' added by pathjoin(). */
47d6a60c
S
106
107 /* Make the directories */
addf0c4a 108 while ((p = strchr(p, '/')) != NULL) {
47d6a60c 109 *p = 0;
addf0c4a 110 if (do_lstat(fullpath, &st) != 0) {
47d6a60c 111 do_mkdir(fullpath, 0777 & ~orig_umask);
addf0c4a
WD
112 if (p > q) {
113 if (do_lstat(q, &st) != 0) {
ea42541f
WD
114 rprintf(FERROR, "make_bak_dir stat %s failed: %s\n",
115 full_fname(fullpath), strerror(errno));
47d6a60c
S
116 } else {
117 st2 = &st;
118 set_modtime(fullpath, st2->st_mtime);
addf0c4a 119 if (do_lchown(fullpath, st2->st_uid, st2->st_gid) != 0) {
ea42541f
WD
120 rprintf(FERROR, "make_bak_dir chown %s failed: %s\n",
121 full_fname(fullpath), strerror(errno));
47d6a60c 122 }
addf0c4a 123 if (do_chmod(fullpath, st2->st_mode) != 0) {
ea42541f
WD
124 rprintf(FERROR, "make_bak_dir failed to set permissions on %s: %s\n",
125 full_fname(fullpath), strerror(errno));
47d6a60c
S
126 }
127 }
128 }
129 }
5d2a7071 130 *p++ = '/';
47d6a60c
S
131 }
132 return 0;
378a074c
AT
133}
134
66203a98
AT
135/* robustly move a file, creating new directory structures if necessary */
136static int robust_move(char *src, char *dst)
137{
138 int keep_trying = 4;
139 int keep_path_extfs = 0;
140 int failed;
141
142 while (keep_trying) {
49d6fdc0
AT
143 if (keep_path_extfs) {
144 failed = copy_file(src, dst, 0755);
addf0c4a 145 if (!failed)
49d6fdc0 146 do_unlink(src);
addf0c4a
WD
147 } else
148 failed = robust_rename(src, dst);
66203a98
AT
149
150 if (failed) {
addf0c4a
WD
151 if (verbose > 2) {
152 rprintf(FERROR, "robust_move failed: %s(%d)\n",
153 strerror(errno), errno);
154 }
66203a98 155 switch (errno) {
addf0c4a
WD
156 case EXDEV: /* external filesystem */
157 keep_path_extfs = 1;
158 keep_trying--;
159 break;
160 case ENOENT: /* no directory to write to */
161 make_dir(dst, 0700);
162 keep_trying--;
163 break;
164 default:
165 keep_trying = 0;
166 break;
167 }
66203a98
AT
168 } else
169 keep_trying = 0;
170 } /* while */
addf0c4a 171 return !failed;
66203a98
AT
172} /* robust_move */
173
174
175/* if we have a backup_dir, then we get here from make_backup().
176 We will move the file to be deleted into a parallel directory tree */
177static int keep_backup(char *fname)
178{
179 static int initialised;
5d2a7071 180 char keep_name[MAXPATHLEN];
66203a98
AT
181 STRUCT_STAT st;
182 struct file_struct *file;
47d6a60c 183 int kept = 0;
378a074c
AT
184 int ret_code;
185
66203a98 186 if (!initialised) {
daa8ce83
WD
187 if (backup_dir_len && backup_dir[backup_dir_len - 1] == '/')
188 backup_dir[--backup_dir_len] = '\0';
66203a98 189 if (verbose > 0)
addf0c4a 190 rprintf(FINFO, "backup_dir is %s\n", backup_dir);
9935066b 191
66203a98
AT
192 initialised = 1;
193 }
194
195 /* return if no file to keep */
196#if SUPPORT_LINKS
addf0c4a 197 if (do_lstat(fname, &st)) return 1;
66203a98 198#else
addf0c4a 199 if (do_stat(fname, &st)) return 1;
66203a98
AT
200#endif
201
9935066b 202 file = make_file(fname, NULL, NO_EXCLUDES);
f2cbf44b
AT
203
204 /* the file could have disappeared */
205 if (!file) return 1;
66203a98 206
47d6a60c 207 /* make a complete pathname for backup file */
5d2a7071
WD
208 if (stringjoin(keep_name, sizeof keep_name,
209 backup_dir, "/", fname, backup_suffix, NULL)
210 >= sizeof keep_name) {
addf0c4a 211 rprintf(FERROR, "keep_backup filename too long\n");
47d6a60c
S
212 return 0;
213 }
378a074c 214
378a074c
AT
215#ifdef HAVE_MKNOD
216 /* Check to see if this is a device file, or link */
addf0c4a
WD
217 if (IS_DEVICE(file->mode)) {
218 if (am_root && preserve_devices) {
47d6a60c 219 make_bak_dir(fname, backup_dir);
728d0922 220 if (do_mknod(keep_name, file->mode, file->u.rdev) != 0) {
ea42541f
WD
221 rprintf(FERROR, "mknod %s failed: %s\n",
222 full_fname(keep_name), strerror(errno));
addf0c4a
WD
223 } else if (verbose > 2) {
224 rprintf(FINFO,
225 "make_backup: DEVICE %s successful.\n",
226 fname);
47d6a60c
S
227 }
228 }
229 kept = 1;
230 do_unlink(fname);
231 }
378a074c
AT
232#endif
233
addf0c4a 234 if (!kept && S_ISDIR(file->mode)) {
378a074c 235 /* make an empty directory */
47d6a60c
S
236 make_bak_dir(fname, backup_dir);
237 do_mkdir(keep_name, file->mode);
238 ret_code = do_rmdir(fname);
239
addf0c4a 240 if (verbose > 2) {
ea42541f
WD
241 rprintf(FINFO, "make_backup: RMDIR %s returns %i\n",
242 full_fname(fname), ret_code);
47d6a60c
S
243 }
244 kept = 1;
245 }
378a074c
AT
246
247#if SUPPORT_LINKS
addf0c4a 248 if (!kept && preserve_links && S_ISLNK(file->mode)) {
47d6a60c 249 extern int safe_symlinks;
728d0922 250 if (safe_symlinks && unsafe_symlink(file->u.link, keep_name)) {
47d6a60c
S
251 if (verbose) {
252 rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n",
728d0922 253 full_fname(keep_name), file->u.link);
47d6a60c
S
254 }
255 kept = 1;
256 }
257 make_bak_dir(fname, backup_dir);
728d0922 258 if (do_symlink(file->u.link, keep_name) != 0) {
47d6a60c 259 rprintf(FERROR, "link %s -> %s : %s\n",
728d0922 260 full_fname(keep_name), file->u.link, strerror(errno));
47d6a60c
S
261 }
262 do_unlink(fname);
263 kept = 1;
264 }
378a074c 265#endif
378a074c 266
addf0c4a 267 if (!kept && !S_ISREG(file->mode)) {
47d6a60c 268 rprintf(FINFO, "make_bak: skipping non-regular file %s\n",
addf0c4a 269 fname);
47d6a60c 270 }
66203a98 271
66203a98 272 /* move to keep tree if a file */
addf0c4a
WD
273 if (!kept) {
274 if (!robust_move(fname, keep_name)) {
ea42541f
WD
275 rprintf(FERROR, "keep_backup failed: %s -> \"%s\": %s\n",
276 full_fname(fname), keep_name, strerror(errno));
47d6a60c
S
277 }
278 }
addf0c4a 279 set_perms(keep_name, file, NULL, 0);
9935066b 280 free(file);
378a074c 281
66203a98 282 if (verbose > 1)
addf0c4a 283 rprintf(FINFO, "keep_backup %s -> %s\n", fname, keep_name);
66203a98
AT
284 return 1;
285} /* keep_backup */
286
287
288/* main backup switch routine */
289int make_backup(char *fname)
290{
291 if (backup_dir)
addf0c4a
WD
292 return keep_backup(fname);
293 return make_simple_backup(fname);
66203a98 294}