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.

76 lines
1.6 KiB

4 years ago
5 years ago
5 years ago
4 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "github.com/golang/protobuf/jsonpb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandFsMetaCat{})
  12. }
  13. type commandFsMetaCat struct {
  14. }
  15. func (c *commandFsMetaCat) Name() string {
  16. return "fs.meta.cat"
  17. }
  18. func (c *commandFsMetaCat) Help() string {
  19. return `print out the meta data content for a file or directory
  20. fs.meta.cat /dir/
  21. fs.meta.cat /dir/file_name
  22. `
  23. }
  24. func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  25. path, err := commandEnv.parseUrl(findInputDirectory(args))
  26. if err != nil {
  27. return err
  28. }
  29. dir, name := util.FullPath(path).DirAndName()
  30. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  31. request := &filer_pb.LookupDirectoryEntryRequest{
  32. Name: name,
  33. Directory: dir,
  34. }
  35. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  36. if err != nil {
  37. return err
  38. }
  39. m := jsonpb.Marshaler{
  40. EmitDefaults: true,
  41. Indent: " ",
  42. }
  43. sort.Slice(respLookupEntry.Entry.Chunks, func(i, j int) bool {
  44. if respLookupEntry.Entry.Chunks[i].Offset == respLookupEntry.Entry.Chunks[j].Offset {
  45. return respLookupEntry.Entry.Chunks[i].Mtime < respLookupEntry.Entry.Chunks[j].Mtime
  46. }
  47. return respLookupEntry.Entry.Chunks[i].Offset < respLookupEntry.Entry.Chunks[j].Offset
  48. })
  49. text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
  50. if marshalErr != nil {
  51. return fmt.Errorf("marshal meta: %v", marshalErr)
  52. }
  53. fmt.Fprintf(writer, "%s\n", text)
  54. return nil
  55. })
  56. }