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