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.

102 lines
2.0 KiB

6 years ago
6 years ago
5 years ago
6 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strings"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/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. }