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.

83 lines
2.1 KiB

4 years ago
5 years ago
5 years ago
4 years ago
  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(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. 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. fmt.Fprintf(writer, "chunks %d meta size: %d gzip:%d\n", len(respLookupEntry.Entry.Chunks), len(bytes), len(gzippedBytes))
  60. return nil
  61. })
  62. }