2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2004 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,
34 #include <acl/libacl.h>
41 /* Data received from Storage Daemon */
42 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
44 /* Forward referenced functions */
46 static const char *zlib_strerror(int stat);
48 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
49 uint64_t *addr, int flags);
51 #define RETRY 10 /* retry wait time */
54 * Close a bfd check that we are at the expected file offset.
55 * Makes some code in set_attributes().
57 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
59 char ec1[50], ec2[50];
62 fsize = blseek(bfd, 0, SEEK_CUR);
63 bclose(bfd); /* first close file */
64 if (fsize > 0 && fsize != osize) {
65 Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
66 jcr->last_fname, edit_uint64(osize, ec1),
67 edit_uint64(fsize, ec2));
74 * Restore the requested files.
77 void do_restore(JCR *jcr)
80 int32_t stream, prev_stream;
81 uint32_t VolSessionId, VolSessionTime;
84 char ec1[50]; /* Buffer printing huge values */
86 BFILE bfd; /* File content */
87 uint64_t fileAddr = 0; /* file write address */
88 uint32_t size; /* Size of file */
89 BFILE altbfd; /* Alternative data stream */
90 uint64_t alt_addr = 0; /* Write address for alternative stream */
91 intmax_t alt_size = 0; /* Size of alternate stream */
92 int flags; /* Options for extract_data() */
96 /* The following variables keep track of "known unknowns" */
97 int non_support_data = 0;
98 int non_support_attr = 0;
99 int non_support_rsrc = 0;
100 int non_support_finfo = 0;
101 int non_support_acl = 0;
102 int non_support_progname = 0;
104 /* Finally, set up for special configurations */
108 #ifdef HAVE_DARWIN_OS
109 intmax_t rsrc_len = 0; /* Original length of resource fork */
110 struct attrlist attrList;
112 memset(&attrList, 0, sizeof(attrList));
113 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
114 attrList.commonattr = ATTR_CMN_FNDRINFO;
117 sd = jcr->store_bsock;
118 set_jcr_job_status(jcr, JS_Running);
121 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
125 buf_size = client->max_network_buffer_size;
127 buf_size = 0; /* use default */
129 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
130 set_jcr_job_status(jcr, JS_ErrorTerminated);
133 jcr->buf_size = sd->msglen;
136 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
137 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
138 jcr->compress_buf_size = compress_buf_size;
142 * Get a record from the Storage daemon. We are guaranteed to
143 * receive records in the following order:
144 * 1. Stream record header
146 * a. Attributes (Unix or Win32)
147 * or b. File data for the file
148 * or c. Alternate data stream (e.g. Resource Fork)
151 * or f. Possibly MD5 or SHA1 record
154 * NOTE: We keep track of two bacula file descriptors:
155 * 1. bfd for file data.
156 * This fd is opened for non empty files when an attribute stream is
157 * encountered and closed when we find the next attribute stream.
158 * 2. alt_bfd for alternate data streams
159 * This fd is opened every time we encounter a new alternate data
160 * stream for the current file. When we find any other stream, we
162 * The expected size of the stream, alt_len, should be set when
169 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
170 /* Remember previous stream type */
171 prev_stream = stream;
173 /* First we expect a Stream Record Header */
174 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
175 &stream, &size) != 5) {
176 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
179 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
181 /* * Now we expect the Stream Data */
182 if (bget_msg(sd) < 0) {
183 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
186 if (size != (uint32_t)sd->msglen) {
187 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
190 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
192 /* If we change streams, close and reset alternate data streams */
193 if (prev_stream != stream) {
194 if (is_bopen(&altbfd)) {
195 bclose_chksize(jcr, &altbfd, alt_size);
197 alt_size = -1; /* Use an impossible value and set a proper one below */
201 /* File Attributes stream */
203 case STREAM_UNIX_ATTRIBUTES:
204 case STREAM_UNIX_ATTRIBUTES_EX:
205 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
207 * If extracting, it was from previous stream, so
208 * close the output file.
211 if (size > 0 && !is_bopen(&bfd)) {
212 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
214 set_attributes(jcr, attr, &bfd);
216 Dmsg0(30, "Stop extracting.\n");
217 } else if (is_bopen(&bfd)) {
218 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
223 * Unpack and do sanity check fo attributes.
225 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
228 if (file_index != attr->file_index) {
229 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
230 file_index, attr->file_index);
231 Dmsg0(100, "File index error\n");
235 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
236 attr->attr, attr->attrEx);
238 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
240 if (!is_stream_supported(attr->data_stream)) {
241 if (!non_support_data++) {
242 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
243 stream_to_ascii(attr->data_stream));
248 build_attr_output_fnames(jcr, attr);
251 * Now determine if we are extracting or not.
253 jcr->num_files_examined++;
254 Dmsg1(30, "Outfile=%s\n", attr->ofname);
256 stat = create_file(jcr, attr, &bfd, jcr->replace);
261 case CF_EXTRACT: /* File created and we expect file data */
264 case CF_CREATED: /* File created, but there is no content */
266 pm_strcpy(jcr->last_fname, attr->ofname);
270 print_ls_output(jcr, attr);
271 #ifdef HAVE_DARWIN_OS
272 /* Only restore the resource fork for regular files */
273 from_base64(&rsrc_len, attr->attrEx);
274 if (attr->type == FT_REG && rsrc_len > 0) {
279 /* set attributes now because file will not be extracted */
280 set_attributes(jcr, attr, &bfd);
287 case STREAM_FILE_DATA:
288 case STREAM_SPARSE_DATA:
289 case STREAM_WIN32_DATA:
290 case STREAM_GZIP_DATA:
291 case STREAM_SPARSE_GZIP_DATA:
292 case STREAM_WIN32_GZIP_DATA:
293 /* Force an expected, consistent stream type here */
294 if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
295 || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
297 if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
300 if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
301 || stream == STREAM_WIN32_GZIP_DATA) {
304 if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags) < 0) {
312 /* Resource fork stream - only recorded after a file to be restored */
313 /* Silently ignore if we cannot write - we already reported that */
314 case STREAM_MACOS_FORK_DATA:
315 #ifdef HAVE_DARWIN_OS
317 if (prev_stream != stream) {
318 if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
319 Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s\n"), jcr->last_fname);
324 Dmsg0(30, "Restoring resource fork\n");
327 if (extract_data(jcr, &altbfd, sd->msg, sd->msglen, &alt_addr, flags) < 0) {
338 case STREAM_HFSPLUS_ATTRIBUTES:
339 #ifdef HAVE_DARWIN_OS
340 Dmsg0(30, "Restoring Finder Info\n");
341 if (sd->msglen != 32) {
342 Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen);
345 if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
346 Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s\n"), jcr->last_fname);
355 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
357 /* Recover Acess ACL from stream and check it */
358 acl = acl_from_text(sd->msg);
359 if (acl_valid(acl) != 0) {
360 Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
364 /* Try to restore ACL */
365 if (attr->type == FT_DIREND) {
367 if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
368 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
371 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
372 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
375 Dmsg1(200, "ACL of file: %s successfully restored!\n", jcr->last_fname);
379 break; /* unconfigured, ignore */
381 case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
383 /* Recover Default ACL from stream and check it */
384 acl = acl_from_text(sd->msg);
385 if (acl_valid(acl) != 0) {
386 Jmsg1(jcr, M_WARNING, 0, "Failure in the Default ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
390 /* Try to restore ACL */
391 if (attr->type == FT_DIREND) {
393 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0) {
394 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore Default ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
398 Dmsg1(200, "Default ACL of file: %s successfully restored!\n", jcr->last_fname);
402 break; /* unconfigured, ignore */
406 case STREAM_MD5_SIGNATURE:
407 case STREAM_SHA1_SIGNATURE:
410 case STREAM_PROGRAM_NAMES:
411 case STREAM_PROGRAM_DATA:
412 if (!non_support_progname) {
413 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
414 non_support_progname++;
419 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
421 Dmsg1(30, "Found wierd stream %d\n", stream);
422 if (size > 0 && !is_bopen(&bfd)) {
423 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
425 set_attributes(jcr, attr, &bfd);
427 } else if (is_bopen(&bfd)) {
428 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
431 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
432 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
434 } /* end switch(stream) */
436 } /* end while get_msg() */
438 /* If output file is still open, it was the last one in the
439 * archive since we just hit an end of file, so close the file.
441 if (is_bopen(&altbfd)) {
442 bclose_chksize(jcr, &altbfd, alt_size);
445 set_attributes(jcr, attr, &bfd);
447 if (is_bopen(&bfd)) {
450 set_jcr_job_status(jcr, JS_Terminated);
454 set_jcr_job_status(jcr, JS_ErrorTerminated);
456 if (jcr->compress_buf) {
457 free(jcr->compress_buf);
458 jcr->compress_buf = NULL;
459 jcr->compress_buf_size = 0;
464 Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
465 edit_uint64(jcr->JobBytes, ec1));
466 if (non_support_data > 1 || non_support_attr > 1) {
467 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
468 non_support_data, non_support_attr);
470 if (non_support_rsrc) {
471 Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
473 if (non_support_finfo) {
474 Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
476 if (non_support_acl) {
477 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
484 * Convert ZLIB error code into an ASCII message
486 static const char *zlib_strerror(int stat)
495 return "Zlib stream error";
497 return "Zlib data error";
499 return "Zlib memory error";
501 return "Zlib buffer error";
502 case Z_VERSION_ERROR:
503 return "Zlib version error";
511 * In the context of jcr, write data to bfd.
512 * We write buflen bytes in buf at addr. addr is updated in place.
513 * The flags specify whether to use sparse files or compression.
514 * Return value is the number of bytes written, or -1 on errors.
516 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
517 uint64_t *addr, int flags)
521 char *wbuf; /* write buffer */
522 uint32_t wsize; /* write size */
523 uint32_t rsize; /* read size */
524 char ec1[50]; /* Buffer printing huge values */
526 if (flags & FO_SPARSE) {
530 wbuf = buf + SPARSE_FADDR_SIZE;
531 rsize = buflen - SPARSE_FADDR_SIZE;
532 ser_begin(buf, SPARSE_FADDR_SIZE);
534 if (*addr != faddr) {
536 if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
538 be.set_errno(bfd->berrno);
539 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
540 edit_uint64(*addr, ec1), jcr->last_fname, be.strerror());
550 if (flags & FO_GZIP) {
552 compress_len = jcr->compress_buf_size;
553 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
554 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
555 (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
556 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
557 jcr->last_fname, zlib_strerror(stat));
560 wbuf = jcr->compress_buf;
561 wsize = compress_len;
562 Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
564 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
568 Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
571 if ((uLong)bwrite(bfd, wbuf, wsize) != wsize) {
572 Dmsg0(0, "===Write error===\n");
574 be.set_errno(bfd->berrno);
575 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), jcr->last_fname, be.strerror());
579 jcr->JobBytes += wsize;
580 jcr->ReadBytes += rsize;