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