LCOV - code coverage report
Current view: top level - src/server - compress.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 23 41 56.1 %
Date: 2016-02-29 Functions: 2 4 50.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../alloc.h"
       3             : #include "../asfd.h"
       4             : #include "../async.h"
       5             : #include "../fsops.h"
       6             : #include "../fzp.h"
       7             : #include "../log.h"
       8             : #include "../prepend.h"
       9             : 
      10           0 : char *comp_level(int compression)
      11             : {
      12             :         static char comp[8]="";
      13             :         snprintf(comp, sizeof(comp), "wb%d", compression);
      14           0 :         return comp;
      15             : }
      16             : 
      17           9 : static int compress(const char *src, const char *dst, int compression)
      18             : {
      19             :         int res;
      20             :         int got;
      21           9 :         struct fzp *sfzp=NULL;
      22           9 :         struct fzp *dfzp=NULL;
      23             :         char buf[ZCHUNK];
      24             : 
      25          18 :         if(!(sfzp=fzp_open(src, "rb"))
      26          18 :           || !(dfzp=fzp_gzopen(dst, comp_level(compression))))
      27             :                 goto error;
      28          12 :         while((got=fzp_read(sfzp, buf, sizeof(buf)))>0)
      29             :         {
      30           3 :                 res=fzp_write(dfzp, buf, got);
      31           3 :                 if(res!=got)
      32             :                 {
      33             :                         logp("compressing %s - read %d but wrote %d\n",
      34           0 :                                 src, got, res);
      35           0 :                         goto error;
      36             :                 }
      37             :         }
      38           9 :         fzp_close(&sfzp);
      39           9 :         return fzp_close(&dfzp);
      40             : error:
      41           0 :         fzp_close(&sfzp);
      42           0 :         fzp_close(&dfzp);
      43           0 :         return -1;
      44             : }
      45             : 
      46           9 : int compress_file(const char *src, const char *dst, int compression)
      47             : {
      48           9 :         int ret=-1;
      49           9 :         char *dsttmp=NULL;
      50           9 :         pid_t pid=getpid();
      51           9 :         char p[12]="";
      52             :         snprintf(p, sizeof(p), "%d", (int)pid);
      53             : 
      54           9 :         if(!(dsttmp=prepend(dst, p)))
      55             :                 return -1;
      56             : 
      57             :         // Need to compress the log.
      58           9 :         logp("Compressing %s to %s...\n", src, dst);
      59          18 :         if(compress(src, dsttmp, compression)
      60             :         // Possible rename race condition is of little consequence here.
      61             :         // You will still have the uncompressed log file.
      62           9 :           || do_rename(dsttmp, dst))
      63             :         {
      64           0 :                 unlink(dsttmp);
      65           0 :                 goto end;
      66             :         }
      67             :         // succeeded - get rid of the uncompressed version
      68           9 :         unlink(src);
      69           9 :         ret=0;
      70             : end:
      71           9 :         free_w(&dsttmp);
      72           9 :         return ret;
      73             : }
      74             : 
      75           0 : int compress_filename(const char *d,
      76             :         const char *file, const char *zfile, int compression)
      77             : {
      78           0 :         char *fullfile=NULL;
      79           0 :         char *fullzfile=NULL;
      80           0 :         if(!(fullfile=prepend_s(d, file))
      81           0 :           || !(fullzfile=prepend_s(d, zfile))
      82           0 :           || compress_file(fullfile, fullzfile, compression))
      83             :         {
      84           0 :                 free_w(&fullfile);
      85           0 :                 free_w(&fullzfile);
      86           0 :                 return -1;
      87             :         }
      88             :         return 0;
      89             : }

Generated by: LCOV version 1.10