combine: fix CopyDirMetadata error on upstream root

Before this change, operations.CopyDirMetadata would fail with: `internal error:
expecting directory string from combine root '' to have SetMetadata method:
optional feature not implemented` if the dst was the root directory of a combine
upstream. This is because combine was returning a *fs.Dir, which does not
satisfy the fs.SetMetadataer interface.

While it is true that combine cannot set metadata on the root of an upstream
(see also #7652), this should not be considered an error that causes sync to do
high-level retries, abort without doing deletes, etc.

This change addresses the issue by creating a new type of DirWrapper that is
allowed to fail silently, for exceptional cases such as this where certain
special directories have more limited abilities than what the Fs usually
supports.

It is possible that other similar wrapping backends (Union?) may need this same
fix.
This commit is contained in:
nielash 2024-03-04 02:48:34 -05:00 committed by Nick Craig-Wood
parent 6a72cfd6e1
commit 252562d00a
2 changed files with 23 additions and 4 deletions

View File

@ -813,7 +813,7 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
if f.root == "" && dir == "" { if f.root == "" && dir == "" {
entries = make(fs.DirEntries, 0, len(f.upstreams)) entries = make(fs.DirEntries, 0, len(f.upstreams))
for combineDir := range f.upstreams { for combineDir := range f.upstreams {
d := fs.NewDir(combineDir, f.when) d := fs.NewLimitedDirWrapper(combineDir, fs.NewDir(combineDir, f.when))
entries = append(entries, d) entries = append(entries, d)
} }
return entries, nil return entries, nil
@ -1002,7 +1002,7 @@ func (f *Fs) DirSetModTime(ctx context.Context, dir string, modTime time.Time) e
return err return err
} }
if uDir == "" { if uDir == "" {
fs.Debugf(dir, "can't set modtime on upstream root. skipping.") fs.Debugf(dir, "Can't set modtime on upstream root. skipping.")
return nil return nil
} }
if do := u.f.Features().DirSetModTime; do != nil { if do := u.f.Features().DirSetModTime; do != nil {

View File

@ -7,8 +7,9 @@ import (
// DirWrapper wraps a Directory object so the Remote can be overridden // DirWrapper wraps a Directory object so the Remote can be overridden
type DirWrapper struct { type DirWrapper struct {
Directory // Directory we are wrapping Directory // Directory we are wrapping
remote string // name of the directory remote string // name of the directory
failSilently bool // if set, ErrorNotImplemented should not be considered an error for this directory
} }
// NewDirWrapper creates a wrapper for a directory object // NewDirWrapper creates a wrapper for a directory object
@ -22,6 +23,16 @@ func NewDirWrapper(remote string, d Directory) *DirWrapper {
} }
} }
// NewLimitedDirWrapper creates a DirWrapper that should fail silently instead of erroring for ErrorNotImplemented.
//
// Intended for exceptional dirs lacking abilities that the Fs otherwise usually supports
// (ex. a Combine root which can't set metadata/modtime, regardless of support by wrapped backend)
func NewLimitedDirWrapper(remote string, d Directory) *DirWrapper {
dw := NewDirWrapper(remote, d)
dw.failSilently = true
return dw
}
// String returns the name // String returns the name
func (d *DirWrapper) String() string { func (d *DirWrapper) String() string {
return d.remote return d.remote
@ -55,6 +66,10 @@ func (d *DirWrapper) Metadata(ctx context.Context) (Metadata, error) {
func (d *DirWrapper) SetMetadata(ctx context.Context, metadata Metadata) error { func (d *DirWrapper) SetMetadata(ctx context.Context, metadata Metadata) error {
do, ok := d.Directory.(SetMetadataer) do, ok := d.Directory.(SetMetadataer)
if !ok { if !ok {
if d.failSilently {
Debugf(d, "Can't SetMetadata for this directory (%T from %v) -- skipping", d.Directory, d.Fs())
return nil
}
return ErrorNotImplemented return ErrorNotImplemented
} }
return do.SetMetadata(ctx, metadata) return do.SetMetadata(ctx, metadata)
@ -66,6 +81,10 @@ func (d *DirWrapper) SetMetadata(ctx context.Context, metadata Metadata) error {
func (d *DirWrapper) SetModTime(ctx context.Context, t time.Time) error { func (d *DirWrapper) SetModTime(ctx context.Context, t time.Time) error {
do, ok := d.Directory.(SetModTimer) do, ok := d.Directory.(SetModTimer)
if !ok { if !ok {
if d.failSilently {
Debugf(d, "Can't SetModTime for this directory (%T from %v) -- skipping", d.Directory, d.Fs())
return nil
}
return ErrorNotImplemented return ErrorNotImplemented
} }
return do.SetModTime(ctx, t) return do.SetModTime(ctx, t)