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

Generated by: LCOV version 1.13