Skip to content

Commit ed09d51

Browse files
committed
fs: introduce DefaultOptions()
This adds a way to base your Options on the defaults *except* some setting. Also allows to deduplicate some code in go-fuse, which this change also does. I have only touched the exactly-equivalent cases here. Change-Id: I99dcd06c0239537c15bda2c27236d00c1a474cf0
1 parent 9b1593c commit ed09d51

File tree

9 files changed

+38
-57
lines changed

9 files changed

+38
-57
lines changed

example/loopback/main.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"path"
1717
"runtime/pprof"
1818
"syscall"
19-
"time"
2019

2120
"github.com/hanwen/go-fuse/v2/fs"
2221
"github.com/hanwen/go-fuse/v2/fuse"
@@ -92,24 +91,16 @@ func main() {
9291
log.Fatalf("NewLoopbackRoot(%s): %v\n", orig, err)
9392
}
9493

95-
sec := time.Second
96-
opts := &fs.Options{
97-
// The timeout options are to be compatible with libfuse defaults,
98-
// making benchmarking easier.
99-
AttrTimeout: &sec,
100-
EntryTimeout: &sec,
101-
102-
NullPermissions: true, // Leave file permissions on "000" files as-is
103-
104-
MountOptions: fuse.MountOptions{
105-
AllowOther: *other,
106-
Debug: *debug,
107-
DirectMount: *directmount,
108-
DirectMountStrict: *directmountstrict,
109-
IDMappedMount: *idmap,
110-
FsName: orig, // First column in "df -T": original dir
111-
Name: "loopback", // Second column in "df -T" will be shown as "fuse." + Name
112-
},
94+
opts := fs.DefaultOptions()
95+
opts.NullPermissions = true // Leave file permissions on "000" files as-is
96+
opts.MountOptions = fuse.MountOptions{
97+
AllowOther: *other,
98+
Debug: *debug,
99+
DirectMount: *directmount,
100+
DirectMountStrict: *directmountstrict,
101+
IDMappedMount: *idmap,
102+
FsName: orig, // First column in "df -T": original dir
103+
Name: "loopback", // Second column in "df -T" will be shown as "fuse." + Name
113104
}
114105
if opts.AllowOther {
115106
// Make the kernel check file permissions for us

example/multizip/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"fmt"
1414
"os"
1515
"path/filepath"
16-
"time"
1716

1817
"github.com/hanwen/go-fuse/v2/fs"
1918
"github.com/hanwen/go-fuse/v2/zipfs"
@@ -30,13 +29,9 @@ func main() {
3029
}
3130

3231
root := &zipfs.MultiZipFs{}
33-
sec := time.Second
34-
opts := fs.Options{
35-
EntryTimeout: &sec,
36-
AttrTimeout: &sec,
37-
}
32+
opts := fs.DefaultOptions()
3833
opts.Debug = *debug
39-
server, err := fs.Mount(flag.Arg(0), root, &opts)
34+
server, err := fs.Mount(flag.Arg(0), root, opts)
4035
if err != nil {
4136
fmt.Printf("Mount fail: %v\n", err)
4237
os.Exit(1)

example/winfs/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ func main() {
142142
Path: orig,
143143
}
144144

145-
sec := time.Second
146-
opts := &fs.Options{
147-
AttrTimeout: &sec,
148-
EntryTimeout: &sec,
149-
}
145+
opts := fs.DefaultOptions()
150146
opts.Debug = *debug
151147
opts.MountOptions.Options = append(opts.MountOptions.Options, "fsname="+orig)
152148
opts.MountOptions.Name = "winfs"

fs/bridge.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"runtime/debug"
1111
"sync"
1212
"syscall"
13-
"time"
1413

1514
"github.com/hanwen/go-fuse/v2/fuse"
1615
"github.com/hanwen/go-fuse/v2/internal"
@@ -290,11 +289,7 @@ func (b *rawBridge) setAttrTimeout(out *fuse.AttrOut) {
290289
// applied, which are 1 second entry and attribute timeout.
291290
func NewNodeFS(root InodeEmbedder, opts *Options) fuse.RawFileSystem {
292291
if opts == nil {
293-
oneSec := time.Second
294-
opts = &Options{
295-
EntryTimeout: &oneSec,
296-
AttrTimeout: &oneSec,
297-
}
292+
opts = DefaultOptions()
298293
}
299294
bridge := &rawBridge{
300295
automaticIno: opts.FirstAutomaticIno,
@@ -305,7 +300,7 @@ func NewNodeFS(root InodeEmbedder, opts *Options) fuse.RawFileSystem {
305300
}
306301

307302
if bridge.automaticIno == 0 {
308-
bridge.automaticIno = 1 << 63
303+
bridge.automaticIno = DefaultOptions().FirstAutomaticIno
309304
}
310305

311306
stableAttr := StableAttr{

fs/default.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,22 @@
33
// license that can be found in the LICENSE file.
44

55
package fs
6+
7+
import (
8+
"time"
9+
)
10+
11+
// DefaultOptions returns the default Options that are used when
12+
// nil is passed for *Options.
13+
//
14+
// When you do want to set something in Options, get the defaults
15+
// from this function and adjust as required.
16+
func DefaultOptions() *Options {
17+
oneSec := time.Second
18+
return &Options{
19+
// libfuse also uses one second per default
20+
EntryTimeout: &oneSec,
21+
AttrTimeout: &oneSec,
22+
FirstAutomaticIno: 1 << 63,
23+
}
24+
}

fs/dir_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ func (n *syncNode) OpendirHandle(ctx context.Context, flags uint32) (FileHandle,
244244

245245
func TestFsyncDir(t *testing.T) {
246246
root := &syncNode{}
247-
opts := Options{}
248-
mnt, _ := testMount(t, root, &opts)
247+
mnt, _ := testMount(t, root, nil)
249248

250249
fd, err := syscall.Open(mnt, syscall.O_DIRECTORY, 0)
251250
if err != nil {

fs/interrupt_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ func (o *interruptOps) Open(ctx context.Context, flags uint32) (FileHandle, uint
5454
func TestInterrupt(t *testing.T) {
5555
root := &interruptRoot{}
5656

57-
oneSec := time.Second
58-
mntDir, server := testMount(t, root, &Options{
59-
EntryTimeout: &oneSec,
60-
AttrTimeout: &oneSec,
61-
})
57+
mntDir, server := testMount(t, root, DefaultOptions())
6258

6359
cmd := exec.Command("cat", mntDir+"/file")
6460
if err := cmd.Start(); err != nil {

fs/loopback_linux_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"sync"
1111
"syscall"
1212
"testing"
13-
"time"
1413
"unsafe"
1514

1615
"github.com/hanwen/go-fuse/v2/fuse"
@@ -287,11 +286,7 @@ func TestParallelDiropsHang(t *testing.T) {
287286
if err != nil {
288287
t.Fatalf("NewLoopbackRoot(%s): %v\n", orig, err)
289288
}
290-
sec := time.Second
291-
opts := &Options{
292-
AttrTimeout: &sec,
293-
EntryTimeout: &sec,
294-
}
289+
opts := DefaultOptions()
295290
opts.Debug = testutil.VerboseTest()
296291

297292
rawFS := NewNodeFS(loopbackRoot, opts)

fs/windows_example_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"log"
1111
"sync"
1212
"syscall"
13-
"time"
1413

1514
"github.com/hanwen/go-fuse/v2/fs"
1615
"github.com/hanwen/go-fuse/v2/fuse"
@@ -107,11 +106,7 @@ func Example_loopbackReuse() {
107106
Path: origDir,
108107
}
109108

110-
sec := time.Second
111-
opts := &fs.Options{
112-
AttrTimeout: &sec,
113-
EntryTimeout: &sec,
114-
}
109+
opts := fs.DefaultOptions()
115110

116111
root := &WindowsNode{
117112
LoopbackNode: &fs.LoopbackNode{

0 commit comments

Comments
 (0)