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