You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

9 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. )
  7. func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) error {
  8. var indexSize int64
  9. var e error
  10. if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
  11. return fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
  12. }
  13. if indexSize == 0 {
  14. return nil
  15. }
  16. var lastIdxEntry []byte
  17. if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleIndexSize); e != nil {
  18. return fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
  19. }
  20. key, offset, size := idxFileEntry(lastIdxEntry)
  21. if e = verifyNeedleIntegrity(v.dataFile, v.Version(), int64(offset)*NeedlePaddingSize, key, size); e != nil {
  22. return fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
  23. }
  24. return nil
  25. }
  26. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  27. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  28. if indexSize%NeedleIndexSize != 0 {
  29. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  30. }
  31. }
  32. return
  33. }
  34. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  35. if offset < 0 {
  36. err = fmt.Errorf("offset %d for index file is invalid", offset)
  37. return
  38. }
  39. bytes = make([]byte, NeedleIndexSize)
  40. _, err = indexFile.ReadAt(bytes, offset)
  41. return
  42. }
  43. func verifyNeedleIntegrity(datFile *os.File, v Version, offset int64, key uint64, size uint32) error {
  44. n := new(Needle)
  45. err := n.ReadData(datFile, offset, size, v)
  46. if err != nil {
  47. return err
  48. }
  49. if n.Id != key {
  50. return fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
  51. }
  52. return nil
  53. }