Commit 18bf653e authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

nodefs: add Mount convenience function

parent d93e94d8
...@@ -308,6 +308,9 @@ type FileHandle interface { ...@@ -308,6 +308,9 @@ type FileHandle interface {
// Options sets options for the entire filesystem // Options sets options for the entire filesystem
type Options struct { type Options struct {
// MountOptions contain the options for mounting the fuse server
fuse.MountOptions
// If set to nonnil, this defines the overall entry timeout // If set to nonnil, this defines the overall entry timeout
// for the file system. See fuse.EntryOut for more information. // for the file system. See fuse.EntryOut for more information.
EntryTimeout *time.Duration EntryTimeout *time.Duration
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sync" "sync"
"testing" "testing"
"time" "time"
...@@ -84,28 +85,19 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) { ...@@ -84,28 +85,19 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) {
func TestKeepCache(t *testing.T) { func TestKeepCache(t *testing.T) {
mntDir := testutil.TempDir() mntDir := testutil.TempDir()
defer os.RemoveAll(mntDir)
sec := time.Second sec := time.Second
root := &keepCacheRoot{} root := &keepCacheRoot{}
rawFS := NewNodeFS(root, &Options{ server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1, FirstAutomaticIno: 1,
AttrTimeout: &sec, AttrTimeout: &sec,
EntryTimeout: &sec, EntryTimeout: &sec,
}) })
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil {
t.Fatal(err)
}
defer server.Unmount() defer server.Unmount()
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
c1, err := ioutil.ReadFile(mntDir + "/keep") c1, err := ioutil.ReadFile(mntDir + "/keep")
if err != nil { if err != nil {
t.Fatalf("read keep 1: %v", err) t.Fatalf("read keep 1: %v", err)
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
// An example of creating a loopback file system, and mounting it onto // An example of creating a loopback file system, and mounting it onto
// a directory // a directory
func ExampleNewNodeFS() { func ExampleMount() {
mntDir, _ := ioutil.TempDir("", "") mntDir, _ := ioutil.TempDir("", "")
home := os.Getenv("HOME") home := os.Getenv("HOME")
...@@ -23,14 +23,14 @@ func ExampleNewNodeFS() { ...@@ -23,14 +23,14 @@ func ExampleNewNodeFS() {
log.Panic(err) log.Panic(err)
} }
rawFS := NewNodeFS(root, &Options{}) server, err := Mount(mntDir, root, &Options{
server, err := fuse.NewServer(rawFS, mntDir, MountOptions: fuse.MountOptions{Debug: true},
&fuse.MountOptions{Debug: true}) })
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
log.Printf("Mounted %s as loopback on %s", home, mntDir) log.Printf("Mounted %s as loopback on %s", home, mntDir)
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir) log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
server.Serve() server.Wait()
} }
...@@ -53,26 +53,17 @@ func TestInterrupt(t *testing.T) { ...@@ -53,26 +53,17 @@ func TestInterrupt(t *testing.T) {
defer os.Remove(mntDir) defer os.Remove(mntDir)
root := &interruptRoot{} root := &interruptRoot{}
_ = time.Second
oneSec := time.Second oneSec := time.Second
rawFS := NewNodeFS(root, &Options{ server, err := Mount(mntDir, root, &Options{
// NOSUBMIT - should run all tests without cache too MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
EntryTimeout: &oneSec, EntryTimeout: &oneSec,
AttrTimeout: &oneSec, AttrTimeout: &oneSec,
}) })
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
defer server.Unmount() defer server.Unmount()
cmd := exec.Command("cat", mntDir+"/file") cmd := exec.Command("cat", mntDir+"/file")
......
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"time"
"github.com/hanwen/go-fuse/fuse"
)
// Mount mounts the given NodeFS on the directory, and starts serving
// requests. This is a convenience wrapper around NewNodeFS and
// fuse.NewServer. If nil is given as options, default settings are
// applied, which are 1 second entry and attribute timeout.
func Mount(dir string, root DirOperations, options *Options) (*fuse.Server, error) {
if options == nil {
oneSec := time.Second
options = &Options{
EntryTimeout: &oneSec,
AttrTimeout: &oneSec,
}
}
rawFS := NewNodeFS(root, options)
server, err := fuse.NewServer(rawFS, dir, &options.MountOptions)
if err != nil {
return nil, err
}
go server.Serve()
if err := server.WaitMount(); err != nil {
// XXX should shutdown the serve loop?
return nil, err
}
return server, nil
}
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"bytes" "bytes"
"context" "context"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
...@@ -63,23 +64,17 @@ func TestZipFS(t *testing.T) { ...@@ -63,23 +64,17 @@ func TestZipFS(t *testing.T) {
root := &zipRoot{r: r} root := &zipRoot{r: r}
mntDir := testutil.TempDir() mntDir := testutil.TempDir()
defer os.Remove(mntDir)
rawFS := NewNodeFS(root, &Options{ server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1, FirstAutomaticIno: 1,
}) })
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer server.Unmount() defer server.Unmount()
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
for k, v := range testData { for k, v := range testData {
c, err := ioutil.ReadFile(filepath.Join(mntDir, k)) c, err := ioutil.ReadFile(filepath.Join(mntDir, k))
if err != nil { if err != nil {
......
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