2021-04-06 20:19:11 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
package ulfs
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
2021-04-06 20:19:11 +01:00
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Filesystem represents either the local Filesystem or the data backed by a project.
|
|
|
|
type Filesystem interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
Close() error
|
2021-05-06 17:56:57 +01:00
|
|
|
Open(ctx clingy.Context, loc ulloc.Location) (ReadHandle, error)
|
|
|
|
Create(ctx clingy.Context, loc ulloc.Location) (WriteHandle, error)
|
|
|
|
ListObjects(ctx context.Context, prefix ulloc.Location, recursive bool) (ObjectIterator, error)
|
|
|
|
ListUploads(ctx context.Context, prefix ulloc.Location, recursive bool) (ObjectIterator, error)
|
|
|
|
IsLocalDir(ctx context.Context, loc ulloc.Location) bool
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// object info
|
|
|
|
//
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// ObjectInfo is a simpler *uplink.Object that contains the minimal information the
|
2021-04-06 20:19:11 +01:00
|
|
|
// uplink command needs that multiple types can be converted to.
|
2021-05-06 17:56:57 +01:00
|
|
|
type ObjectInfo struct {
|
|
|
|
Loc ulloc.Location
|
2021-04-06 20:19:11 +01:00
|
|
|
IsPrefix bool
|
|
|
|
Created time.Time
|
|
|
|
ContentLength int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// uplinkObjectToObjectInfo returns an objectInfo converted from an *uplink.Object.
|
2021-05-06 17:56:57 +01:00
|
|
|
func uplinkObjectToObjectInfo(bucket string, obj *uplink.Object) ObjectInfo {
|
|
|
|
return ObjectInfo{
|
|
|
|
Loc: ulloc.NewRemote(bucket, obj.Key),
|
2021-04-06 20:19:11 +01:00
|
|
|
IsPrefix: obj.IsPrefix,
|
|
|
|
Created: obj.System.Created,
|
|
|
|
ContentLength: obj.System.ContentLength,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uplinkUploadInfoToObjectInfo returns an objectInfo converted from an *uplink.Object.
|
2021-05-06 17:56:57 +01:00
|
|
|
func uplinkUploadInfoToObjectInfo(bucket string, upl *uplink.UploadInfo) ObjectInfo {
|
|
|
|
return ObjectInfo{
|
|
|
|
Loc: ulloc.NewRemote(bucket, upl.Key),
|
2021-04-06 20:19:11 +01:00
|
|
|
IsPrefix: upl.IsPrefix,
|
|
|
|
Created: upl.System.Created,
|
|
|
|
ContentLength: upl.System.ContentLength,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// read handles
|
|
|
|
//
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// ReadHandle is something that can be read from.
|
|
|
|
type ReadHandle interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
io.Reader
|
|
|
|
io.Closer
|
2021-05-06 17:56:57 +01:00
|
|
|
Info() ObjectInfo
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// uplinkReadHandle implements readHandle for *uplink.Downloads.
|
|
|
|
type uplinkReadHandle struct {
|
|
|
|
bucket string
|
|
|
|
dl *uplink.Download
|
|
|
|
}
|
|
|
|
|
|
|
|
// newUplinkReadHandle constructs an *uplinkReadHandle from an *uplink.Download.
|
|
|
|
func newUplinkReadHandle(bucket string, dl *uplink.Download) *uplinkReadHandle {
|
|
|
|
return &uplinkReadHandle{
|
|
|
|
bucket: bucket,
|
|
|
|
dl: dl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uplinkReadHandle) Read(p []byte) (int, error) { return u.dl.Read(p) }
|
|
|
|
func (u *uplinkReadHandle) Close() error { return u.dl.Close() }
|
2021-05-06 17:56:57 +01:00
|
|
|
func (u *uplinkReadHandle) Info() ObjectInfo { return uplinkObjectToObjectInfo(u.bucket, u.dl.Info()) }
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
// osReadHandle implements readHandle for *os.Files.
|
|
|
|
type osReadHandle struct {
|
|
|
|
raw *os.File
|
2021-05-06 17:56:57 +01:00
|
|
|
info ObjectInfo
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newOsReadHandle constructs an *osReadHandle from an *os.File.
|
|
|
|
func newOSReadHandle(fh *os.File) (*osReadHandle, error) {
|
|
|
|
fi, err := fh.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
return &osReadHandle{
|
|
|
|
raw: fh,
|
2021-05-06 17:56:57 +01:00
|
|
|
info: ObjectInfo{
|
|
|
|
Loc: ulloc.NewLocal(fh.Name()),
|
2021-04-06 20:19:11 +01:00
|
|
|
IsPrefix: false,
|
|
|
|
Created: fi.ModTime(), // TODO: os specific crtime
|
|
|
|
ContentLength: fi.Size(),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *osReadHandle) Read(p []byte) (int, error) { return o.raw.Read(p) }
|
|
|
|
func (o *osReadHandle) Close() error { return o.raw.Close() }
|
2021-05-06 17:56:57 +01:00
|
|
|
func (o *osReadHandle) Info() ObjectInfo { return o.info }
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
// genericReadHandle implements readHandle for an io.Reader.
|
|
|
|
type genericReadHandle struct{ r io.Reader }
|
|
|
|
|
|
|
|
// newGenericReadHandle constructs a *genericReadHandle from any io.Reader.
|
|
|
|
func newGenericReadHandle(r io.Reader) *genericReadHandle {
|
|
|
|
return &genericReadHandle{r: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *genericReadHandle) Read(p []byte) (int, error) { return g.r.Read(p) }
|
|
|
|
func (g *genericReadHandle) Close() error { return nil }
|
2021-05-06 17:56:57 +01:00
|
|
|
func (g *genericReadHandle) Info() ObjectInfo { return ObjectInfo{ContentLength: -1} }
|
2021-04-06 20:19:11 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// write handles
|
|
|
|
//
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// WriteHandle is anything that can be written to with commit/abort semantics.
|
|
|
|
type WriteHandle interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
io.Writer
|
|
|
|
Commit() error
|
|
|
|
Abort() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// uplinkWriteHandle implements writeHandle for *uplink.Uploads.
|
|
|
|
type uplinkWriteHandle uplink.Upload
|
|
|
|
|
|
|
|
// newUplinkWriteHandle constructs an *uplinkWriteHandle from an *uplink.Upload.
|
|
|
|
func newUplinkWriteHandle(dl *uplink.Upload) *uplinkWriteHandle {
|
|
|
|
return (*uplinkWriteHandle)(dl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uplinkWriteHandle) raw() *uplink.Upload {
|
|
|
|
return (*uplink.Upload)(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uplinkWriteHandle) Write(p []byte) (int, error) { return u.raw().Write(p) }
|
|
|
|
func (u *uplinkWriteHandle) Commit() error { return u.raw().Commit() }
|
|
|
|
func (u *uplinkWriteHandle) Abort() error { return u.raw().Abort() }
|
|
|
|
|
|
|
|
// osWriteHandle implements writeHandle for *os.Files.
|
|
|
|
type osWriteHandle struct {
|
|
|
|
fh *os.File
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOSWriteHandle constructs an *osWriteHandle from an *os.File.
|
|
|
|
func newOSWriteHandle(fh *os.File) *osWriteHandle {
|
|
|
|
return &osWriteHandle{fh: fh}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *osWriteHandle) Write(p []byte) (int, error) { return o.fh.Write(p) }
|
|
|
|
|
|
|
|
func (o *osWriteHandle) Commit() error {
|
|
|
|
if o.done {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
o.done = true
|
|
|
|
|
|
|
|
return o.fh.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *osWriteHandle) Abort() error {
|
|
|
|
if o.done {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
o.done = true
|
|
|
|
|
|
|
|
return errs.Combine(
|
|
|
|
o.fh.Close(),
|
|
|
|
os.Remove(o.fh.Name()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// genericWriteHandle implements writeHandle for an io.Writer.
|
|
|
|
type genericWriteHandle struct{ w io.Writer }
|
|
|
|
|
|
|
|
// newGenericWriteHandle constructs a *genericWriteHandle from an io.Writer.
|
|
|
|
func newGenericWriteHandle(w io.Writer) *genericWriteHandle {
|
|
|
|
return &genericWriteHandle{w: w}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *genericWriteHandle) Write(p []byte) (int, error) { return g.w.Write(p) }
|
|
|
|
func (g *genericWriteHandle) Commit() error { return nil }
|
|
|
|
func (g *genericWriteHandle) Abort() error { return nil }
|
|
|
|
|
|
|
|
//
|
|
|
|
// object iteration
|
|
|
|
//
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// ObjectIterator is an interface type for iterating over objectInfo values.
|
|
|
|
type ObjectIterator interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
Next() bool
|
|
|
|
Err() error
|
2021-05-06 17:56:57 +01:00
|
|
|
Item() ObjectInfo
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// filteredObjectIterator removes any iteration entries that do not begin with the filter.
|
|
|
|
// all entries must begin with the trim string which is removed before checking for the
|
|
|
|
// filter.
|
|
|
|
type filteredObjectIterator struct {
|
|
|
|
trim string
|
|
|
|
filter string
|
2021-05-06 17:56:57 +01:00
|
|
|
iter ObjectIterator
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filteredObjectIterator) Next() bool {
|
|
|
|
for {
|
|
|
|
if !f.iter.Next() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
key := f.iter.Item().Loc.Key()
|
|
|
|
if !strings.HasPrefix(key, f.trim) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(key, f.filter) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filteredObjectIterator) Err() error { return f.iter.Err() }
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (f *filteredObjectIterator) Item() ObjectInfo {
|
2021-04-06 20:19:11 +01:00
|
|
|
item := f.iter.Item()
|
2021-05-06 17:56:57 +01:00
|
|
|
item.Loc = item.Loc.RemoveKeyPrefix(f.trim)
|
2021-04-06 20:19:11 +01:00
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// emptyObjectIterator is an objectIterator that has no objects.
|
|
|
|
type emptyObjectIterator struct{}
|
|
|
|
|
|
|
|
func (emptyObjectIterator) Next() bool { return false }
|
|
|
|
func (emptyObjectIterator) Err() error { return nil }
|
2021-05-06 17:56:57 +01:00
|
|
|
func (emptyObjectIterator) Item() ObjectInfo { return ObjectInfo{} }
|