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.

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