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.

62 lines
1.7 KiB

9 years ago
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 offset == 0 {
  22. return nil
  23. }
  24. if e = verifyNeedleIntegrity(v.dataFile, v.Version(), int64(offset)*NeedlePaddingSize, key, size); e != nil {
  25. return fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
  26. }
  27. return nil
  28. }
  29. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  30. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  31. if indexSize%NeedleIndexSize != 0 {
  32. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  33. }
  34. }
  35. return
  36. }
  37. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  38. if offset < 0 {
  39. err = fmt.Errorf("offset %d for index file is invalid", offset)
  40. return
  41. }
  42. bytes = make([]byte, NeedleIndexSize)
  43. _, err = indexFile.ReadAt(bytes, offset)
  44. return
  45. }
  46. func verifyNeedleIntegrity(datFile *os.File, v Version, offset int64, key uint64, size uint32) error {
  47. n := new(Needle)
  48. err := n.ReadData(datFile, offset, size, v)
  49. if err != nil {
  50. return err
  51. }
  52. if n.Id != key {
  53. return fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
  54. }
  55. return nil
  56. }