fusego/samples/interruptfs/interrupt_fs.go

204 lines
4.3 KiB
Go
Raw Permalink Normal View History

2015-05-04 09:30: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.
2015-05-04 09:34:11 +03:00
package interruptfs
2015-05-04 09:30:03 +03:00
import (
"context"
2015-05-04 09:30:03 +03:00
"fmt"
"os"
2015-05-04 14:53:39 +03:00
"sync"
2015-05-04 09:30:03 +03:00
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
)
var rootAttrs = fuseops.InodeAttributes{
Nlink: 1,
Mode: os.ModeDir | 0777,
}
const fooID = fuseops.RootInodeID + 1
var fooAttrs = fuseops.InodeAttributes{
Nlink: 1,
Mode: 0777,
2015-05-04 14:54:33 +03:00
Size: 1234,
2015-05-04 09:30:03 +03:00
}
2015-07-29 07:51:10 +03:00
// A file system containing exactly one file, named "foo". ReadFile and
// FlushFile ops can be made to hang until interrupted. Exposes a method for
// synchronizing with the arrival of a read or a flush.
2015-05-04 09:30:03 +03:00
//
// Must be created with New.
type InterruptFS struct {
fuseutil.NotImplementedFileSystem
2015-05-04 14:53:39 +03:00
2015-07-29 07:51:10 +03:00
mu sync.Mutex
blockForReads bool // GUARDED_BY(mu)
blockForFlushes bool // GUARDED_BY(mu)
// Must hold the mutex when closing these.
readReceived chan struct{}
flushReceived chan struct{}
2015-05-04 09:30:03 +03:00
}
func New() *InterruptFS {
return &InterruptFS{
2015-07-29 07:51:10 +03:00
readReceived: make(chan struct{}),
flushReceived: make(chan struct{}),
}
2015-05-04 09:30:03 +03:00
}
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
2015-05-04 14:53:39 +03:00
// Block until the first read is received.
2015-07-29 07:51:10 +03:00
func (fs *InterruptFS) WaitForFirstRead() {
<-fs.readReceived
}
// Block until the first flush is received.
func (fs *InterruptFS) WaitForFirstFlush() {
<-fs.flushReceived
}
// Enable blocking until interrupted for the next (and subsequent) read ops.
func (fs *InterruptFS) EnableReadBlocking() {
2015-05-04 14:53:39 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
2015-07-29 07:51:10 +03:00
fs.blockForReads = true
}
// Enable blocking until interrupted for the next (and subsequent) flush ops.
func (fs *InterruptFS) EnableFlushBlocking() {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.blockForFlushes = true
2015-05-04 09:30:03 +03:00
}
////////////////////////////////////////////////////////////////////////
// FileSystem methods
////////////////////////////////////////////////////////////////////////
2015-09-09 15:55:39 +03:00
func (fs *InterruptFS) StatFS(
ctx context.Context,
op *fuseops.StatFSOp) error {
return nil
2015-09-09 15:55:39 +03:00
}
2015-05-04 09:30:03 +03:00
func (fs *InterruptFS) LookUpInode(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.LookUpInodeOp) error {
2015-05-04 09:30:03 +03:00
// We support only one parent.
if op.Parent != fuseops.RootInodeID {
return fmt.Errorf("Unexpected parent: %v", op.Parent)
2015-05-04 09:30:03 +03:00
}
// We support only one name.
if op.Name != "foo" {
return fuse.ENOENT
2015-05-04 09:30:03 +03:00
}
// Fill in the response.
op.Entry.Child = fooID
op.Entry.Attributes = fooAttrs
return nil
2015-05-04 09:30:03 +03:00
}
func (fs *InterruptFS) GetInodeAttributes(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.GetInodeAttributesOp) error {
2015-05-04 09:30:03 +03:00
switch op.Inode {
case fuseops.RootInodeID:
op.Attributes = rootAttrs
case fooID:
op.Attributes = fooAttrs
default:
return fmt.Errorf("Unexpected inode ID: %v", op.Inode)
2015-05-04 09:30:03 +03:00
}
return nil
2015-05-04 09:30:03 +03:00
}
func (fs *InterruptFS) OpenFile(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.OpenFileOp) error {
return nil
2015-05-04 09:30:03 +03:00
}
2015-05-04 14:56:43 +03:00
func (fs *InterruptFS) ReadFile(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.ReadFileOp) error {
2015-05-04 15:00:24 +03:00
fs.mu.Lock()
2015-07-29 07:51:10 +03:00
shouldBlock := fs.blockForReads
// Signal that a read has been received, if this is the first.
select {
case <-fs.readReceived:
default:
close(fs.readReceived)
}
2015-05-04 15:00:24 +03:00
fs.mu.Unlock()
2015-07-29 07:51:10 +03:00
// Wait for cancellation if enabled.
if shouldBlock {
done := ctx.Done()
if done == nil {
panic("Expected non-nil channel.")
}
<-done
return ctx.Err()
2015-07-29 07:51:10 +03:00
}
return nil
2015-07-29 07:51:10 +03:00
}
func (fs *InterruptFS) FlushFile(
ctx context.Context,
op *fuseops.FlushFileOp) error {
2015-07-29 07:51:10 +03:00
fs.mu.Lock()
shouldBlock := fs.blockForFlushes
// Signal that a flush has been received, if this is the first.
select {
case <-fs.flushReceived:
default:
close(fs.flushReceived)
2015-05-04 14:56:43 +03:00
}
2015-07-29 07:51:10 +03:00
fs.mu.Unlock()
2015-05-04 14:56:43 +03:00
2015-07-29 07:51:10 +03:00
// Wait for cancellation if enabled.
if shouldBlock {
done := ctx.Done()
if done == nil {
panic("Expected non-nil channel.")
}
2015-05-04 14:56:43 +03:00
2015-07-29 07:51:10 +03:00
<-done
return ctx.Err()
2015-07-29 07:51:10 +03:00
}
2015-05-04 14:56:43 +03:00
return nil
2015-05-04 14:56:43 +03:00
}