@@ -119,9 +119,9 @@ int MockBPA_GetBlockMetadata(const BSL_BundleRef_t *bundle_ref, uint64_t block_n
119119 return 0 ;
120120}
121121
122- int MockBPA_ReallocBTSD (BSL_BundleRef_t * bundle_ref , uint64_t block_num , size_t bytesize )
122+ int MockBPA_ReallocBTSD (BSL_BundleRef_t * bundle_ref , uint64_t block_num , size_t btsd_size )
123123{
124- if (!bundle_ref || !bundle_ref -> data || block_num == 0 || bytesize == 0 )
124+ if (!bundle_ref || !bundle_ref -> data || block_num == 0 || btsd_size == 0 )
125125 {
126126 return -1 ;
127127 }
@@ -137,13 +137,13 @@ int MockBPA_ReallocBTSD(BSL_BundleRef_t *bundle_ref, uint64_t block_num, size_t
137137
138138 if (found_block -> btsd == NULL )
139139 {
140- found_block -> btsd = BSL_calloc (1 , bytesize );
141- found_block -> btsd_len = bytesize ;
140+ found_block -> btsd = BSL_calloc (1 , btsd_size );
141+ found_block -> btsd_len = btsd_size ;
142142 }
143143 else
144144 {
145- found_block -> btsd = BSL_realloc (found_block -> btsd , bytesize );
146- found_block -> btsd_len = bytesize ;
145+ found_block -> btsd = BSL_realloc (found_block -> btsd , btsd_size );
146+ found_block -> btsd_len = btsd_size ;
147147 }
148148
149149 // Return -9 if malloc/realloc faile. Return 0 for success.
@@ -162,6 +162,8 @@ struct MockBPA_BTSD_Data_s
162162 size_t size ;
163163 /// File opened for the buffer
164164 FILE * file ;
165+ /// Local dead-reckoned cursor into #file
166+ size_t curs ;
165167};
166168
167169static int MockBPA_ReadBTSD_Read (void * user_data , void * buf , size_t * bufsize )
@@ -173,6 +175,7 @@ static int MockBPA_ReadBTSD_Read(void *user_data, void *buf, size_t *bufsize)
173175 ASSERT_PRECONDITION (obj -> file );
174176
175177 const size_t got = fread (buf , 1 , * bufsize , obj -> file );
178+ obj -> curs += got ;
176179 BSL_LOG_DEBUG ("reading up to %zd bytes, got %zd" , * bufsize , got );
177180 * bufsize = got ;
178181 return 0 ;
@@ -208,6 +211,7 @@ static struct BSL_SeqReader_s *MockBPA_ReadBTSD(const BSL_BundleRef_t *bundle_re
208211 obj -> ptr = found_block -> btsd ;
209212 obj -> size = found_block -> btsd_len ;
210213 obj -> file = fmemopen (obj -> ptr , obj -> size , "rb" );
214+ obj -> curs = 0 ;
211215
212216 BSL_SeqReader_t * reader = BSL_calloc (1 , sizeof (BSL_SeqReader_t ));
213217 if (!reader )
@@ -229,7 +233,15 @@ static int MockBPA_WriteBTSD_Write(void *user_data, const void *buf, size_t size
229233 CHK_ARG_NONNULL (buf );
230234 ASSERT_PRECONDITION (obj -> file );
231235
236+ const size_t excess = (obj -> curs + size ) - obj -> size ;
237+ if (excess > 0 )
238+ {
239+ BSL_LOG_ERR ("write too large for buffer of %zu by %zu bytes" , obj -> size , excess );
240+ return BSL_ERR_FAILURE ;
241+ }
242+
232243 const size_t got = fwrite (buf , 1 , size , obj -> file );
244+ obj -> curs += got ;
233245 BSL_LOG_DEBUG ("writing up to %zd bytes, got %zd" , size , got );
234246 if (got < size )
235247 {
@@ -245,7 +257,11 @@ static void MockBPA_WriteBTSD_Deinit(void *user_data)
245257 ASSERT_PRECONDITION (obj -> file );
246258
247259 fclose (obj -> file );
248- BSL_LOG_DEBUG ("closed block %p with size %zu" , obj -> block , obj -> size );
260+ BSL_LOG_DEBUG ("closed block %p with size %zu and cursor %zu" , obj -> block , obj -> size , obj -> curs );
261+ if (obj -> curs < obj -> size )
262+ {
263+ BSL_LOG_ERR ("closed block %p for writing with only %zu of %zu written" , obj -> block , obj -> curs , obj -> size );
264+ }
249265
250266 // now write-back the BTSD
251267 BSL_free (obj -> block -> btsd );
@@ -255,7 +271,7 @@ static void MockBPA_WriteBTSD_Deinit(void *user_data)
255271 BSL_free (obj );
256272}
257273
258- static struct BSL_SeqWriter_s * MockBPA_WriteBTSD (BSL_BundleRef_t * bundle_ref , uint64_t block_num , size_t total_size )
274+ static struct BSL_SeqWriter_s * MockBPA_WriteBTSD (BSL_BundleRef_t * bundle_ref , uint64_t block_num , size_t btsd_size )
259275{
260276 MockBPA_Bundle_t * bundle = bundle_ref -> data ;
261277 MockBPA_CanonicalBlock_t * * found_ptr = MockBPA_BlockByNum_get (bundle -> blocks_num , block_num );
@@ -264,7 +280,7 @@ static struct BSL_SeqWriter_s *MockBPA_WriteBTSD(BSL_BundleRef_t *bundle_ref, ui
264280 return NULL ;
265281 }
266282 MockBPA_CanonicalBlock_t * found_block = * found_ptr ;
267- BSL_LOG_DEBUG ("opened block %p for size %zu" , found_block , total_size );
283+ BSL_LOG_DEBUG ("opened block %p for size %zu, previous size %zu " , found_block , btsd_size , found_block -> btsd_len );
268284
269285 struct MockBPA_BTSD_Data_s * obj = BSL_calloc (1 , sizeof (struct MockBPA_BTSD_Data_s ));
270286 if (!obj )
@@ -276,6 +292,15 @@ static struct BSL_SeqWriter_s *MockBPA_WriteBTSD(BSL_BundleRef_t *bundle_ref, ui
276292 obj -> ptr = NULL ;
277293 obj -> size = 0 ;
278294 obj -> file = open_memstream (& obj -> ptr , & obj -> size );
295+ obj -> curs = 0 ;
296+
297+ if (btsd_size )
298+ {
299+ // pre-allocate BTSD size
300+ fseeko (obj -> file , btsd_size , SEEK_SET );
301+ fflush (obj -> file );
302+ fseeko (obj -> file , 0UL , SEEK_SET );
303+ }
279304
280305 BSL_SeqWriter_t * writer = BSL_calloc (1 , sizeof (BSL_SeqWriter_t ));
281306 if (!writer )
0 commit comments