Explain that the mktemp warning is harmless.
[rsync.git] / backup.c
blob6604f0b06e299a63b2af8c815d2c25f2cbc7eae9
1 /*
2 Copyright (C) Andrew Tridgell 1999
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.
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.
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.
19 /* backup handling code */
21 #include "rsync.h"
23 extern int verbose;
24 extern char *backup_suffix;
25 extern char *backup_dir;
28 extern int am_root;
29 extern int preserve_devices;
30 extern int preserve_links;
31 extern int preserve_hard_links;
33 /* simple backup creates a backup with a suffix in the same directory */
34 static int make_simple_backup(char *fname)
36 char fnamebak[MAXPATHLEN];
37 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
38 rprintf(FERROR,"backup filename too long\n");
39 return 0;
42 slprintf(fnamebak,sizeof(fnamebak),"%s%s",fname,backup_suffix);
43 if (do_rename(fname,fnamebak) != 0) {
44 /* cygwin (at least version b19) reports EINVAL */
45 if (errno != ENOENT && errno != EINVAL) {
46 rsyserr(FERROR, errno, "rename %s to backup %s", fname, fnamebak);
47 return 0;
49 } else if (verbose > 1) {
50 rprintf(FINFO,"backed up %s to %s\n",fname,fnamebak);
52 return 1;
56 /* recursively make a directory path */
57 static int make_dir(char *name, int mask)
59 char newdir [MAXPATHLEN];
60 char *p, *d;
62 /* copy pathname over, look for last '/' */
63 for (p = d = newdir; *name; *d++ = *name++)
64 if (*name == '/')
65 p = d;
66 if (p == newdir)
67 return 0;
68 *p = 0;
70 /* make the new directory, if that fails then make its parent */
71 while (do_mkdir (newdir, mask) != 0)
72 if ((errno != ENOENT) || !make_dir (newdir, mask))
73 return 0;
75 return 1;
76 } /* make_dir */
79 /****************************************************************************
80 Create a directory given an absolute path, perms based upon another directory
81 path
82 ****************************************************************************/
83 static int make_bak_dir(char *fname,char *bak_path)
85 STRUCT_STAT st;
86 STRUCT_STAT *st2;
87 char fullpath[MAXPATHLEN];
88 extern int orig_umask;
89 char *p;
90 char *q;
92 while(strncmp(bak_path,"./",2)==0) bak_path += 2;
94 if(bak_path[strlen(bak_path)-1]!='/') {
95 slprintf(fullpath,sizeof(fullpath),"%s/",bak_path);
96 } else {
97 slprintf(fullpath,sizeof(fullpath),"%s",bak_path);
99 p=fullpath;
100 q=&fullpath[strlen(fullpath)]; /* End of bak_path string */
101 strcat(fullpath,fname);
103 /* Make the directories */
104 while ((p=strchr(p,'/'))) {
105 *p = 0;
106 if(do_lstat(fullpath,&st)!=0) {
107 do_mkdir(fullpath,0777 & ~orig_umask);
108 if(p>q) {
109 if(do_lstat(q,&st)!=0) {
110 rprintf(FERROR,"make_bak_dir stat %s : %s\n",fullpath,strerror(errno));
111 } else {
112 st2=&st;
113 set_modtime(fullpath,st2->st_mtime);
114 if(do_lchown(fullpath,st2->st_uid,st2->st_gid)!=0) {
115 rprintf(FERROR,"make_bak_dir chown %s : %s\n",fullpath,strerror(errno));
117 if(do_chmod(fullpath,st2->st_mode)!=0) {
118 rprintf(FERROR,"make_bak_dir failed to set permissions on %s : %s\n",fullpath,strerror(errno));
123 *p = '/';
124 p++;
126 return 0;
129 /* robustly move a file, creating new directory structures if necessary */
130 static int robust_move(char *src, char *dst)
132 int keep_trying = 4;
133 int keep_path_extfs = 0;
134 int failed;
136 while (keep_trying) {
137 if (keep_path_extfs) {
138 failed = copy_file(src, dst, 0755);
139 if (!failed) {
140 do_unlink(src);
142 } else {
143 failed = robust_rename (src, dst);
146 if (failed) {
147 if (verbose > 2)
148 rprintf (FERROR, "robust_move failed: %s(%d)\n",
149 strerror (errno), errno);
150 switch (errno) {
151 /* external filesystem */
152 case EXDEV:
153 keep_path_extfs = 1;
154 keep_trying--;
155 break;
156 /* no directory to write to */
157 case ENOENT:
158 make_dir (dst, 0755);
159 keep_trying--;
160 break;
161 default:
162 keep_trying = 0;
163 } /* switch */
164 } else
165 keep_trying = 0;
166 } /* while */
167 return (!failed);
168 } /* robust_move */
171 /* if we have a backup_dir, then we get here from make_backup().
172 We will move the file to be deleted into a parallel directory tree */
173 static int keep_backup(char *fname)
176 static int initialised;
178 char keep_name [MAXPATHLEN];
179 STRUCT_STAT st;
180 struct file_struct *file;
182 int kept=0;
183 int ret_code;
185 if (!initialised) {
186 if (backup_dir[strlen(backup_dir) - 1] == '/')
187 backup_dir[strlen(backup_dir) - 1] = 0;
188 if (verbose > 0)
189 rprintf (FINFO, "backup_dir is %s\n", backup_dir);
190 initialised = 1;
193 /* return if no file to keep */
194 #if SUPPORT_LINKS
195 if (do_lstat (fname, &st)) return 1;
196 #else
197 if (do_stat (fname, &st)) return 1;
198 #endif
200 file = make_file(-1, fname, NULL, 1);
202 /* the file could have disappeared */
203 if (!file) return 1;
205 /* make a complete pathname for backup file */
206 if (strlen(backup_dir) + strlen(fname) > (MAXPATHLEN - 1)) {
207 rprintf (FERROR, "keep_backup filename too long\n");
208 return 0;
211 slprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname);
214 #ifdef HAVE_MKNOD
215 /* Check to see if this is a device file, or link */
216 if(IS_DEVICE(file->mode)) {
217 if(am_root && preserve_devices) {
218 make_bak_dir(fname,backup_dir);
219 if(do_mknod(keep_name,file->mode,file->rdev)!=0) {
220 rprintf(FERROR,"mknod %s : %s\n",keep_name,strerror(errno));
221 } else {
222 if(verbose>2)
223 rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname);
226 kept=1;
227 do_unlink(fname);
229 #endif
231 if(!kept && S_ISDIR(file->mode)) {
232 /* make an empty directory */
233 make_bak_dir(fname,backup_dir);
234 do_mkdir(keep_name,file->mode);
235 ret_code=do_rmdir(fname);
236 if(verbose>2)
237 rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code);
238 kept=1;
241 #if SUPPORT_LINKS
242 if(!kept && preserve_links && S_ISLNK(file->mode)) {
243 extern int safe_symlinks;
244 if (safe_symlinks && unsafe_symlink(file->link, keep_name)) {
245 if (verbose) {
246 rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n",
247 keep_name,file->link);
249 kept=1;
251 make_bak_dir(fname,backup_dir);
252 if(do_symlink(file->link,keep_name) != 0) {
253 rprintf(FERROR,"link %s -> %s : %s\n",keep_name,file->link,strerror(errno));
255 do_unlink(fname);
256 kept=1;
258 #endif
259 if(!kept && preserve_hard_links && check_hard_link(file)) {
260 if(verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file));
263 if(!kept && !S_ISREG(file->mode)) {
264 rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname);
267 /* move to keep tree if a file */
268 if(!kept) {
269 if (!robust_move (fname, keep_name))
270 rprintf(FERROR, "keep_backup failed %s -> %s : %s\n",
271 fname, keep_name, strerror(errno));
273 set_perms (keep_name, file, NULL, 0);
274 free_file (file);
275 free (file);
277 if (verbose > 1)
278 rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name);
279 return 1;
280 } /* keep_backup */
283 /* main backup switch routine */
284 int make_backup(char *fname)
286 if (backup_dir)
287 return (keep_backup(fname));
288 else
289 return (make_simple_backup(fname));