github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/debug.go (about)

     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  
    15  package fuse
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"strings"
    21  
    22  	"github.com/scaleoutsean/fusego/fuseops"
    23  )
    24  
    25  // Decide on the name of the given op.
    26  func opName(op interface{}) string {
    27  	// We expect all ops to be pointers.
    28  	t := reflect.TypeOf(op).Elem()
    29  
    30  	// Strip the "Op" from "FooOp".
    31  	return strings.TrimSuffix(t.Name(), "Op")
    32  }
    33  
    34  func describeRequest(op interface{}) (s string) {
    35  	v := reflect.ValueOf(op).Elem()
    36  
    37  	// We will set up a comma-separated list of components.
    38  	var components []string
    39  	addComponent := func(format string, v ...interface{}) {
    40  		components = append(components, fmt.Sprintf(format, v...))
    41  	}
    42  
    43  	// Include an inode number, if available.
    44  	if f := v.FieldByName("Inode"); f.IsValid() {
    45  		addComponent("inode %v", f.Interface())
    46  	}
    47  
    48  	// Include a parent inode number, if available.
    49  	if f := v.FieldByName("Parent"); f.IsValid() {
    50  		addComponent("parent %v", f.Interface())
    51  	}
    52  
    53  	// Include a name, if available.
    54  	if f := v.FieldByName("Name"); f.IsValid() {
    55  		addComponent("name %q", f.Interface())
    56  	}
    57  
    58  	// Handle special cases.
    59  	switch typed := op.(type) {
    60  	case *interruptOp:
    61  		addComponent("fuseid 0x%08x", typed.FuseID)
    62  
    63  	case *unknownOp:
    64  		addComponent("opcode %d", typed.OpCode)
    65  
    66  	case *fuseops.SetInodeAttributesOp:
    67  		if typed.Size != nil {
    68  			addComponent("size %d", *typed.Size)
    69  		}
    70  
    71  		if typed.Mode != nil {
    72  			addComponent("mode %v", *typed.Mode)
    73  		}
    74  
    75  		if typed.Atime != nil {
    76  			addComponent("atime %v", *typed.Atime)
    77  		}
    78  
    79  		if typed.Mtime != nil {
    80  			addComponent("mtime %v", *typed.Mtime)
    81  		}
    82  
    83  	case *fuseops.ReadFileOp:
    84  		addComponent("handle %d", typed.Handle)
    85  		addComponent("offset %d", typed.Offset)
    86  		addComponent("%d bytes", len(typed.Dst))
    87  
    88  	case *fuseops.WriteFileOp:
    89  		addComponent("handle %d", typed.Handle)
    90  		addComponent("offset %d", typed.Offset)
    91  		addComponent("%d bytes", len(typed.Data))
    92  
    93  	case *fuseops.RemoveXattrOp:
    94  		addComponent("name %s", typed.Name)
    95  
    96  	case *fuseops.GetXattrOp:
    97  		addComponent("name %s", typed.Name)
    98  
    99  	case *fuseops.SetXattrOp:
   100  		addComponent("name %s", typed.Name)
   101  
   102  	case *fuseops.FallocateOp:
   103  		addComponent("offset %d", typed.Offset)
   104  		addComponent("length %d", typed.Length)
   105  		addComponent("mode %d", typed.Mode)
   106  	}
   107  
   108  	// Use just the name if there is no extra info.
   109  	if len(components) == 0 {
   110  		return opName(op)
   111  	}
   112  
   113  	// Otherwise, include the extra info.
   114  	return fmt.Sprintf("%s (%s)", opName(op), strings.Join(components, ", "))
   115  }
   116  
   117  func describeResponse(op interface{}) string {
   118  	v := reflect.ValueOf(op).Elem()
   119  
   120  	// We will set up a comma-separated list of components.
   121  	var components []string
   122  	addComponent := func(format string, v ...interface{}) {
   123  		components = append(components, fmt.Sprintf(format, v...))
   124  	}
   125  
   126  	// Include a resulting inode number, if available.
   127  	if f := v.FieldByName("Entry"); f.IsValid() {
   128  		if entry, ok := f.Interface().(fuseops.ChildInodeEntry); ok {
   129  			addComponent("inode %v", entry.Child)
   130  		}
   131  	}
   132  
   133  	return fmt.Sprintf("%s", strings.Join(components, ", "))
   134  }