Commit c1418498 authored by Aaron Jacobs's avatar Aaron Jacobs

Eliminated a bunch of per-op error checking boilerplate.

parents b79a1f55 bc6e09f8
......@@ -36,13 +36,24 @@ var fTraceByPID = flag.Bool(
"individual PID. Not a good idea to use in production; races, bugs, and "+
"resource leaks likely lurk.")
// 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{}
}
// A helper for embedding common behavior.
type commonOp struct {
// The context exposed to the user.
ctx context.Context
// The op in which this struct is embedded.
op Op
op internalOp
// The underlying bazilfuse request for this op.
bazilReq bazilfuse.Request
......@@ -153,7 +164,7 @@ func (o *commonOp) ShortDesc() (desc string) {
func (o *commonOp) init(
ctx context.Context,
op Op,
op internalOp,
bazilReq bazilfuse.Request,
log func(int, string, ...interface{}),
finished func(error)) {
......@@ -196,15 +207,17 @@ func (o *commonOp) Logf(format string, v ...interface{}) {
o.log(calldepth, format, v...)
}
func (o *commonOp) respondErr(err error) {
if err == nil {
panic("Expect non-nil here.")
}
func (o *commonOp) Respond(err error) {
// Don't forget to report back to the connection that we are finished.
defer o.finished(err)
// Log that we are finished.
// If successful, we should respond to bazilfuse with the appropriate struct.
if err == nil {
o.sendBazilfuseResponse(o.op.toBazilfuseResponse())
return
}
// Log the error.
o.Logf(
"-> (%s) error: %v",
o.op.ShortDesc(),
......@@ -218,10 +231,7 @@ func (o *commonOp) respondErr(err error) {
// method called Respond on o.bazilReq.
//
// Special case: nil means o.bazilReq.Respond accepts no parameters.
func (o *commonOp) respond(resp interface{}) {
// Don't forget to report back to the connection that we are finished.
defer o.finished(nil)
func (o *commonOp) sendBazilfuseResponse(resp interface{}) {
// Find the Respond method.
v := reflect.ValueOf(o.bazilReq)
respond := v.MethodByName("Respond")
......
......@@ -35,13 +35,14 @@ func Convert(
finished func(error)) (o Op) {
var co *commonOp
var io internalOp
switch typed := r.(type) {
case *bazilfuse.InitRequest:
to := &InitOp{
maxReadahead: typed.MaxReadahead,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.LookupRequest:
......@@ -49,14 +50,14 @@ func Convert(
Parent: InodeID(typed.Header.Node),
Name: typed.Name,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.GetattrRequest:
to := &GetInodeAttributesOp{
Inode: InodeID(typed.Header.Node),
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.SetattrRequest:
......@@ -80,7 +81,7 @@ func Convert(
to.Mtime = &typed.Mtime
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.ForgetRequest:
......@@ -88,7 +89,7 @@ func Convert(
Inode: InodeID(typed.Header.Node),
N: typed.N,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.MkdirRequest:
......@@ -97,7 +98,7 @@ func Convert(
Name: typed.Name,
Mode: typed.Mode,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.CreateRequest:
......@@ -107,7 +108,7 @@ func Convert(
Mode: typed.Mode,
Flags: typed.Flags,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.RemoveRequest:
......@@ -116,14 +117,14 @@ func Convert(
Parent: InodeID(typed.Header.Node),
Name: typed.Name,
}
o = to
io = to
co = &to.commonOp
} else {
to := &UnlinkOp{
Parent: InodeID(typed.Header.Node),
Name: typed.Name,
}
o = to
io = to
co = &to.commonOp
}
......@@ -133,14 +134,14 @@ func Convert(
Inode: InodeID(typed.Header.Node),
Flags: typed.Flags,
}
o = to
io = to
co = &to.commonOp
} else {
to := &OpenFileOp{
Inode: InodeID(typed.Header.Node),
Flags: typed.Flags,
}
o = to
io = to
co = &to.commonOp
}
......@@ -152,7 +153,7 @@ func Convert(
Offset: DirOffset(typed.Offset),
Size: typed.Size,
}
o = to
io = to
co = &to.commonOp
} else {
to := &ReadFileOp{
......@@ -161,7 +162,7 @@ func Convert(
Offset: typed.Offset,
Size: typed.Size,
}
o = to
io = to
co = &to.commonOp
}
......@@ -170,13 +171,13 @@ func Convert(
to := &ReleaseDirHandleOp{
Handle: HandleID(typed.Handle),
}
o = to
io = to
co = &to.commonOp
} else {
to := &ReleaseFileHandleOp{
Handle: HandleID(typed.Handle),
}
o = to
io = to
co = &to.commonOp
}
......@@ -187,7 +188,7 @@ func Convert(
Data: typed.Data,
Offset: typed.Offset,
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.FsyncRequest:
......@@ -200,7 +201,7 @@ func Convert(
Inode: InodeID(typed.Header.Node),
Handle: HandleID(typed.Handle),
}
o = to
io = to
co = &to.commonOp
case *bazilfuse.FlushRequest:
......@@ -208,14 +209,16 @@ func Convert(
Inode: InodeID(typed.Header.Node),
Handle: HandleID(typed.Handle),
}
o = to
io = to
co = &to.commonOp
default:
return
}
co.init(opCtx, o, r, logForOp, finished)
co.init(opCtx, io, r, logForOp, finished)
o = io
return
}
......
......@@ -64,13 +64,9 @@ type InitOp struct {
maxReadahead uint32
}
func (o *InitOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *InitOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.InitResponse{}
bfResp = &resp
// Ask the Linux kernel for larger write requests.
//
......@@ -111,8 +107,7 @@ func (o *InitOp) Respond(err error) {
// willing to give us.
resp.MaxReadahead = o.maxReadahead
// Respond.
o.commonOp.respond(&resp)
return
}
////////////////////////////////////////////////////////////////////////
......@@ -150,16 +145,13 @@ func (o *LookUpInodeOp) ShortDesc() (desc string) {
return
}
func (o *LookUpInodeOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *LookUpInodeOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.LookupResponse{}
bfResp = &resp
convertChildInodeEntry(&o.Entry, &resp)
o.commonOp.respond(&resp)
return
}
// Refresh the attributes for an inode whose ID was previously returned in a
......@@ -179,18 +171,14 @@ type GetInodeAttributesOp struct {
AttributesExpiration time.Time
}
func (o *GetInodeAttributesOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *GetInodeAttributesOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.GetattrResponse{
Attr: convertAttributes(o.Inode, o.Attributes),
AttrValid: convertExpirationTime(o.AttributesExpiration),
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Change attributes for an inode.
......@@ -216,18 +204,14 @@ type SetInodeAttributesOp struct {
AttributesExpiration time.Time
}
func (o *SetInodeAttributesOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *SetInodeAttributesOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.SetattrResponse{
Attr: convertAttributes(o.Inode, o.Attributes),
AttrValid: convertExpirationTime(o.AttributesExpiration),
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Decrement the reference count for an inode ID previously issued by the file
......@@ -279,13 +263,8 @@ type ForgetInodeOp struct {
N uint64
}
func (o *ForgetInodeOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *ForgetInodeOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
////////////////////////////////////////////////////////////////////////
......@@ -321,16 +300,13 @@ func (o *MkDirOp) ShortDesc() (desc string) {
return
}
func (o *MkDirOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *MkDirOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.MkdirResponse{}
bfResp = &resp
convertChildInodeEntry(&o.Entry, &resp.LookupResponse)
o.commonOp.respond(&resp)
return
}
// Create a file inode and open it.
......@@ -381,20 +357,17 @@ func (o *CreateFileOp) ShortDesc() (desc string) {
return
}
func (o *CreateFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *CreateFileOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.CreateResponse{
OpenResponse: bazilfuse.OpenResponse{
Handle: bazilfuse.HandleID(o.Handle),
},
}
bfResp = &resp
convertChildInodeEntry(&o.Entry, &resp.LookupResponse)
o.commonOp.respond(&resp)
return
}
////////////////////////////////////////////////////////////////////////
......@@ -417,13 +390,8 @@ type RmDirOp struct {
Name string
}
func (o *RmDirOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *RmDirOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
// Unlink a file from its parent. If this brings the inode's link count to
......@@ -440,13 +408,8 @@ type UnlinkOp struct {
Name string
}
func (o *UnlinkOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *UnlinkOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
////////////////////////////////////////////////////////////////////////
......@@ -479,17 +442,13 @@ type OpenDirOp struct {
Handle HandleID
}
func (o *OpenDirOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *OpenDirOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.OpenResponse{
Handle: bazilfuse.HandleID(o.Handle),
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Read entries from a directory previously opened with OpenDir.
......@@ -582,17 +541,13 @@ type ReadDirOp struct {
Data []byte
}
func (o *ReadDirOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *ReadDirOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.ReadResponse{
Data: o.Data,
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Release a previously-minted directory handle. The kernel sends this when
......@@ -612,13 +567,8 @@ type ReleaseDirHandleOp struct {
Handle HandleID
}
func (o *ReleaseDirHandleOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *ReleaseDirHandleOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
////////////////////////////////////////////////////////////////////////
......@@ -650,17 +600,13 @@ type OpenFileOp struct {
Handle HandleID
}
func (o *OpenFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *OpenFileOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.OpenResponse{
Handle: bazilfuse.HandleID(o.Handle),
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Read data from a file previously opened with CreateFile or OpenFile.
......@@ -692,17 +638,13 @@ type ReadFileOp struct {
Data []byte
}
func (o *ReadFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *ReadFileOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.ReadResponse{
Data: o.Data,
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Write data to a file previously opened with CreateFile or OpenFile.
......@@ -775,17 +717,13 @@ type WriteFileOp struct {
Data []byte
}
func (o *WriteFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
func (o *WriteFileOp) toBazilfuseResponse() (bfResp interface{}) {
resp := bazilfuse.WriteResponse{
Size: len(o.Data),
}
bfResp = &resp
o.commonOp.respond(&resp)
return
}
// Synchronize the current contents of an open file to storage.
......@@ -812,13 +750,8 @@ type SyncFileOp struct {
Handle HandleID
}
func (o *SyncFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *SyncFileOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
// Flush the current state of an open file to storage upon closing a file
......@@ -876,13 +809,8 @@ type FlushFileOp struct {
Handle HandleID
}
func (o *FlushFileOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *FlushFileOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
// Release a previously-minted file handle. The kernel calls this when there
......@@ -902,11 +830,6 @@ type ReleaseFileHandleOp struct {
Handle HandleID
}
func (o *ReleaseFileHandleOp) Respond(err error) {
if err != nil {
o.commonOp.respondErr(err)
return
}
o.commonOp.respond(nil)
func (o *ReleaseFileHandleOp) toBazilfuseResponse() (bfResp interface{}) {
return
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment