2021-05-06 17:56:57 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ultest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-12-09 18:36:11 +00:00
|
|
|
"io"
|
2021-06-25 02:55:13 +01:00
|
|
|
"path/filepath"
|
2021-05-06 17:56:57 +01:00
|
|
|
"sort"
|
2021-06-25 02:55:13 +01:00
|
|
|
"strings"
|
2021-10-05 17:48:13 +01:00
|
|
|
"sync"
|
2021-05-06 17:56:57 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-05-06 17:56:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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-06-25 02:55:13 +01:00
|
|
|
locals map[string]bool // true means path is a directory
|
2021-05-06 17:56:57 +01:00
|
|
|
buckets map[string]struct{}
|
2021-10-05 17:48:13 +01:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-25 02:55:13 +01:00
|
|
|
locals: make(map[string]bool),
|
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
|
2022-03-07 00:54:48 +00:00
|
|
|
expires time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mf memFileData) expired() bool {
|
|
|
|
return mf.expires != time.Time{} && mf.expires.Before(time.Now())
|
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 {
|
2022-03-07 00:54:48 +00:00
|
|
|
if mf.expired() {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
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-10-02 00:47:53 +01:00
|
|
|
func (tfs *testFilesystem) Pending() (files []File) {
|
|
|
|
for loc, mh := range tfs.pending {
|
|
|
|
for _, h := range mh {
|
|
|
|
files = append(files, File{
|
|
|
|
Loc: loc.String(),
|
2021-12-09 19:03:42 +00:00
|
|
|
Contents: string(h.buf),
|
2021-10-02 00:47:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:36:11 +00:00
|
|
|
type nopClosingGenericReader struct{ io.ReaderAt }
|
|
|
|
|
|
|
|
func (n nopClosingGenericReader) Close() error { return nil }
|
|
|
|
|
|
|
|
func newMultiReadHandle(contents string) ulfs.MultiReadHandle {
|
|
|
|
return ulfs.NewGenericMultiReadHandle(nopClosingGenericReader{
|
|
|
|
ReaderAt: bytes.NewReader([]byte(contents)),
|
|
|
|
}, ulfs.ObjectInfo{
|
|
|
|
ContentLength: int64(len(contents)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) Open(ctx clingy.Context, loc ulloc.Location) (ulfs.MultiReadHandle, error) {
|
2021-10-05 17:48:13 +01:00
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if loc.Std() {
|
2021-12-09 18:36:11 +00:00
|
|
|
return newMultiReadHandle("-"), nil
|
2021-06-25 02:55:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
mf, ok := tfs.files[loc]
|
2021-05-06 17:56:57 +01:00
|
|
|
if !ok {
|
2021-10-05 17:48:13 +01:00
|
|
|
return nil, errs.New("file does not exist %q", loc)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-10-26 11:49:03 +01:00
|
|
|
|
2021-12-09 18:36:11 +00:00
|
|
|
return newMultiReadHandle(mf.contents), nil
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2022-03-07 00:54:48 +00:00
|
|
|
func (tfs *testFilesystem) Create(ctx clingy.Context, loc ulloc.Location, opts *ulfs.CreateOptions) (_ ulfs.MultiWriteHandle, err error) {
|
2021-10-05 17:48:13 +01:00
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if loc.Std() {
|
2021-12-09 19:03:42 +00:00
|
|
|
return ulfs.NewGenericMultiWriteHandle(new(discardWriteHandle)), nil
|
2021-06-25 02:55:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
if bucket, _, ok := loc.RemoteParts(); ok {
|
|
|
|
if _, ok := tfs.buckets[bucket]; !ok {
|
|
|
|
return nil, errs.New("bucket %q does not exist", bucket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if path, ok := loc.LocalParts(); ok {
|
2021-10-05 17:48:13 +01:00
|
|
|
if loc.Directoryish() || tfs.isLocalDir(ctx, loc) {
|
2021-06-25 02:55:13 +01:00
|
|
|
return nil, errs.New("unable to open file for writing: %q", loc)
|
|
|
|
}
|
2021-08-26 17:52:51 +01:00
|
|
|
dir := ulloc.CleanPath(filepath.Dir(path))
|
|
|
|
if err := tfs.mkdirAll(ctx, dir); err != nil {
|
2021-06-25 02:55:13 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:54:48 +00:00
|
|
|
expires := time.Time{}
|
|
|
|
if opts != nil {
|
|
|
|
expires = opts.Expires
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
tfs.created++
|
2021-05-12 17:16:56 +01:00
|
|
|
wh := &memWriteHandle{
|
2022-03-07 00:54:48 +00:00
|
|
|
loc: loc,
|
|
|
|
tfs: tfs,
|
|
|
|
cre: tfs.created,
|
|
|
|
expires: expires,
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if loc.Remote() {
|
|
|
|
tfs.pending[loc] = append(tfs.pending[loc], wh)
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2021-12-09 19:03:42 +00:00
|
|
|
return ulfs.NewGenericMultiWriteHandle(wh), nil
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:48:13 +01:00
|
|
|
func (tfs *testFilesystem) Move(ctx clingy.Context, source, dest ulloc.Location) error {
|
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
|
|
|
mf, ok := tfs.files[source]
|
|
|
|
if !ok {
|
|
|
|
return errs.New("file does not exist %q", source)
|
|
|
|
}
|
|
|
|
delete(tfs.files, source)
|
|
|
|
tfs.files[dest] = mf
|
2022-03-15 10:07:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) Copy(ctx clingy.Context, source, dest ulloc.Location) error {
|
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
|
|
|
mf, ok := tfs.files[source]
|
|
|
|
if !ok {
|
|
|
|
return errs.New("file does not exist %q", source)
|
|
|
|
}
|
|
|
|
tfs.files[dest] = mf
|
2021-10-05 17:48:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
func (tfs *testFilesystem) Remove(ctx context.Context, loc ulloc.Location, opts *ulfs.RemoveOptions) error {
|
2021-10-05 17:48:13 +01:00
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts == nil || !opts.Pending {
|
|
|
|
delete(tfs.files, loc)
|
|
|
|
} else {
|
|
|
|
// TODO: Remove needs an API that understands that multiple pending files may exist
|
|
|
|
delete(tfs.pending, loc)
|
|
|
|
}
|
2021-05-14 20:20:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
func (tfs *testFilesystem) List(ctx context.Context, prefix ulloc.Location, opts *ulfs.ListOptions) (ulfs.ObjectIterator, error) {
|
2021-10-05 17:48:13 +01:00
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts != nil && opts.Pending {
|
|
|
|
return tfs.listPending(ctx, prefix, opts)
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
prefixDir := prefix.AsDirectoryish()
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
var infos []ulfs.ObjectInfo
|
2021-05-12 17:16:56 +01:00
|
|
|
for loc, mf := range tfs.files {
|
2022-03-07 00:54:48 +00:00
|
|
|
if (loc.HasPrefix(prefixDir) || loc == prefix) && !mf.expired() {
|
2021-05-06 17:56:57 +01:00
|
|
|
infos = append(infos, ulfs.ObjectInfo{
|
|
|
|
Loc: loc,
|
2021-05-12 17:16:56 +01:00
|
|
|
Created: time.Unix(mf.created, 0),
|
2022-03-07 00:54:48 +00:00
|
|
|
Expires: mf.expires,
|
2021-05-06 17:56:57 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(objectInfos(infos))
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts == nil || !opts.Recursive {
|
2021-05-06 17:56:57 +01:00
|
|
|
infos = collapseObjectInfos(prefix, infos)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objectInfoIterator{infos: infos}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
func (tfs *testFilesystem) listPending(ctx context.Context, prefix ulloc.Location, opts *ulfs.ListOptions) (ulfs.ObjectIterator, error) {
|
|
|
|
if prefix.Local() {
|
|
|
|
return &objectInfoIterator{}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
prefixDir := prefix.AsDirectoryish()
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
var infos []ulfs.ObjectInfo
|
|
|
|
for loc, whs := range tfs.pending {
|
2021-10-02 00:47:53 +01:00
|
|
|
if loc.HasPrefix(prefixDir) || loc == prefix {
|
2021-05-06 17:56:57 +01:00
|
|
|
for _, wh := range whs {
|
|
|
|
infos = append(infos, ulfs.ObjectInfo{
|
|
|
|
Loc: loc,
|
|
|
|
Created: time.Unix(wh.cre, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(objectInfos(infos))
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
if opts == nil || !opts.Recursive {
|
2021-05-06 17:56:57 +01:00
|
|
|
infos = collapseObjectInfos(prefix, infos)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objectInfoIterator{infos: infos}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
func (tfs *testFilesystem) IsLocalDir(ctx context.Context, loc ulloc.Location) (local bool) {
|
2021-10-05 17:48:13 +01:00
|
|
|
tfs.mu.Lock()
|
|
|
|
defer tfs.mu.Unlock()
|
|
|
|
|
|
|
|
return tfs.isLocalDir(ctx, loc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs *testFilesystem) isLocalDir(ctx context.Context, loc ulloc.Location) (local bool) {
|
2021-06-25 02:55:13 +01:00
|
|
|
path, ok := loc.LocalParts()
|
2021-08-26 17:52:51 +01:00
|
|
|
return ok && (ulloc.CleanPath(path) == "." || tfs.locals[path])
|
2021-06-25 02:55:13 +01:00
|
|
|
}
|
|
|
|
|
2021-10-26 11:49:03 +01:00
|
|
|
func (tfs *testFilesystem) Stat(ctx context.Context, loc ulloc.Location) (*ulfs.ObjectInfo, error) {
|
|
|
|
if loc.Std() {
|
|
|
|
return nil, errs.New("unable to stat loc %q", loc.Loc())
|
|
|
|
}
|
|
|
|
|
|
|
|
mf, ok := tfs.files[loc]
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("file does not exist: %q", loc.Loc())
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:54:48 +00:00
|
|
|
if mf.expired() {
|
|
|
|
return nil, errs.New("file does not exist: %q", loc.Loc())
|
|
|
|
}
|
|
|
|
|
2021-10-26 11:49:03 +01:00
|
|
|
return &ulfs.ObjectInfo{
|
|
|
|
Loc: loc,
|
|
|
|
Created: time.Unix(mf.created, 0),
|
2022-03-07 00:54:48 +00:00
|
|
|
Expires: mf.expires,
|
2021-10-26 11:49:03 +01:00
|
|
|
ContentLength: int64(len(mf.contents)),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
func (tfs *testFilesystem) mkdirAll(ctx context.Context, dir string) error {
|
|
|
|
i := 0
|
|
|
|
for i < len(dir) {
|
|
|
|
slash := strings.Index(dir[i:], "/")
|
|
|
|
if slash == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := tfs.mkdir(ctx, dir[:i+slash]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i += slash + 1
|
|
|
|
}
|
|
|
|
if len(dir) > 0 {
|
|
|
|
return tfs.mkdir(ctx, dir)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
func (tfs *testFilesystem) mkdir(ctx context.Context, dir string) error {
|
|
|
|
if isDir, ok := tfs.locals[dir]; ok && !isDir {
|
|
|
|
return errs.New("cannot create directory: %q is a file", dir)
|
|
|
|
}
|
|
|
|
tfs.locals[dir] = true
|
|
|
|
return nil
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// ulfs.WriteHandle
|
|
|
|
//
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
type memWriteHandle struct {
|
2022-03-07 00:54:48 +00:00
|
|
|
buf []byte
|
|
|
|
loc ulloc.Location
|
|
|
|
tfs *testFilesystem
|
|
|
|
cre int64
|
|
|
|
expires time.Time
|
|
|
|
done bool
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 19:03:42 +00:00
|
|
|
func (b *memWriteHandle) WriteAt(p []byte, off int64) (int, error) {
|
|
|
|
if b.done {
|
|
|
|
return 0, errs.New("write to closed handle")
|
|
|
|
}
|
|
|
|
end := int64(len(p)) + off
|
|
|
|
if grow := end - int64(len(b.buf)); grow > 0 {
|
|
|
|
b.buf = append(b.buf, make([]byte, grow)...)
|
|
|
|
}
|
|
|
|
return copy(b.buf[off:], p), nil
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) Commit() error {
|
2021-10-05 17:48:13 +01:00
|
|
|
b.tfs.mu.Lock()
|
|
|
|
defer b.tfs.mu.Unlock()
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
if err := b.close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if path, ok := b.loc.LocalParts(); ok {
|
|
|
|
b.tfs.locals[path] = false
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
b.tfs.files[b.loc] = memFileData{
|
2021-12-09 19:03:42 +00:00
|
|
|
contents: string(b.buf),
|
2021-05-12 17:16:56 +01:00
|
|
|
created: b.cre,
|
2022-03-07 00:54:48 +00:00
|
|
|
expires: b.expires,
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-12-09 19:03:42 +00:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 17:16:56 +01:00
|
|
|
func (b *memWriteHandle) Abort() error {
|
2021-10-05 17:48:13 +01:00
|
|
|
b.tfs.mu.Lock()
|
|
|
|
defer b.tfs.mu.Unlock()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
type discardWriteHandle struct{}
|
|
|
|
|
2021-12-09 19:03:42 +00:00
|
|
|
func (discardWriteHandle) WriteAt(p []byte, off int64) (int, error) { return len(p), nil }
|
|
|
|
func (discardWriteHandle) Commit() error { return nil }
|
|
|
|
func (discardWriteHandle) Abort() error { return nil }
|
2021-06-25 02:55:13 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:55:13 +01:00
|
|
|
if bucket, _, ok := oi.Loc.RemoteParts(); ok {
|
|
|
|
oi.Loc = ulloc.NewRemote(bucket, first)
|
|
|
|
} else if _, ok := oi.Loc.LocalParts(); ok {
|
|
|
|
oi.Loc = ulloc.NewLocal(first)
|
|
|
|
} else {
|
|
|
|
panic("invalid object returned from list")
|
|
|
|
}
|
2021-05-06 17:56:57 +01:00
|
|
|
|
|
|
|
infos[j] = oi
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
return infos[:j]
|
|
|
|
}
|