File: dircache_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,584 kB
  • sloc: cpp: 78; sh: 47; makefile: 16
file content (104 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (2)
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
// Copyright 2023 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 (
	"context"
	"fmt"
	"reflect"
	"sync"
	"syscall"
	"testing"

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

type dirCacheTestNode struct {
	Inode
	mu          sync.Mutex
	openCount   int
	cachedReads int
}

var _ = (NodeOpendirHandler)((*dirCacheTestNode)(nil))

type countingReaddirenter struct {
	FileReaddirenter
	*dirCacheTestNode
}

func (r *countingReaddirenter) Readdirent(ctx context.Context) (*fuse.DirEntry, syscall.Errno) {
	de, errno := r.FileReaddirenter.Readdirent(ctx)
	r.dirCacheTestNode.mu.Lock()
	defer r.dirCacheTestNode.mu.Unlock()
	if r.dirCacheTestNode.openCount > 1 {
		r.dirCacheTestNode.cachedReads++
	}
	return de, errno
}

func (n *dirCacheTestNode) OpendirHandle(ctx context.Context, flags uint32) (FileHandle, uint32, syscall.Errno) {
	n.mu.Lock()
	defer n.mu.Unlock()

	n.openCount++
	var entries []fuse.DirEntry

	for i := 0; i < 1024; i++ {
		entries = append(entries, fuse.DirEntry{
			Name: fmt.Sprintf("file%04d", i),
			Ino:  uint64(i + 42),
			Mode: fuse.S_IFREG,
		})
	}
	return &countingReaddirenter{&dirStreamAsFile{
		creator: func(context.Context) (DirStream, syscall.Errno) {
			return NewListDirStream(entries), 0
		},
	}, n}, fuse.FOPEN_CACHE_DIR, 0
}

func TestDirCacheFlag(t *testing.T) {
	root := &dirCacheTestNode{}
	opts := Options{}
	opts.DisableReadDirPlus = true
	mnt, server := testMount(t, root, &opts)

	if !server.KernelSettings().SupportsVersion(7, 28) {
		t.Skip("need v7.28 for directory caching")
	}

	s, errno := NewLoopbackDirStream(mnt)
	if errno != 0 {
		t.Fatalf("NewLoopbackDirStream: %v", errno)
	}

	want, errno := readDirStream(s)
	if errno != 0 {
		t.Fatalf("NewLoopbackDirStream: %v", errno)
	}
	s.Close()

	s, errno = NewLoopbackDirStream(mnt)
	if errno != 0 {
		t.Fatalf("NewLoopbackDirStream: %v", errno)
	}
	got, errno := readDirStream(s)
	if errno != 0 {
		t.Fatalf("NewLoopbackDirStream: %v", errno)
	}
	s.Close()

	if !reflect.DeepEqual(got, want) {
		t.Fatalf("got %v, want %v", got, want)
	}

	root.mu.Lock()
	defer root.mu.Unlock()
	if root.openCount != 2 || root.cachedReads != 0 {
		t.Errorf("got %d, %d want 2,0", root.openCount, root.cachedReads)
	}

}