2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
32 /* Data received from Storage Daemon */
33 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
35 /* Forward referenced functions */
36 static char *zlib_strerror(int stat);
38 #define RETRY 10 /* retry wait time */
41 * Restore the requested files.
44 void do_restore(JCR *jcr)
49 uint32_t VolSessionId, VolSessionTime;
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;
65 sd = jcr->store_bsock;
66 set_jcr_job_status(jcr, JS_Running);
68 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
69 set_jcr_job_status(jcr, JS_ErrorTerminated);
72 jcr->buf_size = sd->msglen;
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);
82 * Get a record from the Storage daemon. We are guaranteed to
83 * receive records in the following order:
84 * 1. Stream record header
86 * a. Attributes (Unix or Win32)
87 * or b. File data for the file
88 * or c. Possibly MD5 or SHA1 record
91 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
93 * First we expect a Stream Record Header
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);
100 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
103 * Now we expect the Stream Data
105 if (bget_msg(sd) < 0) {
106 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
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);
113 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
115 /* File Attributes stream */
117 case STREAM_UNIX_ATTRIBUTES:
118 case STREAM_UNIX_ATTRIBUTES_EX:
120 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
121 /* If extracting, it was from previous stream, so
122 * close the output file.
125 if (!is_bopen(&bfd)) {
126 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
128 set_attributes(jcr, attr, &bfd);
130 Dmsg0(30, "Stop extracting.\n");
133 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
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");
143 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
144 attr->attr, attr->attrEx);
146 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
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));
156 build_attr_output_fnames(jcr, attr);
158 jcr->num_files_examined++;
160 Dmsg1(30, "Outfile=%s\n", attr->ofname);
162 stat = create_file(jcr, attr, &bfd, jcr->replace);
170 pm_strcpy(&jcr->last_fname, attr->ofname);
174 print_ls_output(jcr, attr);
175 /* Set attributes after file extracted */
179 pm_strcpy(&jcr->last_fname, attr->ofname);
183 print_ls_output(jcr, attr);
184 /* set attributes now because file will not be extracted */
185 set_attributes(jcr, attr, &bfd);
191 case STREAM_FILE_DATA:
192 case STREAM_SPARSE_DATA:
193 case STREAM_WIN32_DATA:
196 if (stream == STREAM_SPARSE_DATA) {
201 wbuf = sd->msg + SPARSE_FADDR_SIZE;
202 wsize = sd->msglen - SPARSE_FADDR_SIZE;
203 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
205 if (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));
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));
228 jcr->JobBytes += wsize;
229 jcr->ReadBytes += wsize;
234 /* GZIP data stream */
235 case STREAM_GZIP_DATA:
236 case STREAM_SPARSE_GZIP_DATA:
237 case STREAM_WIN32_GZIP_DATA:
243 if (stream == STREAM_SPARSE_GZIP_DATA) {
247 wbuf = sd->msg + SPARSE_FADDR_SIZE;
248 wsize = sd->msglen - SPARSE_FADDR_SIZE;
249 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
251 if (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));
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));
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));
284 total += compress_len;
285 jcr->JobBytes += compress_len;
286 jcr->ReadBytes += wsize;
287 fileAddr += compress_len;
291 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
299 case STREAM_MD5_SIGNATURE:
300 case STREAM_SHA1_SIGNATURE:
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");
312 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
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"));
318 set_attributes(jcr, attr, &bfd);
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);
324 } /* end switch(stream) */
326 } /* end while get_msg() */
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.
331 if (is_bopen(&bfd)) {
332 set_attributes(jcr, attr, &bfd);
334 set_jcr_job_status(jcr, JS_Terminated);
338 set_jcr_job_status(jcr, JS_ErrorTerminated);
340 if (jcr->compress_buf) {
341 free(jcr->compress_buf);
342 jcr->compress_buf = NULL;
346 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
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);
355 * Convert ZLIB error code into an ASCII message
357 static char *zlib_strerror(int stat)
366 return "Zlib stream error";
368 return "Zlib data error";
370 return "Zlib memory error";
372 return "Zlib buffer error";
373 case Z_VERSION_ERROR:
374 return "Zlib version error";