summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/experimental/sys/dir.go
blob: 0b997cb8fc4db9675c1cb33ba1fb80ad175852a8 (plain)
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
package sys

import (
	"fmt"
	"io/fs"

	"github.com/tetratelabs/wazero/sys"
)

// FileType is fs.FileMode masked on fs.ModeType. For example, zero is a
// regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown.
//
// Note: This is defined by Linux, not POSIX.
type FileType = fs.FileMode

// Dirent is an entry read from a directory via File.Readdir.
//
// # Notes
//
//   - This extends `dirent` defined in POSIX with some fields defined by
//     Linux. See https://man7.org/linux/man-pages/man3/readdir.3.html and
//     https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html
//   - This has a subset of fields defined in sys.Stat_t. Notably, there is no
//     field corresponding to Stat_t.Dev because that value will be constant
//     for all files in a directory. To get the Dev value, call File.Stat on
//     the directory File.Readdir was called on.
type Dirent struct {
	// Ino is the file serial number, or zero if not available. See Ino for
	// more details including impact returning a zero value.
	Ino sys.Inode

	// Name is the base name of the directory entry. Empty is invalid.
	Name string

	// Type is fs.FileMode masked on fs.ModeType. For example, zero is a
	// regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown.
	//
	// Note: This is defined by Linux, not POSIX.
	Type fs.FileMode
}

func (d *Dirent) String() string {
	return fmt.Sprintf("name=%s, type=%v, ino=%d", d.Name, d.Type, d.Ino)
}

// IsDir returns true if the Type is fs.ModeDir.
func (d *Dirent) IsDir() bool {
	return d.Type == fs.ModeDir
}

// DirFile is embeddable to reduce the amount of functions to implement a file.
type DirFile struct{}

// IsAppend implements File.IsAppend
func (DirFile) IsAppend() bool {
	return false
}

// SetAppend implements File.SetAppend
func (DirFile) SetAppend(bool) Errno {
	return EISDIR
}

// IsDir implements File.IsDir
func (DirFile) IsDir() (bool, Errno) {
	return true, 0
}

// Read implements File.Read
func (DirFile) Read([]byte) (int, Errno) {
	return 0, EISDIR
}

// Pread implements File.Pread
func (DirFile) Pread([]byte, int64) (int, Errno) {
	return 0, EISDIR
}

// Write implements File.Write
func (DirFile) Write([]byte) (int, Errno) {
	return 0, EISDIR
}

// Pwrite implements File.Pwrite
func (DirFile) Pwrite([]byte, int64) (int, Errno) {
	return 0, EISDIR
}

// Truncate implements File.Truncate
func (DirFile) Truncate(int64) Errno {
	return EISDIR
}