File: directio_test.go

package info (click to toggle)
golang-github-hanwen-go-fuse 2.1.0%2Bgit20220822.58a7e14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,292 kB
  • sloc: cpp: 78; sh: 43; makefile: 16
file content (96 lines) | stat: -rw-r--r-- 2,281 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
// 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"
	"syscall"
	"testing"

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

type dioRoot struct {
	Inode
}

func (r *dioRoot) OnAdd(ctx context.Context) {
	r.Inode.AddChild("file", r.Inode.NewInode(ctx, &dioFile{}, StableAttr{}), false)
}

// A file handle that pretends that every hole/data starts at
// multiples of 1024
type dioFH struct {
}

var _ = (FileLseeker)((*dioFH)(nil))
var _ = (FileReader)((*dioFH)(nil))

func (f *dioFH) Lseek(ctx context.Context, off uint64, whence uint32) (uint64, syscall.Errno) {
	next := (off + 1023) & (^uint64(1023))
	return next, OK
}

func (fh *dioFH) Read(ctx context.Context, data []byte, off int64) (fuse.ReadResult, syscall.Errno) {
	r := bytes.Repeat([]byte(fmt.Sprintf("%010d", off)), 1+len(data)/10)
	return fuse.ReadResultData(r[:len(data)]), OK
}

// overrides Open so it can return a dioFH file handle
type dioFile struct {
	Inode
}

var _ = (NodeOpener)((*dioFile)(nil))

func (f *dioFile) Open(ctx context.Context, flags uint32) (fh FileHandle, fuseFlags uint32, errno syscall.Errno) {
	return &dioFH{}, fuse.FOPEN_DIRECT_IO, OK
}

// this tests FOPEN_DIRECT_IO (as opposed to O_DIRECTIO)
func TestFUSEDirectIO(t *testing.T) {
	root := &dioRoot{}
	mntDir, server, clean := testMount(t, root, nil)
	defer clean()

	f, err := os.Open(mntDir + "/file")
	if err != nil {
		t.Fatalf("Open %v", err)
	}
	defer f.Close()

	var buf [10]byte
	n, err := f.Read(buf[:])
	if err != nil {
		t.Fatalf("Read %v", err)
	}
	want := bytes.Repeat([]byte{'0'}, 10)
	got := buf[:n]
	if bytes.Compare(got, want) != 0 {
		t.Errorf("got %q want %q", got, want)
	}

	if !server.KernelSettings().SupportsVersion(7, 24) {
		t.Skip("Kernel does not support lseek")
	}
	if n, err := syscall.Seek(int(f.Fd()), 512, _SEEK_DATA); err != nil {
		t.Errorf("Seek: %v", err)
	} else if n != 1024 {
		t.Errorf("seek: got %d, want %d", n, 1024)
	}

	n, err = f.Read(buf[:])
	if err != nil {
		t.Fatalf("Read %v", err)
	}
	want = []byte(fmt.Sprintf("%010d", 1024))
	got = buf[:n]
	if bytes.Compare(got, want) != 0 {
		t.Errorf("got %q want %q", got, want)
	}
}