command_fs_meta_load.go 2.0 KB

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