1
0
mirror of https://github.com/rclone/rclone.git synced 2025-02-14 21:23:01 +02:00

fs: Implement UnWrapObject and UnWrapFs

This commit is contained in:
Nick Craig-Wood 2019-08-12 22:03:53 +01:00
parent 7c146e2618
commit 752d43d6fa
3 changed files with 35 additions and 22 deletions

View File

@ -1023,6 +1023,38 @@ type ObjectPair struct {
Src, Dst Object Src, Dst Object
} }
// UnWrapFs unwraps f as much as possible and returns the base Fs
func UnWrapFs(f Fs) Fs {
for {
unwrap := f.Features().UnWrap
if unwrap == nil {
break // not a wrapped Fs, use current
}
next := unwrap()
if next == nil {
break // no base Fs found, use current
}
f = next
}
return f
}
// UnWrapObject unwraps o as much as possible and returns the base object
func UnWrapObject(o Object) Object {
for {
u, ok := o.(ObjectUnWrapper)
if !ok {
break // not a wrapped object, use current
}
next := u.UnWrap()
if next == nil {
break // no base object found, use current
}
o = next
}
return o
}
// Find looks for an RegInfo object for the name passed in. The name // Find looks for an RegInfo object for the name passed in. The name
// can be either the Name or the Prefix. // can be either the Name or the Prefix.
// //

View File

@ -136,20 +136,8 @@ func ListJSON(ctx context.Context, fsrc fs.Fs, remote string, opt *ListJSONOpt,
if do, ok := entry.(fs.IDer); ok { if do, ok := entry.(fs.IDer); ok {
item.ID = do.ID() item.ID = do.ID()
} }
if opt.ShowOrigIDs { if o, ok := entry.(fs.Object); opt.ShowOrigIDs && ok {
cur := entry if do, ok := fs.UnWrapObject(o).(fs.IDer); ok {
for {
u, ok := cur.(fs.ObjectUnWrapper)
if !ok {
break // not a wrapped object, use current id
}
next := u.UnWrap()
if next == nil {
break // no base object found, use current id
}
cur = next
}
if do, ok := cur.(fs.IDer); ok {
item.OrigID = do.ID() item.OrigID = do.ID()
} }
} }

View File

@ -330,14 +330,7 @@ func Run(t *testing.T, opt *Opt) {
// Return true if f (or any of the things it wraps) is bucket // Return true if f (or any of the things it wraps) is bucket
// based but not at the root. // based but not at the root.
isBucketBasedButNotRoot := func(f fs.Fs) bool { isBucketBasedButNotRoot := func(f fs.Fs) bool {
for { return fs.UnWrapFs(f).Features().BucketBased && strings.Contains(strings.Trim(f.Root(), "/"), "/")
doUnWrap := f.Features().UnWrap
if doUnWrap == nil {
break
}
f = doUnWrap()
}
return f.Features().BucketBased && strings.Contains(strings.Trim(f.Root(), "/"), "/")
} }
// Remove bad characters from Windows file name if set // Remove bad characters from Windows file name if set