]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/backup.c
- improved code style of last commit ;-)
[bacula/bacula] / bacula / src / filed / backup.c
index 7f277ba9da78efa97593da7958d8cd6039626713..73a57e5be9838d9e37bb7e9f8f25c3e2c9dad59d 100644 (file)
@@ -8,7 +8,7 @@
  *
  */
 /*
-   Copyright (C) 2000-2005 Kern Sibbald
+   Copyright (C) 2000-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -182,14 +182,13 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr, bool top_level)
 #else
    crypto_digest_t signing_algorithm = CRYPTO_DIGEST_SHA1;
 #endif
-   BSOCK *sd;
    JCR *jcr = (JCR *)vjcr;
+   BSOCK *sd = jcr->store_bsock;
 
    if (job_canceled(jcr)) {
       return 0;
    }
 
-   sd = jcr->store_bsock;
    jcr->num_files_examined++;         /* bump total file count */
 
    switch (ff_pkt->type) {
@@ -206,6 +205,7 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr, bool top_level)
       Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
       break;
    case FT_DIRBEGIN:
+      jcr->num_files_examined--;      /* correct file count */
       return 1;                       /* not used */
    case FT_NORECURSE:
      Jmsg(jcr, M_INFO, 1, _("     Recursion turned off. Will not descend into %s\n"),
@@ -587,13 +587,15 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *sign
       wbuf = jcr->compress_buf;    /* compressed output here */
       cipher_input = jcr->compress_buf; /* encrypt compressed data */
    }
+#else
+   const uint32_t max_compress_len = 0;
 #endif
 
    if (ff_pkt->flags & FO_ENCRYPT) {
       /* Allocate the cipher context */
       if ((cipher_ctx = crypto_cipher_new(jcr->pki_session, true, &cipher_block_size)) == NULL) {
          /* Shouldn't happen! */
-         Jmsg0(jcr, M_FATAL, 0, _("Failed to initialize encryption context"));
+         Jmsg0(jcr, M_FATAL, 0, _("Failed to initialize encryption context\n"));
          goto err;
       }
 
@@ -641,6 +643,21 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *sign
    if (S_ISBLK(ff_pkt->statp.st_mode))
       rsize = (rsize/512) * 512;      
 #endif
+   
+#ifdef HAVE_LIBZ
+   /* 
+    * instead of using compress2 for every block (with 256KB alloc + free per block)
+    * we init the zlib once per file which leads to less pagefaults on large files (>64K)
+    */
+   
+   z_stream zstream;
+   zstream.zalloc = Z_NULL;
+   zstream.zfree = Z_NULL;
+   zstream.opaque = Z_NULL;
+   zstream.state = Z_NULL;
+
+       BOOL blibzInited = deflateInit(&zstream, ff_pkt->GZIP_level) == Z_OK;
+#endif
 
    /*
     * Read the file data
@@ -680,19 +697,25 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *sign
 
 #ifdef HAVE_LIBZ
       /* Do compression if turned on */
-      if (!sparseBlock && ff_pkt->flags & FO_GZIP) {
+      if (!sparseBlock && (ff_pkt->flags & FO_GZIP) && blibzInited) {
          int zstat;
          compress_len = max_compress_len;
          Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len,
             rbuf, sd->msglen);
-         /* NOTE! This call modifies compress_len !!! */
-         if ((zstat=compress2((Bytef *)cbuf, &compress_len,
-               (const Bytef *)rbuf, (uLong)sd->msglen,
-               ff_pkt->GZIP_level)) != Z_OK) {
+         
+         zstream.next_in   = (Bytef *)rbuf;
+                       zstream.avail_in  = sd->msglen;         
+         zstream.next_out  = (Bytef *)cbuf;
+                       zstream.avail_out = compress_len;
+
+         if ((zstat=deflate(&zstream, Z_FINISH)) != Z_STREAM_END) {
             Jmsg(jcr, M_FATAL, 0, _("Compression error: %d\n"), zstat);
             set_jcr_job_status(jcr, JS_ErrorTerminated);
             goto err;
          }
+         compress_len = zstream.total_out;
+         blibzInited = deflateReset(&zstream) == Z_OK;
+
          Dmsg2(400, "compressed len=%d uncompressed len=%d\n",
             compress_len, sd->msglen);
 
@@ -784,13 +807,23 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *sign
    if (cipher_ctx) {
       crypto_cipher_free(cipher_ctx);
    }
+#ifdef HAVE_LIBZ
+   /* Free the zlib stream */
+   deflateEnd(&zstream);
+#endif
 
    return 1;
 
 err:
+   /* Free the cipher context */
    if (cipher_ctx) {
       crypto_cipher_free(cipher_ctx);
    }
+#ifdef HAVE_LIBZ
+   /* Free the zlib stream */
+   deflateEnd(&zstream);
+#endif
+
    sd->msg = msgsave; /* restore bnet buffer */
    sd->msglen = 0;
    return 0;
@@ -875,11 +908,11 @@ static bool encode_and_send_attributes(JCR *jcr, FF_PKT *ff_pkt, int &data_strea
 
    Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
 
-   P(jcr->mutex);
+   jcr->lock();
    jcr->JobFiles++;                    /* increment number of files sent */
    ff_pkt->FileIndex = jcr->JobFiles;  /* return FileIndex */
    pm_strcpy(jcr->last_fname, ff_pkt->fname);
-   V(jcr->mutex);
+   jcr->unlock();
 
    /*
     * Send Attributes header to Storage daemon