-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
220 lines (188 loc) · 5.55 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
package hookfs
import (
"fmt"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
log "github.com/sirupsen/logrus"
)
type hookFile struct {
file nodefs.File
name string
hook Hook
}
func newHookFile(file nodefs.File, name string, hook Hook) (*hookFile, error) {
log.WithFields(log.Fields{
"file": file,
"name": name,
}).Debug("Hooking a file")
hookfile := &hookFile{
file: file,
name: name,
hook: hook,
}
return hookfile, nil
}
// implements nodefs.File
func (h *hookFile) SetInode(inode *nodefs.Inode) {
h.file.SetInode(inode)
}
// implements nodefs.File
func (h *hookFile) String() string {
return fmt.Sprintf("HookFile{file=%s, name=%s}", h.file.String(), h.name)
}
// implements nodefs.File
func (h *hookFile) InnerFile() nodefs.File {
return h.file.InnerFile()
}
// implements nodefs.File
func (h *hookFile) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
hook, hookEnabled := h.hook.(HookOnRead)
var prehookBuf, posthookBuf []byte
var prehookErr, posthookErr error
var prehooked, posthooked bool
var prehookCtx HookContext
if hookEnabled {
prehookBuf, prehooked, prehookCtx, prehookErr = hook.PreRead(h.name, int64(len(dest)), off)
if prehooked {
log.WithFields(log.Fields{
"h": h,
// "prehookBuf": prehookBuf,
"prehookErr": prehookErr,
"prehookCtx": prehookCtx,
}).Debug("Read: Prehooked")
return fuse.ReadResultData(prehookBuf), fuse.ToStatus(prehookErr)
}
}
lowerRR, lowerCode := h.file.Read(dest, off)
if hookEnabled {
lowerRRBuf, lowerRRBufStatus := lowerRR.Bytes(make([]byte, lowerRR.Size()))
if lowerRRBufStatus != fuse.OK {
log.WithField("error", lowerRRBufStatus).Panic("lowerRR.Bytes() should not cause an error")
}
posthookBuf, posthooked, posthookErr = hook.PostRead(int32(lowerCode), lowerRRBuf, prehookCtx)
if posthooked {
if len(posthookBuf) != len(lowerRRBuf) {
log.WithFields(log.Fields{
"h": h,
// "posthookBuf": posthookBuf,
"posthookErr": posthookErr,
"posthookBufLen": len(posthookBuf),
"lowerRRBufLen": len(lowerRRBuf),
"destLen": len(dest),
}).Warn("Read: Posthooked, but posthookBuf length != lowerrRRBuf length. You may get a strange behavior.")
}
log.WithFields(log.Fields{
"h": h,
// "posthookBuf": posthookBuf,
"posthookErr": posthookErr,
}).Debug("Read: Posthooked")
return fuse.ReadResultData(posthookBuf), fuse.ToStatus(posthookErr)
}
}
return lowerRR, lowerCode
}
// implements nodefs.File
func (h *hookFile) Write(data []byte, off int64) (uint32, fuse.Status) {
hook, hookEnabled := h.hook.(HookOnWrite)
var prehookErr, posthookErr error
var prehooked, posthooked bool
var prehookCtx HookContext
if hookEnabled {
prehooked, prehookCtx, prehookErr = hook.PreWrite(h.name, data, off)
if prehooked {
log.WithFields(log.Fields{
"h": h,
"prehookErr": prehookErr,
"prehookCtx": prehookCtx,
}).Debug("Write: Prehooked")
return 0, fuse.ToStatus(prehookErr)
}
}
lowerWritten, lowerCode := h.file.Write(data, off)
if hookEnabled {
posthooked, posthookErr = hook.PostWrite(int32(lowerCode), prehookCtx)
if posthooked {
log.WithFields(log.Fields{
"h": h,
"posthookErr": posthookErr,
}).Debug("Write: Posthooked")
return 0, fuse.ToStatus(posthookErr)
}
}
return lowerWritten, lowerCode
}
// implements nodefs.File
func (h *hookFile) Flush() fuse.Status {
return h.file.Flush()
}
// implements nodefs.File
func (h *hookFile) Release() {
h.file.Release()
}
// implements nodefs.File
func (h *hookFile) Fsync(flags int) fuse.Status {
hook, hookEnabled := h.hook.(HookOnFsync)
var prehookErr, posthookErr error
var prehooked, posthooked bool
var prehookCtx HookContext
if hookEnabled {
prehooked, prehookCtx, prehookErr = hook.PreFsync(h.name, uint32(flags))
if prehooked {
log.WithFields(log.Fields{
"h": h,
"prehookErr": prehookErr,
"prehookCtx": prehookCtx,
}).Debug("Fsync: Prehooked")
return fuse.ToStatus(prehookErr)
}
}
lowerCode := h.file.Fsync(flags)
if hookEnabled {
posthooked, posthookErr = hook.PostFsync(int32(lowerCode), prehookCtx)
if posthooked {
log.WithFields(log.Fields{
"h": h,
"posthookErr": posthookErr,
}).Debug("Fsync: Posthooked")
return fuse.ToStatus(posthookErr)
}
}
return lowerCode
}
// implements nodefs.File
func (h *hookFile) Truncate(size uint64) fuse.Status {
return h.file.Truncate(size)
}
// implements nodefs.File
func (h *hookFile) GetAttr(out *fuse.Attr) fuse.Status {
return h.file.GetAttr(out)
}
// implements nodefs.File
func (h *hookFile) Chown(uid uint32, gid uint32) fuse.Status {
return h.file.Chown(uid, gid)
}
// implements nodefs.File
func (h *hookFile) Chmod(perms uint32) fuse.Status {
return h.file.Chmod(perms)
}
// implements nodefs.File
func (h *hookFile) Utimens(atime *time.Time, mtime *time.Time) fuse.Status {
return h.file.Utimens(atime, mtime)
}
// implements nodefs.File
func (h *hookFile) Allocate(off uint64, size uint64, mode uint32) fuse.Status {
return h.file.Allocate(off, size, mode)
}
//implements nodefs,GetLk
func (h *hookFile) GetLk(owner uint64, lk *fuse.FileLock, flags uint32, out *fuse.FileLock) (code fuse.Status) {
return h.file.GetLk(owner, lk, flags, out)
}
//implements nodefs.SetLk
func (h *hookFile) SetLk(owner uint64, lk *fuse.FileLock, flags uint32) (code fuse.Status) {
return h.file.SetLk(owner, lk, flags)
}
//implements nodefs,SetLkw
func (h *hookFile) SetLkw(owner uint64, lk *fuse.FileLock, flags uint32) (code fuse.Status) {
return h.file.SetLkw(owner, lk, flags)
}