-
Notifications
You must be signed in to change notification settings - Fork 3
/
files.go
315 lines (291 loc) · 7.06 KB
/
files.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
package cas
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/dennwc/cas/schema"
"github.com/dennwc/cas/storage/local"
"github.com/dennwc/cas/types"
)
const (
maxDirEntries = 1024
)
type FileDesc interface {
Name() string
Open() (io.ReadCloser, SizedRef, error)
SetRef(ref types.SizedRef)
}
func LocalFile(path string) FileDesc {
return &localFile{path: path}
}
func (s *Storage) storeAsFile(ctx context.Context, fd FileDesc, conf *StoreConfig) (*schema.DirEntry, error) {
sr, err := s.storeFileContent(ctx, fd, conf)
if err != nil {
return nil, err
}
return &schema.DirEntry{
Ref: sr.Ref,
Name: filepath.Base(fd.Name()),
Stats: Stats{
schema.StatDataSize: sr.Size,
},
}, nil
}
func (s *Storage) storeFileContent(ctx context.Context, fd FileDesc, conf *StoreConfig) (types.SizedRef, error) {
// open the file, snapshot metadata
rc, xr, err := fd.Open()
if err != nil {
return types.SizedRef{}, err
}
defer rc.Close()
// if we know the ref - check if we expect it, and if not - set as expected
if !xr.Ref.Zero() {
if err = conf.checkRef(xr); err != nil {
return types.SizedRef{}, fmt.Errorf("file %q: %v", fd.Name(), err)
}
conf.Expect = xr
}
// not splitting the file - can optimize in few cases
if conf.Split == nil {
// we know the ref beforehand, so we might return earlier
// if we are in indexing mode or we have this blob
if !xr.Ref.Zero() {
if conf.IndexOnly {
// if only indexing - return the response directly
return xr, nil
}
// if storing, check if a blob store has this ref already
_, err := s.StatBlob(ctx, xr.Ref)
if err == nil {
return xr, nil
}
// if not, continue as usual
}
if !conf.IndexOnly {
// if we are not indexing, storing a local file and the backend
// is a local FS, then try to import the file directly without copying it
if lf, ok := fd.(*localFile); ok {
if l, ok := s.st.(*local.Storage); ok {
// clone file, if possible
if sr, err := l.ImportFile(ctx, lf.path); err == nil {
// write resulting ref to source file, so we know it next time
fd.SetRef(sr)
return sr, nil
}
}
}
}
}
sr, err := s.StoreBlob(ctx, rc, conf)
if err != nil {
return types.SizedRef{}, err
}
if conf.Split == nil {
fd.SetRef(sr)
}
return sr, nil
}
func (s *Storage) storeDirList(ctx context.Context, list []schema.DirEntry) (SizedRef, Stats, error) {
stats := make(Stats)
olist := make([]schema.Object, 0, len(list))
for _, e := range list {
for k, v := range e.Stats {
stats[k] += v
}
stats[schema.StatDataCount]++
e := e
olist = append(olist, &e)
}
if len(stats) == 0 {
stats = nil
}
m := &schema.InlineList{Elem: typeDirEnt, List: olist, Stats: stats}
sr, err := s.StoreSchema(ctx, m)
if err != nil {
return SizedRef{}, nil, err
}
return sr, stats, nil
}
func (s *Storage) storeDirJoin(ctx context.Context, refs []Ref, list []schema.List) (SizedRef, schema.List, error) {
stats := make(schema.Stats)
for _, e := range list {
for k, v := range e.Stats {
stats[k] += v
}
}
if len(stats) == 0 {
stats = nil
}
m := schema.List{Elem: typeDirEnt, List: refs, Stats: stats}
sr, err := s.StoreSchema(ctx, &m)
if err != nil {
return SizedRef{}, schema.List{}, err
}
return sr, m, nil
}
func (s *Storage) storeDir(ctx context.Context, dir string, conf *StoreConfig) (SizedRef, Stats, error) {
d, err := os.Open(dir)
if err != nil {
return SizedRef{}, nil, err
}
defer d.Close()
var base []schema.DirEntry
for {
buf, err := d.Readdir(maxDirEntries)
if err == io.EOF {
d.Close()
break
} else if err != nil {
return SizedRef{}, nil, err
}
for _, fi := range buf {
if fi.Name() == DefaultDir {
continue
}
fpath := filepath.Join(dir, fi.Name())
if fi.IsDir() {
sr, st, err := s.storeDir(ctx, fpath, conf)
if err != nil {
return SizedRef{}, nil, err
}
base = append(base, schema.DirEntry{
Ref: sr.Ref, Name: fi.Name(),
Stats: st,
})
} else {
c := *conf
c.Expect = SizedRef{}
ent, err := s.storeAsFile(ctx, LocalFile(fpath), &c)
if err != nil {
return SizedRef{}, nil, err
}
base = append(base, *ent)
}
}
}
sort.Slice(base, func(i, j int) bool {
return base[i].Name < base[j].Name
})
var (
level []schema.List
refs []Ref
cur schema.List
)
if len(base) <= maxDirEntries {
return s.storeDirList(ctx, base)
}
for len(base) > 0 {
page := base
if len(page) > maxDirEntries {
page = page[:maxDirEntries]
}
base = base[len(page):]
sr, stats, err := s.storeDirList(ctx, page)
if err != nil {
return SizedRef{}, nil, err
}
if cur.Stats == nil {
cur.Stats = make(schema.Stats, 2)
for k, v := range stats {
cur.Stats[k] += v
}
}
cur.List = append(cur.List, sr.Ref)
if len(cur.List) >= maxDirEntries || len(base) == 0 {
cur.Elem = typeDirEnt
sr, err = s.StoreSchema(ctx, &cur)
if err != nil {
return SizedRef{}, nil, err
}
level = append(level, cur)
refs = append(refs, sr.Ref)
cur = schema.List{}
}
}
for len(level) > 1 {
var (
newLevel []schema.List
newRefs []Ref
)
for len(level) > 0 {
page := level
if len(page) > maxDirEntries {
page = page[:maxDirEntries]
}
pref := refs[:len(page)]
level = level[len(page):]
refs = refs[len(page):]
sr, cur, err := s.storeDirJoin(ctx, pref, page)
if err != nil {
return SizedRef{}, nil, err
}
newLevel = append(newLevel, cur)
newRefs = append(newRefs, sr.Ref)
}
level, refs = newLevel, newRefs
}
top := level[0]
sr, err := s.StoreSchema(ctx, &top)
if err != nil {
return SizedRef{}, nil, err
}
return sr, top.Stats, nil
}
func (s *Storage) StoreAsFile(ctx context.Context, fd FileDesc, conf *StoreConfig) (SizedRef, error) {
m, err := s.storeAsFile(ctx, fd, conf)
if err != nil {
return SizedRef{}, err
}
return s.StoreSchema(ctx, m)
}
func (s *Storage) StoreFilePath(ctx context.Context, path string, conf *StoreConfig) (SizedRef, error) {
conf = checkConfig(conf)
fi, err := os.Stat(path)
if err != nil {
return SizedRef{}, err
}
if fi.IsDir() {
sr, _, err := s.storeDir(ctx, path, conf)
return sr, err
}
ent, err := s.storeAsFile(ctx, LocalFile(path), conf)
if err != nil {
return SizedRef{}, err
}
return SizedRef{Ref: ent.Ref, Size: ent.Size()}, err
}
type localFile struct {
path string
fi os.FileInfo
}
func (f *localFile) Name() string {
return filepath.Base(f.path)
}
func (f *localFile) Open() (io.ReadCloser, SizedRef, error) {
fd, err := os.Open(f.path)
if err != nil {
return nil, types.SizedRef{}, err
}
st, err := fd.Stat()
if err != nil {
fd.Close()
return nil, SizedRef{}, err
}
f.fi = st
sr := SizedRef{Size: uint64(st.Size())}
if xr, err := StatFile(context.Background(), fd); err == nil && xr.Size == sr.Size {
sr.Ref = xr.Ref
}
return fd, sr, nil
}
func (f *localFile) SetRef(ref types.SizedRef) {
if f.fi == nil || uint64(f.fi.Size()) != ref.Size {
// this is the only case that we can reject directly
// all other checks happen at read time
return
}
_ = SaveRef(context.Background(), f.path, f.fi, ref.Ref)
}