2016-07-03 14:50:58 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2017-01-07 02:22:20 +08:00
|
|
|
func getActualSize(size uint32) int64 {
|
|
|
|
padding := NeedlePaddingSize - ((NeedleHeaderSize + size + NeedleChecksumSize) % NeedlePaddingSize)
|
|
|
|
return NeedleHeaderSize + int64(size) + NeedleChecksumSize + int64(padding)
|
|
|
|
}
|
|
|
|
|
2018-05-28 02:52:26 +08:00
|
|
|
func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) error {
|
2016-07-03 14:50:58 +08:00
|
|
|
var indexSize int64
|
|
|
|
var e error
|
|
|
|
if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
|
2017-01-20 23:37:45 +08:00
|
|
|
return fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
|
2016-07-03 14:50:58 +08:00
|
|
|
}
|
|
|
|
if indexSize == 0 {
|
2017-01-20 23:37:45 +08:00
|
|
|
return nil
|
2016-07-03 14:50:58 +08:00
|
|
|
}
|
|
|
|
var lastIdxEntry []byte
|
|
|
|
if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleIndexSize); e != nil {
|
2017-01-20 23:37:45 +08:00
|
|
|
return fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
|
2016-07-03 14:50:58 +08:00
|
|
|
}
|
|
|
|
key, offset, size := idxFileEntry(lastIdxEntry)
|
2017-01-07 02:22:20 +08:00
|
|
|
if offset == 0 || size == TombstoneFileSize {
|
2017-01-20 23:37:45 +08:00
|
|
|
return nil
|
2016-07-25 14:54:40 +08:00
|
|
|
}
|
2016-07-03 14:50:58 +08:00
|
|
|
if e = verifyNeedleIntegrity(v.dataFile, v.Version(), int64(offset)*NeedlePaddingSize, key, size); e != nil {
|
2017-01-20 23:37:45 +08:00
|
|
|
return fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
|
2016-07-03 14:50:58 +08:00
|
|
|
}
|
2017-01-07 02:22:20 +08:00
|
|
|
|
2017-01-20 23:37:45 +08:00
|
|
|
return nil
|
2016-07-03 14:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
|
|
|
|
if indexSize, err = util.GetFileSize(indexFile); err == nil {
|
|
|
|
if indexSize%NeedleIndexSize != 0 {
|
|
|
|
err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
|
|
|
|
if offset < 0 {
|
|
|
|
err = fmt.Errorf("offset %d for index file is invalid", offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bytes = make([]byte, NeedleIndexSize)
|
|
|
|
_, err = indexFile.ReadAt(bytes, offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyNeedleIntegrity(datFile *os.File, v Version, offset int64, key uint64, size uint32) error {
|
|
|
|
n := new(Needle)
|
|
|
|
err := n.ReadData(datFile, offset, size, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n.Id != key {
|
|
|
|
return fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|