2019-11-30 15:41:39 +01:00
|
|
|
package union
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-29 15:29:15 +02:00
|
|
|
"errors"
|
2021-11-04 11:12:57 +01:00
|
|
|
"fmt"
|
2019-11-30 15:41:39 +01:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/backend/union/upstream"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Object describes a union Object
|
|
|
|
//
|
|
|
|
// This is a wrapped object which returns the Union Fs as its parent
|
|
|
|
type Object struct {
|
|
|
|
*upstream.Object
|
2023-07-22 09:23:57 +02:00
|
|
|
fs *Fs // what this object is part of
|
|
|
|
co []upstream.Entry
|
|
|
|
writebackMu sync.Mutex
|
2019-11-30 15:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directory describes a union Directory
|
|
|
|
//
|
|
|
|
// This is a wrapped object contains all candidates
|
|
|
|
type Directory struct {
|
|
|
|
*upstream.Directory
|
2024-02-27 12:04:38 +01:00
|
|
|
fs *Fs // what this directory is part of
|
2019-11-30 15:41:39 +01:00
|
|
|
cd []upstream.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
type entry interface {
|
|
|
|
upstream.Entry
|
|
|
|
candidates() []upstream.Entry
|
|
|
|
}
|
|
|
|
|
2023-07-22 09:23:57 +02:00
|
|
|
// Update o with the contents of newO excluding the lock
|
|
|
|
func (o *Object) update(newO *Object) {
|
|
|
|
o.Object = newO.Object
|
|
|
|
o.fs = newO.fs
|
|
|
|
o.co = newO.co
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:29:15 +02:00
|
|
|
// UnWrapUpstream returns the upstream Object that this Object is wrapping
|
|
|
|
func (o *Object) UnWrapUpstream() *upstream.Object {
|
2019-11-30 15:41:39 +01:00
|
|
|
return o.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fs returns the union Fs as the parent
|
|
|
|
func (o *Object) Fs() fs.Info {
|
|
|
|
return o.fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) candidates() []upstream.Entry {
|
|
|
|
return o.co
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Directory) candidates() []upstream.Entry {
|
|
|
|
return d.cd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update in to the object with the modTime given of the given size
|
|
|
|
//
|
2020-05-25 08:05:53 +02:00
|
|
|
// When called from outside an Fs by rclone, src.Size() will always be >= 0.
|
2019-11-30 15:41:39 +01:00
|
|
|
// But for unknown-sized objects (indicated by src.Size() == -1), Upload should either
|
|
|
|
// return an error or update the object properly (rather than e.g. calling panic).
|
|
|
|
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
|
|
|
entries, err := o.fs.actionEntries(o.candidates()...)
|
2021-01-15 13:18:28 +01:00
|
|
|
if err == fs.ErrorPermissionDenied {
|
|
|
|
// There are no candidates in this object which can be written to
|
|
|
|
// So attempt to create a new object instead
|
|
|
|
newO, err := o.fs.put(ctx, in, src, false, options...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update current object
|
2023-07-22 09:23:57 +02:00
|
|
|
o.update(newO.(*Object))
|
2021-01-15 13:18:28 +01:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2019-11-30 15:41:39 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(entries) == 1 {
|
|
|
|
obj := entries[0].(*upstream.Object)
|
|
|
|
return obj.Update(ctx, in, src, options...)
|
|
|
|
}
|
|
|
|
// Multi-threading
|
2020-08-30 19:30:45 +02:00
|
|
|
readers, errChan := multiReader(len(entries), in)
|
|
|
|
errs := Errors(make([]error, len(entries)+1))
|
2019-11-30 15:41:39 +01:00
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
if o, ok := entries[i].(*upstream.Object); ok {
|
|
|
|
err := o.Update(ctx, readers[i], src, options...)
|
2021-11-09 14:00:51 +01:00
|
|
|
if err != nil {
|
|
|
|
errs[i] = fmt.Errorf("%s: %w", o.UpstreamFs().Name(), err)
|
2022-03-07 19:21:16 +01:00
|
|
|
if len(entries) > 1 {
|
|
|
|
// Drain the input buffer to allow other uploads to continue
|
2022-08-20 16:38:02 +02:00
|
|
|
_, _ = io.Copy(io.Discard, readers[i])
|
2022-03-07 19:21:16 +01:00
|
|
|
}
|
2021-11-09 14:00:51 +01:00
|
|
|
}
|
2019-11-30 15:41:39 +01:00
|
|
|
} else {
|
|
|
|
errs[i] = fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
})
|
2020-08-30 19:30:45 +02:00
|
|
|
errs[len(entries)] = <-errChan
|
2019-11-30 15:41:39 +01:00
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove candidate objects selected by ACTION policy
|
|
|
|
func (o *Object) Remove(ctx context.Context) error {
|
|
|
|
entries, err := o.fs.actionEntries(o.candidates()...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errs := Errors(make([]error, len(entries)))
|
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
if o, ok := entries[i].(*upstream.Object); ok {
|
|
|
|
err := o.Remove(ctx)
|
2021-11-09 14:00:51 +01:00
|
|
|
if err != nil {
|
|
|
|
errs[i] = fmt.Errorf("%s: %w", o.UpstreamFs().Name(), err)
|
|
|
|
}
|
2019-11-30 15:41:39 +01:00
|
|
|
} else {
|
|
|
|
errs[i] = fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the metadata on the object to set the modification date
|
|
|
|
func (o *Object) SetModTime(ctx context.Context, t time.Time) error {
|
|
|
|
entries, err := o.fs.actionEntries(o.candidates()...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errs := Errors(make([]error, len(entries)))
|
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
if o, ok := entries[i].(*upstream.Object); ok {
|
|
|
|
err := o.SetModTime(ctx, t)
|
2021-11-09 14:00:51 +01:00
|
|
|
if err != nil {
|
|
|
|
errs[i] = fmt.Errorf("%s: %w", o.UpstreamFs().Name(), err)
|
|
|
|
}
|
2019-11-30 15:41:39 +01:00
|
|
|
} else {
|
|
|
|
errs[i] = fs.ErrorNotAFile
|
|
|
|
}
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:29:15 +02:00
|
|
|
// GetTier returns storage tier or class of the Object
|
|
|
|
func (o *Object) GetTier() string {
|
|
|
|
do, ok := o.Object.Object.(fs.GetTierer)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return do.GetTier()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the ID of the Object if known, or "" if not
|
|
|
|
func (o *Object) ID() string {
|
|
|
|
do, ok := o.Object.Object.(fs.IDer)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return do.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MimeType returns the content type of the Object if known
|
|
|
|
func (o *Object) MimeType(ctx context.Context) (mimeType string) {
|
|
|
|
if do, ok := o.Object.Object.(fs.MimeTyper); ok {
|
|
|
|
mimeType = do.MimeType(ctx)
|
|
|
|
}
|
|
|
|
return mimeType
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTier performs changing storage tier of the Object if
|
|
|
|
// multiple storage classes supported
|
|
|
|
func (o *Object) SetTier(tier string) error {
|
|
|
|
do, ok := o.Object.Object.(fs.SetTierer)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("underlying remote does not support SetTier")
|
|
|
|
}
|
|
|
|
return do.SetTier(tier)
|
|
|
|
}
|
|
|
|
|
2023-07-22 09:23:57 +02:00
|
|
|
// Open opens the file for read. Call Close() on the returned io.ReadCloser
|
|
|
|
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
|
|
|
|
// Need some sort of locking to prevent multiple downloads
|
|
|
|
o.writebackMu.Lock()
|
|
|
|
defer o.writebackMu.Unlock()
|
|
|
|
|
|
|
|
// FIXME what if correct object is already in o.co
|
|
|
|
|
|
|
|
newObj, err := o.Object.Writeback(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if newObj != nil {
|
|
|
|
o.Object = newObj
|
|
|
|
o.co = append(o.co, newObj) // FIXME should this append or overwrite or update?
|
|
|
|
}
|
|
|
|
return o.Object.Object.Open(ctx, options...)
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:41:39 +01:00
|
|
|
// ModTime returns the modification date of the directory
|
|
|
|
// It returns the latest ModTime of all candidates
|
|
|
|
func (d *Directory) ModTime(ctx context.Context) (t time.Time) {
|
|
|
|
entries := d.candidates()
|
|
|
|
times := make([]time.Time, len(entries))
|
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
times[i] = entries[i].ModTime(ctx)
|
|
|
|
})
|
|
|
|
for _, ti := range times {
|
|
|
|
if t.Before(ti) {
|
|
|
|
t = ti
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the directory
|
|
|
|
// It returns the sum of all candidates
|
|
|
|
func (d *Directory) Size() (s int64) {
|
|
|
|
for _, e := range d.candidates() {
|
|
|
|
s += e.Size()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2022-06-29 15:29:15 +02:00
|
|
|
|
2024-02-27 12:04:38 +01:00
|
|
|
// SetMetadata sets metadata for an DirEntry
|
|
|
|
//
|
|
|
|
// It should return fs.ErrorNotImplemented if it can't set metadata
|
|
|
|
func (d *Directory) SetMetadata(ctx context.Context, metadata fs.Metadata) error {
|
|
|
|
entries, err := d.fs.actionEntries(d.candidates()...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errs := Errors(make([]error, len(entries)))
|
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
if d, ok := entries[i].(*upstream.Directory); ok {
|
|
|
|
err := d.SetMetadata(ctx, metadata)
|
|
|
|
if err != nil {
|
|
|
|
errs[i] = fmt.Errorf("%s: %w", d.UpstreamFs().Name(), err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errs[i] = fs.ErrorIsFile
|
|
|
|
}
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the metadata on the DirEntry to set the modification date
|
|
|
|
//
|
|
|
|
// If there is any other metadata it does not overwrite it.
|
|
|
|
func (d *Directory) SetModTime(ctx context.Context, t time.Time) error {
|
|
|
|
entries, err := d.fs.actionEntries(d.candidates()...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errs := Errors(make([]error, len(entries)))
|
|
|
|
multithread(len(entries), func(i int) {
|
|
|
|
if d, ok := entries[i].(*upstream.Directory); ok {
|
|
|
|
err := d.SetModTime(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
errs[i] = fmt.Errorf("%s: %w", d.UpstreamFs().Name(), err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errs[i] = fs.ErrorIsFile
|
|
|
|
}
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:29:15 +02:00
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var (
|
2024-02-27 12:04:38 +01:00
|
|
|
_ fs.FullObject = (*Object)(nil)
|
|
|
|
_ fs.FullDirectory = (*Directory)(nil)
|
2022-06-29 15:29:15 +02:00
|
|
|
)
|