X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Ffiled%2Frestore.c;h=f9e8c5f8cb827b2f3db94c28b0335364fc22919b;hb=5c53840c9a02cefaaa45aee96c02c6f9cdaf3703;hp=eba51ce3fa9fd3aadf799a70bd119d24f580ea46;hpb=f4891446832bfaaadc8b7f7a0f9a6974ae961110;p=bacula%2Fbacula diff --git a/bacula/src/filed/restore.c b/bacula/src/filed/restore.c index eba51ce3fa..f9e8c5f8cb 100644 --- a/bacula/src/filed/restore.c +++ b/bacula/src/filed/restore.c @@ -30,8 +30,6 @@ * * Kern Sibbald, November MM * - * Version $Id$ - * */ #include "bacula.h" @@ -57,9 +55,9 @@ const bool have_acl = false; #endif #ifdef HAVE_SHA2 - const bool have_sha2 = true; +const bool have_sha2 = true; #else - const bool have_sha2 = false; +const bool have_sha2 = false; #endif #if defined(HAVE_XATTR) @@ -120,7 +118,7 @@ static void close_previous_stream(r_ctx &rctx); static bool verify_signature(JCR *jcr, r_ctx &rctx); -int32_t extract_data(JCR *jcr, r_ctx &rctx, POOLMEM *buf, int32_t buflen, +int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, uint64_t *addr, int flags, RESTORE_CIPHER_CTX *cipher_ctx); bool flush_cipher(JCR *jcr, BFILE *bfd, uint64_t *addr, int flags, RESTORE_CIPHER_CTX *cipher_ctx); @@ -207,7 +205,7 @@ void do_restore(JCR *jcr) if (have_libz) { uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100; - jcr->compress_buf = (char *)bmalloc(compress_buf_size); + jcr->compress_buf = get_memory(compress_buf_size); jcr->compress_buf_size = compress_buf_size; } @@ -229,8 +227,9 @@ void do_restore(JCR *jcr) * d. Alternate data stream (e.g. Resource Fork) * e. Finder info * f. ACLs - * g. Possibly a cryptographic signature - * h. Possibly MD5 or SHA1 record + * g. XATTRs + * h. Possibly a cryptographic signature + * i. Possibly MD5 or SHA1 record * 3. Repeat step 1 * * NOTE: We keep track of two bacula file descriptors: @@ -249,8 +248,16 @@ void do_restore(JCR *jcr) binit(&rctx.bfd); binit(&rctx.forkbfd); attr = rctx.attr = new_attr(jcr); - jcr->acl_data = get_pool_memory(PM_MESSAGE); - jcr->xattr_data = get_pool_memory(PM_MESSAGE); + if (have_acl) { + jcr->acl_data = (acl_data_t *)malloc(sizeof(acl_data_t)); + memset((caddr_t)jcr->acl_data, 0, sizeof(acl_data_t)); + jcr->acl_data->content = get_pool_memory(PM_MESSAGE); + } + if (have_xattr) { + jcr->xattr_data = (xattr_data_t *)malloc(sizeof(xattr_data_t)); + memset((caddr_t)jcr->xattr_data, 0, sizeof(xattr_data_t)); + jcr->xattr_data->content = get_pool_memory(PM_MESSAGE); + } while (bget_msg(sd) >= 0 && !job_canceled(jcr)) { /* Remember previous stream type */ @@ -358,7 +365,6 @@ void do_restore(JCR *jcr) rctx.extract = true; /* FALLTHROUGH */ case CF_CREATED: /* File created, but there is no content */ - jcr->JobFiles++; rctx.fileAddr = 0; print_ls_output(jcr, attr); @@ -368,7 +374,17 @@ void do_restore(JCR *jcr) if (attr->type == FT_REG && rsrc_len > 0) { rctx.extract = true; } + + /* + * Count the resource forks not as regular files being restored. + */ + if (rsrc_len == 0) { + jcr->JobFiles++; + } + } else { + jcr->JobFiles++; } + if (!rctx.extract) { /* set attributes now because file will not be extracted */ if (jcr->plugin) { @@ -499,100 +515,104 @@ void do_restore(JCR *jcr) rctx.flags |= FO_WIN32DECOMP; /* "decompose" BackupWrite data */ } - if (extract_data(jcr, rctx, sd->msg, sd->msglen, &rctx.fileAddr, + if (extract_data(jcr, &rctx.bfd, sd->msg, sd->msglen, &rctx.fileAddr, rctx.flags, &rctx.cipher_ctx) < 0) { + rctx.extract = false; bclose(&rctx.bfd); continue; } } break; - /* Resource fork stream - only recorded after a file to be restored */ - /* Silently ignore if we cannot write - we already reported that */ + /* + * Resource fork stream - only recorded after a file to be restored + * Silently ignore if we cannot write - we already reported that + */ case STREAM_ENCRYPTED_MACOS_FORK_DATA: case STREAM_MACOS_FORK_DATA: -#ifdef HAVE_DARWIN_OS - rctx.fork_flags = 0; - jcr->ff->flags |= FO_HFSPLUS; + if (have_darwin_os) { + rctx.fork_flags = 0; + jcr->ff->flags |= FO_HFSPLUS; - if (rctx.stream == STREAM_ENCRYPTED_MACOS_FORK_DATA) { - rctx.fork_flags |= FO_ENCRYPT; + if (rctx.stream == STREAM_ENCRYPTED_MACOS_FORK_DATA) { + rctx.fork_flags |= FO_ENCRYPT; - /* Set up a decryption context */ - if (rctx.extract && !rctx.fork_cipher_ctx.cipher) { - if (!rctx.cs) { - Jmsg1(jcr, M_ERROR, 0, _("Missing encryption session data stream for %s\n"), jcr->last_fname); - rctx.extract = false; - bclose(&rctx.bfd); - continue; - } + /* Set up a decryption context */ + if (rctx.extract && !rctx.fork_cipher_ctx.cipher) { + if (!rctx.cs) { + Jmsg1(jcr, M_ERROR, 0, _("Missing encryption session data stream for %s\n"), jcr->last_fname); + rctx.extract = false; + bclose(&rctx.bfd); + continue; + } - if ((rctx.fork_cipher_ctx.cipher = crypto_cipher_new(rctx.cs, false, &rctx.fork_cipher_ctx.block_size)) == NULL) { - Jmsg1(jcr, M_ERROR, 0, _("Failed to initialize decryption context for %s\n"), jcr->last_fname); - free_session(rctx); - rctx.extract = false; - bclose(&rctx.bfd); - continue; + if ((rctx.fork_cipher_ctx.cipher = crypto_cipher_new(rctx.cs, false, &rctx.fork_cipher_ctx.block_size)) == NULL) { + Jmsg1(jcr, M_ERROR, 0, _("Failed to initialize decryption context for %s\n"), jcr->last_fname); + free_session(rctx); + rctx.extract = false; + bclose(&rctx.bfd); + continue; + } } } - } - if (rctx.extract) { - if (rctx.prev_stream != rctx.stream) { - if (bopen_rsrc(&rctx.forkbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) { - Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s.\n"), jcr->last_fname); + if (rctx.extract) { + if (rctx.prev_stream != rctx.stream) { + if (bopen_rsrc(&rctx.forkbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) { + Jmsg(jcr, M_ERROR, 0, _(" Cannot open resource fork for %s.\n"), jcr->last_fname); + rctx.extract = false; + continue; + } + + rctx.fork_size = rsrc_len; + Dmsg0(130, "Restoring resource fork\n"); + } + + if (extract_data(jcr, &rctx.forkbfd, sd->msg, sd->msglen, &rctx.fork_addr, rctx.fork_flags, + &rctx.fork_cipher_ctx) < 0) { rctx.extract = false; + bclose(&rctx.forkbfd); continue; } - - rctx.fork_size = rsrc_len; - Dmsg0(130, "Restoring resource fork\n"); - } - - if (extract_data(jcr, rctx, sd->msg, sd->msglen, &rctx.fork_addr, rctx.fork_flags, - &rctx.fork_cipher_ctx) < 0) { - bclose(&rctx.forkbfd); - continue; } + } else { + non_support_rsrc++; } -#else - non_support_rsrc++; -#endif break; case STREAM_HFSPLUS_ATTRIBUTES: -#ifdef HAVE_DARWIN_OS - Dmsg0(130, "Restoring Finder Info\n"); - jcr->ff->flags |= FO_HFSPLUS; - if (sd->msglen != 32) { - Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen); - continue; - } - if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) { - Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s\n"), jcr->last_fname); - continue; + if (have_darwin_os) { + Dmsg0(130, "Restoring Finder Info\n"); + jcr->ff->flags |= FO_HFSPLUS; + if (sd->msglen != 32) { + Jmsg(jcr, M_ERROR, 0, _(" Invalid length of Finder Info (got %d, not 32)\n"), sd->msglen); + continue; + } + if (setattrlist(jcr->last_fname, &attrList, sd->msg, sd->msglen, 0) != 0) { + Jmsg(jcr, M_ERROR, 0, _(" Could not set Finder Info on %s\n"), jcr->last_fname); + continue; + } + } else { + non_support_finfo++; } -#else - non_support_finfo++; -#endif break; case STREAM_UNIX_ACCESS_ACL: case STREAM_UNIX_DEFAULT_ACL: case STREAM_ACL_AIX_TEXT: - case STREAM_ACL_DARWIN_ACCESS_ACL_T: - case STREAM_ACL_FREEBSD_DEFAULT_ACL_T: - case STREAM_ACL_FREEBSD_ACCESS_ACL_T: + case STREAM_ACL_DARWIN_ACCESS_ACL: + case STREAM_ACL_FREEBSD_DEFAULT_ACL: + case STREAM_ACL_FREEBSD_ACCESS_ACL: case STREAM_ACL_HPUX_ACL_ENTRY: - case STREAM_ACL_IRIX_DEFAULT_ACL_T: - case STREAM_ACL_IRIX_ACCESS_ACL_T: - case STREAM_ACL_LINUX_DEFAULT_ACL_T: - case STREAM_ACL_LINUX_ACCESS_ACL_T: - case STREAM_ACL_TRU64_DEFAULT_ACL_T: - case STREAM_ACL_TRU64_DEFAULT_DIR_ACL_T: - case STREAM_ACL_TRU64_ACCESS_ACL_T: - case STREAM_ACL_SOLARIS_ACLENT_T: - case STREAM_ACL_SOLARIS_ACE_T: + case STREAM_ACL_IRIX_DEFAULT_ACL: + case STREAM_ACL_IRIX_ACCESS_ACL: + case STREAM_ACL_LINUX_DEFAULT_ACL: + case STREAM_ACL_LINUX_ACCESS_ACL: + case STREAM_ACL_TRU64_DEFAULT_ACL: + case STREAM_ACL_TRU64_DEFAULT_DIR_ACL: + case STREAM_ACL_TRU64_ACCESS_ACL: + case STREAM_ACL_SOLARIS_ACLENT: + case STREAM_ACL_SOLARIS_ACE: /* * Do not restore ACLs when * a) The current file is not extracted @@ -603,19 +623,35 @@ void do_restore(JCR *jcr) break; } if (have_acl) { - pm_memcpy(jcr->acl_data, sd->msg, sd->msglen); - jcr->acl_data_len = sd->msglen; - if (!parse_acl_stream(jcr, rctx.stream)) { - Qmsg1(jcr, M_WARNING, 0, _("Can't restore ACLs of %s\n"), jcr->last_fname); + pm_memcpy(jcr->acl_data->content, sd->msg, sd->msglen); + jcr->acl_data->content_length = sd->msglen; + switch (parse_acl_streams(jcr, rctx.stream)) { + case bacl_exit_fatal: + goto bail_out; + case bacl_exit_error: + /* + * Non-fatal errors, count them and when the number is under ACL_REPORT_ERR_MAX_PER_JOB + * print the error message set by the lower level routine in jcr->errmsg. + */ + if (jcr->acl_data->nr_errors < ACL_REPORT_ERR_MAX_PER_JOB) { + Qmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg); + } + jcr->acl_data->nr_errors++; + break; + case bacl_exit_ok: + break; } } else { non_support_acl++; } break; + case STREAM_XATTR_SOLARIS_SYS: + case STREAM_XATTR_SOLARIS: case STREAM_XATTR_DARWIN: case STREAM_XATTR_FREEBSD: case STREAM_XATTR_LINUX: + case STREAM_XATTR_NETBSD: /* * Do not restore Extended Attributes when * a) The current file is not extracted @@ -626,10 +662,23 @@ void do_restore(JCR *jcr) break; } if (have_xattr) { - pm_memcpy(jcr->xattr_data, sd->msg, sd->msglen); - jcr->xattr_data_len = sd->msglen; - if (!parse_xattr_stream(jcr, rctx.stream)) { - Qmsg1(jcr, M_WARNING, 0, _("Can't restore Extended Attributes of %s\n"), jcr->last_fname); + pm_memcpy(jcr->xattr_data->content, sd->msg, sd->msglen); + jcr->xattr_data->content_length = sd->msglen; + switch (parse_xattr_streams(jcr, rctx.stream)) { + case bxattr_exit_fatal: + goto bail_out; + case bxattr_exit_error: + /* + * Non-fatal errors, count them and when the number is under XATTR_REPORT_ERR_MAX_PER_JOB + * print the error message set by the lower level routine in jcr->errmsg. + */ + if (jcr->xattr_data->nr_errors < XATTR_REPORT_ERR_MAX_PER_JOB) { + Qmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg); + } + jcr->xattr_data->nr_errors++; + break; + case bxattr_exit_ok: + break; } } else { non_support_xattr++; @@ -695,7 +744,42 @@ bail_out: set_jcr_job_status(jcr, JS_ErrorTerminated); ok_out: - /* Free Signature & Crypto Data */ + /* + * First output the statistics. + */ + Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles, + edit_uint64(jcr->JobBytes, ec1)); + if (have_acl && jcr->acl_data->nr_errors > 0) { + Jmsg(jcr, M_ERROR, 0, _("Encountered %ld acl errors while doing restore\n"), + jcr->acl_data->nr_errors); + } + if (have_xattr && jcr->xattr_data->nr_errors > 0) { + Jmsg(jcr, M_ERROR, 0, _("Encountered %ld xattr errors while doing restore\n"), + jcr->xattr_data->nr_errors); + } + if (non_support_data > 1 || non_support_attr > 1) { + Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"), + non_support_data, non_support_attr); + } + if (non_support_rsrc) { + Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc); + } + if (non_support_finfo) { + Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc); + } + if (non_support_acl) { + Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl); + } + if (non_support_crypto) { + Jmsg(jcr, M_INFO, 0, _("%d non-supported crypto streams ignored.\n"), non_support_acl); + } + if (non_support_xattr) { + Jmsg(jcr, M_INFO, 0, _("%d non-supported xattr streams ignored.\n"), non_support_xattr); + } + + /* + * Free Signature & Crypto Data + */ free_signature(rctx); free_session(rctx); if (jcr->crypto.digest) { @@ -703,17 +787,22 @@ ok_out: jcr->crypto.digest = NULL; } - /* Free file cipher restore context */ + /* + * Free file cipher restore context + */ if (rctx.cipher_ctx.cipher) { crypto_cipher_free(rctx.cipher_ctx.cipher); rctx.cipher_ctx.cipher = NULL; } + if (rctx.cipher_ctx.buf) { free_pool_memory(rctx.cipher_ctx.buf); rctx.cipher_ctx.buf = NULL; } - /* Free alternate stream cipher restore context */ + /* + * Free alternate stream cipher restore context + */ if (rctx.fork_cipher_ctx.cipher) { crypto_cipher_free(rctx.fork_cipher_ctx.cipher); rctx.fork_cipher_ctx.cipher = NULL; @@ -724,37 +813,26 @@ ok_out: } if (jcr->compress_buf) { - free(jcr->compress_buf); + free_pool_memory(jcr->compress_buf); jcr->compress_buf = NULL; jcr->compress_buf_size = 0; } - bclose(&rctx.forkbfd); - bclose(&rctx.bfd); - free_attr(rctx.attr); - free_pool_memory(jcr->xattr_data); - free_pool_memory(jcr->acl_data); - Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles, - edit_uint64(jcr->JobBytes, ec1)); - if (non_support_data > 1 || non_support_attr > 1) { - Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"), - non_support_data, non_support_attr); - } - if (non_support_rsrc) { - Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_support_rsrc); - } - if (non_support_finfo) { - Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_support_rsrc); - } - if (non_support_acl) { - Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_support_acl); - } - if (non_support_crypto) { - Jmsg(jcr, M_INFO, 0, _("%d non-supported crypto streams ignored.\n"), non_support_acl); + + if (have_acl && jcr->acl_data) { + free_pool_memory(jcr->acl_data->content); + free(jcr->acl_data); + jcr->acl_data = NULL; } - if (non_support_xattr) { - Jmsg(jcr, M_INFO, 0, _("%d non-supported xattr streams ignored.\n"), non_support_xattr); + + if (have_xattr && jcr->xattr_data) { + free_pool_memory(jcr->xattr_data->content); + free(jcr->xattr_data); + jcr->xattr_data = NULL; } + bclose(&rctx.forkbfd); + bclose(&rctx.bfd); + free_attr(rctx.attr); } #ifdef HAVE_LIBZ @@ -940,10 +1018,18 @@ bool decompress_data(JCR *jcr, char **data, uint32_t *length) */ compress_len = jcr->compress_buf_size; Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length); - if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, - (const Byte *)*data, (uLong)*length)) != Z_OK) { + while ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, + (const Byte *)*data, (uLong)*length)) == Z_BUF_ERROR) + { + /* The buffer size is too small, try with a bigger one */ + compress_len = jcr->compress_buf_size = jcr->compress_buf_size + jcr->compress_buf_size >> 1; + Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length); + jcr->compress_buf = check_pool_memory_size(jcr->compress_buf, + compress_len); + } + if (stat != Z_OK) { Qmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"), - jcr->last_fname, zlib_strerror(stat)); + jcr->last_fname, zlib_strerror(stat)); return false; } *data = jcr->compress_buf; @@ -994,10 +1080,9 @@ bool store_data(JCR *jcr, BFILE *bfd, char *data, const int32_t length, bool win * The flags specify whether to use sparse files or compression. * Return value is the number of bytes written, or -1 on errors. */ -int32_t extract_data(JCR *jcr, r_ctx &rctx, POOLMEM *buf, int32_t buflen, - uint64_t *addr, int flags, RESTORE_CIPHER_CTX *cipher_ctx) +int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, + uint64_t *addr, int flags, RESTORE_CIPHER_CTX *cipher_ctx) { - BFILE *bfd = &rctx.bfd; char *wbuf; /* write buffer */ uint32_t wsize; /* write size */ uint32_t rsize; /* read size */ @@ -1097,9 +1182,7 @@ int32_t extract_data(JCR *jcr, r_ctx &rctx, POOLMEM *buf, int32_t buflen, return wsize; bail_out: - rctx.extract = false; return -1; - }