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. "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. fixVolumeCollection = cmdFix.Flag.String("collection", "", "the volume collection name")
  22. fixVolumeId = cmdFix.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  23. )
  24. func runFix(cmd *Command, args []string) bool {
  25. if *fixVolumeId == -1 {
  26. return false
  27. }
  28. fileName := strconv.Itoa(*fixVolumeId)
  29. if *fixVolumeCollection != "" {
  30. fileName = *fixVolumeCollection + "_" + fileName
  31. }
  32. indexFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".idx"), os.O_WRONLY|os.O_CREATE, 0644)
  33. if err != nil {
  34. glog.Fatalf("Create Volume Index [ERROR] %s\n", err)
  35. }
  36. defer indexFile.Close()
  37. nm := storage.NewNeedleMap(indexFile)
  38. defer nm.Close()
  39. vid := storage.VolumeId(*fixVolumeId)
  40. err = storage.ScanVolumeFile(*fixVolumePath, *fixVolumeCollection, vid, func(superBlock storage.SuperBlock) error {
  41. return nil
  42. }, func(n *storage.Needle, offset int64) error {
  43. debug("key", n.Id, "offset", offset, "size", n.Size, "disk_size", n.DiskSize(), "gzip", n.IsGzipped())
  44. if n.Size > 0 {
  45. count, pe := nm.Put(n.Id, uint32(offset/storage.NeedlePaddingSize), n.Size)
  46. debug("saved", count, "with error", pe)
  47. } else {
  48. debug("skipping deleted file ...")
  49. return nm.Delete(n.Id)
  50. }
  51. return nil
  52. })
  53. if err != nil {
  54. glog.Fatalf("Export Volume File [ERROR] %s\n", err)
  55. }
  56. return true
  57. }