command_fs_meta_load.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. fileName := args[len(args)-1]
  29. dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
  30. if err != nil {
  31. return nil
  32. }
  33. defer dst.Close()
  34. var dirCount, fileCount uint64
  35. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. sizeBuf := make([]byte, 4)
  37. for {
  38. if n, err := dst.Read(sizeBuf); n != 4 {
  39. if err == io.EOF {
  40. return nil
  41. }
  42. return err
  43. }
  44. size := util.BytesToUint32(sizeBuf)
  45. data := make([]byte, int(size))
  46. if n, err := dst.Read(data); n != len(data) {
  47. return err
  48. }
  49. fullEntry := &filer_pb.FullEntry{}
  50. if err = proto.Unmarshal(data, fullEntry); err != nil {
  51. return err
  52. }
  53. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  54. Directory: fullEntry.Dir,
  55. Entry: fullEntry.Entry,
  56. }); err != nil {
  57. return err
  58. }
  59. fmt.Fprintf(writer, "load %s\n", util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
  60. if fullEntry.Entry.IsDirectory {
  61. dirCount++
  62. } else {
  63. fileCount++
  64. }
  65. }
  66. })
  67. if err == nil {
  68. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  69. fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
  70. }
  71. return err
  72. }