@@ -223,7 +223,9 @@ func (t *freezerTable) repair() error {
223223 if t .readonly {
224224 return fmt .Errorf ("index file(path: %s, name: %s) size is not a multiple of %d" , t .path , t .name , indexEntrySize )
225225 }
226- truncateFreezerFile (t .index , stat .Size ()- overflow ) // New file can't trigger this path
226+ if err := truncateFreezerFile (t .index , stat .Size ()- overflow ); err != nil {
227+ return err
228+ } // New file can't trigger this path
227229 }
228230 // Retrieve the file sizes and prepare for truncation
229231 if stat , err = t .index .Stat (); err != nil {
@@ -268,8 +270,8 @@ func (t *freezerTable) repair() error {
268270 // Print an error log if the index is corrupted due to an incorrect
269271 // last index item. While it is theoretically possible to have a zero offset
270272 // by storing all zero-size items, it is highly unlikely to occur in practice.
271- if lastIndex .offset == 0 && offsetsSize % indexEntrySize > 1 {
272- log .Error ("Corrupted index file detected" , "lastOffset" , lastIndex .offset , "items " , offsetsSize % indexEntrySize - 1 )
273+ if lastIndex .offset == 0 && offsetsSize / indexEntrySize > 1 {
274+ log .Error ("Corrupted index file detected" , "lastOffset" , lastIndex .offset , "indexes " , offsetsSize / indexEntrySize )
273275 }
274276 if t .readonly {
275277 t .head , err = t .openFile (lastIndex .filenum , openFreezerFileForReadOnly )
@@ -424,6 +426,9 @@ func (t *freezerTable) truncateHead(items uint64) error {
424426 if err := truncateFreezerFile (t .index , int64 (length + 1 )* indexEntrySize ); err != nil {
425427 return err
426428 }
429+ if err := t .index .Sync (); err != nil {
430+ return err
431+ }
427432 // Calculate the new expected size of the data file and truncate it
428433 var expected indexEntry
429434 if length == 0 {
@@ -446,13 +451,17 @@ func (t *freezerTable) truncateHead(items uint64) error {
446451 // Release any files _after the current head -- both the previous head
447452 // and any files which may have been opened for reading
448453 t .releaseFilesAfter (expected .filenum , true )
454+
449455 // Set back the historic head
450456 t .head = newHead
451457 t .headId = expected .filenum
452458 }
453459 if err := truncateFreezerFile (t .head , int64 (expected .offset )); err != nil {
454460 return err
455461 }
462+ if err := t .head .Sync (); err != nil {
463+ return err
464+ }
456465 // All data files truncated, set internal counters and return
457466 t .headBytes = int64 (expected .offset )
458467 t .items .Store (items )
@@ -597,10 +606,12 @@ func (t *freezerTable) Close() error {
597606 // error on Windows.
598607 doClose (t .index , true , true )
599608 doClose (t .meta , true , true )
609+
600610 // The preopened non-head data-files are all opened in readonly.
601611 // The head is opened in rw-mode, so we sync it here - but since it's also
602612 // part of t.files, it will be closed in the loop below.
603613 doClose (t .head , true , false ) // sync but do not close
614+
604615 for _ , f := range t .files {
605616 doClose (f , false , true ) // close but do not sync
606617 }
0 commit comments