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