command_fs_meta_cat.go 1.8 KB

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