]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
12f7c487badb84a704e6c8fd26d82449dcbacc9c
[bacula/bacula] / bacula / src / findlib / create_file.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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 John Walker.
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    int 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 (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
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 file %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          Dmsg1(50, "Create file: %s\n", attr->ofname);
213          if (is_bopen(bfd)) {
214             Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
215             bclose(bfd);
216          }
217       
218
219          if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
220             berrno be;
221             be.set_errno(bfd->berrno);
222             Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
223                   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(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             return CF_ERROR;
331 #ifdef HAVE_CHFLAGS
332                   }
333                   /* finally restore original file flags */
334                   if (chflags(attr->olname, s.st_flags) < 0) {
335                      Qmsg2(jcr, M_ERROR, 0, _("Could not restore file flags for file %s: ERR=%s\n"),
336                             attr->olname, be.bstrerror());
337                   }
338                } else {
339                  Qmsg2(jcr, M_ERROR, 0, _("Could not reset file flags for file %s: ERR=%s\n"),
340                        attr->olname, be.bstrerror());
341                }
342             } else {
343               Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
344                     attr->ofname, attr->olname, be.bstrerror());
345               return CF_ERROR;
346             }
347 #endif /* HAVE_CHFLAGS */
348
349          }
350          return CF_CREATED;
351 #endif
352       } /* End inner switch */
353
354    case FT_REPARSE:
355       bfd->reparse_point = true;
356       /* Fall through wanted */
357    case FT_DIRBEGIN:
358    case FT_DIREND:
359       Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
360       if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
361          return CF_ERROR;
362       }
363       /*
364        * If we are using the Win32 Backup API, we open the
365        *   directory so that the security info will be read
366        *   and saved.
367        */
368       if (!is_portable_backup(bfd)) {
369          if (is_bopen(bfd)) {
370             Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
371          }
372          if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
373             berrno be;
374             be.set_errno(bfd->berrno);
375 #ifdef HAVE_WIN32
376             /* Check for trying to create a drive, if so, skip */
377             if (attr->ofname[1] == ':' && 
378                 IsPathSeparator(attr->ofname[2]) && 
379                 attr->ofname[3] == '\0') {
380                return CF_SKIP;
381             }
382 #endif
383             Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
384                   attr->ofname, be.bstrerror());
385             return CF_ERROR;
386          }
387          return CF_EXTRACT;
388       } else {
389          return CF_CREATED;
390       }
391
392    /* The following should not occur */
393    case FT_NOACCESS:
394    case FT_NOFOLLOW:
395    case FT_NOSTAT:
396    case FT_DIRNOCHG:
397    case FT_NOCHG:
398    case FT_ISARCH:
399    case FT_NORECURSE:
400    case FT_NOFSCHG:
401    case FT_NOOPEN:
402       Qmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
403       break;
404    default:
405       Qmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
406       break;
407    }
408    return CF_ERROR;
409 }
410
411 /*
412  *  Returns: > 0 index into path where last path char is.
413  *           0  no path
414  *           -1 filename is zero length
415  */
416 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
417 {
418    char *f, *p, *q;
419    int fnl, pnl;
420
421    /* Separate pathname and filename */
422    for (q=p=f=ofile; *p; p++) {
423 #ifdef HAVE_WIN32
424       if (IsPathSeparator(*p)) {
425          f = q;
426          if (IsPathSeparator(p[1])) {
427             p++;
428          }
429       }
430       *q++ = *p;                   /* copy data */
431 #else
432       if (IsPathSeparator(*p)) {
433          f = q;                    /* possible filename */
434       }
435       q++;
436 #endif
437    }
438
439    if (IsPathSeparator(*f)) {
440       f++;
441    }
442    *q = 0;                         /* terminate string */
443
444    fnl = q - f;
445    if (fnl == 0) {
446       /* The filename length must not be zero here because we
447        *  are dealing with a file (i.e. FT_REGE or FT_REG).
448        */
449       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
450       return -1;
451    }
452    pnl = f - ofile - 1;
453    return pnl;
454 }
455
456 /*
457  * Primitive caching of path to prevent recreating a pathname
458  *   each time as long as we remain in the same directory.
459  */
460 static int path_already_seen(JCR *jcr, char *path, int pnl)
461 {
462    if (!jcr->cached_path) {
463       jcr->cached_path = get_pool_memory(PM_FNAME);
464    }
465    if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
466       return 1;
467    }
468    pm_strcpy(jcr->cached_path, path);
469    jcr->cached_pnl = pnl;
470    return 0;
471 }