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.

117 lines
2.4 KiB

6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/golang/protobuf/proto"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandFsMetaLoad{})
  15. }
  16. type commandFsMetaLoad struct {
  17. }
  18. func (c *commandFsMetaLoad) Name() string {
  19. return "fs.meta.load"
  20. }
  21. func (c *commandFsMetaLoad) Help() string {
  22. return `load saved filer meta data to restore the directory and file structure
  23. fs.meta.load <filer_host>-<port>-<time>.meta
  24. fs.meta.load -v=false <filer_host>-<port>-<time>.meta // skip printing out the verbose output
  25. `
  26. }
  27. func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  28. if len(args) == 0 {
  29. fmt.Fprintf(writer, "missing a metadata file\n")
  30. return nil
  31. }
  32. fileName := args[len(args)-1]
  33. metaLoadCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  34. verbose := metaLoadCommand.Bool("v", true, "verbose mode")
  35. if err = metaLoadCommand.Parse(args[0 : len(args)-1]); err != nil {
  36. return nil
  37. }
  38. dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
  39. if err != nil {
  40. return nil
  41. }
  42. defer dst.Close()
  43. var dirCount, fileCount uint64
  44. lastLogTime := time.Now()
  45. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  46. sizeBuf := make([]byte, 4)
  47. for {
  48. if n, err := dst.Read(sizeBuf); n != 4 {
  49. if err == io.EOF {
  50. return nil
  51. }
  52. return err
  53. }
  54. size := util.BytesToUint32(sizeBuf)
  55. data := make([]byte, int(size))
  56. if n, err := dst.Read(data); n != len(data) {
  57. return err
  58. }
  59. fullEntry := &filer_pb.FullEntry{}
  60. if err = proto.Unmarshal(data, fullEntry); err != nil {
  61. return err
  62. }
  63. fullEntry.Entry.Name = strings.ReplaceAll(fullEntry.Entry.Name, "/", "x")
  64. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  65. Directory: fullEntry.Dir,
  66. Entry: fullEntry.Entry,
  67. }); err != nil {
  68. return err
  69. }
  70. if *verbose || lastLogTime.Add(time.Second).Before(time.Now()) {
  71. if !*verbose {
  72. lastLogTime = time.Now()
  73. }
  74. fmt.Fprintf(writer, "load %s\n", util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
  75. }
  76. if fullEntry.Entry.IsDirectory {
  77. dirCount++
  78. } else {
  79. fileCount++
  80. }
  81. }
  82. })
  83. if err == nil {
  84. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  85. fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
  86. }
  87. return err
  88. }