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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-04-06 20:19:11 +01:00
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
2022-03-07 00:54:48 +00:00
|
|
|
// CreateOptions contains extra options to create an object.
|
|
|
|
type CreateOptions struct {
|
2022-05-13 11:21:01 +01:00
|
|
|
Expires time.Time
|
|
|
|
Metadata map[string]string
|
2022-03-07 00:54:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 00:47:53 +01:00
|
|
|
// ListOptions describes options to the List command.
|
|
|
|
type ListOptions struct {
|
|
|
|
Recursive bool
|
|
|
|
Pending bool
|
2021-10-28 17:51:33 +01:00
|
|
|
Expanded bool
|
2021-10-02 00:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lo *ListOptions) isRecursive() bool { return lo != nil && lo.Recursive }
|
|
|
|
func (lo *ListOptions) isPending() bool { return lo != nil && lo.Pending }
|
|
|
|
|
|
|
|
// RemoveOptions describes options to the Remove command.
|
|
|
|
type RemoveOptions struct {
|
|
|
|
Pending bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ro *RemoveOptions) isPending() bool { return ro != nil && ro.Pending }
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
// Filesystem represents either the local filesystem or the data backed by a project.
|
2021-05-06 17:56:57 +01:00
|
|
|
type Filesystem interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
Close() error
|
2021-12-09 18:36:11 +00:00
|
|
|
Open(ctx clingy.Context, loc ulloc.Location) (MultiReadHandle, error)
|
2022-03-07 00:54:48 +00:00
|
|
|
Create(ctx clingy.Context, loc ulloc.Location, opts *CreateOptions) (MultiWriteHandle, error)
|
2021-10-05 17:48:13 +01:00
|
|
|
Move(ctx clingy.Context, source, dest ulloc.Location) error
|
2022-03-15 10:07:19 +00:00
|
|
|
Copy(ctx clingy.Context, source, dest ulloc.Location) error
|
2021-10-02 00:47:53 +01:00
|
|
|
Remove(ctx context.Context, loc ulloc.Location, opts *RemoveOptions) error
|
|
|
|
List(ctx context.Context, prefix ulloc.Location, opts *ListOptions) (ObjectIterator, error)
|
2021-05-06 17:56:57 +01:00
|
|
|
IsLocalDir(ctx context.Context, loc ulloc.Location) bool
|
2021-10-26 11:49:03 +01:00
|
|
|
Stat(ctx context.Context, loc ulloc.Location) (*ObjectInfo, error)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
// FilesystemLocal is the interface for a local filesystem.
|
|
|
|
type FilesystemLocal interface {
|
|
|
|
IsLocalDir(ctx context.Context, path string) bool
|
|
|
|
Open(ctx context.Context, path string) (MultiReadHandle, error)
|
|
|
|
Create(ctx context.Context, path string) (MultiWriteHandle, error)
|
|
|
|
Move(ctx context.Context, oldpath string, newpath string) error
|
|
|
|
Copy(ctx context.Context, oldpath string, newpath string) error
|
|
|
|
Remove(ctx context.Context, path string, opts *RemoveOptions) error
|
|
|
|
List(ctx context.Context, path string, opts *ListOptions) (ObjectIterator, error)
|
|
|
|
Stat(ctx context.Context, path string) (*ObjectInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilesystemRemote is the interface for a remote filesystem.
|
|
|
|
type FilesystemRemote interface {
|
|
|
|
Close() error
|
|
|
|
Open(ctx context.Context, bucket, key string) (MultiReadHandle, error)
|
|
|
|
Create(ctx context.Context, bucket, key string, opts *CreateOptions) (MultiWriteHandle, error)
|
|
|
|
Move(ctx context.Context, oldbucket, oldkey string, newbucket, newkey string) error
|
|
|
|
Copy(ctx context.Context, oldbucket, oldkey string, newbucket, newkey string) error
|
|
|
|
Remove(ctx context.Context, bucket, key string, opts *RemoveOptions) error
|
|
|
|
List(ctx context.Context, bucket, key string, opts *ListOptions) ObjectIterator
|
|
|
|
Stat(ctx context.Context, bucket, key string) (*ObjectInfo, error)
|
|
|
|
}
|
|
|
|
|
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
|
2021-10-28 17:51:33 +01:00
|
|
|
Expires time.Time
|
|
|
|
Metadata uplink.CustomMetadata
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2021-10-28 17:51:33 +01:00
|
|
|
Expires: obj.System.Expires,
|
|
|
|
Metadata: obj.Custom,
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2021-10-28 17:51:33 +01:00
|
|
|
Expires: upl.System.Expires,
|
|
|
|
Metadata: upl.Custom,
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// read handles
|
|
|
|
//
|
|
|
|
|
2021-12-09 18:36:11 +00:00
|
|
|
// MultiReadHandle allows one to read different sections of something.
|
|
|
|
// The offset parameter can be negative to signal that the offset should
|
|
|
|
// start that many bytes back from the end. Any negative value for length
|
|
|
|
// indicates to read up to the end.
|
|
|
|
//
|
|
|
|
// TODO: A negative offset requires a negative length, but there is no
|
|
|
|
// reason why that must be so.
|
|
|
|
type MultiReadHandle interface {
|
2021-04-06 20:19:11 +01:00
|
|
|
io.Closer
|
2021-12-09 18:36:11 +00:00
|
|
|
SetOffset(offset int64) error
|
|
|
|
NextPart(ctx context.Context, length int64) (ReadHandle, error)
|
|
|
|
Info(ctx context.Context) (*ObjectInfo, error)
|
2022-05-12 21:40:26 +01:00
|
|
|
Length() int64
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 18:36:11 +00:00
|
|
|
// ReadHandle is something that can be read from distinct parts possibly
|
|
|
|
// in parallel.
|
|
|
|
type ReadHandle interface {
|
|
|
|
io.Closer
|
|
|
|
io.Reader
|
|
|
|
Info() ObjectInfo
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// write handles
|
|
|
|
//
|
|
|
|
|
2021-12-09 19:03:42 +00:00
|
|
|
// MultiWriteHandle lets one create multiple sequential WriteHandles for
|
|
|
|
// different sections of something.
|
|
|
|
//
|
|
|
|
// The returned WriteHandle will error if data is attempted to be written
|
|
|
|
// past the provided length. A negative length implies an unknown amount
|
|
|
|
// of data, and future calls to NextPart will error.
|
|
|
|
type MultiWriteHandle interface {
|
|
|
|
NextPart(ctx context.Context, length int64) (WriteHandle, error)
|
|
|
|
Commit(ctx context.Context) error
|
|
|
|
Abort(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
}
|