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: 43 45 95.6 %
Date: 2019-04-28 12:12:12 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          25 : int rabin_open_file(struct sbuf *sb, struct asfd *asfd, struct cntr *cntr,
      60             :         struct conf **confs)
      61             : {
      62          25 :         struct BFILE *bfd=&sb->protocol2->bfd;
      63             : #ifdef HAVE_WIN32
      64             :         if(win32_lstat(sb->path.buf, &sb->statp, &sb->winattr))
      65             : #else
      66          50 :         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",
      71             :                         iobuf_to_printable(&sb->path));
      72           3 :                 return 0;
      73             :         }
      74          22 :         sb->compression=get_int(confs[OPT_COMPRESSION]);
      75             :         // Encryption not yet implemented in protocol2.
      76             :         //sb->encryption=conf->protocol2->encryption_password?1:0;
      77          22 :         sb->encryption=ENCRYPTION_NONE;
      78          22 :         if(attribs_encode(sb)) return -1;
      79          22 :         if(sbuf_is_metadata(sb))
      80           2 :                 return rabin_open_file_extrameta(sb, asfd, cntr);
      81             : 
      82          60 :         if(bfd->open_for_send(bfd, asfd,
      83          40 :                 sb->path.buf, sb->winattr,
      84             :                 get_int(confs[OPT_ATIME]), cntr, PROTO_2))
      85             :         {
      86           0 :                 logw(asfd, get_cntr(confs),
      87             :                         "Could not open %s\n",
      88             :                         iobuf_to_printable(&sb->path));
      89           0 :                 return 0;
      90             :         }
      91             :         return 1;
      92             : }
      93             : 
      94          22 : int rabin_close_file(struct sbuf *sb, struct asfd *asfd)
      95             : {
      96             :         struct BFILE *bfd;
      97          22 :         if(sbuf_is_metadata(sb))
      98           4 :                 return rabin_close_file_extrameta(sb);
      99          20 :         bfd=&sb->protocol2->bfd;
     100          20 :         return bfd->close(bfd, asfd);
     101             : }
     102             : 
     103          69 : ssize_t rabin_read(struct sbuf *sb, char *buf, size_t bufsize)
     104             : {
     105             :         struct BFILE *bfd;
     106          69 :         if(sbuf_is_metadata(sb))
     107          30 :                 return rabin_read_extrameta(buf, bufsize);
     108          39 :         bfd=&sb->protocol2->bfd;
     109          39 :         return (ssize_t)bfd->read(bfd, buf, bufsize);
     110             : }

Generated by: LCOV version 1.13