command_fs_meta_cat.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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) 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(false, 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. chunks := respLookupEntry.Entry.Chunks
  41. sort.Slice(chunks, func(i, j int) bool {
  42. return chunks[i].Offset < chunks[j].Offset
  43. })
  44. filer.ProtoToText(writer, respLookupEntry.Entry)
  45. bytes, _ := proto.Marshal(respLookupEntry.Entry)
  46. gzippedBytes, _ := util.GzipData(bytes)
  47. // zstdBytes, _ := util.ZstdData(bytes)
  48. // fmt.Fprintf(writer, "chunks %d meta size: %d gzip:%d zstd:%d\n", len(respLookupEntry.Entry.GetChunks()), len(bytes), len(gzippedBytes), len(zstdBytes))
  49. fmt.Fprintf(writer, "chunks %d meta size: %d gzip:%d\n", len(respLookupEntry.Entry.GetChunks()), len(bytes), len(gzippedBytes))
  50. return nil
  51. })
  52. }