2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2004 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,
34 #include <acl/libacl.h>
39 #include <sys/paths.h>
42 /* Data received from Storage Daemon */
43 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
45 /* Forward referenced functions */
47 static const char *zlib_strerror(int stat);
50 #define RETRY 10 /* retry wait time */
53 /* helper routine for closing resource forks */
54 int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize)
56 char ec1[50], ec2[50];
59 fsize = blseek(bfd, 0, SEEK_CUR);
60 bclose(bfd); /* first close file */
61 if (fsize > 0 && fsize != osize) {
62 Jmsg3(jcr, M_ERROR, 0, _("File size of resource fork for restored file %s not correct. Original %s, restored %s.\n"),
63 jcr->last_fname, edit_uint64(osize, ec1),
64 edit_uint64(fsize, ec2));
72 * Restore the requested files.
75 void do_restore(JCR *jcr)
80 uint32_t VolSessionId, VolSessionTime;
85 uint32_t total = 0; /* Job total but only 32 bits for debug */
86 char *wbuf; /* write buffer */
87 uint32_t wsize; /* write size */
88 uint64_t fileAddr = 0; /* file write address */
89 int non_support_data = 0;
90 int non_support_attr = 0;
91 int non_support_rsrc = 0;
92 int non_support_finfo = 0;
93 int non_support_acl = 0;
94 int prog_name_msg = 0;
99 BFILE rsrc_bfd; /* we often check if it is open */
100 #ifdef HAVE_DARWIN_OS
102 off_t rsrc_len; /* original length of resource fork */
104 /* TODO: initialise attrList once elsewhere? */
105 struct attrlist attrList;
106 memset(&attrList, 0, sizeof(attrList));
107 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
108 attrList.commonattr = ATTR_CMN_FNDRINFO;
113 sd = jcr->store_bsock;
114 set_jcr_job_status(jcr, JS_Running);
117 CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
121 buf_size = client->max_network_buffer_size;
123 buf_size = 0; /* use default */
125 if (!bnet_set_buffer_size(sd, buf_size, BNET_SETBUF_WRITE)) {
126 set_jcr_job_status(jcr, JS_ErrorTerminated);
129 jcr->buf_size = sd->msglen;
134 uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
135 jcr->compress_buf = (char *)bmalloc(compress_buf_size);
139 * Get a record from the Storage daemon. We are guaranteed to
140 * receive records in the following order:
141 * 1. Stream record header
143 * a. Attributes (Unix or Win32)
144 * or b. File data for the file
145 * or c. Resource fork
148 * or f. Possibly MD5 or SHA1 record
151 while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
153 * First we expect a Stream Record Header
155 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
156 &stream, &size) != 5) {
157 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
160 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
163 * 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);
180 /* If extracting, it was from previous stream, so
181 * close the output file.
185 #ifdef HAVE_DARWIN_OS
186 && !is_bopen(&rsrc_bfd)
189 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
191 #ifdef HAVE_DARWIN_OS
192 if (is_bopen(&rsrc_bfd)) {
193 bclose_chksize(jcr, &rsrc_bfd, rsrc_len);
196 if (is_bopen(&bfd)) {
197 set_attributes(jcr, attr, &bfd);
200 Dmsg0(30, "Stop extracting.\n");
204 if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
207 if (file_index != attr->file_index) {
208 Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
209 file_index, attr->file_index);
210 Dmsg0(100, "File index error\n");
214 Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname,
215 attr->attr, attr->attrEx);
217 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
219 if (!is_stream_supported(attr->data_stream)) {
220 if (!non_support_data++) {
221 Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
222 stream_to_ascii(attr->data_stream));
227 build_attr_output_fnames(jcr, attr);
229 #ifdef HAVE_DARWIN_OS
230 from_base64(&rsrc_len, attr->attrEx);
233 jcr->num_files_examined++;
235 Dmsg1(30, "Outfile=%s\n", attr->ofname);
237 stat = create_file(jcr, attr, &bfd, jcr->replace);
247 pm_strcpy(jcr->last_fname, attr->ofname);
251 print_ls_output(jcr, attr);
253 /* set attributes now because file will not be extracted */
254 set_attributes(jcr, attr, &bfd);
256 #ifdef HAVE_DARWIN_OS
259 if (bopen_rsrc(&rsrc_bfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
260 Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s"), jcr->last_fname);
262 Dmsg0(30, "Restoring resource fork");
272 case STREAM_FILE_DATA:
273 case STREAM_SPARSE_DATA:
274 case STREAM_WIN32_DATA:
276 if (stream == STREAM_SPARSE_DATA) {
281 wbuf = sd->msg + SPARSE_FADDR_SIZE;
282 wsize = sd->msglen - SPARSE_FADDR_SIZE;
283 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
285 if (fileAddr != faddr) {
287 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
289 be.set_errno(bfd.berrno);
290 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
291 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
294 if (is_bopen(&rsrc_bfd)) {
304 Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
305 if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
306 Dmsg0(0, "===Write error===\n");
308 be.set_errno(bfd.berrno);
309 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname,
313 if (is_bopen(&rsrc_bfd)) {
319 jcr->JobBytes += wsize;
320 jcr->ReadBytes += wsize;
325 /* GZIP data stream */
326 case STREAM_GZIP_DATA:
327 case STREAM_SPARSE_GZIP_DATA:
328 case STREAM_WIN32_GZIP_DATA:
334 if (stream == STREAM_SPARSE_GZIP_DATA) {
338 wbuf = sd->msg + SPARSE_FADDR_SIZE;
339 wsize = sd->msglen - SPARSE_FADDR_SIZE;
340 ser_begin(sd->msg, SPARSE_FADDR_SIZE);
342 if (fileAddr != faddr) {
344 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
346 be.set_errno(bfd.berrno);
347 Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
348 edit_uint64(fileAddr, ec1), attr->ofname, be.strerror());
351 if (is_bopen(&rsrc_bfd)) {
361 compress_len = compress_buf_size;
362 Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
363 if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
364 (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
365 Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
366 attr->ofname, zlib_strerror(stat));
369 if (is_bopen(&rsrc_bfd)) {
375 Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
376 if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
377 Dmsg0(0, "===Write error===\n");
379 be.set_errno(bfd.berrno);
380 Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror());
383 if (is_bopen(&rsrc_bfd)) {
388 total += compress_len;
389 jcr->JobBytes += compress_len;
390 jcr->ReadBytes += wsize;
391 fileAddr += compress_len;
395 Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
398 if (is_bopen(&rsrc_bfd)) {
406 /* Resource fork stream - only recorded after a file to be restored */
407 /* Silently ignore if we cannot write - we already reported that */
408 case STREAM_MACOS_FORK_DATA:
409 #ifdef HAVE_DARWIN_OS
410 if (is_bopen(&rsrc_bfd) && sd->msglen) {
411 Dmsg2(30, "Write %u bytes, total before write=%u\n", sd->msglen, total);
412 if (bwrite(&rsrc_bfd, sd->msg, sd->msglen) != sd->msglen) {
413 Dmsg0(0, "===Write error===\n");
415 be.set_errno(rsrc_bfd.berrno);
416 Jmsg2(jcr, M_ERROR, 0, _("Write error on resource fork of %s: ERR=%s\n"), jcr->last_fname,
419 if (is_bopen(&bfd)) {
426 jcr->JobBytes += sd->msglen;
427 jcr->ReadBytes += sd->msglen;
428 rsrcAddr += sd->msglen;
435 case STREAM_HFSPLUS_ATTRIBUTES:
436 #ifdef HAVE_DARWIN_OS
437 Dmsg0(30, "Restoring Finder Info");
438 if (sd->msglen != 32) {
439 Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)"), sd->msglen);
442 if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) {
443 Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s"), jcr->last_fname);
451 case STREAM_UNIX_ATTRIBUTES_ACL:
453 /* Recover ACL from stream and check it */
454 acl = acl_from_text(sd->msg);
455 if (acl_valid(acl) != 0) {
456 Jmsg1(jcr, M_WARNING, 0, "Failure in the ACL of %s! FD is not able to restore it!\n", jcr->last_fname);
460 /* Try to restore ACL */
461 if (attr->type == FT_DIREND) {
463 if (acl_set_file(jcr->last_fname, ACL_TYPE_DEFAULT, acl) != 0 &&
464 acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
465 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of directory: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
468 } else if (acl_set_file(jcr->last_fname, ACL_TYPE_ACCESS, acl) != 0) {
469 Jmsg1(jcr, M_WARNING, 0, "Error! Can't restore ACL of file: %s! Maybe system does not support ACLs!\n", jcr->last_fname);
472 Dmsg1(200, "ACL of file: %s successfully restored!", jcr->last_fname);
476 break; /* unconfigured, ignore */
479 case STREAM_MD5_SIGNATURE:
480 case STREAM_SHA1_SIGNATURE:
483 case STREAM_PROGRAM_NAMES:
484 case STREAM_PROGRAM_DATA:
485 if (!prog_name_msg) {
486 Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
492 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
494 Dmsg1(30, "Found wierd stream %d\n", stream);
495 if (!is_bopen(&bfd)) {
496 Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
498 set_attributes(jcr, attr, &bfd);
501 Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
502 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
504 } /* end switch(stream) */
506 } /* end while get_msg() */
508 /* If output file is still open, it was the last one in the
509 * archive since we just hit an end of file, so close the file.
511 if (is_bopen(&bfd)) {
512 set_attributes(jcr, attr, &bfd);
514 set_jcr_job_status(jcr, JS_Terminated);
518 set_jcr_job_status(jcr, JS_ErrorTerminated);
520 if (jcr->compress_buf) {
521 free(jcr->compress_buf);
522 jcr->compress_buf = NULL;
524 #ifdef HAVE_DARWIN_OS
525 if (is_bopen(&rsrc_bfd)) {
526 bclose_chksize(jcr, &rsrc_bfd, rsrc_len);
531 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
533 if (non_support_data > 1 || non_support_attr > 1) {
534 Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
535 non_support_data, non_support_attr);
537 if (non_support_rsrc) {
538 Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc);
540 if (non_support_finfo) {
541 Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc);
543 if (non_support_acl) {
544 Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl);
551 * Convert ZLIB error code into an ASCII message
553 static const char *zlib_strerror(int stat)
562 return "Zlib stream error";
564 return "Zlib data error";
566 return "Zlib memory error";
568 return "Zlib buffer error";
569 case Z_VERSION_ERROR:
570 return "Zlib version error";