command_fs_meta_cat.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package shell
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/filer"
  5. "google.golang.org/protobuf/proto"
  6. "io"
  7. "sort"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/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) HasTag(CommandTag) bool {
  26. return false
  27. }
  28. func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. path, err := commandEnv.parseUrl(findInputDirectory(args))
  30. if err != nil {
  31. return err
  32. }
  33. dir, name := util.FullPath(path).DirAndName()
  34. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  35. request := &filer_pb.LookupDirectoryEntryRequest{
  36. Name: name,
  37. Directory: dir,
  38. }
  39. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  40. if err != nil {
  41. return err
  42. }
  43. chunks := respLookupEntry.Entry.Chunks
  44. sort.Slice(chunks, func(i, j int) bool {
  45. return chunks[i].Offset < chunks[j].Offset
  46. })
  47. filer.ProtoToText(writer, respLookupEntry.Entry)
  48. bytes, _ := proto.Marshal(respLookupEntry.Entry)
  49. gzippedBytes, _ := util.GzipData(bytes)
  50. // zstdBytes, _ := util.ZstdData(bytes)
  51. // fmt.Fprintf(writer, "chunks %d meta size: %d gzip:%d zstd:%d\n", len(respLookupEntry.Entry.GetChunks()), len(bytes), len(gzippedBytes), len(zstdBytes))
  52. fmt.Fprintf(writer, "chunks %d meta size: %d gzip:%d\n", len(respLookupEntry.Entry.GetChunks()), len(bytes), len(gzippedBytes))
  53. return nil
  54. })
  55. }