LCOV - code coverage report
Current view: top level - src/client/protocol2 - rabin_read.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 42 44 95.5 %
Date: 2017-01-01 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../alloc.h"
       3             : #include "../../attribs.h"
       4             : #include "../../bfile.h"
       5             : #include "../../cntr.h"
       6             : #include "../../log.h"
       7             : #include "../../sbuf.h"
       8             : #include "../extrameta.h"
       9             : #include "rabin_read.h"
      10             : 
      11             : static char *meta_buffer=NULL;
      12             : static size_t meta_buffer_len=0;
      13             : static char *mp=NULL;
      14             : 
      15             : static int rabin_close_file_extrameta(struct sbuf *sb)
      16             : {
      17           4 :         free_w(&meta_buffer);
      18           4 :         meta_buffer_len=0;
      19           4 :         mp=NULL;
      20           4 :         sb->protocol2->bfd.mode=BF_CLOSED;
      21             :         return 0;
      22             : }
      23             : 
      24             : // Return -1 for error, 0 for could not get data, 1 for success.
      25           2 : static int rabin_open_file_extrameta(struct sbuf *sb, struct asfd *asfd,
      26             :         struct cntr *cntr)
      27             : {
      28             :         // Load all of the metadata into a buffer.
      29           2 :         rabin_close_file_extrameta(sb);
      30           4 :         if(get_extrameta(asfd,
      31             : #ifdef HAVE_WIN32
      32             :                 NULL,
      33             : #endif
      34           4 :                 sb->path.buf, S_ISDIR(sb->statp.st_mode),
      35             :                 &meta_buffer, &meta_buffer_len, cntr))
      36             :                         return -1;
      37           2 :         if(!meta_buffer)
      38             :                 return 0;
      39           2 :         mp=meta_buffer;
      40           2 :         sb->protocol2->bfd.mode=BF_READ;
      41           2 :         return 1;
      42             : }
      43             : 
      44          30 : static ssize_t rabin_read_extrameta(char *buf, size_t bufsize)
      45             : {
      46             :         // Place bufsize of the meta buffer contents into buf.
      47          30 :         size_t to_read=meta_buffer_len;
      48          30 :         if(!meta_buffer_len)
      49             :                 return 0;
      50          28 :         if(bufsize<meta_buffer_len)
      51          26 :                 to_read=bufsize;
      52          28 :         memcpy(buf, mp, to_read);
      53          28 :         meta_buffer_len-=to_read;
      54          28 :         mp+=to_read;
      55          28 :         return (ssize_t)to_read;
      56             : }
      57             : 
      58             : // Return -1 for error, 0 for could not open file, 1 for success.
      59          24 : int rabin_open_file(struct sbuf *sb, struct asfd *asfd, struct cntr *cntr,
      60             :         struct conf **confs)
      61             : {
      62          24 :         struct BFILE *bfd=&sb->protocol2->bfd;
      63             : #ifdef HAVE_WIN32
      64             :         if(win32_lstat(sb->path.buf, &sb->statp, &sb->winattr))
      65             : #else
      66          48 :         if(lstat(sb->path.buf, &sb->statp))
      67             : #endif
      68             :         {
      69             :                 // This file is no longer available.
      70           3 :                 logw(asfd, cntr, "%s has vanished\n", sb->path.buf);
      71           3 :                 return 0;
      72             :         }
      73          21 :         sb->compression=get_int(confs[OPT_COMPRESSION]);
      74             :         // Encryption not yet implemented in protocol2.
      75             :         //sb->protocol2->encryption=conf->protocol2->encryption_password?1:0;
      76          21 :         if(attribs_encode(sb)) return -1;
      77          21 :         if(sbuf_is_metadata(sb))
      78           2 :                 return rabin_open_file_extrameta(sb, asfd, cntr);
      79             : 
      80          57 :         if(bfd->open_for_send(bfd, asfd,
      81          38 :                 sb->path.buf, sb->winattr,
      82             :                 get_int(confs[OPT_ATIME]), cntr, PROTO_2))
      83             :         {
      84           0 :                 logw(asfd, get_cntr(confs),
      85             :                         "Could not open %s\n", sb->path.buf);
      86           0 :                 return 0;
      87             :         }
      88             :         return 1;
      89             : }
      90             : 
      91          21 : int rabin_close_file(struct sbuf *sb, struct asfd *asfd)
      92             : {
      93             :         struct BFILE *bfd;
      94          21 :         if(sbuf_is_metadata(sb))
      95           4 :                 return rabin_close_file_extrameta(sb);
      96          19 :         bfd=&sb->protocol2->bfd;
      97          19 :         return bfd->close(bfd, asfd);
      98             : }
      99             : 
     100          68 : ssize_t rabin_read(struct sbuf *sb, char *buf, size_t bufsize)
     101             : {
     102             :         struct BFILE *bfd;
     103          68 :         if(sbuf_is_metadata(sb))
     104          30 :                 return rabin_read_extrameta(buf, bufsize);
     105          38 :         bfd=&sb->protocol2->bfd;
     106          38 :         return (ssize_t)bfd->read(bfd, buf, bufsize);
     107             : }

Generated by: LCOV version 1.10