]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
kes Raise some restore debug levels.
[bacula/bacula] / bacula / src / findlib / create_file.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Create a file, and reset the modes
30  *
31  *    Kern Sibbald, November MM
32  *
33  *   Version $Id$
34  *
35  */
36
37 #include "bacula.h"
38 #include "find.h"
39
40 #ifndef S_IRWXUGO
41 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
42 #endif
43
44 #ifndef IS_CTG
45 #define IS_CTG(x) 0
46 #define O_CTG 0
47 #endif
48
49 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile);
50 static int path_already_seen(JCR *jcr, char *path, int pnl);
51
52
53 /*
54  * Create the file, or the directory
55  *
56  *  fname is the original filename
57  *  ofile is the output filename (may be in a different directory)
58  *
59  * Returns:  CF_SKIP     if file should be skipped
60  *           CF_ERROR    on error
61  *           CF_EXTRACT  file created and data to restore
62  *           CF_CREATED  file created no data to restore
63  *
64  *   Note, we create the file here, except for special files,
65  *     we do not set the attributes because we want to first
66  *     write the file, then when the writing is done, set the
67  *     attributes.
68  *   So, we return with the file descriptor open for normal
69  *     files.
70  *
71  */
72 int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
73 {
74    mode_t new_mode, parent_mode, mode;
75    uid_t uid;
76    gid_t gid;
77    int pnl;
78    bool exists = false;
79    struct stat mstatp;
80
81    bfd->reparse_point = false;
82    if (is_win32_stream(attr->data_stream)) {
83       set_win32_backup(bfd);
84    } else {
85       set_portable_backup(bfd);
86    }
87
88    new_mode = attr->statp.st_mode;
89    Dmsg3(200, "type=%d newmode=%x file=%s\n", attr->type, new_mode, attr->ofname);
90    parent_mode = S_IWUSR | S_IXUSR | new_mode;
91    gid = attr->statp.st_gid;
92    uid = attr->statp.st_uid;
93
94 #ifdef HAVE_WIN32
95    if (!bfd->use_backup_api) {
96       // eliminate invalid windows filename characters from foreign filenames
97       char *ch = (char *)attr->ofname;
98       if (ch[0] != 0 && ch[1] != 0) {
99          ch += 2;
100          while (*ch) {
101             switch (*ch) {
102             case ':':
103             case '<':
104             case '>':
105             case '*':
106             case '?':
107             case '|':
108                *ch = '_';
109                 break;
110             }
111             ch++;
112          }
113       }
114    }
115 #endif
116
117    Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
118    if (lstat(attr->ofname, &mstatp) == 0) {
119       exists = true;
120       switch (replace) {
121       case REPLACE_IFNEWER:
122          if (attr->statp.st_mtime <= mstatp.st_mtime) {
123             Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
124             return CF_SKIP;
125          }
126          break;
127
128       case REPLACE_IFOLDER:
129          if (attr->statp.st_mtime >= mstatp.st_mtime) {
130             Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
131             return CF_SKIP;
132          }
133          break;
134
135       case REPLACE_NEVER:
136          Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
137          return CF_SKIP;
138
139       case REPLACE_ALWAYS:
140          break;
141       }
142    }
143    switch (attr->type) {
144    case FT_RAW:                       /* raw device to be written */
145    case FT_FIFO:                      /* FIFO to be written to */
146    case FT_LNKSAVED:                  /* Hard linked, file already saved */
147    case FT_LNK:
148    case FT_SPEC:                      /* fifo, ... to be backed up */
149    case FT_REGE:                      /* empty file */
150    case FT_REG:                       /* regular file */
151       /* 
152        * Note, we do not delete FT_RAW because these are device files
153        *  or FIFOs that should already exist. If we blow it away,
154        *  we may blow away a FIFO that is being used to read the
155        *  restore data, or we may blow away a partition definition.
156        */
157       if (exists && attr->type != FT_RAW && attr->type != FT_FIFO) {
158          /* Get rid of old copy */
159          Dmsg1(400, "unlink %s\n", attr->ofname);
160          if (unlink(attr->ofname) == -1) {
161             berrno be;
162             Qmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
163                attr->ofname, be.bstrerror());
164             /* Continue despite error */
165          }
166       }
167       /*
168        * Here we do some preliminary work for all the above
169        *   types to create the path to the file if it does
170        *   not already exist.  Below, we will split to
171        *   do the file type specific work
172        */
173       pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
174       if (pnl < 0) {
175          return CF_ERROR;
176       }
177
178       /*
179        * If path length is <= 0 we are making a file in the root
180        *  directory. Assume that the directory already exists.
181        */
182       if (pnl > 0) {
183          char savechr;
184          savechr = attr->ofname[pnl];
185          attr->ofname[pnl] = 0;                 /* terminate path */
186
187          if (!path_already_seen(jcr, attr->ofname, pnl)) {
188             Dmsg1(400, "Make path %s\n", attr->ofname);
189             /*
190              * If we need to make the directory, ensure that it is with
191              * execute bit set (i.e. parent_mode), and preserve what already
192              * exists. Normally, this should do nothing.
193              */
194             if (!makepath(attr, attr->ofname, parent_mode, parent_mode, uid, gid, 1)) {
195                Dmsg1(10, "Could not make path. %s\n", attr->ofname);
196                attr->ofname[pnl] = savechr;     /* restore full name */
197                return CF_ERROR;
198             }
199          }
200          attr->ofname[pnl] = savechr;           /* restore full name */
201       }
202
203       /* Now we do the specific work for each file type */
204       switch(attr->type) {
205       case FT_REGE:
206       case FT_REG:
207          Dmsg1(100, "Create=%s\n", attr->ofname);
208          mode =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /*  O_NOFOLLOW; */
209          if (IS_CTG(attr->statp.st_mode)) {
210             mode |= O_CTG;               /* set contiguous bit if needed */
211          }
212          if (is_bopen(bfd)) {
213             Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
214             bclose(bfd);
215          }
216       
217
218          if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
219             berrno be;
220             be.set_errno(bfd->berrno);
221             Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
222                   attr->ofname, be.bstrerror());
223             Dmsg2(100,"Could not create %s: ERR=%s\n", attr->ofname, be.bstrerror());
224             return CF_ERROR;
225          }
226          return CF_EXTRACT;
227
228 #ifndef HAVE_WIN32  // none of these exists on MS Windows
229       case FT_RAW:                    /* Bacula raw device e.g. /dev/sda1 */
230       case FT_FIFO:                   /* Bacula fifo to save data */
231       case FT_SPEC:
232          if (S_ISFIFO(attr->statp.st_mode)) {
233             Dmsg1(400, "Restore fifo: %s\n", attr->ofname);
234             if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
235                berrno be;
236                Qmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
237                      attr->ofname, be.bstrerror());
238                return CF_ERROR;
239             }
240          } else if (S_ISSOCK(attr->statp.st_mode)) {
241              Dmsg1(200, "Skipping restore of socket: %s\n", attr->ofname);
242 #ifdef S_IFDOOR     // Solaris high speed RPC mechanism
243          } else if (S_ISDOOR(attr->statp.st_mode)) {
244              Dmsg1(200, "Skipping restore of door file: %s\n", attr->ofname);
245 #endif
246 #ifdef S_IFPORT     // Solaris event port for handling AIO
247          } else if (S_ISPORT(attr->statp.st_mode)) {
248              Dmsg1(200, "Skipping restore of event port file: %s\n", attr->ofname);
249 #endif
250          } else {
251             Dmsg1(400, "Restore node: %s\n", attr->ofname);
252             if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
253                berrno be;
254                Qmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
255                      attr->ofname, be.bstrerror());
256                return CF_ERROR;
257             }
258          }
259          /*
260           * Here we are going to attempt to restore to a FIFO, which
261           *   means that the FIFO must already exist, AND there must
262           *   be some process already attempting to read from the
263           *   FIFO, so we open it write-only. 
264           */
265          if (attr->type == FT_RAW || attr->type == FT_FIFO) {
266             btimer_t *tid;
267             Dmsg1(400, "FT_RAW|FT_FIFO %s\n", attr->ofname);
268             mode =  O_WRONLY | O_BINARY;
269             /* Timeout open() in 60 seconds */
270             if (attr->type == FT_FIFO) {
271                Dmsg0(400, "Set FIFO timer\n");
272                tid = start_thread_timer(jcr, pthread_self(), 60);
273             } else {
274                tid = NULL;
275             }
276             if (is_bopen(bfd)) {
277                Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
278             }
279             Dmsg2(400, "open %s mode=0x%x\n", attr->ofname, mode);
280             if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
281                berrno be;
282                be.set_errno(bfd->berrno);
283                Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
284                      attr->ofname, be.bstrerror());
285                Dmsg2(400, "Could not open %s: ERR=%s\n", attr->ofname, be.bstrerror());
286                stop_thread_timer(tid);
287                return CF_ERROR;
288             }
289             stop_thread_timer(tid);
290             return CF_EXTRACT;
291          }
292          Dmsg1(400, "FT_SPEC %s\n", attr->ofname);
293          return CF_CREATED;
294
295       case FT_LNK:
296          Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
297          if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
298             berrno be;
299             Qmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
300                   attr->ofname, attr->olname, be.bstrerror());
301             return CF_ERROR;
302          }
303          return CF_CREATED;
304
305       case FT_LNKSAVED:                  /* Hard linked, file already saved */
306          Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
307          if (link(attr->olname, attr->ofname) != 0) {
308             berrno be;
309 #ifdef HAVE_CHFLAGS
310             struct stat s;
311
312         /*
313             * If using BSD user flags, maybe has a file flag
314             * preventing this. So attempt to disable, retry link,
315             * and reset flags.
316             * Note that BSD securelevel may prevent disabling flag.
317         */
318
319             if (stat(attr->olname, &s) == 0 && s.st_flags != 0) {
320                if (chflags(attr->olname, 0) == 0) {
321                   if (link(attr->olname, attr->ofname) != 0) {
322                      /* restore original file flags even when linking failed */
323                      if (chflags(attr->olname, s.st_flags) < 0) {
324                         Qmsg2(jcr, M_ERROR, 0, _("Could not restore file flags for file %s: ERR=%s\n"),
325                               attr->olname, be.bstrerror());
326                      }
327 #endif /* HAVE_CHFLAGS */
328             Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
329                   attr->ofname, attr->olname, be.bstrerror());
330             Dmsg3(200, "Could not hard link %s -> %s: ERR=%s\n",
331                   attr->ofname, attr->olname, be.bstrerror());
332             return CF_ERROR;
333 #ifdef HAVE_CHFLAGS
334                   }
335                   /* finally restore original file flags */
336                   if (chflags(attr->olname, s.st_flags) < 0) {
337                      Qmsg2(jcr, M_ERROR, 0, _("Could not restore file flags for file %s: ERR=%s\n"),
338                             attr->olname, be.bstrerror());
339                   }
340                } else {
341                  Qmsg2(jcr, M_ERROR, 0, _("Could not reset file flags for file %s: ERR=%s\n"),
342                        attr->olname, be.bstrerror());
343                }
344             } else {
345               Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
346                     attr->ofname, attr->olname, be.bstrerror());
347               return CF_ERROR;
348             }
349 #endif /* HAVE_CHFLAGS */
350
351          }
352          return CF_CREATED;
353 #endif
354       } /* End inner switch */
355
356    case FT_REPARSE:
357       bfd->reparse_point = true;
358       /* Fall through wanted */
359    case FT_DIRBEGIN:
360    case FT_DIREND:
361       Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
362       if (!makepath(attr, attr->ofname, new_mode, parent_mode, uid, gid, 0)) {
363          return CF_ERROR;
364       }
365       /*
366        * If we are using the Win32 Backup API, we open the
367        *   directory so that the security info will be read
368        *   and saved.
369        */
370       if (!is_portable_backup(bfd)) {
371          if (is_bopen(bfd)) {
372             Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
373          }
374          if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
375             berrno be;
376             be.set_errno(bfd->berrno);
377 #ifdef HAVE_WIN32
378             /* Check for trying to create a drive, if so, skip */
379             if (attr->ofname[1] == ':' && 
380                 IsPathSeparator(attr->ofname[2]) && 
381                 attr->ofname[3] == '\0') {
382                return CF_SKIP;
383             }
384 #endif
385             Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
386                   attr->ofname, be.bstrerror());
387             return CF_ERROR;
388          }
389          return CF_EXTRACT;
390       } else {
391          return CF_CREATED;
392       }
393
394    case FT_DELETED:
395       Qmsg2(jcr, M_INFO, 0, _("Original file %s have been deleted: type=%d\n"), attr->fname, attr->type);
396       break;
397    /* The following should not occur */
398    case FT_NOACCESS:
399    case FT_NOFOLLOW:
400    case FT_NOSTAT:
401    case FT_DIRNOCHG:
402    case FT_NOCHG:
403    case FT_ISARCH:
404    case FT_NORECURSE:
405    case FT_NOFSCHG:
406    case FT_NOOPEN:
407       Qmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
408       break;
409    default:
410       Qmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
411       break;
412    }
413    return CF_ERROR;
414 }
415
416 /*
417  *  Returns: > 0 index into path where last path char is.
418  *           0  no path
419  *           -1 filename is zero length
420  */
421 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
422 {
423    char *f, *p, *q;
424    int fnl, pnl;
425
426    /* Separate pathname and filename */
427    for (q=p=f=ofile; *p; p++) {
428 #ifdef HAVE_WIN32
429       if (IsPathSeparator(*p)) {
430          f = q;
431          if (IsPathSeparator(p[1])) {
432             p++;
433          }
434       }
435       *q++ = *p;                   /* copy data */
436 #else
437       if (IsPathSeparator(*p)) {
438          f = q;                    /* possible filename */
439       }
440       q++;
441 #endif
442    }
443
444    if (IsPathSeparator(*f)) {
445       f++;
446    }
447    *q = 0;                         /* terminate string */
448
449    fnl = q - f;
450    if (fnl == 0) {
451       /* The filename length must not be zero here because we
452        *  are dealing with a file (i.e. FT_REGE or FT_REG).
453        */
454       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
455       return -1;
456    }
457    pnl = f - ofile - 1;
458    return pnl;
459 }
460
461 /*
462  * Primitive caching of path to prevent recreating a pathname
463  *   each time as long as we remain in the same directory.
464  */
465 static int path_already_seen(JCR *jcr, char *path, int pnl)
466 {
467    if (!jcr->cached_path) {
468       jcr->cached_path = get_pool_memory(PM_FNAME);
469    }
470    if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
471       return 1;
472    }
473    pm_strcpy(jcr->cached_path, path);
474    jcr->cached_pnl = pnl;
475    return 0;
476 }