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