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