2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2005 Kern Sibbald
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,
36 /* Data received from Storage Daemon */
37 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
39 /* Forward referenced functions */
41 static const char *zlib_strerror(int stat);
43 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
44 uint64_t *addr, int flags);
46 #define RETRY 10 /* retry wait time */
49 * Close a bfd check that we are at the expected file offset.
50 * Makes some code in set_attributes().
52 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
54 char ec1[50], ec2[50];
57 fsize = blseek(bfd, 0, SEEK_CUR);
58 bclose(bfd); /* first close file */
59 if (fsize > 0 && fsize != osize) {
60 Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
61 jcr->last_fname, edit_uint64(osize, ec1),
62 edit_uint64(fsize, ec2));
69 * Restore the requested files.
72 void do_restore(JCR *jcr)
75 int32_t stream, prev_stream;
76 uint32_t VolSessionId, VolSessionTime;
79 char ec1[50]; /* Buffer printing huge values */
81 BFILE bfd; /* File content */
82 uint64_t fileAddr = 0; /* file write address */
83 uint32_t size; /* Size of file */
84 BFILE altbfd; /* Alternative data stream */
85 uint64_t alt_addr = 0; /* Write address for alternative stream */
86 intmax_t alt_size = 0; /* Size of alternate stream */
87 int flags; /* Options for extract_data() */
91 /* The following variables keep track of "known unknowns" */
92 int non_support_data = 0;
93 int non_support_attr = 0;
94 int non_support_rsrc = 0;
95 int non_support_finfo = 0;
96 int non_support_acl = 0;
97 int non_support_progname = 0;
99 /* Finally, set up for special configurations */
100 #ifdef HAVE_DARWIN_OS
101 intmax_t rsrc_len = 0; /* Original length of resource fork */
102 struct attrlist attrList;
104 memset(&attrList, 0, sizeof(attrList));
105 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
106 attrList.commonattr = ATTR_CMN_FNDRINFO;
109 sd = jcr->store_bsock;
110 set_jcr_job_status(jcr, JS_Running);
113 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
117 buf_size = client->max_network_buffer_size;
119 buf_size = 0; /* use default */
121 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
122 set_jcr_job_status(jcr, JS_ErrorTerminated);
125 jcr->buf_size = sd->msglen;
128 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
129 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
130 jcr->compress_buf_size = compress_buf_size;
134 * Get a record from the Storage daemon. We are guaranteed to
135 * receive records in the following order:
136 * 1. Stream record header
138 * a. Attributes (Unix or Win32)
139 * or b. File data for the file
140 * or c. Alternate data stream (e.g. Resource Fork)
143 * or f. Possibly MD5 or SHA1 record
146 * NOTE: We keep track of two bacula file descriptors:
147 * 1. bfd for file data.
148 * This fd is opened for non empty files when an attribute stream is
149 * encountered and closed when we find the next attribute stream.
150 * 2. alt_bfd for alternate data streams
151 * This fd is opened every time we encounter a new alternate data
152 * stream for the current file. When we find any other stream, we
154 * The expected size of the stream, alt_len, should be set when
160 jcr->acl_text = get_pool_memory(PM_MESSAGE);
162 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
163 /* Remember previous stream type */
164 prev_stream = stream;
166 /* First we expect a Stream Record Header */
167 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
168 &stream, &size) != 5) {
169 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
172 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
174 /* * Now we expect the Stream Data */
175 if (bget_msg(sd) < 0) {
176 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
179 if (size != (uint32_t)sd->msglen) {
180 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
183 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
185 /* If we change streams, close and reset alternate data streams */
186 if (prev_stream != stream) {
187 if (is_bopen(&altbfd)) {
188 bclose_chksize(jcr, &altbfd, alt_size);
190 alt_size = -1; /* Use an impossible value and set a proper one below */
194 /* File Attributes stream */
196 case STREAM_UNIX_ATTRIBUTES:
197 case STREAM_UNIX_ATTRIBUTES_EX:
198 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
200 * If extracting, it was from previous stream, so
201 * close the output file.
204 if (size > 0 && !is_bopen(&bfd)) {
205 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
207 set_attributes(jcr, attr, &bfd);
209 Dmsg0(30, "Stop extracting.\n");
210 } else if (is_bopen(&bfd)) {
211 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
216 * Unpack and do sanity check fo attributes.
218 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
221 if (file_index != attr->file_index) {
222 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
223 file_index, attr->file_index);
224 Dmsg0(100, "File index error\n");
228 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
229 attr->attr, attr->attrEx);
231 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
233 if (!is_stream_supported(attr->data_stream)) {
234 if (!non_support_data++) {
235 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
236 stream_to_ascii(attr->data_stream));
241 build_attr_output_fnames(jcr, attr);
244 * Now determine if we are extracting or not.
246 jcr->num_files_examined++;
247 Dmsg1(30, "Outfile=%s\n", attr->ofname);
249 stat = create_file(jcr, attr, &bfd, jcr->replace);
254 case CF_EXTRACT: /* File created and we expect file data */
257 case CF_CREATED: /* File created, but there is no content */
259 pm_strcpy(jcr->last_fname, attr->ofname);
263 print_ls_output(jcr, attr);
264 #ifdef HAVE_DARWIN_OS
265 /* Only restore the resource fork for regular files */
266 from_base64(&rsrc_len, attr->attrEx);
267 if (attr->type == FT_REG && rsrc_len > 0) {
272 /* set attributes now because file will not be extracted */
273 set_attributes(jcr, attr, &bfd);
280 case STREAM_FILE_DATA:
281 case STREAM_SPARSE_DATA:
282 case STREAM_WIN32_DATA:
283 case STREAM_GZIP_DATA:
284 case STREAM_SPARSE_GZIP_DATA:
285 case STREAM_WIN32_GZIP_DATA:
286 /* Force an expected, consistent stream type here */
287 if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
288 || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
290 if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
293 if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
294 || stream == STREAM_WIN32_GZIP_DATA) {
297 if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags) < 0) {
305 /* Resource fork stream - only recorded after a file to be restored */
306 /* Silently ignore if we cannot write - we already reported that */
307 case STREAM_MACOS_FORK_DATA:
308 #ifdef HAVE_DARWIN_OS
310 if (prev_stream != stream) {
311 if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
312 Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s\n"), jcr->last_fname);
317 Dmsg0(30, "Restoring resource fork\n");
320 if (extract_data(jcr, &altbfd, sd->msg, sd->msglen, &alt_addr, flags) < 0) {
331 case STREAM_HFSPLUS_ATTRIBUTES:
332 #ifdef HAVE_DARWIN_OS
333 Dmsg0(30, "Restoring Finder Info\n");
334 if (sd->msglen != 32) {
335 Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen);
338 if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
339 Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s\n"), jcr->last_fname);
346 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
348 pm_strcpy(jcr->acl_text, sd->msg);
349 Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_ACCESS, jcr->acl_text);
350 if (bacl_set(jcr, BACL_TYPE_ACCESS) != 0) {
351 Jmsg1(jcr, M_WARNING, 0, "Can't restore ACL of %s\n", jcr->last_fname);
358 case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
360 pm_strcpy(jcr->acl_text, sd->msg);
361 Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_DEFAULT, jcr->acl_text);
362 if (bacl_set(jcr, BACL_TYPE_DEFAULT) != 0) {
363 Jmsg1(jcr, M_WARNING, 0, "Can't restore default ACL of %s\n", jcr->last_fname);
370 case STREAM_MD5_SIGNATURE:
371 case STREAM_SHA1_SIGNATURE:
374 case STREAM_PROGRAM_NAMES:
375 case STREAM_PROGRAM_DATA:
376 if (!non_support_progname) {
377 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
378 non_support_progname++;
383 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
385 Dmsg1(30, "Found wierd stream %d\n", stream);
386 if (size > 0 && !is_bopen(&bfd)) {
387 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
389 set_attributes(jcr, attr, &bfd);
391 } else if (is_bopen(&bfd)) {
392 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
395 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
396 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
398 } /* end switch(stream) */
400 } /* end while get_msg() */
402 /* If output file is still open, it was the last one in the
403 * archive since we just hit an end of file, so close the file.
405 if (is_bopen(&altbfd)) {
406 bclose_chksize(jcr, &altbfd, alt_size);
409 set_attributes(jcr, attr, &bfd);
411 if (is_bopen(&bfd)) {
414 set_jcr_job_status(jcr, JS_Terminated);
418 set_jcr_job_status(jcr, JS_ErrorTerminated);
420 if (jcr->compress_buf) {
421 free(jcr->compress_buf);
422 jcr->compress_buf = NULL;
423 jcr->compress_buf_size = 0;
428 free_pool_memory(jcr->acl_text);
429 Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
430 edit_uint64(jcr->JobBytes, ec1));
431 if (non_support_data > 1 || non_support_attr > 1) {
432 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
433 non_support_data, non_support_attr);
435 if (non_support_rsrc) {
436 Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
438 if (non_support_finfo) {
439 Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
441 if (non_support_acl) {
442 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
449 * Convert ZLIB error code into an ASCII message
451 static const char *zlib_strerror(int stat)
460 return "Zlib stream error";
462 return "Zlib data error";
464 return "Zlib memory error";
466 return "Zlib buffer error";
467 case Z_VERSION_ERROR:
468 return "Zlib version error";
476 * In the context of jcr, write data to bfd.
477 * We write buflen bytes in buf at addr. addr is updated in place.
478 * The flags specify whether to use sparse files or compression.
479 * Return value is the number of bytes written, or -1 on errors.
481 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
482 uint64_t *addr, int flags)
486 char *wbuf; /* write buffer */
487 uint32_t wsize; /* write size */
488 uint32_t rsize; /* read size */
489 char ec1[50]; /* Buffer printing huge values */
491 if (flags & FO_SPARSE) {
495 wbuf = buf + SPARSE_FADDR_SIZE;
496 rsize = buflen - SPARSE_FADDR_SIZE;
497 ser_begin(buf, SPARSE_FADDR_SIZE);
499 if (*addr != faddr) {
501 if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
503 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
504 edit_uint64(*addr, ec1), jcr->last_fname,
505 be.strerror(bfd->berrno));
515 if (flags & FO_GZIP) {
518 * NOTE! We only use uLong and Byte because they are
519 * needed by the zlib routines, they should not otherwise
522 compress_len = jcr->compress_buf_size;
523 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
524 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
525 (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
526 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
527 jcr->last_fname, zlib_strerror(stat));
530 wbuf = jcr->compress_buf;
531 wsize = compress_len;
532 Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
534 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
538 Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
541 if (bwrite(bfd, wbuf, wsize) != (ssize_t)wsize) {
543 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"),
544 jcr->last_fname, be.strerror(bfd->berrno));
548 jcr->JobBytes += wsize;
549 jcr->ReadBytes += rsize;