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)
77 uint32_t VolSessionId, VolSessionTime;
80 char ec1[50]; /* Buffer printing huge values */
82 BFILE bfd; /* File content */
83 uint64_t fileAddr = 0; /* file write address */
84 uint32_t size; /* Size of file */
85 BFILE altbfd; /* Alternative data stream */
86 uint64_t alt_addr = 0; /* Write address for alternative stream */
87 intmax_t alt_size = 0; /* Size of alternate stream */
88 int flags; /* Options for extract_data() */
92 /* The following variables keep track of "known unknowns" */
93 int non_support_data = 0;
94 int non_support_attr = 0;
95 int non_support_rsrc = 0;
96 int non_support_finfo = 0;
97 int non_support_acl = 0;
98 int non_support_progname = 0;
100 /* Finally, set up for special configurations */
101 #ifdef HAVE_DARWIN_OS
102 intmax_t rsrc_len = 0; /* Original length of resource fork */
103 struct attrlist attrList;
105 memset(&attrList, 0, sizeof(attrList));
106 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
107 attrList.commonattr = ATTR_CMN_FNDRINFO;
110 sd = jcr->store_bsock;
111 set_jcr_job_status(jcr, JS_Running);
114 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
118 buf_size = client->max_network_buffer_size;
120 buf_size = 0; /* use default */
122 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
123 set_jcr_job_status(jcr, JS_ErrorTerminated);
126 jcr->buf_size = sd->msglen;
129 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
130 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
131 jcr->compress_buf_size = compress_buf_size;
135 * Get a record from the Storage daemon. We are guaranteed to
136 * receive records in the following order:
137 * 1. Stream record header
139 * a. Attributes (Unix or Win32)
140 * or b. File data for the file
141 * or c. Alternate data stream (e.g. Resource Fork)
144 * or f. Possibly MD5 or SHA1 record
147 * NOTE: We keep track of two bacula file descriptors:
148 * 1. bfd for file data.
149 * This fd is opened for non empty files when an attribute stream is
150 * encountered and closed when we find the next attribute stream.
151 * 2. alt_bfd for alternate data streams
152 * This fd is opened every time we encounter a new alternate data
153 * stream for the current file. When we find any other stream, we
155 * The expected size of the stream, alt_len, should be set when
161 jcr->acl_text = get_pool_memory(PM_MESSAGE);
163 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
164 /* Remember previous stream type */
165 prev_stream = stream;
167 /* First we expect a Stream Record Header */
168 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
169 &stream, &size) != 5) {
170 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
173 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
175 /* * Now we expect the Stream Data */
176 if (bget_msg(sd) < 0) {
177 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
180 if (size != (uint32_t)sd->msglen) {
181 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
184 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
186 /* If we change streams, close and reset alternate data streams */
187 if (prev_stream != stream) {
188 if (is_bopen(&altbfd)) {
189 bclose_chksize(jcr, &altbfd, alt_size);
191 alt_size = -1; /* Use an impossible value and set a proper one below */
195 /* File Attributes stream */
197 case STREAM_UNIX_ATTRIBUTES:
198 case STREAM_UNIX_ATTRIBUTES_EX:
199 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
201 * If extracting, it was from previous stream, so
202 * close the output file.
205 if (size > 0 && !is_bopen(&bfd)) {
206 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
208 set_attributes(jcr, attr, &bfd);
210 Dmsg0(30, "Stop extracting.\n");
211 } else if (is_bopen(&bfd)) {
212 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
217 * Unpack and do sanity check fo attributes.
219 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
222 if (file_index != attr->file_index) {
223 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
224 file_index, attr->file_index);
225 Dmsg0(100, "File index error\n");
229 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
230 attr->attr, attr->attrEx);
232 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
234 if (!is_stream_supported(attr->data_stream)) {
235 if (!non_support_data++) {
236 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
237 stream_to_ascii(attr->data_stream));
242 build_attr_output_fnames(jcr, attr);
245 * Now determine if we are extracting or not.
247 jcr->num_files_examined++;
248 Dmsg1(30, "Outfile=%s\n", attr->ofname);
250 stat = create_file(jcr, attr, &bfd, jcr->replace);
255 case CF_EXTRACT: /* File created and we expect file data */
258 case CF_CREATED: /* File created, but there is no content */
260 pm_strcpy(jcr->last_fname, attr->ofname);
264 print_ls_output(jcr, attr);
265 #ifdef HAVE_DARWIN_OS
266 /* Only restore the resource fork for regular files */
267 from_base64(&rsrc_len, attr->attrEx);
268 if (attr->type == FT_REG && rsrc_len > 0) {
273 /* set attributes now because file will not be extracted */
274 set_attributes(jcr, attr, &bfd);
281 case STREAM_FILE_DATA:
282 case STREAM_SPARSE_DATA:
283 case STREAM_WIN32_DATA:
284 case STREAM_GZIP_DATA:
285 case STREAM_SPARSE_GZIP_DATA:
286 case STREAM_WIN32_GZIP_DATA:
287 /* Force an expected, consistent stream type here */
288 if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
289 || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
291 if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
294 if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
295 || stream == STREAM_WIN32_GZIP_DATA) {
298 if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, flags) < 0) {
306 /* Resource fork stream - only recorded after a file to be restored */
307 /* Silently ignore if we cannot write - we already reported that */
308 case STREAM_MACOS_FORK_DATA:
309 #ifdef HAVE_DARWIN_OS
311 if (prev_stream != stream) {
312 if (bopen_rsrc(&altbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
313 Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s\n"), jcr->last_fname);
318 Dmsg0(30, "Restoring resource fork\n");
321 if (extract_data(jcr, &altbfd, sd->msg, sd->msglen, &alt_addr, flags) < 0) {
332 case STREAM_HFSPLUS_ATTRIBUTES:
333 #ifdef HAVE_DARWIN_OS
334 Dmsg0(30, "Restoring Finder Info\n");
335 if (sd->msglen != 32) {
336 Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen);
339 if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
340 Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s\n"), jcr->last_fname);
347 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
349 pm_strcpy(jcr->acl_text, sd->msg);
350 Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_ACCESS, jcr->acl_text);
351 if (bacl_set(jcr, BACL_TYPE_ACCESS) != 0) {
352 Jmsg1(jcr, M_WARNING, 0, "Can't restore ACL of %s\n", jcr->last_fname);
359 case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
361 pm_strcpy(jcr->acl_text, sd->msg);
362 Dmsg2(400, "Restoring ACL type 0x%2x <%s>\n", BACL_TYPE_DEFAULT, jcr->acl_text);
363 if (bacl_set(jcr, BACL_TYPE_DEFAULT) != 0) {
364 Jmsg1(jcr, M_WARNING, 0, "Can't restore default ACL of %s\n", jcr->last_fname);
371 case STREAM_MD5_SIGNATURE:
372 case STREAM_SHA1_SIGNATURE:
375 case STREAM_PROGRAM_NAMES:
376 case STREAM_PROGRAM_DATA:
377 if (!non_support_progname) {
378 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
379 non_support_progname++;
384 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
386 Dmsg1(30, "Found wierd stream %d\n", stream);
387 if (size > 0 && !is_bopen(&bfd)) {
388 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
390 set_attributes(jcr, attr, &bfd);
392 } else if (is_bopen(&bfd)) {
393 Jmsg0(jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
396 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
397 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
399 } /* end switch(stream) */
401 } /* end while get_msg() */
403 /* If output file is still open, it was the last one in the
404 * archive since we just hit an end of file, so close the file.
406 if (is_bopen(&altbfd)) {
407 bclose_chksize(jcr, &altbfd, alt_size);
410 set_attributes(jcr, attr, &bfd);
412 if (is_bopen(&bfd)) {
415 set_jcr_job_status(jcr, JS_Terminated);
419 set_jcr_job_status(jcr, JS_ErrorTerminated);
421 if (jcr->compress_buf) {
422 free(jcr->compress_buf);
423 jcr->compress_buf = NULL;
424 jcr->compress_buf_size = 0;
429 free_pool_memory(jcr->acl_text);
430 Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
431 edit_uint64(jcr->JobBytes, ec1));
432 if (non_support_data > 1 || non_support_attr > 1) {
433 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
434 non_support_data, non_support_attr);
436 if (non_support_rsrc) {
437 Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
439 if (non_support_finfo) {
440 Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
442 if (non_support_acl) {
443 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
450 * Convert ZLIB error code into an ASCII message
452 static const char *zlib_strerror(int stat)
461 return "Zlib stream error";
463 return "Zlib data error";
465 return "Zlib memory error";
467 return "Zlib buffer error";
468 case Z_VERSION_ERROR:
469 return "Zlib version error";
477 * In the context of jcr, write data to bfd.
478 * We write buflen bytes in buf at addr. addr is updated in place.
479 * The flags specify whether to use sparse files or compression.
480 * Return value is the number of bytes written, or -1 on errors.
482 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
483 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) {
519 * NOTE! We only use uLong and Byte because they are
520 * needed by the zlib routines, they should not otherwise
523 compress_len = jcr->compress_buf_size;
524 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
525 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
526 (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
527 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
528 jcr->last_fname, zlib_strerror(stat));
531 wbuf = jcr->compress_buf;
532 wsize = compress_len;
533 Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
535 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
539 Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
542 if (bwrite(bfd, wbuf, wsize) != (ssize_t)wsize) {
544 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"),
545 jcr->last_fname, be.strerror(bfd->berrno));
549 jcr->JobBytes += wsize;
550 jcr->ReadBytes += rsize;