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, uint64_t *addr,
49 uint32_t *total, 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 * NOTE: set_win32_attributes() may now have to reopen the file :-(
59 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
61 char ec1[50], ec2[50];
64 fsize = blseek(bfd, 0, SEEK_CUR);
65 bclose(bfd); /* first close file */
66 if (fsize > 0 && fsize != osize) {
67 Jmsg3(jcr, M_ERROR, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
68 jcr->last_fname, edit_uint64(osize, ec1),
69 edit_uint64(fsize, ec2));
76 * Restore the requested files.
79 void do_restore(JCR *jcr)
82 int32_t stream, prev_stream;
84 uint32_t VolSessionId, VolSessionTime;
89 uint32_t total = 0; /* Job total but only 32 bits for debug */
90 uint64_t fileAddr = 0; /* file write address */
91 intmax_t want_len = 0; /* How many bytes we expect to write */
92 int flags; /* Options for extract_data() */
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 prog_name_msg = 0;
103 #ifdef HAVE_DARWIN_OS
104 intmax_t rsrc_len; /* original length of resource fork */
106 /* TODO: initialise attrList once elsewhere? */
107 struct attrlist attrList;
108 memset(&attrList, 0, sizeof(attrList));
109 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
110 attrList.commonattr = ATTR_CMN_FNDRINFO;
114 sd = jcr->store_bsock;
115 set_jcr_job_status(jcr, JS_Running);
118 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
122 buf_size = client->max_network_buffer_size;
124 buf_size = 0; /* use default */
126 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
127 set_jcr_job_status(jcr, JS_ErrorTerminated);
130 jcr->buf_size = sd->msglen;
135 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
136 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
137 jcr->compress_buf_size = compress_buf_size;
141 * Get a record from the Storage daemon. We are guaranteed to
142 * receive records in the following order:
143 * 1. Stream record header
145 * a. Attributes (Unix or Win32)
146 * or b. File data for the file
147 * or c. Resource fork
150 * or f. Possibly MD5 or SHA1 record
153 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
154 prev_stream = stream;
156 /* First we expect a Stream Record Header */
157 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
158 &stream, &size) != 5) {
159 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
162 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
164 /* * Now we expect the Stream Data */
165 if (bget_msg(sd) < 0) {
166 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
169 if (size != (uint32_t)sd->msglen) {
170 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
173 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
175 /* File Attributes stream */
177 case STREAM_UNIX_ATTRIBUTES:
178 case STREAM_UNIX_ATTRIBUTES_EX:
179 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
181 * If extracting, it was from previous stream, so
182 * close the output file.
185 if (!is_bopen(&bfd)) {
186 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
188 bclose_chksize(jcr, &bfd, want_len);
189 set_attributes(jcr, attr, &bfd);
192 Dmsg0(30, "Stop extracting.\n");
195 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
198 if (file_index != attr->file_index) {
199 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
200 file_index, attr->file_index);
201 Dmsg0(100, "File index error\n");
205 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
206 attr->attr, attr->attrEx);
208 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
210 if (!is_stream_supported(attr->data_stream)) {
211 if (!non_support_data++) {
212 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
213 stream_to_ascii(attr->data_stream));
218 build_attr_output_fnames(jcr, attr);
220 jcr->num_files_examined++;
222 Dmsg1(30, "Outfile=%s\n", attr->ofname);
224 stat = create_file(jcr, attr, &bfd, jcr->replace);
234 pm_strcpy(jcr->last_fname, attr->ofname);
238 want_len = attr->statp.st_size;
239 print_ls_output(jcr, attr);
240 #ifdef HAVE_DARWIN_OS
241 /* Only restore the resource fork for regular files */
242 from_base64(&rsrc_len, attr->attrEx);
243 if (attr->type == FT_REG && rsrc_len > 0) {
248 /* set attributes now because file will not be extracted */
249 set_attributes(jcr, attr, &bfd);
256 case STREAM_FILE_DATA:
257 case STREAM_SPARSE_DATA:
258 case STREAM_WIN32_DATA:
259 case STREAM_GZIP_DATA:
260 case STREAM_SPARSE_GZIP_DATA:
261 case STREAM_WIN32_GZIP_DATA:
262 /* Force an expected, consistent stream type here */
263 if (extract && (prev_stream == stream || prev_stream == STREAM_UNIX_ATTRIBUTES
264 || prev_stream == STREAM_UNIX_ATTRIBUTES_EX)) {
266 if (stream == STREAM_SPARSE_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
269 if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA
270 || stream == STREAM_WIN32_GZIP_DATA) {
273 if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, &total, flags) < 0) {
281 /* Resource fork stream - only recorded after a file to be restored */
282 /* Silently ignore if we cannot write - we already reported that */
283 case STREAM_MACOS_FORK_DATA:
284 #ifdef HAVE_DARWIN_OS
286 if (prev_stream != stream) {
287 if (is_bopen(&bfd)) {
288 bclose_chksize(jcr, &bfd, want_len);
290 if (bopen_rsrc(&bfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
291 Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s"), jcr->last_fname);
296 Dmsg0(30, "Restoring resource fork");
299 if (extract_data(jcr, &bfd, sd->msg, sd->msglen, &fileAddr, &total, flags) < 0) {
310 case STREAM_HFSPLUS_ATTRIBUTES:
311 #ifdef HAVE_DARWIN_OS
312 Dmsg0(30, "Restoring Finder Info");
313 if (sd->msglen != 32) {
314 Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)"), sd->msglen);
317 if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
318 Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s"), jcr->last_fname);
327 case STREAM_UNIX_ATTRIBUTES_ACCESS_ACL:
329 /* Recover Acess ACL from stream and check it */
330 acl = acl_from_text(sd->msg);
331 if (acl_valid(acl) != 0) {
332 Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
336 /* Try to restore ACL */
337 if (attr->type == FT_DIREND) {
339 if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
340 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
343 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
344 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
347 Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
351 break; /* unconfigured, ignore */
353 case STREAM_UNIX_ATTRIBUTES_DEFAULT_ACL:
355 /* Recover Default ACL from stream and check it */
356 acl = acl_from_text(sd->msg);
357 if (acl_valid(acl) != 0) {
358 Jmsg1(jcr, M_WARNING, 0, "Failure in the Default ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
362 /* Try to restore ACL */
363 if (attr->type == FT_DIREND) {
365 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0) {
366 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore Default ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
370 Dmsg1(200, "Default ACL of file: %s successfully restored!", jcr->last_fname);
374 break; /* unconfigured, ignore */
378 case STREAM_MD5_SIGNATURE:
379 case STREAM_SHA1_SIGNATURE:
382 case STREAM_PROGRAM_NAMES:
383 case STREAM_PROGRAM_DATA:
384 if (!prog_name_msg) {
385 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
391 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
393 Dmsg1(30, "Found wierd stream %d\n", stream);
394 if (!is_bopen(&bfd)) {
395 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
397 set_attributes(jcr, attr, &bfd);
400 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
401 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
403 } /* end switch(stream) */
405 } /* end while get_msg() */
407 /* If output file is still open, it was the last one in the
408 * archive since we just hit an end of file, so close the file.
410 if (is_bopen(&bfd)) {
411 bclose_chksize(jcr, &bfd, want_len);
412 set_attributes(jcr, attr, &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;
427 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
429 if (non_support_data > 1 || non_support_attr > 1) {
430 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
431 non_support_data, non_support_attr);
433 if (non_support_rsrc) {
434 Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
436 if (non_support_finfo) {
437 Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
439 if (non_support_acl) {
440 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
447 * Convert ZLIB error code into an ASCII message
449 static const char *zlib_strerror(int stat)
458 return "Zlib stream error";
460 return "Zlib data error";
462 return "Zlib memory error";
464 return "Zlib buffer error";
465 case Z_VERSION_ERROR:
466 return "Zlib version error";
474 * In the context of jcr, write data to bfd.
475 * We write buflen bytes in buf at addr. addr is updated in place.
476 * The flags specify whether to use sparse files or compression.
477 * Return value is the number of bytes written, or -1 on errors.
480 * We update total in here. For some reason, jcr->JobBytes does not work here.
482 int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, uint64_t *addr,
483 uint32_t *total, int flags)
487 char *wbuf; /* write buffer */
488 uint32_t wsize; /* write size */
489 uint32_t rsize; /* read size */
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 be.set_errno(bfd->berrno);
504 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
505 edit_uint64(*addr, ec1), jcr->last_fname, be.strerror());
515 if (flags & FO_GZIP) {
517 compress_len = jcr->compress_buf_size;
518 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
519 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
520 (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
521 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
522 jcr->last_fname, zlib_strerror(stat));
525 wbuf = jcr->compress_buf;
526 wsize = compress_len;
527 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, *total);
529 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
533 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, *total);
536 if ((uLong)bwrite(bfd, wbuf, wsize) != wsize) {
537 Dmsg0(0, "===Write error===\n");
539 be.set_errno(bfd->berrno);
540 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), jcr->last_fname, be.strerror());
545 jcr->JobBytes += wsize;
546 jcr->ReadBytes += rsize;