LCOV - code coverage report
Current view: top level - flamenco/snapshot - fd_snapshot_base.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 56 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 2 0.0 %

          Line data    Source code
       1             : #include "fd_snapshot_base.h"
       2             : 
       3             : #include <stdlib.h>
       4             : 
       5             : fd_snapshot_name_t *
       6             : fd_snapshot_name_from_buf( fd_snapshot_name_t * id,
       7             :                            char const *         str,
       8             :                            ulong                str_len,
       9           0 :                            ulong                base_slot ) {
      10           0 :   char buf[ 4096 ];
      11           0 :   str_len = fd_ulong_min( sizeof(buf)-1, str_len );
      12           0 :   fd_memcpy( buf, str, str_len );
      13           0 :   buf[ str_len ] = '\0';
      14             : 
      15           0 :   return fd_snapshot_name_from_cstr( id, buf, base_slot );
      16           0 : }
      17             : 
      18             : fd_snapshot_name_t *
      19             : fd_snapshot_name_from_cstr( fd_snapshot_name_t * id,
      20             :                             char const *         cstr,
      21           0 :                             ulong                base_slot ) {
      22             : 
      23           0 :   fd_memset( id, 0, sizeof(fd_snapshot_name_t) );
      24             : 
      25           0 :   const char * orig_cstr = cstr;
      26             : 
      27           0 :   char * last_slash = strrchr( cstr, '/' );
      28           0 :   if( last_slash && last_slash[0]=='/' ) cstr = last_slash + 1;
      29             : 
      30           0 :   if( 0==strncmp( cstr, "snapshot-", sizeof("snapshot-")-1 ) ) {
      31           0 :     cstr += sizeof("snapshot-")-1;
      32           0 :     id->type = FD_SNAPSHOT_TYPE_FULL;
      33           0 :   } else if( 0==strncmp( cstr, "incremental-snapshot-", sizeof("incremental-snapshot-")-1 ) ) {
      34           0 :     cstr += sizeof("incremental-snapshot-")-1;
      35           0 :     id->type = FD_SNAPSHOT_TYPE_INCREMENTAL;
      36           0 :   } else {
      37           0 :     FD_LOG_WARNING(( "unrecognized snapshot type: \"%s\"", orig_cstr ));
      38           0 :     return NULL;
      39           0 :   }
      40             : 
      41           0 :   char const * endptr = NULL;
      42           0 :   id->slot = strtoul( cstr, fd_type_pun( &endptr ), 10 );
      43           0 :   if( !endptr || endptr[0]!='-' ) {
      44           0 :     FD_LOG_WARNING(( "invalid snapshot file name: \"%s\"", orig_cstr ));
      45           0 :     return NULL;
      46           0 :   }
      47           0 :   cstr = endptr+1;
      48             : 
      49           0 :   if( id->type == FD_SNAPSHOT_TYPE_INCREMENTAL ) {
      50           0 :     id->incremental_slot = strtoul( cstr, fd_type_pun( &endptr ), 10 );
      51           0 :     if( !endptr || endptr[0]!='-' ) {
      52           0 :       FD_LOG_WARNING(( "invalid snapshot file name: \"%s\"", orig_cstr ));
      53           0 :       return NULL;
      54           0 :     }
      55           0 :     cstr = endptr+1;
      56             : 
      57           0 :     if( base_slot != id->slot ) {
      58           0 :       FD_LOG_WARNING(( "failed to load snapshot: \"%s\"", orig_cstr ));
      59           0 :       FD_LOG_WARNING(( "expected base slot %lu but got %lu, incremental snapshot does not match full snapshot", base_slot, id->slot ));
      60           0 :       return NULL;
      61           0 :     }
      62           0 :   }
      63             : 
      64           0 :   char const * file_ext = strchr( cstr, '.' );
      65           0 :   ulong        file_ext_off = (ulong)( file_ext - cstr );
      66             : 
      67           0 :   char hash_cstr[ FD_BASE58_ENCODED_32_SZ ] = {0};
      68           0 :   strncpy( hash_cstr, cstr, sizeof(hash_cstr)-1 );
      69           0 :   if( file_ext_off < sizeof(hash_cstr) ) {
      70           0 :     hash_cstr[ file_ext_off ] = '\0';
      71           0 :   }
      72           0 :   strncpy( id->file_ext, file_ext, sizeof(id->file_ext)-1 );
      73             : 
      74           0 :   if( FD_UNLIKELY( !fd_base58_decode_32( hash_cstr, id->fhash.hash ) ) ) {
      75           0 :     FD_LOG_WARNING(( "invalid snapshot file name: \"%s\"", orig_cstr ));
      76           0 :     return NULL;
      77           0 :   }
      78           0 :   return id;
      79           0 : }

Generated by: LCOV version 1.14