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