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.

106 lines
2.1 KiB

6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(nil))
  30. if err != nil {
  31. return err
  32. }
  33. fileName := args[len(args)-1]
  34. dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
  35. if err != nil {
  36. return nil
  37. }
  38. defer dst.Close()
  39. var dirCount, fileCount uint64
  40. err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  41. sizeBuf := make([]byte, 4)
  42. for {
  43. if n, err := dst.Read(sizeBuf); n != 4 {
  44. if err == io.EOF {
  45. return nil
  46. }
  47. return err
  48. }
  49. size := util.BytesToUint32(sizeBuf)
  50. data := make([]byte, int(size))
  51. if n, err := dst.Read(data); n != len(data) {
  52. return err
  53. }
  54. fullEntry := &filer_pb.FullEntry{}
  55. if err = proto.Unmarshal(data, fullEntry); err != nil {
  56. return err
  57. }
  58. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  59. Directory: fullEntry.Dir,
  60. Entry: fullEntry.Entry,
  61. }); err != nil {
  62. return err
  63. }
  64. fmt.Fprintf(writer, "load %s\n", filer2.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
  65. if fullEntry.Entry.IsDirectory {
  66. dirCount++
  67. } else {
  68. fileCount++
  69. }
  70. }
  71. })
  72. if err == nil {
  73. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  74. fmt.Fprintf(writer, "\n%s is loaded to http://%s:%d%s\n", fileName, filerServer, filerPort, path)
  75. }
  76. return err
  77. }