debug.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fuse
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "github.com/jacobsa/fuse/fuseops"
  20. )
  21. // Decide on the name of the given op.
  22. func opName(op interface{}) string {
  23. // We expect all ops to be pointers.
  24. t := reflect.TypeOf(op).Elem()
  25. // Strip the "Op" from "FooOp".
  26. return strings.TrimSuffix(t.Name(), "Op")
  27. }
  28. func describeRequest(op interface{}) (s string) {
  29. v := reflect.ValueOf(op).Elem()
  30. // We will set up a comma-separated list of components.
  31. var components []string
  32. addComponent := func(format string, v ...interface{}) {
  33. components = append(components, fmt.Sprintf(format, v...))
  34. }
  35. // Include an inode number, if available.
  36. if f := v.FieldByName("Inode"); f.IsValid() {
  37. addComponent("inode %v", f.Interface())
  38. }
  39. // Include a parent inode number, if available.
  40. if f := v.FieldByName("Parent"); f.IsValid() {
  41. addComponent("parent %v", f.Interface())
  42. }
  43. // Include a name, if available.
  44. if f := v.FieldByName("Name"); f.IsValid() {
  45. addComponent("name %q", f.Interface())
  46. }
  47. // Handle special cases.
  48. switch typed := op.(type) {
  49. case *interruptOp:
  50. addComponent("fuseid 0x%08x", typed.FuseID)
  51. case *unknownOp:
  52. addComponent("opcode %d", typed.OpCode)
  53. case *fuseops.SetInodeAttributesOp:
  54. if typed.Size != nil {
  55. addComponent("size %d", *typed.Size)
  56. }
  57. if typed.Mode != nil {
  58. addComponent("mode %v", *typed.Mode)
  59. }
  60. if typed.Atime != nil {
  61. addComponent("atime %v", *typed.Atime)
  62. }
  63. if typed.Mtime != nil {
  64. addComponent("mtime %v", *typed.Mtime)
  65. }
  66. case *fuseops.ReadFileOp:
  67. addComponent("handle %d", typed.Handle)
  68. addComponent("offset %d", typed.Offset)
  69. addComponent("%d bytes", len(typed.Dst))
  70. case *fuseops.WriteFileOp:
  71. addComponent("handle %d", typed.Handle)
  72. addComponent("offset %d", typed.Offset)
  73. addComponent("%d bytes", len(typed.Data))
  74. case *fuseops.RemoveXattrOp:
  75. addComponent("name %s", typed.Name)
  76. case *fuseops.GetXattrOp:
  77. addComponent("name %s", typed.Name)
  78. case *fuseops.SetXattrOp:
  79. addComponent("name %s", typed.Name)
  80. }
  81. // Use just the name if there is no extra info.
  82. if len(components) == 0 {
  83. return opName(op)
  84. }
  85. // Otherwise, include the extra info.
  86. return fmt.Sprintf("%s (%s)", opName(op), strings.Join(components, ", "))
  87. }
  88. func describeResponse(op interface{}) string {
  89. v := reflect.ValueOf(op).Elem()
  90. // We will set up a comma-separated list of components.
  91. var components []string
  92. addComponent := func(format string, v ...interface{}) {
  93. components = append(components, fmt.Sprintf(format, v...))
  94. }
  95. // Include a resulting inode number, if available.
  96. if f := v.FieldByName("Entry"); f.IsValid() {
  97. if entry, ok := f.Interface().(fuseops.ChildInodeEntry); ok {
  98. addComponent("inode %v", entry.Child)
  99. }
  100. }
  101. return fmt.Sprintf("%s", strings.Join(components, ", "))
  102. }