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