]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/restore.c
Loose #ifdef and use const bool wrapper for some cleaner coding.
[bacula/bacula] / bacula / src / filed / restore.c
index 4bbd5dbd14659d277e6bb4aa6a23157f1de135b5..f9e8c5f8cb827b2f3db94c28b0335364fc22919b 100644 (file)
@@ -30,8 +30,6 @@
  *
  *    Kern Sibbald, November MM
  *
- *   Version $Id$
- *
  */
 
 #include "bacula.h"
@@ -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;
    }
 
@@ -251,10 +249,14 @@ void do_restore(JCR *jcr)
    binit(&rctx.forkbfd);
    attr = rctx.attr = new_attr(jcr);
    if (have_acl) {
-      jcr->acl_data = get_pool_memory(PM_MESSAGE);
+      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 = get_pool_memory(PM_MESSAGE);
+      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)) {
@@ -363,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);
 
@@ -373,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) {
@@ -504,82 +515,86 @@ 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:
@@ -608,10 +623,23 @@ 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_streams(jcr, rctx.stream) != bsub_exit_ok) {
-               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++;
@@ -634,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_streams(jcr, rctx.stream) != bsub_exit_ok) {
-               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++;
@@ -703,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) {
@@ -711,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;
@@ -732,45 +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;
    }
 
-   if (have_xattr && jcr->xattr_data) {
-      free_pool_memory(jcr->xattr_data);
-      jcr->xattr_data = NULL;
-   }
    if (have_acl && jcr->acl_data) {
-      free_pool_memory(jcr->acl_data);
+      free_pool_memory(jcr->acl_data->content);
+      free(jcr->acl_data);
       jcr->acl_data = NULL;
    }
 
+   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);
-   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 (non_support_xattr) {
-      Jmsg(jcr, M_INFO, 0, _("%d non-supported xattr streams ignored.\n"), non_support_xattr);
-   }
-
 }
 
 #ifdef HAVE_LIBZ
@@ -956,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;
@@ -1010,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 */
@@ -1113,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;
-
 }