2016-07-03 06:50:58 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2016-07-03 06:50:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2018-07-24 08:36:04 +00:00
|
|
|
func getActualSize(size uint32, version Version) int64 {
|
|
|
|
return NeedleEntrySize + NeedleBodyLength(size, version)
|
2017-01-06 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 18:52:26 +00:00
|
|
|
func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) error {
|
2016-07-03 06:50:58 +00:00
|
|
|
var indexSize int64
|
|
|
|
var e error
|
|
|
|
if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
|
2017-01-20 15:37:45 +00:00
|
|
|
return fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
if indexSize == 0 {
|
2017-01-20 15:37:45 +00:00
|
|
|
return nil
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
var lastIdxEntry []byte
|
2018-07-08 09:28:04 +00:00
|
|
|
if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleEntrySize); e != nil {
|
2017-01-20 15:37:45 +00:00
|
|
|
return fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
key, offset, size := IdxFileEntry(lastIdxEntry)
|
2017-01-06 18:22:20 +00:00
|
|
|
if offset == 0 || size == TombstoneFileSize {
|
2017-01-20 15:37:45 +00:00
|
|
|
return nil
|
2016-07-25 06:54:40 +00:00
|
|
|
}
|
2016-07-03 06:50:58 +00:00
|
|
|
if e = verifyNeedleIntegrity(v.dataFile, v.Version(), int64(offset)*NeedlePaddingSize, key, size); e != nil {
|
2017-01-20 15:37:45 +00:00
|
|
|
return fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
2017-01-06 18:22:20 +00:00
|
|
|
|
2017-01-20 15:37:45 +00:00
|
|
|
return nil
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
|
|
|
|
if indexSize, err = util.GetFileSize(indexFile); err == nil {
|
2018-07-08 09:28:04 +00:00
|
|
|
if indexSize%NeedleEntrySize != 0 {
|
2016-07-03 06:50:58 +00:00
|
|
|
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
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
bytes = make([]byte, NeedleEntrySize)
|
2016-07-03 06:50:58 +00:00
|
|
|
_, err = indexFile.ReadAt(bytes, offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
func verifyNeedleIntegrity(datFile *os.File, v Version, offset int64, key NeedleId, size uint32) error {
|
2016-07-03 06:50:58 +00:00
|
|
|
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
|
|
|
|
}
|