-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathloggingfs_test.go
149 lines (137 loc) · 2.98 KB
/
loggingfs_test.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
package extract_test
import (
"fmt"
"os"
)
// LoggingFS is a disk that logs every operation, useful for unit-testing.
type LoggingFS struct {
Journal []*LoggedOp
}
// LoggedOp is an operation logged in a LoggingFS journal.
type LoggedOp struct {
Op string
Path string
OldPath string
Mode os.FileMode
Info os.FileInfo
Flags int
Err error
}
func (op *LoggedOp) String() string {
res := ""
switch op.Op {
case "link":
res += fmt.Sprintf("link %s -> %s", op.Path, op.OldPath)
case "symlink":
res += fmt.Sprintf("symlink %s -> %s", op.Path, op.OldPath)
case "mkdirall":
res += fmt.Sprintf("mkdirall %v %s", op.Mode, op.Path)
case "open":
res += fmt.Sprintf("open %v %s (flags=%04x)", op.Mode, op.Path, op.Flags)
case "remove":
res += fmt.Sprintf("remove %v", op.Path)
case "stat":
res += fmt.Sprintf("stat %v -> %v", op.Path, op.Info)
case "chmod":
res += fmt.Sprintf("chmod %v %s", op.Mode, op.Path)
default:
panic("unknown LoggedOP " + op.Op)
}
if op.Err != nil {
res += " error: " + op.Err.Error()
} else {
res += " success"
}
return res
}
func (m *LoggingFS) Link(oldname, newname string) error {
err := os.Link(oldname, newname)
op := &LoggedOp{
Op: "link",
OldPath: oldname,
Path: newname,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return err
}
func (m *LoggingFS) MkdirAll(path string, perm os.FileMode) error {
err := os.MkdirAll(path, perm)
op := &LoggedOp{
Op: "mkdirall",
Path: path,
Mode: perm,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return err
}
func (m *LoggingFS) Symlink(oldname, newname string) error {
err := os.Symlink(oldname, newname)
op := &LoggedOp{
Op: "symlink",
OldPath: oldname,
Path: newname,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return err
}
func (m *LoggingFS) OpenFile(name string, flags int, perm os.FileMode) (*os.File, error) {
f, err := os.OpenFile(name, flags, perm)
op := &LoggedOp{
Op: "open",
Path: name,
Mode: perm,
Flags: flags,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return f, err
}
func (m *LoggingFS) Remove(path string) error {
err := os.Remove(path)
op := &LoggedOp{
Op: "remove",
Path: path,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return err
}
func (m *LoggingFS) Stat(path string) (os.FileInfo, error) {
info, err := os.Stat(path)
op := &LoggedOp{
Op: "stat",
Path: path,
Info: info,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return info, err
}
func (m *LoggingFS) Chmod(path string, mode os.FileMode) error {
err := os.Chmod(path, mode)
op := &LoggedOp{
Op: "chmod",
Path: path,
Mode: mode,
Err: err,
}
m.Journal = append(m.Journal, op)
fmt.Println("FS>", op)
return err
}
func (m *LoggingFS) String() string {
res := ""
for _, op := range m.Journal {
res += op.String()
res += "\n"
}
return res
}