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 */
37 #define RETRY 10 /* retry wait time */
40 * Restore the requested files.
43 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;
63 wherelen = strlen(jcr->where);
66 sd = jcr->store_bsock;
67 set_jcr_job_status(jcr, JS_Running);
69 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
70 set_jcr_job_status(jcr, JS_ErrorTerminated);
73 jcr->buf_size = sd->msglen;
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);
83 * Get a record from the Storage daemon. We are guaranteed to
84 * receive records in the following order:
85 * 1. Stream record header
87 * a. Attributes (Unix or Win32)
88 * or b. File data for the file
89 * or c. Possibly MD5 or SHA1 record
92 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
94 * First we expect a Stream Record Header
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);
101 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
104 * Now we expect the Stream Data
106 if (bget_msg(sd) < 0) {
107 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
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);
114 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
116 /* File Attributes stream */
118 case STREAM_UNIX_ATTRIBUTES:
119 case STREAM_UNIX_ATTRIBUTES_EX:
121 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
122 /* If extracting, it was from previous stream, so
123 * close the output file.
126 if (!is_bopen(&bfd)) {
127 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
129 set_attributes(jcr, attr, &bfd);
131 Dmsg0(30, "Stop extracting.\n");
134 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
137 if (file_index != attr->file_index) {
138 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
139 file_index, attr->file_index);
140 Dmsg0(100, "File index error\n");
144 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
145 attr->attr, attr->attrEx);
147 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
149 if (!is_stream_supported(attr->data_stream)) {
150 if (!non_support_data++) {
151 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
152 stream_to_ascii(attr->data_stream));
157 build_attr_output_fnames(jcr, attr);
159 jcr->num_files_examined++;
161 Dmsg1(30, "Outfile=%s\n", attr->ofname);
163 stat = create_file(jcr, attr, &bfd, jcr->replace);
171 pm_strcpy(&jcr->last_fname, attr->ofname);
175 print_ls_output(jcr, attr);
176 /* Set attributes after file extracted */
180 pm_strcpy(&jcr->last_fname, attr->ofname);
184 print_ls_output(jcr, attr);
185 /* set attributes now because file will not be extracted */
186 set_attributes(jcr, attr, &bfd);
191 /* Windows Backup data stream */
192 case STREAM_WIN32_DATA:
193 if (!is_win32_backup()) {
194 if (!non_support_data) {
195 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
196 stream_to_ascii(stream));
205 case STREAM_FILE_DATA:
206 case STREAM_SPARSE_DATA:
210 if (stream == STREAM_SPARSE_DATA) {
215 wbuf = sd->msg + SPARSE_FADDR_SIZE;
216 wsize = sd->msglen - SPARSE_FADDR_SIZE;
217 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
219 if (fileAddr != faddr) {
221 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
222 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
223 edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
232 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
233 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
234 Dmsg0(0, "===Write error===\n");
235 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, berror(&bfd));
240 jcr->JobBytes += wsize;
241 jcr->ReadBytes += wsize;
246 /* Windows Backup GZIP data stream */
247 case STREAM_WIN32_GZIP_DATA:
248 if (!is_win32_backup()) {
249 if (!non_support_attr) {
250 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
251 stream_to_ascii(stream));
257 /* Fall through desired */
259 /* GZIP data stream */
260 case STREAM_GZIP_DATA:
261 case STREAM_SPARSE_GZIP_DATA:
270 if (stream == STREAM_SPARSE_GZIP_DATA) {
271 wbuf = sd->msg + SPARSE_FADDR_SIZE;
272 wsize = sd->msglen - SPARSE_FADDR_SIZE;
273 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
275 if (fileAddr != faddr) {
277 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
278 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
279 edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
288 compress_len = compress_buf_size;
289 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
290 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
291 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
292 Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
297 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
298 if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
299 Dmsg0(0, "===Write error===\n");
300 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, berror(&bfd));
304 total += compress_len;
305 jcr->JobBytes += compress_len;
306 jcr->ReadBytes += wsize;
307 fileAddr += compress_len;
311 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
318 case STREAM_MD5_SIGNATURE:
319 case STREAM_SHA1_SIGNATURE:
322 case STREAM_PROGRAM_NAMES:
323 case STREAM_PROGRAM_DATA:
324 if (!prog_name_msg) {
325 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
331 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
333 Dmsg1(30, "Found wierd stream %d\n", stream);
334 if (!is_bopen(&bfd)) {
335 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
337 set_attributes(jcr, attr, &bfd);
340 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
341 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
343 } /* end switch(stream) */
345 } /* end while get_msg() */
347 /* If output file is still open, it was the last one in the
348 * archive since we just hit an end of file, so close the file.
350 if (is_bopen(&bfd)) {
351 set_attributes(jcr, attr, &bfd);
353 set_jcr_job_status(jcr, JS_Terminated);
357 set_jcr_job_status(jcr, JS_ErrorTerminated);
359 if (jcr->compress_buf) {
360 free(jcr->compress_buf);
361 jcr->compress_buf = NULL;
364 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
366 if (non_support_data > 1 || non_support_attr > 1) {
367 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
368 non_support_data, non_support_attr);