LCOV - code coverage report
Current view: top level - flamenco/snapshot - fd_snapshot_base.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 18 64 28.1 %
Date: 2025-03-20 12:08:36 Functions: 2 3 66.7 %

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

Generated by: LCOV version 1.14