@@ -215,7 +215,9 @@ func (t *freezerTable) repair() error {
215215 if t .readonly {
216216 return fmt .Errorf ("index file(path: %s, name: %s) size is not a multiple of %d" , t .path , t .name , indexEntrySize )
217217 }
218- truncateFreezerFile (t .index , stat .Size ()- overflow ) // New file can't trigger this path
218+ if err := truncateFreezerFile (t .index , stat .Size ()- overflow ); err != nil {
219+ return err
220+ } // New file can't trigger this path
219221 }
220222 // Retrieve the file sizes and prepare for truncation
221223 if stat , err = t .index .Stat (); err != nil {
@@ -260,8 +262,8 @@ func (t *freezerTable) repair() error {
260262 // Print an error log if the index is corrupted due to an incorrect
261263 // last index item. While it is theoretically possible to have a zero offset
262264 // by storing all zero-size items, it is highly unlikely to occur in practice.
263- if lastIndex .offset == 0 && offsetsSize % indexEntrySize > 1 {
264- log .Error ("Corrupted index file detected" , "lastOffset" , lastIndex .offset , "items " , offsetsSize % indexEntrySize - 1 )
265+ if lastIndex .offset == 0 && offsetsSize / indexEntrySize > 1 {
266+ log .Error ("Corrupted index file detected" , "lastOffset" , lastIndex .offset , "indexes " , offsetsSize / indexEntrySize )
265267 }
266268 if t .readonly {
267269 t .head , err = t .openFile (lastIndex .filenum , openFreezerFileForReadOnly )
@@ -416,6 +418,9 @@ func (t *freezerTable) truncateHead(items uint64) error {
416418 if err := truncateFreezerFile (t .index , int64 (length + 1 )* indexEntrySize ); err != nil {
417419 return err
418420 }
421+ if err := t .index .Sync (); err != nil {
422+ return err
423+ }
419424 // Calculate the new expected size of the data file and truncate it
420425 var expected indexEntry
421426 if length == 0 {
@@ -438,13 +443,17 @@ func (t *freezerTable) truncateHead(items uint64) error {
438443 // Release any files _after the current head -- both the previous head
439444 // and any files which may have been opened for reading
440445 t .releaseFilesAfter (expected .filenum , true )
446+
441447 // Set back the historic head
442448 t .head = newHead
443449 t .headId = expected .filenum
444450 }
445451 if err := truncateFreezerFile (t .head , int64 (expected .offset )); err != nil {
446452 return err
447453 }
454+ if err := t .head .Sync (); err != nil {
455+ return err
456+ }
448457 // All data files truncated, set internal counters and return
449458 t .headBytes = int64 (expected .offset )
450459 t .items .Store (items )
@@ -589,10 +598,12 @@ func (t *freezerTable) Close() error {
589598 // error on Windows.
590599 doClose (t .index , true , true )
591600 doClose (t .meta , true , true )
601+
592602 // The preopened non-head data-files are all opened in readonly.
593603 // The head is opened in rw-mode, so we sync it here - but since it's also
594604 // part of t.files, it will be closed in the loop below.
595605 doClose (t .head , true , false ) // sync but do not close
606+
596607 for _ , f := range t .files {
597608 doClose (f , false , true ) // close but do not sync
598609 }
0 commit comments