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