1212#include "je.h"
1313#include "log.h"
1414
15- /// @def INDEXMAXFP
16- /// @brief The maximum filepath length of a file in the index.
17- #define INDEXMAXFP 512
15+ static char indexfpbuf [1024 ];///< Filepath buffer for index I/O operations
1816
1917/// @def indexbucket
2018/// @brief Maps and casts the hash value to a bucket index via the last N bits
@@ -54,20 +52,21 @@ static int indexnodecmp(const void* a, const void* b) {
5452 return strcmp (na -> fp , nb -> fp );
5553}
5654
55+ /// @def INDEXWRITEFMT
56+ /// @brief Format string used for writing index entries to a file stream.
57+ #define INDEXWRITEFMT "%s,%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n"
58+
5759int indexwrite (struct index_s * idx , FILE * s ) {
5860 struct inode_s * * fl ;
5961 if ((fl = indexlist (idx )) == NULL ) return -1 ;
6062 qsort (fl , idx -> size , sizeof (struct inode_s * ), indexnodecmp );
6163
62- char lbuf [INDEXMAXFP ]; /* line output format buffer */
63-
6464 int err = 0 ;
6565 for (long i = 0 ; i < idx -> size ; i ++ ) {
6666 struct inode_s * node = fl [i ];
67- const int n = snprintf (lbuf , sizeof (lbuf ),
68- "%s,%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n" , node -> fp ,
69- node -> st .lmod , node -> st .fsze , node -> xx );
70- if (fwrite (lbuf , n , 1 , s ) != 1 ) {
67+ const int n = snprintf (indexfpbuf , sizeof (indexfpbuf ), INDEXWRITEFMT ,
68+ node -> fp , node -> st .lmod , node -> st .fsze , node -> xx );
69+ if (fwrite (indexfpbuf , n , 1 , s ) != 1 ) {
7170 err = -1 ;
7271 break ;
7372 }
@@ -76,14 +75,16 @@ int indexwrite(struct index_s* idx, FILE* s) {
7675 return err ;
7776}
7877
78+ /// @def INDEXREADFMT
79+ /// @brief Format string used for reading index entries from a file stream.
80+ #define INDEXREADFMT "%[^,],%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n"
81+
7982int indexread (struct index_s * idx , FILE * s ) {
80- char fp [INDEXMAXFP ] = {0 }; /* fscanf filepath string buffer */
81- struct fsstat_s st = {0 }; /* fscanf file stat structure */
82- uint64_t xx = 0 ; /* fscanf xxHash64 value */
83- while (fscanf (s , "%[^,],%" PRIu64 ",%" PRIu64 ",%" PRIu64 "\n" , fp , & st .lmod ,
84- & st .fsze , & xx ) == 4 ) {
85- const uint64_t fphash = indexhash (fp );
86- if (indexput (idx , fp , fphash , & st , xx ) == NULL ) return -1 ;
83+ struct fsstat_s st = {0 }; /* fscanf file stat structure */
84+ uint64_t xx = 0 ; /* fscanf xxHash64 value */
85+ while (fscanf (s , INDEXREADFMT , indexfpbuf , & st .lmod , & st .fsze , & xx ) == 4 ) {
86+ const uint64_t fphash = indexhash (indexfpbuf );
87+ if (indexput (idx , indexfpbuf , fphash , & st , xx ) == NULL ) return -1 ;
8788 }
8889 return 0 ;
8990}
0 commit comments