-
Notifications
You must be signed in to change notification settings - Fork 16
/
store.go
497 lines (418 loc) · 11.3 KB
/
store.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package localfs
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/lytics/cloudstorage"
"github.com/lytics/cloudstorage/csbufio"
"github.com/pborman/uuid"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
)
func init() {
cloudstorage.Register(StoreType, localProvider)
}
func localProvider(conf *cloudstorage.Config) (cloudstorage.Store, error) {
store, err := NewLocalStore(conf.LocalFS, conf.TmpDir)
if err != nil {
return nil, err
}
return store, nil
}
var (
// Ensure Our LocalStore implement CloudStorage interfaces
_ cloudstorage.StoreReader = (*LocalStore)(nil)
)
const (
// AuthFileSystem Authentication Method
AuthFileSystem cloudstorage.AuthMethod = "localfiles"
// StoreType name of our Local Storage provider = "localfs"
StoreType = "localfs"
)
// LocalStore is client to local-filesystem store.
type LocalStore struct {
storepath string // possibly is relative ./tables
pathCleaned string // cleaned removing ./ = "tables"
cachepath string
Id string
}
// NewLocalStore create local store from storage path on local filesystem, and cachepath.
func NewLocalStore(storepath, cachepath string) (*LocalStore, error) {
if storepath == "" {
return nil, fmt.Errorf("storepath=%q cannot be empty", storepath)
}
if storepath == cachepath {
return nil, fmt.Errorf("storepath=%q cannot be the same as cachepath=%q", storepath, cachepath)
}
pathCleaned := strings.TrimPrefix(storepath, "./")
err := os.MkdirAll(storepath, 0775)
if err != nil {
return nil, fmt.Errorf("unable to create path. path=%s err=%v", storepath, err)
}
err = os.MkdirAll(cachepath, 0775)
if err != nil {
return nil, fmt.Errorf("unable to create path. path=%s err=%v", cachepath, err)
}
uid := uuid.NewUUID().String()
uid = strings.Replace(uid, "-", "", -1)
return &LocalStore{
storepath: storepath,
pathCleaned: pathCleaned,
cachepath: cachepath,
Id: uid,
}, nil
}
// Type is store type = "localfs"
func (l *LocalStore) Type() string {
return StoreType
}
func (l *LocalStore) Client() interface{} {
return l
}
// NewObject create new object of given name.
func (l *LocalStore) NewObject(objectname string) (cloudstorage.Object, error) {
obj, err := l.Get(context.Background(), objectname)
if err != nil && err != cloudstorage.ErrObjectNotFound {
return nil, err
} else if obj != nil {
return nil, cloudstorage.ErrObjectExists
}
of := path.Join(l.storepath, objectname)
err = cloudstorage.EnsureDir(of)
if err != nil {
return nil, err
}
cf := cloudstorage.CachePathObj(l.cachepath, objectname, l.Id)
return &object{
name: objectname,
storepath: of,
cachepath: cf,
}, nil
}
// List objects at Query location.
func (l *LocalStore) List(ctx context.Context, query cloudstorage.Query) (*cloudstorage.ObjectsResponse, error) {
resp := cloudstorage.NewObjectsResponse()
objects := make(map[string]*object)
metadatas := make(map[string]map[string]string)
spath := path.Join(l.storepath, query.Prefix)
if !cloudstorage.Exists(spath) {
return resp, nil
}
err := filepath.Walk(spath, func(fo string, f os.FileInfo, err error) error {
if err != nil {
return err
}
obj := strings.Replace(fo, l.pathCleaned, "", 1)
if f.IsDir() {
return nil
} else if filepath.Ext(f.Name()) == ".metadata" {
b, err := ioutil.ReadFile(fo)
if err != nil {
return err
}
md := make(map[string]string)
err = json.Unmarshal(b, &md)
if err != nil {
return err
}
mdkey := strings.Replace(obj, ".metadata", "", 1)
metadatas[mdkey] = md
} else {
oname := strings.TrimPrefix(obj, "/")
objects[obj] = &object{
name: oname,
updated: f.ModTime(),
storepath: fo,
cachepath: cloudstorage.CachePathObj(l.cachepath, oname, l.Id),
}
}
return err
})
if err != nil {
return nil, fmt.Errorf("localfile: error occurred listing files. searchpath=%v err=%v", spath, err)
}
for objname, obj := range objects {
if md, ok := metadatas[objname]; ok {
obj.metadata = md
}
resp.Objects = append(resp.Objects, obj)
}
resp.Objects = query.ApplyFilters(resp.Objects)
return resp, nil
}
// Objects returns an iterator over the objects in the local folder that match the Query q.
// If q is nil, no filtering is done.
func (l *LocalStore) Objects(ctx context.Context, csq cloudstorage.Query) (cloudstorage.ObjectIterator, error) {
resp, err := l.List(ctx, csq)
if err != nil {
return nil, err
}
return &objectIterator{objects: resp.Objects}, nil
}
// Folders list of folders for given path query.
func (l *LocalStore) Folders(ctx context.Context, csq cloudstorage.Query) ([]string, error) {
spath := path.Join(l.storepath, csq.Prefix)
if !cloudstorage.Exists(spath) {
return nil, fmt.Errorf("That folder %q does not exist", spath)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
folders := make([]string, 0)
files, _ := ioutil.ReadDir(spath)
for _, f := range files {
if f.IsDir() {
folders = append(folders, fmt.Sprintf("%s/", path.Join(csq.Prefix, f.Name())))
}
}
return folders, nil
}
// NewReader create local file-system store reader.
func (l *LocalStore) NewReader(o string) (io.ReadCloser, error) {
return l.NewReaderWithContext(context.Background(), o)
}
func (l *LocalStore) NewReaderWithContext(ctx context.Context, o string) (io.ReadCloser, error) {
fo := path.Join(l.storepath, o)
if !cloudstorage.Exists(fo) {
return nil, cloudstorage.ErrObjectNotFound
}
return csbufio.OpenReader(fo)
}
func (l *LocalStore) NewWriter(o string, metadata map[string]string) (io.WriteCloser, error) {
return l.NewWriterWithContext(context.Background(), o, metadata)
}
func (l *LocalStore) NewWriterWithContext(ctx context.Context, o string, metadata map[string]string) (io.WriteCloser, error) {
fo := path.Join(l.storepath, o)
err := cloudstorage.EnsureDir(fo)
if err != nil {
return nil, err
}
if metadata != nil && len(metadata) > 0 {
metadata = make(map[string]string)
}
fmd := fo + ".metadata"
if err := writemeta(fmd, metadata); err != nil {
return nil, err
}
return csbufio.OpenWriter(fo)
}
func (l *LocalStore) Get(ctx context.Context, o string) (cloudstorage.Object, error) {
fo := path.Join(l.storepath, o)
if !cloudstorage.Exists(fo) {
return nil, cloudstorage.ErrObjectNotFound
}
var updated time.Time
if stat, err := os.Stat(fo); err == nil {
updated = stat.ModTime()
}
return &object{
name: o,
updated: updated,
storepath: fo,
cachepath: cloudstorage.CachePathObj(l.cachepath, o, l.Id),
}, nil
}
// Delete the object from underlying store.
func (l *LocalStore) Delete(ctx context.Context, obj string) error {
fo := path.Join(l.storepath, obj)
os.Remove(fo)
mf := fo + ".metadata"
if cloudstorage.Exists(mf) {
os.Remove(mf)
}
return nil
}
func (l *LocalStore) String() string {
return fmt.Sprintf("[id:%s file://%s/]", l.Id, l.storepath)
}
type objectIterator struct {
objects cloudstorage.Objects
err error
cursor int
}
func (l *objectIterator) Next() (cloudstorage.Object, error) {
if l.err != nil {
return nil, l.err
}
if l.cursor >= len(l.objects) {
return nil, iterator.Done
}
o := l.objects[l.cursor]
l.cursor++
return o, nil
}
func (l *objectIterator) Close() {}
type object struct {
name string
updated time.Time
metadata map[string]string
storepath string
cachepath string
cachedcopy *os.File
readonly bool
opened bool
}
func (o *object) StorageSource() string {
return StoreType
}
func (o *object) Name() string {
return o.name
}
func (o *object) String() string {
return o.name
}
func (o *object) Updated() time.Time {
return o.updated
}
func (o *object) MetaData() map[string]string {
return o.metadata
}
func (o *object) SetMetaData(meta map[string]string) {
o.metadata = meta
}
func (o *object) Delete() error {
o.Release()
if err := os.Remove(o.storepath); err != nil {
return err
}
mf := o.storepath + ".metadata"
if cloudstorage.Exists(mf) {
if err := os.Remove(mf); err != nil {
return err
}
}
return nil
}
func (o *object) Open(accesslevel cloudstorage.AccessLevel) (*os.File, error) {
if o.opened {
return nil, fmt.Errorf("the store object is already opened. %s", o.storepath)
}
var readonly = accesslevel == cloudstorage.ReadOnly
storecopy, err := os.OpenFile(o.storepath, os.O_RDWR|os.O_CREATE, 0665)
if err != nil {
return nil, fmt.Errorf("localfs: local=%q could not create storecopy err=%v", o.storepath, err)
}
defer storecopy.Close()
err = cloudstorage.EnsureDir(o.cachepath)
if err != nil {
return nil, fmt.Errorf("localfs: cachepath=%s could not create cachedcopy dir err=%v", o.cachepath, err)
}
cachedcopy, err := os.Create(o.cachepath)
if err != nil {
return nil, fmt.Errorf("localfs: cachepath=%s could not create cachedcopy err=%v", o.cachepath, err)
}
_, err = io.Copy(cachedcopy, storecopy)
if err != nil {
return nil, fmt.Errorf("localfs: storepath=%s cachedcopy=%v could not copy from store to cache err=%v", o.storepath, cachedcopy.Name(), err)
}
if readonly {
cachedcopy.Close()
cachedcopy, err = os.Open(o.cachepath)
if err != nil {
return nil, fmt.Errorf("localfs: storepath=%s cachedcopy=%v could not opencache err=%v", o.storepath, cachedcopy.Name(), err)
}
} else {
if _, err := cachedcopy.Seek(0, os.SEEK_SET); err != nil {
return nil, fmt.Errorf("error seeking to start of cachedcopy err=%v", err) //don't retry on local fs errors
}
}
o.cachedcopy = cachedcopy
o.readonly = readonly
o.opened = true
return o.cachedcopy, nil
}
func (o *object) File() *os.File {
return o.cachedcopy
}
func (o *object) Read(p []byte) (n int, err error) {
return o.cachedcopy.Read(p)
}
// Write the given bytes to object. Won't be writen until Close() or Sync() called.
func (o *object) Write(p []byte) (n int, err error) {
if o.cachedcopy == nil {
_, err := o.Open(cloudstorage.ReadWrite)
if err != nil {
return 0, err
}
}
return o.cachedcopy.Write(p)
}
func (o *object) Sync() error {
if !o.opened {
return fmt.Errorf("object isn't opened %s", o.name)
}
if o.readonly {
return fmt.Errorf("trying to Sync a readonly object %s", o.name)
}
cachedcopy, err := os.OpenFile(o.cachepath, os.O_RDONLY, 0664)
if err != nil {
return err
}
defer cachedcopy.Close()
storecopy, err := os.OpenFile(o.storepath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0664)
if err != nil {
return err
}
defer storecopy.Close()
_, err = io.Copy(storecopy, cachedcopy)
if err != nil {
return err
}
if o.metadata != nil && len(o.metadata) > 0 {
o.metadata = make(map[string]string)
}
fmd := o.storepath + ".metadata"
return writemeta(fmd, o.metadata)
}
func writemeta(filename string, meta map[string]string) error {
bm, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filename, bm, 0664)
if err != nil {
return err
}
return nil
}
func (o *object) Close() error {
if !o.opened {
return nil
}
err := o.cachedcopy.Sync()
if err != nil {
return err
}
err = o.cachedcopy.Close()
if err != nil {
return err
}
if o.opened && !o.readonly {
err := o.Sync()
if err != nil {
return err
}
}
o.cachedcopy = nil
o.opened = false
return nil
}
func (o *object) Release() error {
if o.cachedcopy != nil {
o.cachedcopy.Close()
o.cachedcopy = nil
o.opened = false
return os.Remove(o.cachepath)
}
// most likely this doesn't exist so don't return error
os.Remove(o.cachepath)
return nil
}