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

  1. package main
  2. import (
  3. "os"
  4. "path"
  5. "strconv"
  6. "github.com/chrislusf/weed-fs/go/glog"
  7. "github.com/chrislusf/weed-fs/go/storage"
  8. )
  9. func init() {
  10. cmdFix.Run = runFix // break init cycle
  11. }
  12. var cmdFix = &Command{
  13. UsageLine: "fix -dir=/tmp -volumeId=234",
  14. Short: "run weed tool fix on index file if corrupted",
  15. Long: `Fix runs the SeeweedFS fix command to re-create the index .idx file.
  16. `,
  17. }
  18. var (
  19. fixVolumePath = cmdFix.Flag.String("dir", "/tmp", "data directory to store files")
  20. fixVolumeCollection = cmdFix.Flag.String("collection", "", "the volume collection name")
  21. fixVolumeId = cmdFix.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  22. )
  23. func runFix(cmd *Command, args []string) bool {
  24. if *fixVolumeId == -1 {
  25. return false
  26. }
  27. fileName := strconv.Itoa(*fixVolumeId)
  28. if *fixVolumeCollection != "" {
  29. fileName = *fixVolumeCollection + "_" + fileName
  30. }
  31. indexFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".idx"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  32. if err != nil {
  33. glog.Fatalf("Create Volume Index [ERROR] %s\n", err)
  34. }
  35. defer indexFile.Close()
  36. nm := storage.NewNeedleMap(indexFile)
  37. defer nm.Close()
  38. vid := storage.VolumeId(*fixVolumeId)
  39. err = storage.ScanVolumeFile(*fixVolumePath, *fixVolumeCollection, vid, func(superBlock storage.SuperBlock) error {
  40. return nil
  41. }, false, func(n *storage.Needle, offset int64) error {
  42. glog.V(2).Infof("key %d offset %d size %d disk_size %d gzip %v", n.Id, offset, n.Size, n.DiskSize(), n.IsGzipped())
  43. if n.Size > 0 {
  44. count, pe := nm.Put(n.Id, uint32(offset/storage.NeedlePaddingSize), n.Size)
  45. glog.V(2).Infof("saved %d with error %v", count, pe)
  46. } else {
  47. glog.V(2).Infof("skipping deleted file ...")
  48. return nm.Delete(n.Id)
  49. }
  50. return nil
  51. })
  52. if err != nil {
  53. glog.Fatalf("Export Volume File [ERROR] %s\n", err)
  54. }
  55. return true
  56. }