command_fs_meta_cat.go 1.9 KB

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