forked from karelbilek/wazero-fs-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
248 lines (212 loc) · 5.37 KB
/
file.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package wraplogfs
import (
"fmt"
"io"
"log"
expsys "github.com/tetratelabs/wazero/experimental/sys"
wasys "github.com/tetratelabs/wazero/sys"
)
// fileWithLog implements expsys.File that is instrumented with logging
type fileWithLog struct {
stdlog *log.Logger
base expsys.File
writeBytes bool
name string
fsName string
}
func (d fileWithLog) log(nm string) func(format string, params ...any) {
return func(format string, params ...any) {
txt := fmt.Sprintf(format, params...)
txt = fmt.Sprintf("WrapLogFile %s %s %s: %s", d.fsName, d.name, nm, txt)
d.stdlog.Println(txt)
}
}
// Close implements expsys.File
func (d fileWithLog) Close() (e1 expsys.Errno) {
l := d.log("Chmod")
l("calling with params: <>")
defer func() {
l("returned results: %s", e1)
}()
return d.base.Close()
}
// Datasync implements expsys.File
func (d fileWithLog) Datasync() (e1 expsys.Errno) {
l := d.log("Datasync")
l("calling with params: <>")
defer func() {
l("returned results: %s", e1)
}()
return d.base.Datasync()
}
// Dev implements expsys.File
func (d fileWithLog) Dev() (u1 uint64, e1 expsys.Errno) {
l := d.log("Dev")
l("calling with params: <>")
defer func() {
l("returned results: %s %s", u1, e1)
}()
return d.base.Dev()
}
// Ino implements expsys.File
func (d fileWithLog) Ino() (i1 wasys.Inode, e1 expsys.Errno) {
l := d.log("Ino")
l("calling with params: <>")
defer func() {
l("returned results: %s %s", i1, e1)
}()
return d.base.Ino()
}
// IsAppend implements expsys.File
func (d fileWithLog) IsAppend() (b1 bool) {
l := d.log("IsAppend")
l("calling with params: <>")
defer func() {
l("returned results: %t", b1)
}()
return d.base.IsAppend()
}
// IsDir implements expsys.File
func (d fileWithLog) IsDir() (b1 bool, e1 expsys.Errno) {
l := d.log("IsDir")
l("calling with params: <>")
defer func() {
l("returned results: %t %s", b1, e1)
}()
return d.base.IsDir()
}
// Pread implements expsys.File
func (d fileWithLog) Pread(buf []byte, off int64) (n int, errno expsys.Errno) {
l := d.log("Pread")
if d.writeBytes {
l("calling with params: %v %d", buf, off)
} else {
l("calling with params: (none) %d", off)
}
defer func() {
if d.writeBytes {
l("returned results: %d %s, buffer: %v", n, errno, buf)
} else {
l("returned results: %d %s", n, errno)
}
}()
return d.base.Pread(buf, off)
}
// Pwrite implements expsys.File
func (d fileWithLog) Pwrite(buf []byte, off int64) (n int, errno expsys.Errno) {
l := d.log("Pwrite")
if d.writeBytes {
l("calling with params: %v %d", buf, off)
} else {
l("calling with params: (none) %d", off)
}
defer func() {
l("returned results: %d %s", n, errno)
}()
return d.base.Pwrite(buf, off)
}
// Read implements expsys.File
func (d fileWithLog) Read(buf []byte) (n int, errno expsys.Errno) {
l := d.log("Read")
if d.writeBytes {
l("calling with params: %v", buf)
} else {
l("calling with params: (none)")
}
defer func() {
if d.writeBytes {
l("returned results: %d %s, buffer: %v", n, errno, buf)
} else {
l("returned results: %d %s", n, errno)
}
}()
return d.base.Read(buf)
}
// Readdir implements expsys.File
func (d fileWithLog) Readdir(n int) (dirents []expsys.Dirent, errno expsys.Errno) {
l := d.log("Readdir")
l("calling with params: %d", n)
defer func() {
l("returned results: %+v %s", dirents, errno)
}()
return d.base.Readdir(n)
}
func printWhence(whence int) string {
switch whence {
case io.SeekStart:
return "SeekStart"
case io.SeekEnd:
return "SeekEnd"
case io.SeekCurrent:
return "SeekCurrent"
default:
return fmt.Sprintf("Invalid whence (%d)", whence)
}
}
// Seek implements expsys.File
func (d fileWithLog) Seek(offset int64, whence int) (newOffset int64, errno expsys.Errno) {
l := d.log("Seek")
l("calling with params: %d %s", offset, printWhence(whence))
defer func() {
l("returned results: %d %s", newOffset, errno)
}()
return d.base.Seek(offset, whence)
}
// SetAppend implements expsys.File
func (d fileWithLog) SetAppend(enable bool) (e1 expsys.Errno) {
l := d.log("SetAppend")
l("calling with params: %t", enable)
defer func() {
l("returned results: %d %s", e1)
}()
return d.base.SetAppend(enable)
}
// Stat implements expsys.File
func (d fileWithLog) Stat() (s1 wasys.Stat_t, e1 expsys.Errno) {
l := d.log("Stat")
l("calling with params: <>")
defer func() {
l("returned results: %+v %s", s1, e1)
}()
return d.base.Stat()
}
// Sync implements expsys.File
func (d fileWithLog) Sync() (e1 expsys.Errno) {
l := d.log("Sync")
l("calling with params: <>")
defer func() {
l("returned results: %s", e1)
}()
return d.base.Sync()
}
// Truncate implements expsys.File
func (d fileWithLog) Truncate(size int64) (e1 expsys.Errno) {
l := d.log("Truncate")
l("calling with params: %d", size)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Truncate(size)
}
// Utimens implements expsys.File
func (d fileWithLog) Utimens(atim int64, mtim int64) (e1 expsys.Errno) {
l := d.log("Utimens")
l("calling with params: %d %d", atim, mtim)
defer func() {
l("returned results: %s", e1)
}()
return d.base.Utimens(atim, mtim)
}
// Write implements expsys.File
func (d fileWithLog) Write(buf []byte) (n int, errno expsys.Errno) {
l := d.log("Write")
if d.writeBytes {
l("calling with params: %v", buf)
} else {
l("calling with params: (none)")
}
defer func() {
l("returned results: %d %s", n, errno)
}()
return d.base.Write(buf)
}