]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Integrate Preben's restore patch.
[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-2004 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, uint64_t *addr,
49         uint32_t *total, 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  * ***FIXME***
57  * NOTE: set_win32_attributes() may now have to reopen the file :-(
58  */
59 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
60 {
61    char ec1[50], ec2[50];
62    off_t fsize;
63
64    fsize = blseek(bfd, 0, SEEK_CUR);
65    bclose(bfd);                              /* first close file */
66    if (fsize > 0 && fsize != osize) {
67       Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
68             jcr->last_fname, edit_uint64(osize, ec1),
69             edit_uint64(fsize, ec2));
70       return -1;
71    }
72    return 0;
73 }
74
75 /*
76  * Restore the requested files.
77  *
78  */
79 void do_restore(JCR *jcr)
80 {
81    BSOCK *sd;
82    int32_t stream, prev_stream;
83    uint32_t size;
84    uint32_t VolSessionId, VolSessionTime;
85    int32_t file_index;
86    bool extract = false;
87    BFILE bfd;
88    int stat;
89    uint32_t total = 0;                /* Job total but only 32 bits for debug */
90    uint64_t fileAddr = 0;             /* file write address */
91    intmax_t want_len = 0;             /* How many bytes we expect to write */
92    int flags;                         /* Options for extract_data() */
93    int non_support_data = 0;
94    int non_support_attr = 0;
95    int non_support_rsrc = 0;
96    int non_support_finfo = 0;
97    int non_support_acl = 0;
98    int prog_name_msg = 0;
99    ATTR *attr;
100 #ifdef HAVE_ACL
101    acl_t acl;
102 #endif
103 #ifdef HAVE_DARWIN_OS
104    intmax_t rsrc_len;                 /* original length of resource fork */
105
106    /* TODO: initialise attrList once elsewhere? */
107    struct attrlist attrList;
108    memset(&attrList, 0, sizeof(attrList));
109    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
110    attrList.commonattr = ATTR_CMN_FNDRINFO;
111 #endif
112
113    binit(&bfd);
114    sd = jcr->store_bsock;
115    set_jcr_job_status(jcr, JS_Running);
116
117    LockRes();
118    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
119    UnlockRes();
120    uint32_t buf_size;
121    if (client) {
122       buf_size = client->max_network_buffer_size;
123    } else {
124       buf_size = 0;                   /* use default */
125    }
126    if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
127       set_jcr_job_status(jcr, JS_ErrorTerminated);
128       return;
129    }
130    jcr->buf_size = sd->msglen;
131
132    attr = new_attr();
133
134 #ifdef HAVE_LIBZ
135    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
136    jcr->compress_buf = (char *)bmalloc(compress_buf_size);
137    jcr->compress_buf_size = compress_buf_size;
138 #endif
139
140    /*
141     * Get a record from the Storage daemon. We are guaranteed to
142     *   receive records in the following order:
143     *   1. Stream record header
144     *   2. Stream data
145     *        a. Attributes (Unix or Win32)
146     *    or  b. File data for the file
147     *    or  c. Resource fork
148     *    or  d. Finder info
149     *    or  e. ACLs
150     *    or  f. Possibly MD5 or SHA1 record
151     *   3. Repeat step 1
152     */
153    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
154       prev_stream = stream;
155
156       /* First we expect a Stream Record Header */
157       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
158           &stream, &size) != 5) {
159          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
160          goto bail_out;
161       }
162       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
163
164       /* * Now we expect the Stream Data */
165       if (bget_msg(sd) < 0) {
166          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
167          goto bail_out;
168       }
169       if (size != (uint32_t)sd->msglen) {
170          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
171          goto bail_out;
172       }
173       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
174
175       /* File Attributes stream */
176       switch (stream) {
177       case STREAM_UNIX_ATTRIBUTES:
178       case STREAM_UNIX_ATTRIBUTES_EX:
179          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
180          /*
181           * If extracting, it was from previous stream, so
182           * close the output file.
183           */
184          if (extract) {
185             if (!is_bopen(&bfd)) {
186                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
187             } else {
188                bclose_chksize(jcr, &bfd, want_len);
189                set_attributes(jcr, attr, &bfd);
190             }
191             extract = false;
192             Dmsg0(30, "Stop extracting.\n");
193          }
194
195          if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
196             goto bail_out;
197          }
198          if (file_index != attr->file_index) {
199             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
200                  file_index, attr->file_index);
201             Dmsg0(100, "File index error\n");
202             goto bail_out;
203          }
204
205          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
206                attr->attr, attr->attrEx);
207
208          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
209
210          if (!is_stream_supported(attr->data_stream)) {
211             if (!non_support_data++) {
212                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
213                   stream_to_ascii(attr->data_stream));
214             }
215             continue;
216          }
217
218          build_attr_output_fnames(jcr, attr);
219
220          jcr->num_files_examined++;
221
222          Dmsg1(30, "Outfile=%s\n", attr->ofname);
223          extract = false;
224          stat = create_file(jcr, attr, &bfd, jcr->replace);
225          switch (stat) {
226          case CF_ERROR:
227          case CF_SKIP:
228             break;
229          case CF_EXTRACT:
230             extract = true;
231             /* FALLTHROUGH */
232          case CF_CREATED:
233             P(jcr->mutex);
234             pm_strcpy(jcr->last_fname, attr->ofname);
235             V(jcr->mutex);
236             jcr->JobFiles++;
237             fileAddr = 0;
238             want_len = attr->statp.st_size;
239             print_ls_output(jcr, attr);
240 #ifdef HAVE_DARWIN_OS
241             /* Only restore the resource fork for regular files */
242             from_base64(&rsrc_len, attr->attrEx);
243             if (attr->type == FT_REG && rsrc_len > 0) {
244                extract = true;
245             }
246 #endif
247             if (!extract) {
248                /* set attributes now because file will not be extracted */
249                set_attributes(jcr, attr, &bfd);
250             }
251             break;
252          }
253          break;
254
255       /* Data stream */
256       case STREAM_FILE_DATA:
257       case STREAM_SPARSE_DATA:
258       case STREAM_WIN32_DATA:
259       case STREAM_GZIP_DATA:
260       case STREAM_SPARSE_GZIP_DATA:
261       case STREAM_WIN32_GZIP_DATA:
262          /* Force an expected, consistent stream type here */
263          if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
264                   || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
265             flags = 0;
266             if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
267                flags |= FO_SPARSE;
268             }
269             if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
270                   || stream == STREAM_WIN32_GZIP_DATA) {
271                flags |= FO_GZIP;
272             }
273             if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, &total, flags) < 0) {
274                extract = false;
275                bclose(&bfd);
276                continue;
277             }
278          }
279          break;
280
281       /* Resource fork stream - only recorded after a file to be restored */
282       /* Silently ignore if we cannot write - we already reported that */
283       case STREAM_MACOS_FORK_DATA:
284 #ifdef HAVE_DARWIN_OS
285          if (extract) {
286             if (prev_stream != stream) {
287                if (is_bopen(&bfd)) {
288                   bclose_chksize(jcr, &bfd, want_len);
289                }
290                if (bopen_rsrc(&bfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
291                   Jmsg(jcr, M_ERROR, 0, _("     Cannot open resource fork for %s"), jcr->last_fname);
292                   extract = false;
293                   continue;
294                }
295                want_len = rsrc_len;
296                Dmsg0(30, "Restoring resource fork");
297             }
298             flags = 0;
299             if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, &total, flags) < 0) {
300                extract = false;
301                bclose(&bfd);
302                continue;
303             }
304          }
305 #else
306          non_support_rsrc++;
307 #endif
308          break;
309
310       case STREAM_HFSPLUS_ATTRIBUTES:
311 #ifdef HAVE_DARWIN_OS
312          Dmsg0(30, "Restoring Finder Info");
313          if (sd->msglen != 32) {
314             Jmsg(jcr, M_ERROR, 0, _("     Invalid length of Finder Info (got %d, not 32)"), sd->msglen);
315             continue;
316          }
317          if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
318             Jmsg(jcr, M_ERROR, 0, _("     Could not set Finder Info on %s"), jcr->last_fname);
319             continue;
320          }
321 #else
322          non_support_finfo++;
323 #endif
324          break;
325
326 /*** FIXME ***/
327 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
328 #ifdef HAVE_ACL
329          /* Recover Acess ACL from stream and check it */
330          acl = acl_from_text(sd->msg);
331          if (acl_valid(acl) != 0) {
332             Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
333             acl_free(acl);
334          }
335
336          /* Try to restore ACL */
337          if (attr->type == FT_DIREND) {
338             /* Directory */
339             if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
340                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
341             }
342          /* File or Link */
343          } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
344             Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
345          }
346          acl_free(acl);
347          Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
348          break;
349 #else
350          non_support_acl++;
351          break;                       /* unconfigured, ignore */
352 #endif
353       case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
354 #ifdef HAVE_ACL
355       /* Recover Default ACL from stream and check it */
356          acl = acl_from_text(sd->msg);
357          if (acl_valid(acl) != 0) {
358             Jmsg1(jcr, M_WARNING, 0, "Failure in the Default ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
359             acl_free(acl);
360          }
361
362          /* Try to restore ACL */
363          if (attr->type == FT_DIREND) {
364             /* Directory */
365             if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0) {
366                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore Default ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
367              }
368          }
369          acl_free(acl);
370          Dmsg1(200, "Default ACL of file: %s successfully restored!", jcr->last_fname);
371          break;
372 #else
373          non_support_acl++;
374          break;                       /* unconfigured, ignore */
375 #endif
376 /*** FIXME ***/
377
378       case STREAM_MD5_SIGNATURE:
379       case STREAM_SHA1_SIGNATURE:
380          break;
381
382       case STREAM_PROGRAM_NAMES:
383       case STREAM_PROGRAM_DATA:
384          if (!prog_name_msg) {
385             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
386             prog_name_msg++;
387          }
388          break;
389
390       default:
391          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
392          if (extract) {
393             Dmsg1(30, "Found wierd stream %d\n", stream);
394             if (!is_bopen(&bfd)) {
395                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
396             }
397             set_attributes(jcr, attr, &bfd);
398             extract = false;
399          }
400          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
401          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
402          break;
403       } /* end switch(stream) */
404
405    } /* end while get_msg() */
406
407    /* If output file is still open, it was the last one in the
408     * archive since we just hit an end of file, so close the file.
409     */
410    if (is_bopen(&bfd)) {
411       bclose_chksize(jcr, &bfd, want_len);
412       set_attributes(jcr, attr, &bfd);
413    }
414    set_jcr_job_status(jcr, JS_Terminated);
415    goto ok_out;
416
417 bail_out:
418    set_jcr_job_status(jcr, JS_ErrorTerminated);
419 ok_out:
420    if (jcr->compress_buf) {
421       free(jcr->compress_buf);
422       jcr->compress_buf = NULL;
423       jcr->compress_buf_size = 0;
424    }
425    bclose(&bfd);
426    free_attr(attr);
427    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
428       jcr->JobBytes);
429    if (non_support_data > 1 || non_support_attr > 1) {
430       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
431          non_support_data, non_support_attr);
432    }
433    if (non_support_rsrc) {
434       Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
435    }
436    if (non_support_finfo) {
437       Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
438    }
439    if (non_support_acl) {
440       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
441    }
442
443 }
444
445 #ifdef HAVE_LIBZ
446 /*
447  * Convert ZLIB error code into an ASCII message
448  */
449 static const char *zlib_strerror(int stat)
450 {
451    if (stat >= 0) {
452       return "None";
453    }
454    switch (stat) {
455    case Z_ERRNO:
456       return "Zlib errno";
457    case Z_STREAM_ERROR:
458       return "Zlib stream error";
459    case Z_DATA_ERROR:
460       return "Zlib data error";
461    case Z_MEM_ERROR:
462       return "Zlib memory error";
463    case Z_BUF_ERROR:
464       return "Zlib buffer error";
465    case Z_VERSION_ERROR:
466       return "Zlib version error";
467    default:
468       return "*none*";
469    }
470 }
471 #endif
472
473 /*
474  * In the context of jcr, write data to bfd.
475  * We write buflen bytes in buf at addr. addr is updated in place.
476  * The flags specify whether to use sparse files or compression.
477  * Return value is the number of bytes written, or -1 on errors.
478  *
479  * ***FIXME***
480  * We update total in here. For some reason, jcr->JobBytes does not work here.
481  */
482 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, uint64_t *addr,
483         uint32_t *total, int flags)
484 {
485    uLong compress_len;
486    int stat;
487    char *wbuf;                        /* write buffer */
488    uint32_t wsize;                    /* write size */
489    uint32_t rsize;                    /* read size */
490
491    if (flags & FO_SPARSE) {
492       ser_declare;
493       uint64_t faddr;
494       char ec1[50];
495       wbuf = buf + SPARSE_FADDR_SIZE;
496       rsize = buflen - SPARSE_FADDR_SIZE;
497       ser_begin(buf, SPARSE_FADDR_SIZE);
498       unser_uint64(faddr);
499       if (*addr != faddr) {
500          *addr = faddr;
501          if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
502             berrno be;
503             be.set_errno(bfd->berrno);
504             Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
505                   edit_uint64(*addr, ec1), jcr->last_fname, be.strerror());
506             return -1;
507          }
508       }
509    } else {
510       wbuf = buf;
511       rsize = buflen;
512    }
513    wsize = rsize;
514
515    if (flags & FO_GZIP) {
516 #ifdef HAVE_LIBZ
517       compress_len = jcr->compress_buf_size;
518       Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
519       if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
520                   (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
521          Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
522                jcr->last_fname, zlib_strerror(stat));
523          return -1;
524       }
525       wbuf = jcr->compress_buf;
526       wsize = compress_len;
527       Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, *total);
528 #else
529       Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
530       return -1;
531 #endif
532    } else {
533       Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, *total);
534    }
535
536    if ((uLong)bwrite(bfd, wbuf, wsize) != wsize) {
537       Dmsg0(0, "===Write error===\n");
538       berrno be;
539       be.set_errno(bfd->berrno);
540       Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), jcr->last_fname, be.strerror());
541       return -1;
542    }
543
544    *total += wsize;
545    jcr->JobBytes += wsize;
546    jcr->ReadBytes += rsize;
547    *addr += wsize;
548
549    return wsize;
550 }