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