-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fscache.go
319 lines (277 loc) · 5.92 KB
/
fscache.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package fscache
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/gob"
"hash"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/syndtr/goleveldb/leveldb"
)
type FileInfo struct {
Name string
Size int64
Mode os.FileMode
ModTime time.Time
Dev uint64
Ino uint64
Uid uint64
Gid uint64
}
func NewFileInfoFromStat(stat fs.FileInfo) FileInfo {
return FileInfo{
Name: stat.Name(),
Size: stat.Size(),
Mode: stat.Mode(),
ModTime: stat.ModTime(),
Dev: uint64(stat.Sys().(*syscall.Stat_t).Dev),
Ino: uint64(stat.Sys().(*syscall.Stat_t).Ino),
Uid: uint64(stat.Sys().(*syscall.Stat_t).Uid),
Gid: uint64(stat.Sys().(*syscall.Stat_t).Gid),
}
}
type FSCache struct {
dirname string
db *leveldb.DB
hasher hash.Hash
}
func NewFSCache() (*FSCache, error) {
dname, err := os.MkdirTemp("", "plakar-vfs-")
if err != nil {
return nil, err
}
db, err := leveldb.OpenFile(dname, nil)
if err != nil {
return nil, err
}
return &FSCache{
dirname: dname,
db: db,
hasher: sha256.New(),
}, err
}
func FromBytes(data []byte) (*FSCache, error) {
dname, err := os.MkdirTemp("", "plakar-vfs-")
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
gr, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
return nil, err
}
target := filepath.Join(dname, hdr.Name)
switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(target, os.FileMode(hdr.Mode)); err != nil {
return nil, err
}
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
if err != nil {
return nil, err
}
if _, err := io.Copy(f, tr); err != nil {
f.Close()
return nil, err
}
f.Close()
}
}
db, err := leveldb.OpenFile(dname, nil)
if err != nil {
return nil, err
}
return &FSCache{
dirname: dname,
db: db,
hasher: sha256.New(),
}, err
}
func (fsc *FSCache) Checksum() []byte {
return fsc.hasher.Sum(nil)
}
func (fsc *FSCache) Serialize() ([]byte, error) {
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
tw := tar.NewWriter(gw)
err := filepath.Walk(fsc.dirname, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
hdr, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}
hdr.Name = filepath.ToSlash(file[len(fsc.dirname):])
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if !fi.Mode().IsRegular() {
return nil
}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(tw, f); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, err
}
if err := gw.Close(); err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (fsc *FSCache) Close() error {
if err := fsc.db.Close(); err != nil {
return err
}
return os.RemoveAll(fsc.dirname)
}
func (fsc *FSCache) Record(path string, fileinfo fs.FileInfo) error {
fi := NewFileInfoFromStat(fileinfo)
var fibuf bytes.Buffer
err := gob.NewEncoder(&fibuf).Encode(fi)
if err != nil {
return err
}
storedPath := path
if fi.Mode.IsDir() {
if path != "/" {
storedPath = path + string(filepath.Separator)
}
}
fsc.hasher.Write([]byte(storedPath))
fsc.hasher.Write(fibuf.Bytes())
return fsc.db.Put([]byte(storedPath), fibuf.Bytes(), nil)
}
func (fsc *FSCache) Scan() <-chan string {
ch := make(chan string)
go func() {
iter := fsc.db.NewIterator(nil, nil)
for iter.Next() {
var key string
if iter.Key()[len(iter.Key())-1] == '/' {
if string(iter.Key()) != "/" {
key = string(iter.Key()[:len(iter.Key())-1])
} else {
key = "/"
}
} else {
key = string(iter.Key())
}
ch <- key
}
close(ch)
}()
return ch
}
func (fsc *FSCache) Directories() <-chan string {
ch := make(chan string)
go func() {
iter := fsc.db.NewIterator(nil, nil)
for iter.Next() {
if iter.Key()[len(iter.Key())-1] == '/' {
var key string
if string(iter.Key()) != "/" {
key = string(iter.Key()[:len(iter.Key())-1])
} else {
key = "/"
}
ch <- key
}
}
close(ch)
}()
return ch
}
func (fsc *FSCache) Children(path string) (<-chan string, error) {
storedPath := path
if path != "/" {
storedPath = path + string(filepath.Separator)
}
_, err := fsc.db.Get([]byte(storedPath), nil)
if err != nil {
return nil, err
}
ch := make(chan string)
go func() {
defer close(ch)
prefix := []byte(storedPath)
iter := fsc.db.NewIterator(nil, nil)
defer iter.Release()
for iter.Seek(prefix); iter.Valid() && bytes.HasPrefix(iter.Key(), prefix); iter.Next() {
key := iter.Key()
relativePath := string(key[len(prefix):])
atoms := strings.Split(relativePath, string(filepath.Separator))
if len(atoms) == 1 && atoms[0] == "" {
continue
} else if len(atoms) == 1 || (len(atoms) == 2 && atoms[1] == "") {
ch <- atoms[0]
} else {
break
}
}
}()
return ch, nil
}
func (fsc *FSCache) Files() <-chan string {
ch := make(chan string)
go func() {
iter := fsc.db.NewIterator(nil, nil)
for iter.Next() {
if iter.Key()[len(iter.Key())-1] != '/' {
ch <- string(iter.Key())
}
}
close(ch)
}()
return ch
}
func (fsc *FSCache) Stat(path string) (*FileInfo, error) {
storedPath := path
if path != "/" {
storedPath = path + string(filepath.Separator)
}
// first check if the path is a directory
data, err := fsc.db.Get([]byte(storedPath), nil)
if err != nil {
// then check if the path is a file
data, err = fsc.db.Get([]byte(path), nil)
if err != nil {
return nil, err
}
}
var fileinfo *FileInfo
err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&fileinfo)
if err != nil {
return nil, err
}
return fileinfo, nil
}