]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Add regex to FileSet + fix ACL crash + gnome1 Makefile + 30 second retry for DB
[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-2003 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
139          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
140          /* If extracting, it was from previous stream, so
141           * close the output file.
142           */
143          if (extract) {
144             if (!is_bopen(&bfd)) {
145                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
146             }
147             set_attributes(jcr, attr, &bfd);
148             extract = false;
149             Dmsg0(30, "Stop extracting.\n");
150          }
151
152          if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
153             goto bail_out;
154          }
155          if (file_index != attr->file_index) {
156             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
157                  file_index, attr->file_index);
158             Dmsg0(100, "File index error\n");
159             goto bail_out;
160          }
161             
162          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname, 
163                attr->attr, attr->attrEx);
164
165          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
166
167          if (!is_stream_supported(attr->data_stream)) {
168             if (!non_support_data++) {
169                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
170                   stream_to_ascii(attr->data_stream));
171             }
172             continue;
173          }
174
175          build_attr_output_fnames(jcr, attr);
176
177          jcr->num_files_examined++;
178
179          Dmsg1(30, "Outfile=%s\n", attr->ofname);
180          extract = false;
181          stat = create_file(jcr, attr, &bfd, jcr->replace);
182          switch (stat) {
183          case CF_ERROR:
184          case CF_SKIP:
185             break;
186          case CF_EXTRACT:
187             extract = true;
188             P(jcr->mutex);
189             pm_strcpy(&jcr->last_fname, attr->ofname);
190             V(jcr->mutex);
191             jcr->JobFiles++;
192             fileAddr = 0;
193             print_ls_output(jcr, attr);
194             /* Set attributes after file extracted */
195             break;
196          case CF_CREATED:
197             P(jcr->mutex);
198             pm_strcpy(&jcr->last_fname, attr->ofname);
199             V(jcr->mutex);
200             jcr->JobFiles++;
201             fileAddr = 0;
202             print_ls_output(jcr, attr);
203             /* set attributes now because file will not be extracted */
204             set_attributes(jcr, attr, &bfd);
205             break;
206          }  
207          break;
208
209       /* Data stream */
210       case STREAM_FILE_DATA:
211       case STREAM_SPARSE_DATA:  
212       case STREAM_WIN32_DATA:  
213
214          if (extract) {
215             if (stream == STREAM_SPARSE_DATA) {
216                ser_declare;
217                uint64_t faddr;
218                char ec1[50];
219
220                wbuf = sd->msg + SPARSE_FADDR_SIZE;
221                wsize = sd->msglen - SPARSE_FADDR_SIZE;
222                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
223                unser_uint64(faddr);
224                if (fileAddr != faddr) {
225                   fileAddr = faddr;
226                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
227                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
228                          edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
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                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, berror(&bfd));
242                extract = false;
243                bclose(&bfd);
244                continue;
245             } 
246             total += wsize;
247             jcr->JobBytes += wsize;
248             jcr->ReadBytes += wsize;
249             fileAddr += wsize;
250          }
251          break;
252
253       /* GZIP data stream */
254       case STREAM_GZIP_DATA:
255       case STREAM_SPARSE_GZIP_DATA:  
256       case STREAM_WIN32_GZIP_DATA:  
257 #ifdef HAVE_LIBZ
258          if (extract) {
259             uLong compress_len;
260             int stat;
261
262             if (stream == STREAM_SPARSE_GZIP_DATA) {
263                ser_declare;
264                uint64_t faddr;
265                char ec1[50];
266                wbuf = sd->msg + SPARSE_FADDR_SIZE;
267                wsize = sd->msglen - SPARSE_FADDR_SIZE;
268                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
269                unser_uint64(faddr);
270                if (fileAddr != faddr) {
271                   fileAddr = faddr;
272                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
273                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
274                          edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
275                      extract = false;
276                      bclose(&bfd);
277                      continue;
278                   }
279                }
280             } else {
281                wbuf = sd->msg;
282                wsize = sd->msglen;
283             }
284             compress_len = compress_buf_size;
285             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
286             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
287                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
288                Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"), 
289                   attr->ofname, zlib_strerror(stat));
290                extract = false;
291                bclose(&bfd);
292                continue;
293             }
294
295             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
296             if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
297                Dmsg0(0, "===Write error===\n");
298                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, berror(&bfd));
299                extract = false;
300                bclose(&bfd);
301                continue;
302             }
303             total += compress_len;
304             jcr->JobBytes += compress_len;
305             jcr->ReadBytes += wsize;
306             fileAddr += compress_len;
307          }
308 #else
309          if (extract) {
310             Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
311             extract = false;
312             bclose(&bfd);
313             continue;
314          }
315 #endif
316          break;
317
318       case STREAM_UNIX_ATTRIBUTES_ACL:   
319 #ifdef HAVE_ACL
320          /* Recover ACL from stream and check it */
321          acl = acl_from_text(sd->msg);
322          if (acl_valid(acl) != 0) {
323             Emsg1(M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
324             acl_free(acl);
325          }
326          
327          /* Try to restore ACL */
328          if (attr->type == FT_DIREND) {
329             /* Directory */
330             if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
331                 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
332                Emsg1(M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
333             }
334          /* File or Link */
335          } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
336             Emsg1(M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
337          }
338          acl_free(acl);
339          Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
340          break;
341 #else 
342          non_support_acl++;
343          break;                       /* unconfigured, ignore */
344 #endif   
345          
346       case STREAM_MD5_SIGNATURE:
347       case STREAM_SHA1_SIGNATURE:
348          break;
349
350       case STREAM_PROGRAM_NAMES:
351       case STREAM_PROGRAM_DATA:
352          if (!prog_name_msg) {
353             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
354             prog_name_msg++;
355          }
356          break;
357
358       default:
359          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
360          if (extract) {
361             Dmsg1(30, "Found wierd stream %d\n", stream);
362             if (!is_bopen(&bfd)) {
363                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
364             }
365             set_attributes(jcr, attr, &bfd);
366             extract = false;
367          }
368          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
369          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
370          break;
371       } /* end switch(stream) */
372
373    } /* end while get_msg() */
374
375    /* If output file is still open, it was the last one in the
376     * archive since we just hit an end of file, so close the file. 
377     */
378    if (is_bopen(&bfd)) {
379       set_attributes(jcr, attr, &bfd);
380    }
381    set_jcr_job_status(jcr, JS_Terminated);
382    goto ok_out;
383
384 bail_out:
385    set_jcr_job_status(jcr, JS_ErrorTerminated);
386 ok_out:
387    if (jcr->compress_buf) {
388       free(jcr->compress_buf);
389       jcr->compress_buf = NULL;
390    }
391    bclose(&bfd);
392    free_attr(attr);
393    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
394       jcr->JobBytes);
395    if (non_support_data > 1 || non_support_attr > 1) {
396       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
397          non_support_data, non_support_attr);
398    }
399    if (non_support_acl) {
400       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
401    }
402
403 }          
404
405 #ifdef HAVE_LIBZ
406 /*
407  * Convert ZLIB error code into an ASCII message
408  */
409 static const char *zlib_strerror(int stat)
410 {
411    if (stat >= 0) {
412       return "None";
413    }
414    switch (stat) {
415    case Z_ERRNO:
416       return "Zlib errno";
417    case Z_STREAM_ERROR:
418       return "Zlib stream error";
419    case Z_DATA_ERROR:
420       return "Zlib data error";
421    case Z_MEM_ERROR:
422       return "Zlib memory error";
423    case Z_BUF_ERROR:
424       return "Zlib buffer error";
425    case Z_VERSION_ERROR:
426       return "Zlib version error";
427    default:
428       return "*none*";
429    }
430 }
431 #endif