2015-03-25 01:16:03 +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 fuseutil
|
|
|
|
|
|
|
|
import (
|
2015-03-25 01:24:44 +03:00
|
|
|
"io"
|
2015-06-09 03:47:35 +03:00
|
|
|
"sync"
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-07-27 08:15:07 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-25 01:16:03 +03:00
|
|
|
"github.com/jacobsa/fuse"
|
|
|
|
"github.com/jacobsa/fuse/fuseops"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An interface with a method for each op type in the fuseops package. This can
|
|
|
|
// be used in conjunction with NewFileSystemServer to avoid writing a "dispatch
|
|
|
|
// loop" that switches on op types, instead receiving typed method calls
|
|
|
|
// directly.
|
|
|
|
//
|
2015-07-27 08:44:17 +03:00
|
|
|
// The FileSystem implementation should not call Connection.Reply, instead
|
|
|
|
// returning the error with which the caller should respond.
|
2015-03-25 01:17:58 +03:00
|
|
|
//
|
|
|
|
// See NotImplementedFileSystem for a convenient way to embed default
|
|
|
|
// implementations for methods you don't care about.
|
2015-03-25 01:16:03 +03:00
|
|
|
type FileSystem interface {
|
2015-07-27 08:15:07 +03:00
|
|
|
LookUpInode(context.Context, *fuseops.LookUpInodeOp) error
|
|
|
|
GetInodeAttributes(context.Context, *fuseops.GetInodeAttributesOp) error
|
|
|
|
SetInodeAttributes(context.Context, *fuseops.SetInodeAttributesOp) error
|
|
|
|
ForgetInode(context.Context, *fuseops.ForgetInodeOp) error
|
|
|
|
MkDir(context.Context, *fuseops.MkDirOp) error
|
|
|
|
CreateFile(context.Context, *fuseops.CreateFileOp) error
|
|
|
|
CreateSymlink(context.Context, *fuseops.CreateSymlinkOp) error
|
|
|
|
Rename(context.Context, *fuseops.RenameOp) error
|
|
|
|
RmDir(context.Context, *fuseops.RmDirOp) error
|
|
|
|
Unlink(context.Context, *fuseops.UnlinkOp) error
|
|
|
|
OpenDir(context.Context, *fuseops.OpenDirOp) error
|
|
|
|
ReadDir(context.Context, *fuseops.ReadDirOp) error
|
|
|
|
ReleaseDirHandle(context.Context, *fuseops.ReleaseDirHandleOp) error
|
|
|
|
OpenFile(context.Context, *fuseops.OpenFileOp) error
|
|
|
|
ReadFile(context.Context, *fuseops.ReadFileOp) error
|
|
|
|
WriteFile(context.Context, *fuseops.WriteFileOp) error
|
|
|
|
SyncFile(context.Context, *fuseops.SyncFileOp) error
|
|
|
|
FlushFile(context.Context, *fuseops.FlushFileOp) error
|
|
|
|
ReleaseFileHandle(context.Context, *fuseops.ReleaseFileHandleOp) error
|
|
|
|
ReadSymlink(context.Context, *fuseops.ReadSymlinkOp) error
|
2015-06-05 06:51:27 +03:00
|
|
|
|
|
|
|
// Regard all inodes (including the root inode) as having their lookup counts
|
|
|
|
// decremented to zero, and clean up any resources associated with the file
|
|
|
|
// system. No further calls to the file system will be made.
|
|
|
|
Destroy()
|
2015-03-25 01:16:03 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 01:53:26 +03:00
|
|
|
// Create a fuse.Server that handles ops by calling the associated FileSystem
|
|
|
|
// method.Respond with the resulting error. Unsupported ops are responded to
|
|
|
|
// directly with ENOSYS.
|
|
|
|
//
|
2015-04-02 02:50:44 +03:00
|
|
|
// Each call to a FileSystem method is made on its own goroutine, and is free
|
|
|
|
// to block.
|
|
|
|
//
|
|
|
|
// (It is safe to naively process ops concurrently because the kernel
|
|
|
|
// guarantees to serialize operations that the user expects to happen in order,
|
|
|
|
// cf. http://goo.gl/jnkHPO, fuse-devel thread "Fuse guarantees on concurrent
|
2015-04-02 02:45:38 +03:00
|
|
|
// requests").
|
2015-03-25 01:24:44 +03:00
|
|
|
func NewFileSystemServer(fs FileSystem) fuse.Server {
|
2015-06-09 04:01:11 +03:00
|
|
|
return &fileSystemServer{
|
2015-06-09 03:47:35 +03:00
|
|
|
fs: fs,
|
|
|
|
}
|
2015-03-25 01:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileSystemServer struct {
|
2015-06-09 03:47:35 +03:00
|
|
|
fs FileSystem
|
|
|
|
opsInFlight sync.WaitGroup
|
2015-03-25 01:24:44 +03:00
|
|
|
}
|
|
|
|
|
2015-06-09 04:01:11 +03:00
|
|
|
func (s *fileSystemServer) ServeOps(c *fuse.Connection) {
|
2015-06-09 04:11:52 +03:00
|
|
|
// When we are done, we clean up by waiting for all in-flight ops then
|
|
|
|
// destroying the file system.
|
|
|
|
defer func() {
|
|
|
|
s.opsInFlight.Wait()
|
|
|
|
s.fs.Destroy()
|
|
|
|
}()
|
2015-06-09 04:01:11 +03:00
|
|
|
|
2015-03-25 01:24:44 +03:00
|
|
|
for {
|
2015-07-27 08:15:07 +03:00
|
|
|
ctx, op, err := c.ReadOp()
|
2015-03-25 01:24:44 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-06-09 03:47:35 +03:00
|
|
|
s.opsInFlight.Add(1)
|
2015-07-27 08:15:07 +03:00
|
|
|
go s.handleOp(c, ctx, op)
|
2015-04-02 02:52:58 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-07-27 08:15:07 +03:00
|
|
|
func (s *fileSystemServer) handleOp(
|
|
|
|
c *fuse.Connection,
|
|
|
|
ctx context.Context,
|
2015-07-27 08:44:17 +03:00
|
|
|
op interface{}) {
|
2015-06-09 04:01:11 +03:00
|
|
|
defer s.opsInFlight.Done()
|
|
|
|
|
2015-04-02 03:19:56 +03:00
|
|
|
// Dispatch to the appropriate method.
|
2015-06-05 07:20:39 +03:00
|
|
|
var err error
|
2015-04-02 02:52:58 +03:00
|
|
|
switch typed := op.(type) {
|
|
|
|
default:
|
2015-06-05 07:20:39 +03:00
|
|
|
err = fuse.ENOSYS
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.LookUpInodeOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.LookUpInode(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.GetInodeAttributesOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.GetInodeAttributes(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.SetInodeAttributesOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.SetInodeAttributes(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.ForgetInodeOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ForgetInode(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.MkDirOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.MkDir(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.CreateFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.CreateFile(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-05-19 08:33:19 +03:00
|
|
|
case *fuseops.CreateSymlinkOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.CreateSymlink(ctx, typed)
|
2015-05-19 08:33:19 +03:00
|
|
|
|
2015-06-25 08:40:29 +03:00
|
|
|
case *fuseops.RenameOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.Rename(ctx, typed)
|
2015-06-25 08:40:29 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.RmDirOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.RmDir(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.UnlinkOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.Unlink(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.OpenDirOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.OpenDir(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.ReadDirOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ReadDir(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.ReleaseDirHandleOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ReleaseDirHandle(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.OpenFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.OpenFile(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.ReadFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ReadFile(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.WriteFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.WriteFile(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.SyncFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.SyncFile(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
|
2015-04-02 02:52:58 +03:00
|
|
|
case *fuseops.FlushFileOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.FlushFile(ctx, typed)
|
2015-04-02 02:52:58 +03:00
|
|
|
|
|
|
|
case *fuseops.ReleaseFileHandleOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ReleaseFileHandle(ctx, typed)
|
2015-05-19 09:06:57 +03:00
|
|
|
|
|
|
|
case *fuseops.ReadSymlinkOp:
|
2015-07-27 08:15:07 +03:00
|
|
|
err = s.fs.ReadSymlink(ctx, typed)
|
2015-03-25 01:24:44 +03:00
|
|
|
}
|
2015-06-05 07:20:39 +03:00
|
|
|
|
2015-07-27 08:15:07 +03:00
|
|
|
c.Reply(ctx, err)
|
2015-03-25 01:24:44 +03:00
|
|
|
}
|