github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/experimental/sys/dir.go (about) 1 package sys 2 3 import ( 4 "fmt" 5 "io/fs" 6 7 "github.com/wasilibs/wazerox/sys" 8 ) 9 10 // FileType is fs.FileMode masked on fs.ModeType. For example, zero is a 11 // regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown. 12 // 13 // Note: This is defined by Linux, not POSIX. 14 type FileType = fs.FileMode 15 16 // Dirent is an entry read from a directory via File.Readdir. 17 // 18 // # Notes 19 // 20 // - This extends `dirent` defined in POSIX with some fields defined by 21 // Linux. See https://man7.org/linux/man-pages/man3/readdir.3.html and 22 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html 23 // - This has a subset of fields defined in sys.Stat_t. Notably, there is no 24 // field corresponding to Stat_t.Dev because that value will be constant 25 // for all files in a directory. To get the Dev value, call File.Stat on 26 // the directory File.Readdir was called on. 27 type Dirent struct { 28 // Ino is the file serial number, or zero if not available. See Ino for 29 // more details including impact returning a zero value. 30 Ino sys.Inode 31 32 // Name is the base name of the directory entry. Empty is invalid. 33 Name string 34 35 // Type is fs.FileMode masked on fs.ModeType. For example, zero is a 36 // regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown. 37 // 38 // Note: This is defined by Linux, not POSIX. 39 Type fs.FileMode 40 } 41 42 func (d *Dirent) String() string { 43 return fmt.Sprintf("name=%s, type=%v, ino=%d", d.Name, d.Type, d.Ino) 44 } 45 46 // IsDir returns true if the Type is fs.ModeDir. 47 func (d *Dirent) IsDir() bool { 48 return d.Type == fs.ModeDir 49 } 50 51 // DirFile is embeddable to reduce the amount of functions to implement a file. 52 type DirFile struct{} 53 54 // IsAppend implements File.IsAppend 55 func (DirFile) IsAppend() bool { 56 return false 57 } 58 59 // SetAppend implements File.SetAppend 60 func (DirFile) SetAppend(bool) Errno { 61 return EISDIR 62 } 63 64 // IsDir implements File.IsDir 65 func (DirFile) IsDir() (bool, Errno) { 66 return true, 0 67 } 68 69 // Read implements File.Read 70 func (DirFile) Read([]byte) (int, Errno) { 71 return 0, EISDIR 72 } 73 74 // Pread implements File.Pread 75 func (DirFile) Pread([]byte, int64) (int, Errno) { 76 return 0, EISDIR 77 } 78 79 // Write implements File.Write 80 func (DirFile) Write([]byte) (int, Errno) { 81 return 0, EISDIR 82 } 83 84 // Pwrite implements File.Pwrite 85 func (DirFile) Pwrite([]byte, int64) (int, Errno) { 86 return 0, EISDIR 87 } 88 89 // Truncate implements File.Truncate 90 func (DirFile) Truncate(int64) Errno { 91 return EISDIR 92 }