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