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.

68 lines
1.9 KiB

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