2021-05-06 17:56:57 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ultest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
// ulfs.Filesystem
|
|
|
|
//
|
|
|
|
|
|
|
|
type testFilesystem struct {
|
|
|
|
stdin string
|
|
|
|
created int64
|
2021-05-12 17:16:56 +01:00
|
|
|
files map[ulloc.Location]memFileData
|
|
|
|
pending map[ulloc.Location][]*memWriteHandle
|
2021-05-06 17:56:57 +01:00
|
|
|
buckets map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestFilesystem() *testFilesystem {
|
|
|
|
return &testFilesystem{
|
2021-05-12 17:16:56 +01:00
|
|
|
files: make(map[ulloc.Location]memFileData),
|
|
|
|
pending: make(map[ulloc.Location][]*memWriteHandle),
|
2021-05-06 17:56:57 +01:00
|
|
|
buckets: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
type memFileData struct {
|
|
|
|
contents string
|
|
|
|
created int64
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) ensureBucket(name string) {
|
|
|
|
tfs.buckets[name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (tfs *testFilesystem) Files() (files []File) {
|
|
|
|
for loc, mf := range tfs.files {
|
|
|
|
files = append(files, File{
|
|
|
|
Loc: loc.String(),
|
|
|
|
Contents: mf.contents,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sort.Slice(files, func(i, j int) bool { return files[i].less(files[j]) })
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (tfs *testFilesystem) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) Open(ctx clingy.Context, loc ulloc.Location) (_ ulfs.ReadHandle, err error) {
|
2021-05-12 17:16:56 +01:00
|
|
|
mf, ok := tfs.files[loc]
|
2021-05-06 17:56:57 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("file does not exist")
|
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
return &byteReadHandle{Buffer: bytes.NewBufferString(mf.contents)}, nil
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) Create(ctx clingy.Context, loc ulloc.Location) (_ ulfs.WriteHandle, err error) {
|
|
|
|
if bucket, _, ok := loc.RemoteParts(); ok {
|
|
|
|
if _, ok := tfs.buckets[bucket]; !ok {
|
|
|
|
return nil, errs.New("bucket %q does not exist", bucket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tfs.created++
|
2021-05-12 17:16:56 +01:00
|
|
|
wh := &memWriteHandle{
|
2021-05-06 17:56:57 +01:00
|
|
|
buf: bytes.NewBuffer(nil),
|
|
|
|
loc: loc,
|
|
|
|
tfs: tfs,
|
|
|
|
cre: tfs.created,
|
|
|
|
}
|
|
|
|
|
|
|
|
tfs.pending[loc] = append(tfs.pending[loc], wh)
|
|
|
|
|
|
|
|
return wh, nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:20:21 +01:00
|
|
|
func (tfs *testFilesystem) Remove(ctx context.Context, loc ulloc.Location) error {
|
|
|
|
delete(tfs.files, loc)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (tfs *testFilesystem) ListObjects(ctx context.Context, prefix ulloc.Location, recursive bool) (ulfs.ObjectIterator, error) {
|
|
|
|
var infos []ulfs.ObjectInfo
|
2021-05-12 17:16:56 +01:00
|
|
|
for loc, mf := range tfs.files {
|
2021-05-06 17:56:57 +01:00
|
|
|
if loc.HasPrefix(prefix) {
|
|
|
|
infos = append(infos, ulfs.ObjectInfo{
|
|
|
|
Loc: loc,
|
2021-05-12 17:16:56 +01:00
|
|
|
Created: time.Unix(mf.created, 0),
|
2021-05-06 17:56:57 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(objectInfos(infos))
|
|
|
|
|
|
|
|
if !recursive {
|
|
|
|
infos = collapseObjectInfos(prefix, infos)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objectInfoIterator{infos: infos}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) ListUploads(ctx context.Context, prefix ulloc.Location, recursive bool) (ulfs.ObjectIterator, error) {
|
|
|
|
var infos []ulfs.ObjectInfo
|
|
|
|
for loc, whs := range tfs.pending {
|
|
|
|
if loc.HasPrefix(prefix) {
|
|
|
|
for _, wh := range whs {
|
|
|
|
infos = append(infos, ulfs.ObjectInfo{
|
|
|
|
Loc: loc,
|
|
|
|
Created: time.Unix(wh.cre, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(objectInfos(infos))
|
|
|
|
|
|
|
|
if !recursive {
|
|
|
|
infos = collapseObjectInfos(prefix, infos)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objectInfoIterator{infos: infos}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) IsLocalDir(ctx context.Context, loc ulloc.Location) bool {
|
|
|
|
// TODO: implement this
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ulfs.ReadHandle
|
|
|
|
//
|
|
|
|
|
|
|
|
type byteReadHandle struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *byteReadHandle) Close() error { return nil }
|
|
|
|
func (b *byteReadHandle) Info() ulfs.ObjectInfo { return ulfs.ObjectInfo{} }
|
|
|
|
|
|
|
|
//
|
|
|
|
// ulfs.WriteHandle
|
|
|
|
//
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
type memWriteHandle struct {
|
2021-05-06 17:56:57 +01:00
|
|
|
buf *bytes.Buffer
|
|
|
|
loc ulloc.Location
|
|
|
|
tfs *testFilesystem
|
|
|
|
cre int64
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) Write(p []byte) (int, error) {
|
2021-05-06 17:56:57 +01:00
|
|
|
return b.buf.Write(p)
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) Commit() error {
|
2021-05-06 17:56:57 +01:00
|
|
|
if err := b.close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
b.tfs.files[b.loc] = memFileData{
|
|
|
|
contents: b.buf.String(),
|
|
|
|
created: b.cre,
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) Abort() error {
|
2021-05-06 17:56:57 +01:00
|
|
|
if err := b.close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) close() error {
|
2021-05-06 17:56:57 +01:00
|
|
|
if b.done {
|
|
|
|
return errs.New("already done")
|
|
|
|
}
|
|
|
|
b.done = true
|
|
|
|
|
|
|
|
handles := b.tfs.pending[b.loc]
|
|
|
|
for i, v := range handles {
|
|
|
|
if v == b {
|
|
|
|
handles = append(handles[:i], handles[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(handles) > 0 {
|
|
|
|
b.tfs.pending[b.loc] = handles
|
|
|
|
} else {
|
|
|
|
delete(b.tfs.pending, b.loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ulfs.ObjectIterator
|
|
|
|
//
|
|
|
|
|
|
|
|
type objectInfoIterator struct {
|
|
|
|
infos []ulfs.ObjectInfo
|
|
|
|
current ulfs.ObjectInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (li *objectInfoIterator) Next() bool {
|
|
|
|
if len(li.infos) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
li.current, li.infos = li.infos[0], li.infos[1:]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (li *objectInfoIterator) Err() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (li *objectInfoIterator) Item() ulfs.ObjectInfo {
|
|
|
|
return li.current
|
|
|
|
}
|
|
|
|
|
|
|
|
type objectInfos []ulfs.ObjectInfo
|
|
|
|
|
|
|
|
func (ois objectInfos) Len() int { return len(ois) }
|
|
|
|
func (ois objectInfos) Swap(i int, j int) { ois[i], ois[j] = ois[j], ois[i] }
|
|
|
|
func (ois objectInfos) Less(i int, j int) bool { return ois[i].Loc.Less(ois[j].Loc) }
|
|
|
|
|
|
|
|
func collapseObjectInfos(prefix ulloc.Location, infos []ulfs.ObjectInfo) []ulfs.ObjectInfo {
|
|
|
|
collapsing := false
|
|
|
|
current := ""
|
|
|
|
j := 0
|
|
|
|
|
|
|
|
for _, oi := range infos {
|
|
|
|
first, ok := oi.Loc.ListKeyName(prefix)
|
|
|
|
if ok {
|
|
|
|
if collapsing && first == current {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
collapsing = true
|
|
|
|
current = first
|
|
|
|
|
|
|
|
oi.IsPrefix = true
|
|
|
|
}
|
|
|
|
|
|
|
|
oi.Loc = oi.Loc.SetKey(first)
|
|
|
|
|
|
|
|
infos[j] = oi
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos[:j]
|
|
|
|
}
|