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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// 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"
"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 := os.ReadFile(mntDir + "/keep")
if err != nil {
t.Fatalf("read keep 1: %v", err)
}
c2, err := os.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 := os.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 := os.ReadFile(mntDir + "/nokeep")
if err != nil {
t.Fatalf("read keep 1: %v", err)
}
nc2, err := os.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] {
t.Logf("got %q want %q", l, want[:1])
}
if c := link.count(); c != 2 {
t.Errorf("got %d want 2", c)
}
}
type autoInvalNode struct {
Inode
mu sync.Mutex
content []byte
mtime time.Time
}
var _ = (NodeOpener)((*autoInvalNode)(nil))
func (f *autoInvalNode) Open(ctx context.Context, openFlags uint32) (fh FileHandle, fuseFlags uint32, errno syscall.Errno) {
return nil, 0, 0
}
var _ = (NodeReader)((*autoInvalNode)(nil))
func (f *autoInvalNode) Read(ctx context.Context, fh FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
f.mu.Lock()
defer f.mu.Unlock()
return fuse.ReadResultData(f.content[off : int(off)+len(dest)]), OK
}
var _ = (NodeGetattrer)((*autoInvalNode)(nil))
func (f *autoInvalNode) Getattr(ctx context.Context, fh FileHandle, out *fuse.AttrOut) syscall.Errno {
f.mu.Lock()
defer f.mu.Unlock()
out.Size = uint64(len(f.content))
out.SetTimes(nil, &f.mtime, nil)
return OK
}
/* Test AUTO_INVAL_DATA */
func TestAutoInvalData(t *testing.T) {
mnt := t.TempDir()
node := autoInvalNode{
content: bytes.Repeat([]byte{'x'}, 4096),
mtime: time.Now(),
}
root := &Inode{}
dt := 10 * time.Millisecond
opts := &Options{
EntryTimeout: &dt,
AttrTimeout: &dt,
OnAdd: func(ctx context.Context) {
root.AddChild("file",
root.NewPersistentInode(ctx, &node, StableAttr{Mode: syscall.S_IFREG}), false)
},
}
opts.Debug = testutil.VerboseTest()
srv, err := Mount(mnt, root, opts)
if err != nil {
t.Fatal(err)
}
defer srv.Unmount()
f, err := os.Open(mnt + "/file")
if err != nil {
t.Fatal(err)
}
defer f.Close()
data := make([]byte, len(node.content))
if _, err := f.Read(data); err != nil {
t.Fatal(err)
}
if data[1] != 'x' {
t.Errorf("got %c want x", data[1])
}
if _, err := f.Seek(0, 0); err != nil {
t.Fatal(err)
}
node.mu.Lock()
node.mtime = node.mtime.Add(time.Hour)
node.content = bytes.Repeat([]byte{'y'}, 4096)
node.mu.Unlock()
if _, err := f.Stat(); err != nil {
t.Fatal(err)
}
if n, err := f.Read(data); err != nil || len(data) != n {
t.Fatalf("Read: %d, %v", n, err)
}
if data[1] != 'y' {
t.Errorf("got %c want y", data[1])
}
}
|