File: cache_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: cpp: 78; sh: 45; makefile: 16
file content (240 lines) | stat: -rw-r--r-- 5,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// 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 fs

import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"sync"
	"syscall"
	"testing"
	"time"

	"github.com/hanwen/go-fuse/v2/fuse"
	"github.com/hanwen/go-fuse/v2/internal/testutil"
)

type keepCacheFile struct {
	Inode
	keepCache bool

	mu      sync.Mutex
	content []byte
	count   int
}

var _ = (NodeReader)((*keepCacheFile)(nil))
var _ = (NodeOpener)((*keepCacheFile)(nil))
var _ = (NodeGetattrer)((*keepCacheFile)(nil))

func (f *keepCacheFile) setContent(delta int) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.count += delta
	f.content = []byte(fmt.Sprintf("%010x", f.count))
}

func (f *keepCacheFile) Open(ctx context.Context, flags uint32) (FileHandle, uint32, syscall.Errno) {
	var fl uint32
	if f.keepCache {
		fl = fuse.FOPEN_KEEP_CACHE
	}

	f.setContent(0)
	return nil, fl, OK
}

func (f *keepCacheFile) Getattr(ctx context.Context, fh FileHandle, out *fuse.AttrOut) syscall.Errno {
	f.mu.Lock()
	defer f.mu.Unlock()
	out.Size = uint64(len(f.content))

	return OK
}

func (f *keepCacheFile) Read(ctx context.Context, fh FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
	f.setContent(1)

	f.mu.Lock()
	defer f.mu.Unlock()

	return fuse.ReadResultData(f.content[off:]), OK
}

type keepCacheRoot struct {
	Inode

	keep, nokeep *keepCacheFile
}

var _ = (NodeOnAdder)((*keepCacheRoot)(nil))

func (r *keepCacheRoot) OnAdd(ctx context.Context) {
	i := &r.Inode

	r.keep = &keepCacheFile{
		keepCache: true,
	}
	r.keep.setContent(0)
	i.AddChild("keep", i.NewInode(ctx, r.keep, StableAttr{}), true)

	r.nokeep = &keepCacheFile{
		keepCache: false,
	}
	r.nokeep.setContent(0)
	i.AddChild("nokeep", i.NewInode(ctx, r.nokeep, StableAttr{}), true)
}

// Test FOPEN_KEEP_CACHE. This is a little subtle: the automatic cache
// invalidation triggers if mtime or file size is changed, so only
// change content but no metadata.
func TestKeepCache(t *testing.T) {
	root := &keepCacheRoot{}
	mntDir, _ := testMount(t, root, nil)

	c1, err := ioutil.ReadFile(mntDir + "/keep")
	if err != nil {
		t.Fatalf("read keep 1: %v", err)
	}

	c2, err := ioutil.ReadFile(mntDir + "/keep")
	if err != nil {
		t.Fatalf("read keep 2: %v", err)
	}

	if bytes.Compare(c1, c2) != 0 {
		t.Errorf("keep read 2 got %q want read 1 %q", c2, c1)
	}

	if s := root.keep.Inode.NotifyContent(0, 100); s != OK {
		t.Errorf("NotifyContent: %v", s)
	}

	c3, err := ioutil.ReadFile(mntDir + "/keep")
	if err != nil {
		t.Fatalf("read keep 3: %v", err)
	}
	if bytes.Compare(c2, c3) == 0 {
		t.Errorf("keep read 3 got %q want different", c3)
	}

	nc1, err := ioutil.ReadFile(mntDir + "/nokeep")
	if err != nil {
		t.Fatalf("read keep 1: %v", err)
	}

	nc2, err := ioutil.ReadFile(mntDir + "/nokeep")
	if err != nil {
		t.Fatalf("read keep 2: %v", err)
	}

	if bytes.Compare(nc1, nc2) == 0 {
		t.Errorf("nokeep read 2 got %q want read 1 %q", c2, c1)
	}
}

type countingSymlink struct {
	Inode

	mu        sync.Mutex
	readCount int
	data      []byte
}

var _ = (NodeGetattrer)((*countingSymlink)(nil))

func (l *countingSymlink) Getattr(ctx context.Context, fh FileHandle, out *fuse.AttrOut) syscall.Errno {
	l.mu.Lock()
	defer l.mu.Unlock()
	out.Attr.Size = uint64(len(l.data))
	return 0
}

var _ = (NodeReadlinker)((*countingSymlink)(nil))

func (l *countingSymlink) Readlink(ctx context.Context) ([]byte, syscall.Errno) {
	l.mu.Lock()
	defer l.mu.Unlock()
	l.readCount++
	return l.data, 0
}

func (l *countingSymlink) count() int {
	l.mu.Lock()
	defer l.mu.Unlock()
	return l.readCount
}

func TestSymlinkCaching(t *testing.T) {
	mnt := t.TempDir()
	want := "target"
	link := countingSymlink{
		data: []byte(want),
	}
	sz := len(link.data)
	root := &Inode{}
	dt := 10 * time.Millisecond
	opts := &Options{
		EntryTimeout: &dt,
		AttrTimeout:  &dt,
		OnAdd: func(ctx context.Context) {
			root.AddChild("link",
				root.NewPersistentInode(ctx, &link, StableAttr{Mode: syscall.S_IFLNK}), false)
		},
	}
	opts.Debug = testutil.VerboseTest()
	opts.EnableSymlinkCaching = true

	server, err := Mount(mnt, root, opts)
	if err != nil {
		t.Fatal(err)
	}
	defer server.Unmount()

	for i := 0; i < 2; i++ {
		if got, err := os.Readlink(mnt + "/link"); err != nil {
			t.Fatal(err)
		} else if got != want {
			t.Fatalf("got %q want %q", got, want)
		}
	}

	if c := link.count(); c != 1 {
		t.Errorf("got %d want 1", c)
	}

	if errno := link.NotifyContent(0, int64(sz)); errno != 0 {
		t.Fatalf("NotifyContent: %v", errno)
	}
	if _, err := os.Readlink(mnt + "/link"); err != nil {
		t.Fatal(err)
	}

	if c := link.count(); c != 2 {
		t.Errorf("got %d want 2", c)
	}

	// The actual test goes till here. The below is just to
	// clarify behavior of the feature: changed attributes do not
	// trigger reread, and the Attr.Size is used to truncate a
	// previous read result.
	link.mu.Lock()
	link.data = []byte("x")
	link.mu.Unlock()

	time.Sleep((3 * dt) / 2)
	if l, err := os.Readlink(mnt + "/link"); err != nil {
		t.Fatal(err)
	} else if l != want[:1] {
		log.Printf("got %q want %q", l, want[:1])
	}
	if c := link.count(); c != 2 {
		t.Errorf("got %d want 2", c)
	}
}