]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Integrate Nicolas' patch for direct DVD support.
[bacula/bacula] / bacula / src / filed / restore.c
1 /*
2  *  Bacula File Daemon  restore.c Restorefiles.
3  *
4  *    Kern Sibbald, November MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 #ifdef HAVE_ACL
33 #include <sys/acl.h>
34 #include <acl/libacl.h>
35 #endif
36
37 #ifdef HAVE_DARWIN_OS
38 #include <sys/attr.h>
39 #endif
40
41 /* Data received from Storage Daemon */
42 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
43
44 /* Forward referenced functions */
45 #ifdef HAVE_LIBZ
46 static const char *zlib_strerror(int stat);
47 #endif
48 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
49       uint64_t *addr, int flags);
50
51 #define RETRY 10                      /* retry wait time */
52
53 /*
54  * Close a bfd check that we are at the expected file offset.
55  * Makes some code in set_attributes().
56  */
57 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
58 {
59    char ec1[50], ec2[50];
60    off_t fsize;
61
62    fsize = blseek(bfd, 0, SEEK_CUR);
63    bclose(bfd);                              /* first close file */
64    if (fsize > 0 && fsize != osize) {
65       Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
66             jcr->last_fname, edit_uint64(osize, ec1),
67             edit_uint64(fsize, ec2));
68       return -1;
69    }
70    return 0;
71 }
72
73 /*
74  * Restore the requested files.
75  *
76  */
77 void do_restore(JCR *jcr)
78 {
79    BSOCK *sd;
80    int32_t stream, prev_stream;
81    uint32_t VolSessionId, VolSessionTime;
82    bool extract = false;
83    int32_t file_index;
84    char ec1[50];                      /* Buffer printing huge values */
85
86    BFILE bfd;                         /* File content */
87    uint64_t fileAddr = 0;             /* file write address */
88    uint32_t size;                     /* Size of file */
89    BFILE altbfd;                      /* Alternative data stream */
90    uint64_t alt_addr = 0;             /* Write address for alternative stream */
91    intmax_t alt_size = 0;             /* Size of alternate stream */
92    int flags;                         /* Options for extract_data() */
93    int stat;
94    ATTR *attr;
95
96    /* The following variables keep track of "known unknowns" */
97    int non_support_data = 0;
98    int non_support_attr = 0;
99    int non_support_rsrc = 0;
100    int non_support_finfo = 0;
101    int non_support_acl = 0;
102    int non_support_progname = 0;
103
104    /* Finally, set up for special configurations */
105 #ifdef HAVE_ACL
106    acl_t acl;
107 #endif
108 #ifdef HAVE_DARWIN_OS
109    intmax_t rsrc_len = 0;             /* Original length of resource fork */
110    struct attrlist attrList;
111
112    memset(&attrList, 0, sizeof(attrList));
113    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
114    attrList.commonattr = ATTR_CMN_FNDRINFO;
115 #endif
116
117    sd = jcr->store_bsock;
118    set_jcr_job_status(jcr, JS_Running);
119
120    LockRes();
121    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
122    UnlockRes();
123    uint32_t buf_size;
124    if (client) {
125       buf_size = client->max_network_buffer_size;
126    } else {
127       buf_size = 0;                   /* use default */
128    }
129    if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
130       set_jcr_job_status(jcr, JS_ErrorTerminated);
131       return;
132    }
133    jcr->buf_size = sd->msglen;
134
135 #ifdef HAVE_LIBZ
136    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
137    jcr->compress_buf = (char *)bmalloc(compress_buf_size);
138    jcr->compress_buf_size = compress_buf_size;
139 #endif
140
141    /*
142     * Get a record from the Storage daemon. We are guaranteed to
143     *   receive records in the following order:
144     *   1. Stream record header
145     *   2. Stream data
146     *        a. Attributes (Unix or Win32)
147     *    or  b. File data for the file
148     *    or  c. Alternate data stream (e.g. Resource Fork)
149     *    or  d. Finder info
150     *    or  e. ACLs
151     *    or  f. Possibly MD5 or SHA1 record
152     *   3. Repeat step 1
153     *
154     * NOTE: We keep track of two bacula file descriptors:
155     *   1. bfd for file data.
156     *      This fd is opened for non empty files when an attribute stream is
157     *      encountered and closed when we find the next attribute stream.
158     *   2. alt_bfd for alternate data streams
159     *      This fd is opened every time we encounter a new alternate data
160     *      stream for the current file. When we find any other stream, we
161     *      close it again.
162     *      The expected size of the stream, alt_len, should be set when
163     *      opening the fd.
164     */
165    binit(&bfd);
166    binit(&altbfd);
167    attr = new_attr();
168
169    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
170       /* Remember previous stream type */
171       prev_stream = stream;
172
173       /* First we expect a Stream Record Header */
174       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
175           &stream, &size) != 5) {
176          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
177          goto bail_out;
178       }
179       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
180
181       /* * Now we expect the Stream Data */
182       if (bget_msg(sd) < 0) {
183          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
184          goto bail_out;
185       }
186       if (size != (uint32_t)sd->msglen) {
187          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
188          goto bail_out;
189       }
190       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
191
192       /* If we change streams, close and reset alternate data streams */
193       if (prev_stream != stream) {
194          if (is_bopen(&altbfd)) {
195             bclose_chksize(jcr, &altbfd, alt_size);
196          }
197          alt_size = -1; /* Use an impossible value and set a proper one below */
198          alt_addr = 0;
199       }
200
201       /* File Attributes stream */
202       switch (stream) {
203       case STREAM_UNIX_ATTRIBUTES:
204       case STREAM_UNIX_ATTRIBUTES_EX:
205          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
206          /*
207           * If extracting, it was from previous stream, so
208           * close the output file.
209           */
210          if (extract) {
211             if (size > 0 && !is_bopen(&bfd)) {
212                Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
213             }
214             set_attributes(jcr, attr, &bfd);
215             extract = false;
216             Dmsg0(30, "Stop extracting.\n");
217          } else if (is_bopen(&bfd)) {
218             Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
219             bclose(&bfd);
220          }
221
222          /*
223           * Unpack and do sanity check fo attributes.
224           */
225          if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
226             goto bail_out;
227          }
228          if (file_index != attr->file_index) {
229             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
230                  file_index, attr->file_index);
231             Dmsg0(100, "File index error\n");
232             goto bail_out;
233          }
234
235          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
236                attr->attr, attr->attrEx);
237
238          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
239
240          if (!is_stream_supported(attr->data_stream)) {
241             if (!non_support_data++) {
242                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
243                   stream_to_ascii(attr->data_stream));
244             }
245             continue;
246          }
247
248          build_attr_output_fnames(jcr, attr);
249
250          /*
251           * Now determine if we are extracting or not.
252           */
253          jcr->num_files_examined++;
254          Dmsg1(30, "Outfile=%s\n", attr->ofname);
255          extract = false;
256          stat = create_file(jcr, attr, &bfd, jcr->replace);
257          switch (stat) {
258          case CF_ERROR:
259          case CF_SKIP:
260             break;
261          case CF_EXTRACT:        /* File created and we expect file data */
262             extract = true;
263             /* FALLTHROUGH */
264          case CF_CREATED:        /* File created, but there is no content */
265             P(jcr->mutex);
266             pm_strcpy(jcr->last_fname, attr->ofname);
267             V(jcr->mutex);
268             jcr->JobFiles++;
269             fileAddr = 0;
270             print_ls_output(jcr, attr);
271 #ifdef HAVE_DARWIN_OS
272             /* Only restore the resource fork for regular files */
273             from_base64(&rsrc_len, attr->attrEx);
274             if (attr->type == FT_REG && rsrc_len > 0) {
275                extract = true;
276             }
277 #endif
278             if (!extract) {
279                /* set attributes now because file will not be extracted */
280                set_attributes(jcr, attr, &bfd);
281             }
282             break;
283          }
284          break;
285
286       /* Data stream */
287       case STREAM_FILE_DATA:
288       case STREAM_SPARSE_DATA:
289       case STREAM_WIN32_DATA:
290       case STREAM_GZIP_DATA:
291       case STREAM_SPARSE_GZIP_DATA:
292       case STREAM_WIN32_GZIP_DATA:
293          /* Force an expected, consistent stream type here */
294          if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
295                   || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
296             flags = 0;
297             if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
298                flags |= FO_SPARSE;
299             }
300             if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
301                   || stream == STREAM_WIN32_GZIP_DATA) {
302                flags |= FO_GZIP;
303             }
304             if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags) < 0) {
305                extract = false;
306                bclose(&bfd);
307                continue;
308             }
309          }
310          break;
311
312       /* Resource fork stream - only recorded after a file to be restored */
313       /* Silently ignore if we cannot write - we already reported that */
314       case STREAM_MACOS_FORK_DATA:
315 #ifdef HAVE_DARWIN_OS
316          if (extract) {
317             if (prev_stream != stream) {
318                if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
319                   Jmsg(jcr, M_ERROR, 0, _("     Cannot open resource fork for %s\n"), jcr->last_fname);
320                   extract = false;
321                   continue;
322                }
323                alt_size = rsrc_len;
324                Dmsg0(30, "Restoring resource fork\n");
325             }
326             flags = 0;
327             if (extract_data(jcr, &altbfd, sd->msg, sd->msglen, &alt_addr, flags) < 0) {
328                extract = false;
329                bclose(&altbfd);
330                continue;
331             }
332          }
333 #else
334          non_support_rsrc++;
335 #endif
336          break;
337
338       case STREAM_HFSPLUS_ATTRIBUTES:
339 #ifdef HAVE_DARWIN_OS
340          Dmsg0(30, "Restoring Finder Info\n");
341          if (sd->msglen != 32) {
342             Jmsg(jcr, M_ERROR, 0, _("     Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen);
343             continue;
344          }
345          if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
346             Jmsg(jcr, M_ERROR, 0, _("     Could not set Finder Info on %s\n"), jcr->last_fname);
347             continue;
348          }
349 #else
350          non_support_finfo++;
351 #endif
352          break;
353
354 /*** FIXME ***/
355 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
356 #ifdef HAVE_ACL
357          /* Recover Acess ACL from stream and check it */
358          acl = acl_from_text(sd->msg);
359          if (acl_valid(acl) != 0) {
360             Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
361             acl_free(acl);
362          }
363
364          /* Try to restore ACL */
365          if (attr->type == FT_DIREND) {
366             /* Directory */
367             if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
368                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
369             }
370          /* File or Link */
371          } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
372             Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
373          }
374          acl_free(acl);
375          Dmsg1(200, "ACL of file: %s successfully restored!\n", jcr->last_fname);
376          break;
377 #else
378          non_support_acl++;
379          break;                       /* unconfigured, ignore */
380 #endif
381       case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
382 #ifdef HAVE_ACL
383       /* Recover Default ACL from stream and check it */
384          acl = acl_from_text(sd->msg);
385          if (acl_valid(acl) != 0) {
386             Jmsg1(jcr, M_WARNING, 0, "Failure in the Default ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
387             acl_free(acl);
388          }
389
390          /* Try to restore ACL */
391          if (attr->type == FT_DIREND) {
392             /* Directory */
393             if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0) {
394                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore Default ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
395              }
396          }
397          acl_free(acl);
398          Dmsg1(200, "Default ACL of file: %s successfully restored!\n", jcr->last_fname);
399          break;
400 #else
401          non_support_acl++;
402          break;                       /* unconfigured, ignore */
403 #endif
404 /*** FIXME ***/
405
406       case STREAM_MD5_SIGNATURE:
407       case STREAM_SHA1_SIGNATURE:
408          break;
409
410       case STREAM_PROGRAM_NAMES:
411       case STREAM_PROGRAM_DATA:
412          if (!non_support_progname) {
413             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
414             non_support_progname++;
415          }
416          break;
417
418       default:
419          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
420          if (extract) {
421             Dmsg1(30, "Found wierd stream %d\n", stream);
422             if (size > 0 && !is_bopen(&bfd)) {
423                Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
424             }
425             set_attributes(jcr, attr, &bfd);
426             extract = false;
427          } else if (is_bopen(&bfd)) {
428             Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
429             bclose(&bfd);
430          }
431          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
432          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
433          break;
434       } /* end switch(stream) */
435
436    } /* end while get_msg() */
437
438    /* If output file is still open, it was the last one in the
439     * archive since we just hit an end of file, so close the file.
440     */
441    if (is_bopen(&altbfd)) {
442       bclose_chksize(jcr, &altbfd, alt_size);
443    }
444    if (extract) {
445       set_attributes(jcr, attr, &bfd);
446    }
447    if (is_bopen(&bfd)) {
448       bclose(&bfd);
449    }
450    set_jcr_job_status(jcr, JS_Terminated);
451    goto ok_out;
452
453 bail_out:
454    set_jcr_job_status(jcr, JS_ErrorTerminated);
455 ok_out:
456    if (jcr->compress_buf) {
457       free(jcr->compress_buf);
458       jcr->compress_buf = NULL;
459       jcr->compress_buf_size = 0;
460    }
461    bclose(&altbfd);
462    bclose(&bfd);
463    free_attr(attr);
464    Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
465       edit_uint64(jcr->JobBytes, ec1));
466    if (non_support_data > 1 || non_support_attr > 1) {
467       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
468          non_support_data, non_support_attr);
469    }
470    if (non_support_rsrc) {
471       Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
472    }
473    if (non_support_finfo) {
474       Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
475    }
476    if (non_support_acl) {
477       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
478    }
479
480 }
481
482 #ifdef HAVE_LIBZ
483 /*
484  * Convert ZLIB error code into an ASCII message
485  */
486 static const char *zlib_strerror(int stat)
487 {
488    if (stat >= 0) {
489       return "None";
490    }
491    switch (stat) {
492    case Z_ERRNO:
493       return "Zlib errno";
494    case Z_STREAM_ERROR:
495       return "Zlib stream error";
496    case Z_DATA_ERROR:
497       return "Zlib data error";
498    case Z_MEM_ERROR:
499       return "Zlib memory error";
500    case Z_BUF_ERROR:
501       return "Zlib buffer error";
502    case Z_VERSION_ERROR:
503       return "Zlib version error";
504    default:
505       return "*none*";
506    }
507 }
508 #endif
509
510 /*
511  * In the context of jcr, write data to bfd.
512  * We write buflen bytes in buf at addr. addr is updated in place.
513  * The flags specify whether to use sparse files or compression.
514  * Return value is the number of bytes written, or -1 on errors.
515  */
516 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
517       uint64_t *addr, int flags)
518 {
519    uLong compress_len;
520    int stat;
521    char *wbuf;                        /* write buffer */
522    uint32_t wsize;                    /* write size */
523    uint32_t rsize;                    /* read size */
524    char ec1[50];                      /* Buffer printing huge values */
525
526    if (flags & FO_SPARSE) {
527       ser_declare;
528       uint64_t faddr;
529       char ec1[50];
530       wbuf = buf + SPARSE_FADDR_SIZE;
531       rsize = buflen - SPARSE_FADDR_SIZE;
532       ser_begin(buf, SPARSE_FADDR_SIZE);
533       unser_uint64(faddr);
534       if (*addr != faddr) {
535          *addr = faddr;
536          if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
537             berrno be;
538             be.set_errno(bfd->berrno);
539             Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
540                   edit_uint64(*addr, ec1), jcr->last_fname, be.strerror());
541             return -1;
542          }
543       }
544    } else {
545       wbuf = buf;
546       rsize = buflen;
547    }
548    wsize = rsize;
549
550    if (flags & FO_GZIP) {
551 #ifdef HAVE_LIBZ
552       compress_len = jcr->compress_buf_size;
553       Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
554       if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
555                   (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
556          Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
557                jcr->last_fname, zlib_strerror(stat));
558          return -1;
559       }
560       wbuf = jcr->compress_buf;
561       wsize = compress_len;
562       Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
563 #else
564       Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
565       return -1;
566 #endif
567    } else {
568       Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
569    }
570
571    if ((uLong)bwrite(bfd, wbuf, wsize) != wsize) {
572       Dmsg0(0, "===Write error===\n");
573       berrno be;
574       be.set_errno(bfd->berrno);
575       Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), jcr->last_fname, be.strerror());
576       return -1;
577    }
578
579    jcr->JobBytes += wsize;
580    jcr->ReadBytes += rsize;
581    *addr += wsize;
582
583    return wsize;
584 }