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