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 {
// Options sets options for the entire filesystem
type Options struct {
// MountOptions contain the options for mounting the fuse server
fuse.MountOptions
// If set to nonnil, this defines the overall entry timeout
// for the file system. See fuse.EntryOut for more information.
EntryTimeout *time.Duration
......
......@@ -9,6 +9,7 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"sync"
"testing"
"time"
......@@ -84,28 +85,19 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) {
func TestKeepCache(t *testing.T) {
mntDir := testutil.TempDir()
defer os.RemoveAll(mntDir)
sec := time.Second
root := &keepCacheRoot{}
rawFS := NewNodeFS(root, &Options{
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1,
AttrTimeout: &sec,
EntryTimeout: &sec,
})
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil {
t.Fatal(err)
}
defer server.Unmount()
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
c1, err := ioutil.ReadFile(mntDir + "/keep")
if err != nil {
t.Fatalf("read keep 1: %v", err)
......
......@@ -14,7 +14,7 @@ import (
// An example of creating a loopback file system, and mounting it onto
// a directory
func ExampleNewNodeFS() {
func ExampleMount() {
mntDir, _ := ioutil.TempDir("", "")
home := os.Getenv("HOME")
......@@ -23,14 +23,14 @@ func ExampleNewNodeFS() {
log.Panic(err)
}
rawFS := NewNodeFS(root, &Options{})
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{Debug: true})
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{Debug: true},
})
if err != nil {
log.Panic(err)
}
log.Printf("Mounted %s as loopback on %s", home, mntDir)
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
server.Serve()
server.Wait()
}
......@@ -53,26 +53,17 @@ func TestInterrupt(t *testing.T) {
defer os.Remove(mntDir)
root := &interruptRoot{}
_ = time.Second
oneSec := time.Second
rawFS := NewNodeFS(root, &Options{
// NOSUBMIT - should run all tests without cache too
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
EntryTimeout: &oneSec,
AttrTimeout: &oneSec,
})
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil {
t.Fatal(err)
}
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
defer server.Unmount()
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 (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
......@@ -63,23 +64,17 @@ func TestZipFS(t *testing.T) {
root := &zipRoot{r: r}
mntDir := testutil.TempDir()
rawFS := NewNodeFS(root, &Options{
defer os.Remove(mntDir)
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1,
})
server, err := fuse.NewServer(rawFS, mntDir,
&fuse.MountOptions{
Debug: testutil.VerboseTest(),
})
if err != nil {
t.Fatal(err)
}
defer server.Unmount()
go server.Serve()
if err := server.WaitMount(); err != nil {
t.Fatal(err)
}
for k, v := range testData {
c, err := ioutil.ReadFile(filepath.Join(mntDir, k))
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