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