-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
213 lines (182 loc) · 5.14 KB
/
fs.go
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
// wraplogfs is a wazero filesystem that wraps another, existing filesystem, and logs
// all inputs/outputs to writer.
//
// Was first generated by hexdigest/gowrap, but then edited for better outputs in some cases
package wraplogfs
import (
"fmt"
"io"
"io/fs"
"log"
"strings"
expsys "github.com/tetratelabs/wazero/experimental/sys"
wasys "github.com/tetratelabs/wazero/sys"
)
type fsWithLog struct {
// intentionally does NOT embed unimplemented; I do NOT want to be forward-compatible;
// I want to break on missing funcs
stdlog *log.Logger
base expsys.FS
writeBytes bool
fsName string
}
// New returns a new filesystem on top of another filesystem.
// writeBytes controls if all bytes are written on stdout on reads/writes, or just "(data)".
func New(base expsys.FS, stdout io.Writer, writeBytes bool, name string) expsys.FS {
return fsWithLog{
base: base,
stdlog: log.New(stdout, "", log.LstdFlags),
writeBytes: writeBytes,
fsName: name,
}
}
func (d fsWithLog) log(nm string) func(format string, params ...any) {
return func(format string, params ...any) {
txt := fmt.Sprintf(format, params...)
txt = fmt.Sprintf("WrapLogFS %s %s: %s", d.fsName, nm, txt)
d.stdlog.Println(txt)
}
}
// Chmod implements sys.FS
func (d fsWithLog) Chmod(path string, perm fs.FileMode) (e1 expsys.Errno) {
l := d.log("Chmod")
l("calling with params: %q %s", path, perm)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Chmod(path, perm)
}
// Link implements sys.FS
func (d fsWithLog) Link(oldPath string, newPath string) (e1 expsys.Errno) {
l := d.log("Link")
l("calling with params: %q %q", oldPath, newPath)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Link(oldPath, newPath)
}
// Lstat implements sys.FS
func (d fsWithLog) Lstat(path string) (s1 wasys.Stat_t, e1 expsys.Errno) {
l := d.log("Lstat")
l("calling with params: %q", path)
defer func() {
l("returned results: %+v %s", s1, e1)
}()
return d.base.Lstat(path)
}
// Mkdir implements sys.FS
func (d fsWithLog) Mkdir(path string, perm fs.FileMode) (e1 expsys.Errno) {
l := d.log("Mkdir")
l("calling with params: %q %s", path, perm)
defer func() {
l("returned results: %+v %s", e1)
}()
return d.base.Mkdir(path, perm)
}
func printOflags(flag expsys.Oflag) string {
st := []string{}
flags := map[expsys.Oflag]string{
expsys.O_RDONLY: "O_RDONLY",
expsys.O_RDWR: "O_RDWR",
expsys.O_WRONLY: "O_WRONLY",
expsys.O_APPEND: "O_APPEND",
expsys.O_CREAT: "O_CREAT",
expsys.O_DIRECTORY: "O_DIRECTORY",
expsys.O_DSYNC: "O_DSYNC",
expsys.O_EXCL: "O_EXCL",
expsys.O_NOFOLLOW: "O_NOFOLLOW",
expsys.O_NONBLOCK: "O_NONBLOCK",
expsys.O_RSYNC: "O_RSYNC",
expsys.O_SYNC: "O_SYNC",
expsys.O_TRUNC: "O_TRUNC",
}
for f, d := range flags {
if flag&f != 0 {
st = append(st, d)
}
}
if len(st) == 0 {
return "(none)"
} else {
return strings.Join(st, "|")
}
}
// OpenFile implements sys.FS
func (d fsWithLog) OpenFile(path string, flag expsys.Oflag, perm fs.FileMode) (f1 expsys.File, e1 expsys.Errno) {
l := d.log("OpenFile")
l("calling with params: %q %s; %s", path, printOflags(flag), perm)
defer func() {
l("returned results: %T %+v %s", f1, f1, e1)
}()
fl, errno := d.base.OpenFile(path, flag, perm)
return fileWithLog{
base: fl,
stdlog: d.stdlog,
writeBytes: d.writeBytes,
name: path,
fsName: d.fsName,
}, errno
}
// Readlink implements sys.FS
func (d fsWithLog) Readlink(path string) (s1 string, e1 expsys.Errno) {
l := d.log("Readlink")
l("calling with params: %q", path)
defer func() {
l("returned results: %q %s", s1, e1)
}()
return d.base.Readlink(path)
}
// Rename implements sys.FS
func (d fsWithLog) Rename(from string, to string) (e1 expsys.Errno) {
l := d.log("Rename")
l("calling with params: %q %q", from, to)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Rename(from, to)
}
// Rmdir implements sys.FS
func (d fsWithLog) Rmdir(path string) (e1 expsys.Errno) {
l := d.log("Rmdir")
l("calling with params: %q", path)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Rmdir(path)
}
// Stat implements sys.FS
func (d fsWithLog) Stat(path string) (s1 wasys.Stat_t, e1 expsys.Errno) {
l := d.log("Stat")
l("calling with params: %q", path)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Stat(path)
}
// Symlink implements sys.FS
func (d fsWithLog) Symlink(oldPath string, linkName string) (e1 expsys.Errno) {
l := d.log("Symlink")
l("calling with params: %q %q", oldPath, linkName)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Symlink(oldPath, linkName)
}
// Unlink implements sys.FS
func (d fsWithLog) Unlink(path string) (e1 expsys.Errno) {
l := d.log("Unlink")
l("calling with params: %q", path)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Unlink(path)
}
// Utimens implements sys.FS
func (d fsWithLog) Utimens(path string, atim int64, mtim int64) (e1 expsys.Errno) {
l := d.log("Symlink")
l("calling with params: %q %d %d", path, atim, mtim)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Utimens(path, atim, mtim)
}