]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
3ad0006cf8922ed92d8234d001e2e85897c62fff
[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 /* Data received from Storage Daemon */
38 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
39
40 /* Forward referenced functions */
41 #ifdef HAVE_LIBZ
42 static const char *zlib_strerror(int stat);
43 #endif
44
45 #define RETRY 10                      /* retry wait time */
46
47 /* 
48  * Restore the requested files.
49  * 
50  */
51 void do_restore(JCR *jcr)
52 {
53    BSOCK *sd;
54    int32_t stream;
55    uint32_t size;
56    uint32_t VolSessionId, VolSessionTime;
57    int32_t file_index;
58    bool extract = false;
59    BFILE bfd;
60    int stat;
61    uint32_t total = 0;                /* Job total but only 32 bits for debug */
62    char *wbuf;                        /* write buffer */
63    uint32_t wsize;                    /* write size */
64    uint64_t fileAddr = 0;             /* file write address */
65    int non_support_data = 0;
66    int non_support_attr = 0;
67    int non_support_acl = 0;
68    int prog_name_msg = 0;
69    ATTR *attr;
70 #ifdef HAVE_ACL
71    acl_t acl;
72 #endif
73
74    binit(&bfd);
75    sd = jcr->store_bsock;
76    set_jcr_job_status(jcr, JS_Running);
77
78    LockRes();
79    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
80    UnlockRes();
81    uint32_t buf_size;
82    if (client) {
83       buf_size = client->max_network_buffer_size;
84    } else {
85       buf_size = 0;                   /* use default */
86    }
87    if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
88       set_jcr_job_status(jcr, JS_ErrorTerminated);
89       return;
90    }
91    jcr->buf_size = sd->msglen;
92
93    attr = new_attr();
94
95 #ifdef HAVE_LIBZ
96    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
97    jcr->compress_buf = (char *)bmalloc(compress_buf_size);
98 #endif
99
100    /* 
101     * Get a record from the Storage daemon. We are guaranteed to 
102     *   receive records in the following order:
103     *   1. Stream record header
104     *   2. Stream data
105     *        a. Attributes (Unix or Win32)
106     *    or  b. File data for the file
107     *    or  c. Possibly MD5 or SHA1 record
108     *   3. Repeat step 1
109     */
110    while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
111       /*
112        * First we expect a Stream Record Header 
113        */
114       if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
115           &stream, &size) != 5) {
116          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
117          goto bail_out;
118       }
119       Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
120
121       /* 
122        * Now we expect the Stream Data
123        */
124       if (bget_msg(sd) < 0) {
125          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
126          goto bail_out;
127       }
128       if (size != (uint32_t)sd->msglen) {
129          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
130          goto bail_out;
131       }
132       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
133
134       /* File Attributes stream */
135       switch (stream) {
136       case STREAM_UNIX_ATTRIBUTES:
137       case STREAM_UNIX_ATTRIBUTES_EX:
138          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
139          /* If extracting, it was from previous stream, so
140           * close the output file.
141           */
142          if (extract) {
143             if (!is_bopen(&bfd)) {
144                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
145             }
146             set_attributes(jcr, attr, &bfd);
147             extract = false;
148             Dmsg0(30, "Stop extracting.\n");
149          }
150
151          if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
152             goto bail_out;
153          }
154          if (file_index != attr->file_index) {
155             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
156                  file_index, attr->file_index);
157             Dmsg0(100, "File index error\n");
158             goto bail_out;
159          }
160             
161          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname, 
162                attr->attr, attr->attrEx);
163
164          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
165
166          if (!is_stream_supported(attr->data_stream)) {
167             if (!non_support_data++) {
168                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
169                   stream_to_ascii(attr->data_stream));
170             }
171             continue;
172          }
173
174          build_attr_output_fnames(jcr, attr);
175
176          jcr->num_files_examined++;
177
178          Dmsg1(30, "Outfile=%s\n", attr->ofname);
179          extract = false;
180          stat = create_file(jcr, attr, &bfd, jcr->replace);
181          switch (stat) {
182          case CF_ERROR:
183          case CF_SKIP:
184             break;
185          case CF_EXTRACT:
186             extract = true;
187             P(jcr->mutex);
188             pm_strcpy(jcr->last_fname, attr->ofname);
189             V(jcr->mutex);
190             jcr->JobFiles++;
191             fileAddr = 0;
192             print_ls_output(jcr, attr);
193             /* Set attributes after file extracted */
194             break;
195          case CF_CREATED:
196             P(jcr->mutex);
197             pm_strcpy(jcr->last_fname, attr->ofname);
198             V(jcr->mutex);
199             jcr->JobFiles++;
200             fileAddr = 0;
201             print_ls_output(jcr, attr);
202             /* set attributes now because file will not be extracted */
203             set_attributes(jcr, attr, &bfd);
204             break;
205          }  
206          break;
207
208       /* Data stream */
209       case STREAM_FILE_DATA:
210       case STREAM_SPARSE_DATA:  
211       case STREAM_WIN32_DATA:  
212          if (extract) {
213             if (stream == STREAM_SPARSE_DATA) {
214                ser_declare;
215                uint64_t faddr;
216                char ec1[50];
217
218                wbuf = sd->msg + SPARSE_FADDR_SIZE;
219                wsize = sd->msglen - SPARSE_FADDR_SIZE;
220                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
221                unser_uint64(faddr);
222                if (fileAddr != faddr) {
223                   fileAddr = faddr;
224                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
225                      berrno be;
226                      be.set_errno(bfd.berrno);
227                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
228                          edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
229                      extract = false;
230                      bclose(&bfd);
231                      continue;
232                   }
233                }
234             } else {
235                wbuf = sd->msg;
236                wsize = sd->msglen;
237             }
238             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
239             if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
240                Dmsg0(0, "===Write error===\n");
241                berrno be;
242                be.set_errno(bfd.berrno);
243                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, 
244                      be.strerror());
245                extract = false;
246                bclose(&bfd);
247                continue;
248             } 
249             total += wsize;
250             jcr->JobBytes += wsize;
251             jcr->ReadBytes += wsize;
252             fileAddr += wsize;
253          }
254          break;
255
256       /* GZIP data stream */
257       case STREAM_GZIP_DATA:
258       case STREAM_SPARSE_GZIP_DATA:  
259       case STREAM_WIN32_GZIP_DATA:  
260 #ifdef HAVE_LIBZ
261          if (extract) {
262             uLong compress_len;
263             int stat;
264
265             if (stream == STREAM_SPARSE_GZIP_DATA) {
266                ser_declare;
267                uint64_t faddr;
268                char ec1[50];
269                wbuf = sd->msg + SPARSE_FADDR_SIZE;
270                wsize = sd->msglen - SPARSE_FADDR_SIZE;
271                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
272                unser_uint64(faddr);
273                if (fileAddr != faddr) {
274                   fileAddr = faddr;
275                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
276                      berrno be;
277                      be.set_errno(bfd.berrno);
278                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
279                          edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
280                      extract = false;
281                      bclose(&bfd);
282                      continue;
283                   }
284                }
285             } else {
286                wbuf = sd->msg;
287                wsize = sd->msglen;
288             }
289             compress_len = compress_buf_size;
290             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
291             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
292                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
293                Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"), 
294                   attr->ofname, zlib_strerror(stat));
295                extract = false;
296                bclose(&bfd);
297                continue;
298             }
299
300             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
301             if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
302                Dmsg0(0, "===Write error===\n");
303                berrno be;
304                be.set_errno(bfd.berrno);
305                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror());
306                extract = false;
307                bclose(&bfd);
308                continue;
309             }
310             total += compress_len;
311             jcr->JobBytes += compress_len;
312             jcr->ReadBytes += wsize;
313             fileAddr += compress_len;
314          }
315 #else
316          if (extract) {
317             Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
318             extract = false;
319             bclose(&bfd);
320             continue;
321          }
322 #endif
323          break;
324
325       case STREAM_UNIX_ATTRIBUTES_ACL:   
326 #ifdef HAVE_ACL
327          /* Recover ACL from stream and check it */
328          acl = acl_from_text(sd->msg);
329          if (acl_valid(acl) != 0) {
330             Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
331             acl_free(acl);
332          }
333          
334          /* Try to restore ACL */
335          if (attr->type == FT_DIREND) {
336             /* Directory */
337             if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
338                 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
339                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
340             }
341          /* File or Link */
342          } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
343             Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
344          }
345          acl_free(acl);
346          Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
347          break;
348 #else 
349          non_support_acl++;
350          break;                       /* unconfigured, ignore */
351 #endif   
352          
353       case STREAM_MD5_SIGNATURE:
354       case STREAM_SHA1_SIGNATURE:
355          break;
356
357       case STREAM_PROGRAM_NAMES:
358       case STREAM_PROGRAM_DATA:
359          if (!prog_name_msg) {
360             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
361             prog_name_msg++;
362          }
363          break;
364
365       default:
366          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
367          if (extract) {
368             Dmsg1(30, "Found wierd stream %d\n", stream);
369             if (!is_bopen(&bfd)) {
370                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
371             }
372             set_attributes(jcr, attr, &bfd);
373             extract = false;
374          }
375          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
376          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
377          break;
378       } /* end switch(stream) */
379
380    } /* end while get_msg() */
381
382    /* If output file is still open, it was the last one in the
383     * archive since we just hit an end of file, so close the file. 
384     */
385    if (is_bopen(&bfd)) {
386       set_attributes(jcr, attr, &bfd);
387    }
388    set_jcr_job_status(jcr, JS_Terminated);
389    goto ok_out;
390
391 bail_out:
392    set_jcr_job_status(jcr, JS_ErrorTerminated);
393 ok_out:
394    if (jcr->compress_buf) {
395       free(jcr->compress_buf);
396       jcr->compress_buf = NULL;
397    }
398    bclose(&bfd);
399    free_attr(attr);
400    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
401       jcr->JobBytes);
402    if (non_support_data > 1 || non_support_attr > 1) {
403       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
404          non_support_data, non_support_attr);
405    }
406    if (non_support_acl) {
407       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
408    }
409
410 }          
411
412 #ifdef HAVE_LIBZ
413 /*
414  * Convert ZLIB error code into an ASCII message
415  */
416 static const char *zlib_strerror(int stat)
417 {
418    if (stat >= 0) {
419       return "None";
420    }
421    switch (stat) {
422    case Z_ERRNO:
423       return "Zlib errno";
424    case Z_STREAM_ERROR:
425       return "Zlib stream error";
426    case Z_DATA_ERROR:
427       return "Zlib data error";
428    case Z_MEM_ERROR:
429       return "Zlib memory error";
430    case Z_BUF_ERROR:
431       return "Zlib buffer error";
432    case Z_VERSION_ERROR:
433       return "Zlib version error";
434    default:
435       return "*none*";
436    }
437 }
438 #endif