fusego/fuseops/common_op.go

157 lines
4.0 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-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/bazilfuse"
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
// Convert to a bazilfuse response compatible with the Respond method on the
// wrapped bazilfuse request. If that Respond method takes no arguments,
// return nil.
toBazilfuseResponse() interface{}
}
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
// The underlying bazilfuse request for this op.
2015-05-01 04:57:37 +03:00
bazilReq bazilfuse.Request
2015-05-05 02:52:25 +03:00
// A function that can be used to log information about the op. The first
// argument is a call depth.
log func(int, string, ...interface{})
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,
2015-05-01 04:57:37 +03:00
bazilReq bazilfuse.Request,
2015-05-01 03:49:14 +03:00
log func(int, string, ...interface{}),
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
2015-05-01 03:49:14 +03:00
o.log = log
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) Header() OpHeader {
2015-05-01 04:57:37 +03:00
bh := o.bazilReq.Hdr()
2015-05-01 03:49:14 +03:00
return OpHeader{
Uid: bh.Uid,
Gid: bh.Gid,
}
}
func (o *commonOp) Context() context.Context {
return o.ctx
}
func (o *commonOp) Logf(format string, v ...interface{}) {
const calldepth = 2
o.log(calldepth, format, v...)
}
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
2015-05-05 03:25:25 +03:00
// If successful, we should respond to bazilfuse with the appropriate struct.
if err == nil {
o.sendBazilfuseResponse(o.op.toBazilfuseResponse())
return
}
// Log the error.
2015-05-01 03:49:14 +03:00
o.Logf(
"-> (%s) error: %v",
2015-05-01 05:38:03 +03:00
o.op.ShortDesc(),
2015-05-01 03:49:14 +03:00
err)
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
}
2015-05-01 03:57:49 +03:00
// Respond with the supplied response struct, which must be accepted by a
2015-05-01 04:57:37 +03:00
// method called Respond on o.bazilReq.
2015-05-01 03:57:49 +03:00
//
2015-05-01 04:57:37 +03:00
// Special case: nil means o.bazilReq.Respond accepts no parameters.
2015-05-05 03:25:25 +03:00
func (o *commonOp) sendBazilfuseResponse(resp interface{}) {
2015-05-01 03:57:49 +03:00
// Find the Respond method.
2015-05-01 04:57:37 +03:00
v := reflect.ValueOf(o.bazilReq)
2015-05-01 03:57:49 +03:00
respond := v.MethodByName("Respond")
// Special case: handle successful ops with no response struct.
if resp == nil {
2015-05-01 05:38:03 +03:00
o.Logf("-> (%s) OK", o.op.ShortDesc())
2015-05-01 03:57:49 +03:00
respond.Call([]reflect.Value{})
return
}
2015-05-05 02:58:18 +03:00
// Otherwise, send the response struct to the kernel.
2015-05-01 03:57:49 +03:00
o.Logf("-> %v", resp)
respond.Call([]reflect.Value{reflect.ValueOf(resp)})
}