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.

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