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.

69 lines
2.0 KiB

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