fusego/fuseops/common_op.go

153 lines
3.6 KiB
Go
Raw Normal View History

2015-05-01 03:49:14 +03:00
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fuseops
import (
2015-05-01 04:43:46 +03:00
"fmt"
2015-05-25 07:15:49 +03:00
"log"
2015-05-01 03:57:49 +03:00
"reflect"
2015-05-01 04:24:05 +03:00
"strings"
2015-05-01 03:49:14 +03:00
"github.com/jacobsa/fuse/internal/fuseshim"
2015-05-01 04:04:57 +03:00
"github.com/jacobsa/reqtrace"
2015-05-01 03:49:14 +03:00
"golang.org/x/net/context"
)
2015-05-05 03:20:03 +03:00
// An interface that all ops inside which commonOp is embedded must
// implement.
type internalOp interface {
Op
2015-07-24 01:33:40 +03:00
// Create a response message for the kernel, with leading pading for a
// fusekernel.OutHeader struct.
kernelResponse() []byte
2015-05-05 03:20:03 +03:00
}
2015-05-01 03:49:14 +03:00
// A helper for embedding common behavior.
type commonOp struct {
2015-05-05 02:56:49 +03:00
// The context exposed to the user.
ctx context.Context
2015-05-05 02:48:16 +03:00
// The op in which this struct is embedded.
2015-05-05 03:21:21 +03:00
op internalOp
2015-05-01 05:21:06 +03:00
2015-07-24 01:33:40 +03:00
// The fuse unique ID of this request, as assigned by the kernel.
fuseID uint64
// A function that can be used to send a reply to the kernel.
sendReply func([]byte) error
2015-05-01 04:57:37 +03:00
// A function that can be used to log debug information about the op. The
// first argument is a call depth.
//
// May be nil.
debugLog func(int, string, ...interface{})
2015-05-25 07:15:49 +03:00
// A logger to be used for logging exceptional errors.
//
// May be nil.
2015-05-25 07:15:49 +03:00
errorLogger *log.Logger
2015-05-05 02:52:25 +03:00
// A function that is invoked with the error given to Respond, for use in
// closing off traces and reporting back to the connection.
2015-05-05 02:59:14 +03:00
finished func(error)
2015-05-01 03:49:14 +03:00
}
2015-05-01 05:24:36 +03:00
func (o *commonOp) ShortDesc() (desc string) {
2015-05-01 05:28:25 +03:00
opName := reflect.TypeOf(o.op).String()
2015-05-01 05:24:36 +03:00
2015-05-01 05:28:25 +03:00
// Attempt to better handle the usual case: a string that looks like
// "*fuseops.GetInodeAttributesOp".
2015-05-01 05:24:36 +03:00
const prefix = "*fuseops."
const suffix = "Op"
2015-05-01 05:28:25 +03:00
if strings.HasPrefix(opName, prefix) && strings.HasSuffix(opName, suffix) {
opName = opName[len(prefix) : len(opName)-len(suffix)]
2015-05-01 05:24:36 +03:00
}
2015-05-01 05:28:25 +03:00
// Include the inode number to which the op applies.
desc = fmt.Sprintf("%s(inode=%v)", opName, o.bazilReq.Hdr().Node)
2015-05-01 05:24:36 +03:00
return
}
2015-05-01 03:49:14 +03:00
func (o *commonOp) init(
ctx context.Context,
2015-05-05 03:21:21 +03:00
op internalOp,
bazilReq fuseshim.Request,
debugLog func(int, string, ...interface{}),
2015-05-25 07:15:49 +03:00
errorLogger *log.Logger,
2015-05-05 02:59:14 +03:00
finished func(error)) {
2015-05-01 04:04:57 +03:00
// Initialize basic fields.
2015-05-05 02:56:49 +03:00
o.ctx = ctx
2015-05-01 05:21:06 +03:00
o.op = op
2015-05-01 04:57:37 +03:00
o.bazilReq = bazilReq
o.debugLog = debugLog
2015-05-25 07:15:49 +03:00
o.errorLogger = errorLogger
2015-05-05 02:59:14 +03:00
o.finished = finished
2015-05-05 02:56:49 +03:00
2015-05-01 04:04:57 +03:00
// Set up a trace span for this op.
2015-05-05 02:56:49 +03:00
var reportForTrace reqtrace.ReportFunc
2015-05-05 07:49:59 +03:00
o.ctx, reportForTrace = reqtrace.StartSpan(o.ctx, o.op.ShortDesc())
2015-05-05 02:56:49 +03:00
// When the op is finished, report to both reqtrace and the connection.
2015-05-05 02:59:14 +03:00
prevFinish := o.finished
o.finished = func(err error) {
2015-05-05 02:56:49 +03:00
reportForTrace(err)
prevFinish(err)
}
2015-05-01 03:49:14 +03:00
}
func (o *commonOp) Context() context.Context {
return o.ctx
}
func (o *commonOp) Logf(format string, v ...interface{}) {
if o.debugLog == nil {
return
}
2015-05-01 03:49:14 +03:00
const calldepth = 2
o.debugLog(calldepth, format, v...)
2015-05-01 03:49:14 +03:00
}
2015-05-05 03:25:25 +03:00
func (o *commonOp) Respond(err error) {
// Report that the user is responding.
o.finished(err)
2015-05-05 03:07:34 +03:00
// If successful, we should respond to fuseshim with the appropriate struct.
2015-05-05 03:25:25 +03:00
if err == nil {
o.op.respond()
2015-05-05 03:25:25 +03:00
return
}
// Log the error.
if o.debugLog != nil {
o.Logf(
"-> (%s) error: %v",
o.op.ShortDesc(),
err)
}
2015-05-01 03:49:14 +03:00
if o.errorLogger != nil {
o.errorLogger.Printf(
"(%s) error: %v",
o.op.ShortDesc(),
err)
}
2015-05-25 07:15:49 +03:00
2015-05-05 02:57:41 +03:00
// Send a response to the kernel.
2015-05-01 04:57:37 +03:00
o.bazilReq.RespondError(err)
2015-05-01 03:49:14 +03:00
}