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.

64 lines
1.7 KiB

  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "os"
  6. "path"
  7. "strconv"
  8. )
  9. func init() {
  10. cmdFix.Run = runFix // break init cycle
  11. cmdFix.IsDebug = cmdFix.Flag.Bool("debug", false, "enable debug mode")
  12. }
  13. var cmdFix = &Command{
  14. UsageLine: "fix -dir=/tmp -volumeId=234",
  15. Short: "run weed tool fix on index file if corrupted",
  16. Long: `Fix runs the WeedFS fix command to re-create the index .idx file.
  17. `,
  18. }
  19. var (
  20. fixVolumePath = cmdFix.Flag.String("dir", "/tmp", "data directory to store files")
  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. indexFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".idx"), os.O_WRONLY|os.O_CREATE, 0644)
  29. if err != nil {
  30. glog.Fatalf("Create Volume Index [ERROR] %s\n", err)
  31. }
  32. defer indexFile.Close()
  33. nm := storage.NewNeedleMap(indexFile)
  34. defer nm.Close()
  35. vid := storage.VolumeId(*fixVolumeId)
  36. err = storage.ScanVolumeFile(*fixVolumePath, vid, func(superBlock storage.SuperBlock) error {
  37. return nil
  38. }, func(n *storage.Needle, offset int64) error {
  39. debug("key", n.Id, "offset", offset, "size", n.Size, "disk_size", n.DiskSize(), "gzip", n.IsGzipped())
  40. if n.Size > 0 {
  41. count, pe := nm.Put(n.Id, uint32(offset/storage.NeedlePaddingSize), n.Size)
  42. debug("saved", count, "with error", pe)
  43. } else {
  44. debug("skipping deleted file ...")
  45. return nm.Delete(n.Id)
  46. }
  47. return nil
  48. })
  49. if err != nil {
  50. glog.Fatalf("Export Volume File [ERROR] %s\n", err)
  51. }
  52. return true
  53. }