]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Fix conio.h problem on Solaris
[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
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                      berrno be;
228                      be.set_errno(bfd.berrno);
229                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
230                          edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
231                      extract = false;
232                      bclose(&bfd);
233                      continue;
234                   }
235                }
236             } else {
237                wbuf = sd->msg;
238                wsize = sd->msglen;
239             }
240             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
241             if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
242                Dmsg0(0, "===Write error===\n");
243                berrno be;
244                be.set_errno(bfd.berrno);
245                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, 
246                      be.strerror());
247                extract = false;
248                bclose(&bfd);
249                continue;
250             } 
251             total += wsize;
252             jcr->JobBytes += wsize;
253             jcr->ReadBytes += wsize;
254             fileAddr += wsize;
255          }
256          break;
257
258       /* GZIP data stream */
259       case STREAM_GZIP_DATA:
260       case STREAM_SPARSE_GZIP_DATA:  
261       case STREAM_WIN32_GZIP_DATA:  
262 #ifdef HAVE_LIBZ
263          if (extract) {
264             uLong compress_len;
265             int stat;
266
267             if (stream == STREAM_SPARSE_GZIP_DATA) {
268                ser_declare;
269                uint64_t faddr;
270                char ec1[50];
271                wbuf = sd->msg + SPARSE_FADDR_SIZE;
272                wsize = sd->msglen - SPARSE_FADDR_SIZE;
273                ser_begin(sd->msg, SPARSE_FADDR_SIZE);
274                unser_uint64(faddr);
275                if (fileAddr != faddr) {
276                   fileAddr = faddr;
277                   if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
278                      berrno be;
279                      be.set_errno(bfd.berrno);
280                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
281                          edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
282                      extract = false;
283                      bclose(&bfd);
284                      continue;
285                   }
286                }
287             } else {
288                wbuf = sd->msg;
289                wsize = sd->msglen;
290             }
291             compress_len = compress_buf_size;
292             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
293             if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
294                   (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
295                Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"), 
296                   attr->ofname, zlib_strerror(stat));
297                extract = false;
298                bclose(&bfd);
299                continue;
300             }
301
302             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
303             if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
304                Dmsg0(0, "===Write error===\n");
305                berrno be;
306                be.set_errno(bfd.berrno);
307                Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror());
308                extract = false;
309                bclose(&bfd);
310                continue;
311             }
312             total += compress_len;
313             jcr->JobBytes += compress_len;
314             jcr->ReadBytes += wsize;
315             fileAddr += compress_len;
316          }
317 #else
318          if (extract) {
319             Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
320             extract = false;
321             bclose(&bfd);
322             continue;
323          }
324 #endif
325          break;
326
327       case STREAM_UNIX_ATTRIBUTES_ACL:   
328 #ifdef HAVE_ACL
329          /* Recover ACL from stream and check it */
330          acl = acl_from_text(sd->msg);
331          if (acl_valid(acl) != 0) {
332             Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
333             acl_free(acl);
334          }
335          
336          /* Try to restore ACL */
337          if (attr->type == FT_DIREND) {
338             /* Directory */
339             if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
340                 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
341                Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
342             }
343          /* File or Link */
344          } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
345             Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
346          }
347          acl_free(acl);
348          Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
349          break;
350 #else 
351          non_support_acl++;
352          break;                       /* unconfigured, ignore */
353 #endif   
354          
355       case STREAM_MD5_SIGNATURE:
356       case STREAM_SHA1_SIGNATURE:
357          break;
358
359       case STREAM_PROGRAM_NAMES:
360       case STREAM_PROGRAM_DATA:
361          if (!prog_name_msg) {
362             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
363             prog_name_msg++;
364          }
365          break;
366
367       default:
368          /* If extracting, wierd stream (not 1 or 2), close output file anyway */
369          if (extract) {
370             Dmsg1(30, "Found wierd stream %d\n", stream);
371             if (!is_bopen(&bfd)) {
372                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
373             }
374             set_attributes(jcr, attr, &bfd);
375             extract = false;
376          }
377          Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
378          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
379          break;
380       } /* end switch(stream) */
381
382    } /* end while get_msg() */
383
384    /* If output file is still open, it was the last one in the
385     * archive since we just hit an end of file, so close the file. 
386     */
387    if (is_bopen(&bfd)) {
388       set_attributes(jcr, attr, &bfd);
389    }
390    set_jcr_job_status(jcr, JS_Terminated);
391    goto ok_out;
392
393 bail_out:
394    set_jcr_job_status(jcr, JS_ErrorTerminated);
395 ok_out:
396    if (jcr->compress_buf) {
397       free(jcr->compress_buf);
398       jcr->compress_buf = NULL;
399    }
400    bclose(&bfd);
401    free_attr(attr);
402    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
403       jcr->JobBytes);
404    if (non_support_data > 1 || non_support_attr > 1) {
405       Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
406          non_support_data, non_support_attr);
407    }
408    if (non_support_acl) {
409       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
410    }
411
412 }          
413
414 #ifdef HAVE_LIBZ
415 /*
416  * Convert ZLIB error code into an ASCII message
417  */
418 static const char *zlib_strerror(int stat)
419 {
420    if (stat >= 0) {
421       return "None";
422    }
423    switch (stat) {
424    case Z_ERRNO:
425       return "Zlib errno";
426    case Z_STREAM_ERROR:
427       return "Zlib stream error";
428    case Z_DATA_ERROR:
429       return "Zlib data error";
430    case Z_MEM_ERROR:
431       return "Zlib memory error";
432    case Z_BUF_ERROR:
433       return "Zlib buffer error";
434    case Z_VERSION_ERROR:
435       return "Zlib version error";
436    default:
437       return "*none*";
438    }
439 }
440 #endif