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