Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb17dd9fe7 | ||
|
|
14b56fa7dc |
No files matched your search
+603
-139
@@ -29,6 +29,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
@@ -222,17 +223,17 @@ All other operations will be disabled.`,
|
|||||||
}, {
|
}, {
|
||||||
Name: "shared_folders",
|
Name: "shared_folders",
|
||||||
Help: `Instructs rclone to work on shared folders.
|
Help: `Instructs rclone to work on shared folders.
|
||||||
|
|
||||||
When this flag is used with no path only the List operation is supported and
|
|
||||||
all available shared folders will be listed. If you specify a path the first part
|
|
||||||
will be interpreted as the name of shared folder. Rclone will then try to mount this
|
|
||||||
shared to the root namespace. On success shared folder rclone proceeds normally.
|
|
||||||
The shared folder is now pretty much a normal folder and all normal operations
|
|
||||||
are supported.
|
|
||||||
|
|
||||||
Note that we don't unmount the shared folder afterwards so the
|
When this flag is used with no path only the List operation is supported and
|
||||||
--dropbox-shared-folders can be omitted after the first use of a particular
|
all available shared folders will be listed. If you specify a path the first part
|
||||||
shared folder.
|
will be interpreted as the name of shared folder and rclone will list and access
|
||||||
|
files within it using the shared folder's namespace. This works for all shared
|
||||||
|
folder types including team folders.
|
||||||
|
|
||||||
|
Write operations (upload, delete, mkdir, copy, move) are supported within
|
||||||
|
shared folders. Server-side copy and move operations only work within the
|
||||||
|
same shared folder - cross-shared-folder operations will fall back to
|
||||||
|
upload/download.
|
||||||
|
|
||||||
See also --dropbox-root-namespace for an alternative way to work with shared
|
See also --dropbox-root-namespace for an alternative way to work with shared
|
||||||
folders.`,
|
folders.`,
|
||||||
@@ -320,22 +321,25 @@ type Options struct {
|
|||||||
|
|
||||||
// Fs represents a remote dropbox server
|
// Fs represents a remote dropbox server
|
||||||
type Fs struct {
|
type Fs struct {
|
||||||
name string // name of this remote
|
name string // name of this remote
|
||||||
root string // the path we are working on
|
root string // the path we are working on
|
||||||
opt Options // parsed options
|
opt Options // parsed options
|
||||||
ci *fs.ConfigInfo // global config
|
ci *fs.ConfigInfo // global config
|
||||||
features *fs.Features // optional features
|
features *fs.Features // optional features
|
||||||
srv files.Client // the connection to the dropbox server
|
srv files.Client // the connection to the dropbox server
|
||||||
svc files.Client // the connection to the dropbox server (unauthorized)
|
svc files.Client // the connection to the dropbox server (unauthorized)
|
||||||
sharing sharing.Client // as above, but for generating sharing links
|
sharing sharing.Client // as above, but for generating sharing links
|
||||||
users users.Client // as above, but for accessing user information
|
users users.Client // as above, but for accessing user information
|
||||||
team team.Client // for the Teams API
|
team team.Client // for the Teams API
|
||||||
slashRoot string // root with "/" prefix, lowercase
|
slashRoot string // root with "/" prefix, lowercase
|
||||||
slashRootSlash string // root with "/" prefix and postfix, lowercase
|
slashRootSlash string // root with "/" prefix and postfix, lowercase
|
||||||
pacer *fs.Pacer // To pace the API calls
|
pacer *fs.Pacer // To pace the API calls
|
||||||
ns string // The namespace we are using or "" for none
|
ns string // The namespace we are using or "" for none
|
||||||
batcher *batcher.Batcher[*files.UploadSessionFinishArg, *files.FileMetadata]
|
batcher *batcher.Batcher[*files.UploadSessionFinishArg, *files.FileMetadata]
|
||||||
exportExts []exportExtension
|
exportExts []exportExtension
|
||||||
|
cfg dropbox.Config // SDK config for creating per-namespace clients
|
||||||
|
sharedFolderMu sync.Mutex // protects sharedFolderIDs
|
||||||
|
sharedFolderIDs map[string]string // cache of shared folder name → namespace ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type exportType int
|
type exportType int
|
||||||
@@ -358,11 +362,50 @@ type Object struct {
|
|||||||
bytes int64 // size of the object
|
bytes int64 // size of the object
|
||||||
modTime time.Time // time it was last modified
|
modTime time.Time // time it was last modified
|
||||||
hash string // content_hash of the object
|
hash string // content_hash of the object
|
||||||
|
nsID string // shared folder namespace ID, if applicable
|
||||||
|
|
||||||
exportType exportType
|
exportType exportType
|
||||||
exportAPIFormat exportAPIFormat
|
exportAPIFormat exportAPIFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fileSrv returns the files client to use for this object.
|
||||||
|
// If the object belongs to a shared folder, it returns a
|
||||||
|
// namespace-scoped client; otherwise it returns the default client.
|
||||||
|
func (o *Object) fileSrv() files.Client {
|
||||||
|
if o.nsID != "" {
|
||||||
|
nsCfg := o.fs.cfg.WithNamespaceID(o.nsID)
|
||||||
|
return files.New(nsCfg)
|
||||||
|
}
|
||||||
|
return o.fs.srv
|
||||||
|
}
|
||||||
|
|
||||||
|
// sharedFolderSrvAndPath resolves an rclone path in shared_folders mode
|
||||||
|
// to a namespace-scoped files client and the API path within that namespace.
|
||||||
|
// The rclone path format is "SharedFolderName/sub/path/file.txt".
|
||||||
|
func (f *Fs) sharedFolderSrvAndPath(ctx context.Context, rclonePath string) (srv files.Client, apiPath string, nsID string, err error) {
|
||||||
|
firstDir, subPath, _ := strings.Cut(rclonePath, "/")
|
||||||
|
nsID, err = f.findSharedFolder(ctx, firstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", "", err
|
||||||
|
}
|
||||||
|
srv = files.New(f.cfg.WithNamespaceID(nsID))
|
||||||
|
if subPath != "" {
|
||||||
|
apiPath = f.opt.Enc.FromStandardPath("/" + subPath)
|
||||||
|
}
|
||||||
|
return srv, apiPath, nsID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// nsPath returns the encoded API path for use with namespace-scoped clients.
|
||||||
|
// In shared_folders mode the object's remote is "SharedFolder/sub/file.txt"
|
||||||
|
// but the API path within the namespace is just "/sub/file.txt".
|
||||||
|
func (o *Object) nsPath() string {
|
||||||
|
if o.nsID != "" {
|
||||||
|
_, subPath, _ := strings.Cut(o.remote, "/")
|
||||||
|
return o.fs.opt.Enc.FromStandardPath("/" + subPath)
|
||||||
|
}
|
||||||
|
return o.fs.opt.Enc.FromStandardPath(o.remotePath())
|
||||||
|
}
|
||||||
|
|
||||||
// Name of the remote (as passed into NewFs)
|
// Name of the remote (as passed into NewFs)
|
||||||
func (f *Fs) Name() string {
|
func (f *Fs) Name() string {
|
||||||
return f.name
|
return f.name
|
||||||
@@ -537,6 +580,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
|||||||
cfg.AsMemberID = memberIDs[0].MemberInfo.Profile.MemberProfile.TeamMemberId
|
cfg.AsMemberID = memberIDs[0].MemberInfo.Profile.MemberProfile.TeamMemberId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.cfg = cfg
|
||||||
f.srv = files.New(cfg)
|
f.srv = files.New(cfg)
|
||||||
f.svc = files.New(ucfg)
|
f.svc = files.New(ucfg)
|
||||||
f.sharing = sharing.New(cfg)
|
f.sharing = sharing.New(cfg)
|
||||||
@@ -564,7 +608,8 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
|||||||
if f.opt.SharedFolders {
|
if f.opt.SharedFolders {
|
||||||
f.setRoot(root)
|
f.setRoot(root)
|
||||||
if f.root == "" {
|
if f.root == "" {
|
||||||
return f, nil // our root it empty so we probably want to list shared folders
|
f.features.Fill(ctx, f)
|
||||||
|
return f, nil // our root is empty so we probably want to list shared folders
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := path.Dir(f.root)
|
dir := path.Dir(f.root)
|
||||||
@@ -830,9 +875,53 @@ func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
|||||||
if f.opt.SharedFiles {
|
if f.opt.SharedFiles {
|
||||||
return f.findSharedFile(ctx, remote)
|
return f.findSharedFile(ctx, remote)
|
||||||
}
|
}
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.newSharedFolderObject(ctx, remote)
|
||||||
|
}
|
||||||
return f.newObjectWithInfo(ctx, remote, nil)
|
return f.newObjectWithInfo(ctx, remote, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newSharedFolderObject finds an Object inside a shared folder by
|
||||||
|
// using the shared folder's namespace ID to resolve the path.
|
||||||
|
func (f *Fs) newSharedFolderObject(ctx context.Context, remote string) (fs.Object, error) {
|
||||||
|
firstDir, subPath, ok := strings.Cut(remote, "/")
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorObjectNotFound
|
||||||
|
}
|
||||||
|
id, err := f.findSharedFolder(ctx, firstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
nsCfg := f.cfg.WithNamespaceID(id)
|
||||||
|
nsSrv := files.New(nsCfg)
|
||||||
|
|
||||||
|
filePath := f.opt.Enc.FromStandardPath("/" + subPath)
|
||||||
|
var entry files.IsMetadata
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
entry, err = nsSrv.GetMetadata(&files.GetMetadataArg{Path: filePath})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
switch e := err.(type) {
|
||||||
|
case files.GetMetadataAPIError:
|
||||||
|
if e.EndpointError != nil && e.EndpointError.Path != nil && e.EndpointError.Path.Tag == files.LookupErrorNotFound {
|
||||||
|
return nil, fs.ErrorObjectNotFound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fileInfo, ok := entry.(*files.FileMetadata)
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorObjectNotFound
|
||||||
|
}
|
||||||
|
o, err := f.newObjectWithInfo(ctx, remote, fileInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
o.(*Object).nsID = id
|
||||||
|
return o, nil
|
||||||
|
}
|
||||||
|
|
||||||
// listSharedFolders lists all available shared folders mounted and not mounted
|
// listSharedFolders lists all available shared folders mounted and not mounted
|
||||||
// we'll need the id later so we have to return them in original format
|
// we'll need the id later so we have to return them in original format
|
||||||
func (f *Fs) listSharedFolders(ctx context.Context, callback func(fs.DirEntry) error) (err error) {
|
func (f *Fs) listSharedFolders(ctx context.Context, callback func(fs.DirEntry) error) (err error) {
|
||||||
@@ -879,24 +968,128 @@ func (f *Fs) listSharedFolders(ctx context.Context, callback func(fs.DirEntry) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listDir lists the contents of a directory using the given files client.
|
||||||
|
// root is the Dropbox API path to list, dir is the rclone directory prefix for results.
|
||||||
|
// nsID is the shared folder namespace ID to set on created objects (empty for normal listings).
|
||||||
|
func (f *Fs) listDir(ctx context.Context, srv files.Client, root string, dir string, nsID string, list *list.Helper) (err error) {
|
||||||
|
started := false
|
||||||
|
var res *files.ListFolderResult
|
||||||
|
for {
|
||||||
|
if !started {
|
||||||
|
arg := files.NewListFolderArg(f.opt.Enc.FromStandardPath(root))
|
||||||
|
arg.Recursive = false
|
||||||
|
arg.Limit = 1000
|
||||||
|
if root == "/" || root == "" {
|
||||||
|
arg.Path = "" // Specify root folder as empty string
|
||||||
|
}
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
res, err = srv.ListFolder(arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
switch e := err.(type) {
|
||||||
|
case files.ListFolderAPIError:
|
||||||
|
if e.EndpointError != nil && e.EndpointError.Path != nil && e.EndpointError.Path.Tag == files.LookupErrorNotFound {
|
||||||
|
err = fs.ErrorDirNotFound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
started = true
|
||||||
|
} else {
|
||||||
|
arg := files.ListFolderContinueArg{
|
||||||
|
Cursor: res.Cursor,
|
||||||
|
}
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
res, err = srv.ListFolderContinue(&arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("list continue: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, entry := range res.Entries {
|
||||||
|
var fileInfo *files.FileMetadata
|
||||||
|
var folderInfo *files.FolderMetadata
|
||||||
|
var metadata *files.Metadata
|
||||||
|
switch info := entry.(type) {
|
||||||
|
case *files.FolderMetadata:
|
||||||
|
folderInfo = info
|
||||||
|
metadata = &info.Metadata
|
||||||
|
case *files.FileMetadata:
|
||||||
|
fileInfo = info
|
||||||
|
metadata = &info.Metadata
|
||||||
|
default:
|
||||||
|
fs.Errorf(f, "Unknown type %T", entry)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the last element is reliably cased in PathDisplay
|
||||||
|
entryPath := metadata.PathDisplay
|
||||||
|
leaf := f.opt.Enc.ToStandardName(path.Base(entryPath))
|
||||||
|
remote := path.Join(dir, leaf)
|
||||||
|
if folderInfo != nil {
|
||||||
|
d := fs.NewDir(remote, time.Time{}).SetID(folderInfo.Id)
|
||||||
|
err = list.Add(d)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if fileInfo != nil {
|
||||||
|
o, err := f.newObjectWithInfo(ctx, remote, fileInfo)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
o.(*Object).nsID = nsID
|
||||||
|
if o.(*Object).exportType.listable() {
|
||||||
|
err = list.Add(o)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !res.HasMore {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
// findSharedFolder find the id for a given shared folder name
|
// findSharedFolder find the id for a given shared folder name
|
||||||
// somewhat annoyingly there is no endpoint to query a shared folder by it's name
|
// somewhat annoyingly there is no endpoint to query a shared folder by it's name
|
||||||
// so our only option is to iterate over all shared folders
|
// so our only option is to iterate over all shared folders
|
||||||
|
//
|
||||||
|
// Results are cached for the lifetime of the Fs.
|
||||||
func (f *Fs) findSharedFolder(ctx context.Context, name string) (id string, err error) {
|
func (f *Fs) findSharedFolder(ctx context.Context, name string) (id string, err error) {
|
||||||
errFoundFile := errors.New("found file")
|
f.sharedFolderMu.Lock()
|
||||||
err = f.listSharedFolders(ctx, func(entry fs.DirEntry) error {
|
if f.sharedFolderIDs != nil {
|
||||||
if entry.(*fs.Dir).Remote() == name {
|
if cachedID, ok := f.sharedFolderIDs[name]; ok {
|
||||||
id = entry.(*fs.Dir).ID()
|
f.sharedFolderMu.Unlock()
|
||||||
return errFoundFile
|
return cachedID, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
f.sharedFolderMu.Unlock()
|
||||||
|
|
||||||
|
// Not cached - list all shared folders and populate the cache
|
||||||
|
found := make(map[string]string)
|
||||||
|
err = f.listSharedFolders(ctx, func(entry fs.DirEntry) error {
|
||||||
|
d := entry.(*fs.Dir)
|
||||||
|
found[d.Remote()] = d.ID()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if errors.Is(err, errFoundFile) {
|
if err != nil {
|
||||||
return id, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return "", fs.ErrorDirNotFound
|
|
||||||
|
f.sharedFolderMu.Lock()
|
||||||
|
f.sharedFolderIDs = found
|
||||||
|
f.sharedFolderMu.Unlock()
|
||||||
|
|
||||||
|
id, ok := found[name]
|
||||||
|
if !ok {
|
||||||
|
return "", fs.ErrorDirNotFound
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// mountSharedFolder mount a shared folder to the root namespace
|
// mountSharedFolder mount a shared folder to the root namespace
|
||||||
@@ -1018,99 +1211,34 @@ func (f *Fs) ListP(ctx context.Context, dir string, callback fs.ListRCallback) (
|
|||||||
return list.Flush()
|
return list.Flush()
|
||||||
}
|
}
|
||||||
if f.opt.SharedFolders {
|
if f.opt.SharedFolders {
|
||||||
err := f.listSharedFolders(ctx, list.Add)
|
if dir == "" {
|
||||||
|
err := f.listSharedFolders(ctx, list.Add)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return list.Flush()
|
||||||
|
}
|
||||||
|
// For subdirectories, use the shared folder's namespace
|
||||||
|
// to list its contents via the files API.
|
||||||
|
firstDir, subDir, _ := strings.Cut(dir, "/")
|
||||||
|
id, err := f.findSharedFolder(ctx, firstDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return list.Flush()
|
nsCfg := f.cfg.WithNamespaceID(id)
|
||||||
|
nsSrv := files.New(nsCfg)
|
||||||
|
root := ""
|
||||||
|
if subDir != "" {
|
||||||
|
root = "/" + subDir
|
||||||
|
}
|
||||||
|
return f.listDir(ctx, nsSrv, root, dir, id, list)
|
||||||
}
|
}
|
||||||
|
|
||||||
root := f.slashRoot
|
root := f.slashRoot
|
||||||
if dir != "" {
|
if dir != "" {
|
||||||
root += "/" + dir
|
root += "/" + dir
|
||||||
}
|
}
|
||||||
|
return f.listDir(ctx, f.srv, root, dir, "", list)
|
||||||
started := false
|
|
||||||
var res *files.ListFolderResult
|
|
||||||
for {
|
|
||||||
if !started {
|
|
||||||
arg := files.NewListFolderArg(f.opt.Enc.FromStandardPath(root))
|
|
||||||
arg.Recursive = false
|
|
||||||
arg.Limit = 1000
|
|
||||||
|
|
||||||
if root == "/" {
|
|
||||||
arg.Path = "" // Specify root folder as empty string
|
|
||||||
}
|
|
||||||
err = f.pacer.Call(func() (bool, error) {
|
|
||||||
res, err = f.srv.ListFolder(arg)
|
|
||||||
return shouldRetry(ctx, err)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
switch e := err.(type) {
|
|
||||||
case files.ListFolderAPIError:
|
|
||||||
if e.EndpointError != nil && e.EndpointError.Path != nil && e.EndpointError.Path.Tag == files.LookupErrorNotFound {
|
|
||||||
err = fs.ErrorDirNotFound
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
started = true
|
|
||||||
} else {
|
|
||||||
arg := files.ListFolderContinueArg{
|
|
||||||
Cursor: res.Cursor,
|
|
||||||
}
|
|
||||||
err = f.pacer.Call(func() (bool, error) {
|
|
||||||
res, err = f.srv.ListFolderContinue(&arg)
|
|
||||||
return shouldRetry(ctx, err)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("list continue: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, entry := range res.Entries {
|
|
||||||
var fileInfo *files.FileMetadata
|
|
||||||
var folderInfo *files.FolderMetadata
|
|
||||||
var metadata *files.Metadata
|
|
||||||
switch info := entry.(type) {
|
|
||||||
case *files.FolderMetadata:
|
|
||||||
folderInfo = info
|
|
||||||
metadata = &info.Metadata
|
|
||||||
case *files.FileMetadata:
|
|
||||||
fileInfo = info
|
|
||||||
metadata = &info.Metadata
|
|
||||||
default:
|
|
||||||
fs.Errorf(f, "Unknown type %T", entry)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only the last element is reliably cased in PathDisplay
|
|
||||||
entryPath := metadata.PathDisplay
|
|
||||||
leaf := f.opt.Enc.ToStandardName(path.Base(entryPath))
|
|
||||||
remote := path.Join(dir, leaf)
|
|
||||||
if folderInfo != nil {
|
|
||||||
d := fs.NewDir(remote, time.Time{}).SetID(folderInfo.Id)
|
|
||||||
err = list.Add(d)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else if fileInfo != nil {
|
|
||||||
o, err := f.newObjectWithInfo(ctx, remote, fileInfo)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if o.(*Object).exportType.listable() {
|
|
||||||
err = list.Add(o)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !res.HasMore {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list.Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the object
|
// Put the object
|
||||||
@@ -1119,7 +1247,7 @@ func (f *Fs) ListP(ctx context.Context, dir string, callback fs.ListRCallback) (
|
|||||||
//
|
//
|
||||||
// The new object may have been created if an error is returned
|
// The new object may have been created if an error is returned
|
||||||
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
||||||
if f.opt.SharedFiles || f.opt.SharedFolders {
|
if f.opt.SharedFiles {
|
||||||
return nil, errNotSupportedInSharedMode
|
return nil, errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
// Temporary Object under construction
|
// Temporary Object under construction
|
||||||
@@ -1127,6 +1255,17 @@ func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options .
|
|||||||
fs: f,
|
fs: f,
|
||||||
remote: src.Remote(),
|
remote: src.Remote(),
|
||||||
}
|
}
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
firstDir, _, ok := strings.Cut(src.Remote(), "/")
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("can't upload to shared folder root")
|
||||||
|
}
|
||||||
|
nsID, err := f.findSharedFolder(ctx, firstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
o.nsID = nsID
|
||||||
|
}
|
||||||
return o, o.Update(ctx, in, src, options...)
|
return o, o.Update(ctx, in, src, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1137,9 +1276,12 @@ func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, opt
|
|||||||
|
|
||||||
// Mkdir creates the container if it doesn't exist
|
// Mkdir creates the container if it doesn't exist
|
||||||
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
||||||
if f.opt.SharedFiles || f.opt.SharedFolders {
|
if f.opt.SharedFiles {
|
||||||
return errNotSupportedInSharedMode
|
return errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.mkdirSharedFolder(ctx, dir)
|
||||||
|
}
|
||||||
root := path.Join(f.slashRoot, dir)
|
root := path.Join(f.slashRoot, dir)
|
||||||
|
|
||||||
// can't create or run metadata on root
|
// can't create or run metadata on root
|
||||||
@@ -1170,6 +1312,48 @@ func (f *Fs) Mkdir(ctx context.Context, dir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mkdirSharedFolder creates a directory within a shared folder.
|
||||||
|
// Creating shared folders themselves is not supported.
|
||||||
|
func (f *Fs) mkdirSharedFolder(ctx context.Context, dir string) error {
|
||||||
|
if dir == "" {
|
||||||
|
return nil // root of shared folders listing - nothing to create
|
||||||
|
}
|
||||||
|
if !strings.Contains(dir, "/") {
|
||||||
|
// This is just a shared folder name - it already exists
|
||||||
|
// (you can't create shared folders via the files API)
|
||||||
|
_, err := f.findSharedFolder(ctx, dir)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
srv, apiPath, _, err := f.sharedFolderSrvAndPath(ctx, dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if apiPath == "" {
|
||||||
|
return nil // root of shared folder already exists
|
||||||
|
}
|
||||||
|
// Check if directory exists by trying to get metadata
|
||||||
|
var entry files.IsMetadata
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
entry, err = srv.GetMetadata(&files.GetMetadataArg{Path: apiPath})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
if _, ok := entry.(*files.FolderMetadata); ok {
|
||||||
|
return nil // directory exists already
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Create it
|
||||||
|
arg := files.CreateFolderArg{Path: apiPath}
|
||||||
|
if cErr := checkPathLength(arg.Path); cErr != nil {
|
||||||
|
return cErr
|
||||||
|
}
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
_, err = srv.CreateFolderV2(&arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// purgeCheck removes the root directory, if check is set then it
|
// purgeCheck removes the root directory, if check is set then it
|
||||||
// refuses to do so if it has anything in
|
// refuses to do so if it has anything in
|
||||||
func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) (err error) {
|
func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) (err error) {
|
||||||
@@ -1219,12 +1403,73 @@ func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) (err error)
|
|||||||
//
|
//
|
||||||
// Returns an error if it isn't empty
|
// Returns an error if it isn't empty
|
||||||
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
||||||
if f.opt.SharedFiles || f.opt.SharedFolders {
|
if f.opt.SharedFiles {
|
||||||
return errNotSupportedInSharedMode
|
return errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.rmdirSharedFolder(ctx, dir)
|
||||||
|
}
|
||||||
return f.purgeCheck(ctx, dir, true)
|
return f.purgeCheck(ctx, dir, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rmdirSharedFolder removes a directory within a shared folder.
|
||||||
|
// Removing the shared folder itself (top-level dir) is not allowed.
|
||||||
|
func (f *Fs) rmdirSharedFolder(ctx context.Context, dir string) error {
|
||||||
|
if dir == "" || !strings.Contains(dir, "/") {
|
||||||
|
return errors.New("can't remove shared folder itself - only subdirectories within it")
|
||||||
|
}
|
||||||
|
return f.purgeCheckSharedFolder(ctx, dir, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// purgeCheckSharedFolder removes a directory within a shared folder.
|
||||||
|
// If check is true, it verifies the directory is empty first.
|
||||||
|
func (f *Fs) purgeCheckSharedFolder(ctx context.Context, dir string, check bool) error {
|
||||||
|
if dir == "" || !strings.Contains(dir, "/") {
|
||||||
|
return errors.New("can't remove shared folder itself - only subdirectories within it")
|
||||||
|
}
|
||||||
|
srv, apiPath, _, err := f.sharedFolderSrvAndPath(ctx, dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if apiPath == "" {
|
||||||
|
return errors.New("can't remove shared folder root")
|
||||||
|
}
|
||||||
|
if check {
|
||||||
|
// Check directory exists
|
||||||
|
var entry files.IsMetadata
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
entry, err = srv.GetMetadata(&files.GetMetadataArg{Path: apiPath})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Rmdir: %w", err)
|
||||||
|
}
|
||||||
|
if _, ok := entry.(*files.FolderMetadata); !ok {
|
||||||
|
return fmt.Errorf("Rmdir: %w", fs.ErrorIsFile)
|
||||||
|
}
|
||||||
|
// Check directory is empty
|
||||||
|
arg := files.NewListFolderArg(apiPath)
|
||||||
|
arg.Recursive = false
|
||||||
|
var res *files.ListFolderResult
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
res, err = srv.ListFolder(arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Rmdir: %w", err)
|
||||||
|
}
|
||||||
|
if len(res.Entries) != 0 {
|
||||||
|
return errors.New("directory not empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Remove it
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
_, err = srv.DeleteV2(&files.DeleteArg{Path: apiPath})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Precision returns the precision
|
// Precision returns the precision
|
||||||
func (f *Fs) Precision() time.Duration {
|
func (f *Fs) Precision() time.Duration {
|
||||||
return time.Second
|
return time.Second
|
||||||
@@ -1246,6 +1491,10 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
|
|||||||
return nil, fs.ErrorCantCopy
|
return nil, fs.ErrorCantCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.copySharedFolder(ctx, srcObj, remote)
|
||||||
|
}
|
||||||
|
|
||||||
// Find and remove existing object
|
// Find and remove existing object
|
||||||
cleanup, err := operations.RemoveExisting(ctx, f, remote, "server side copy")
|
cleanup, err := operations.RemoveExisting(ctx, f, remote, "server side copy")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1288,12 +1537,76 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
|
|||||||
return dstObj, nil
|
return dstObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copySharedFolder copies an object within shared folders.
|
||||||
|
// Both source and destination must be in the same shared folder namespace.
|
||||||
|
func (f *Fs) copySharedFolder(ctx context.Context, srcObj *Object, remote string) (fs.Object, error) {
|
||||||
|
srcFirstDir, _, _ := strings.Cut(srcObj.remote, "/")
|
||||||
|
dstFirstDir, _, ok := strings.Cut(remote, "/")
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorCantCopy
|
||||||
|
}
|
||||||
|
srcNsID, err := f.findSharedFolder(ctx, srcFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fs.ErrorCantCopy
|
||||||
|
}
|
||||||
|
dstNsID, err := f.findSharedFolder(ctx, dstFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fs.ErrorCantCopy
|
||||||
|
}
|
||||||
|
if srcNsID != dstNsID {
|
||||||
|
return nil, fs.ErrorCantCopy
|
||||||
|
}
|
||||||
|
srv := files.New(f.cfg.WithNamespaceID(srcNsID))
|
||||||
|
|
||||||
|
// Find and remove existing object
|
||||||
|
cleanup, err := operations.RemoveExisting(ctx, f, remote, "server side copy")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer cleanup(&err)
|
||||||
|
|
||||||
|
dstObj := &Object{
|
||||||
|
fs: f,
|
||||||
|
remote: remote,
|
||||||
|
nsID: srcNsID,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, srcSubPath, _ := strings.Cut(srcObj.remote, "/")
|
||||||
|
_, dstSubPath, _ := strings.Cut(remote, "/")
|
||||||
|
arg := files.RelocationArg{
|
||||||
|
RelocationPath: files.RelocationPath{
|
||||||
|
FromPath: f.opt.Enc.FromStandardPath("/" + srcSubPath),
|
||||||
|
ToPath: f.opt.Enc.FromStandardPath("/" + dstSubPath),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
var result *files.RelocationResult
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
result, err = srv.CopyV2(&arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("copy failed: %w", err)
|
||||||
|
}
|
||||||
|
fileInfo, ok := result.Metadata.(*files.FileMetadata)
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorNotAFile
|
||||||
|
}
|
||||||
|
err = dstObj.setMetadataFromEntry(fileInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("copy failed: %w", err)
|
||||||
|
}
|
||||||
|
return dstObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Purge deletes all the files and the container
|
// Purge deletes all the files and the container
|
||||||
//
|
//
|
||||||
// Optional interface: Only implement this if you have a way of
|
// Optional interface: Only implement this if you have a way of
|
||||||
// deleting all the files quicker than just running Remove() on the
|
// deleting all the files quicker than just running Remove() on the
|
||||||
// result of List()
|
// result of List()
|
||||||
func (f *Fs) Purge(ctx context.Context, dir string) (err error) {
|
func (f *Fs) Purge(ctx context.Context, dir string) (err error) {
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.purgeCheckSharedFolder(ctx, dir, false)
|
||||||
|
}
|
||||||
return f.purgeCheck(ctx, dir, false)
|
return f.purgeCheck(ctx, dir, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1313,6 +1626,10 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
|
|||||||
return nil, fs.ErrorCantMove
|
return nil, fs.ErrorCantMove
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.moveSharedFolder(ctx, srcObj, remote)
|
||||||
|
}
|
||||||
|
|
||||||
// Temporary Object under construction
|
// Temporary Object under construction
|
||||||
dstObj := &Object{
|
dstObj := &Object{
|
||||||
fs: f,
|
fs: f,
|
||||||
@@ -1358,6 +1675,67 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
|
|||||||
return dstObj, nil
|
return dstObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// moveSharedFolder moves an object within shared folders.
|
||||||
|
// Both source and destination must be in the same shared folder namespace.
|
||||||
|
func (f *Fs) moveSharedFolder(ctx context.Context, srcObj *Object, remote string) (fs.Object, error) {
|
||||||
|
srcFirstDir, _, _ := strings.Cut(srcObj.remote, "/")
|
||||||
|
dstFirstDir, _, ok := strings.Cut(remote, "/")
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorCantMove
|
||||||
|
}
|
||||||
|
srcNsID, err := f.findSharedFolder(ctx, srcFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fs.ErrorCantMove
|
||||||
|
}
|
||||||
|
dstNsID, err := f.findSharedFolder(ctx, dstFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fs.ErrorCantMove
|
||||||
|
}
|
||||||
|
if srcNsID != dstNsID {
|
||||||
|
return nil, fs.ErrorCantMove
|
||||||
|
}
|
||||||
|
srv := files.New(f.cfg.WithNamespaceID(srcNsID))
|
||||||
|
|
||||||
|
dstObj := &Object{
|
||||||
|
fs: f,
|
||||||
|
remote: remote,
|
||||||
|
nsID: srcNsID,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, srcSubPath, _ := strings.Cut(srcObj.remote, "/")
|
||||||
|
_, dstSubPath, _ := strings.Cut(remote, "/")
|
||||||
|
arg := files.RelocationArg{
|
||||||
|
RelocationPath: files.RelocationPath{
|
||||||
|
FromPath: f.opt.Enc.FromStandardPath("/" + srcSubPath),
|
||||||
|
ToPath: f.opt.Enc.FromStandardPath("/" + dstSubPath),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
var result *files.RelocationResult
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
result, err = srv.MoveV2(&arg)
|
||||||
|
switch e := err.(type) {
|
||||||
|
case files.MoveV2APIError:
|
||||||
|
if e.EndpointError != nil && e.EndpointError.FromLookup != nil && e.EndpointError.FromLookup.Tag == files.LookupErrorNotFound {
|
||||||
|
fs.Debugf(srcObj, "Retrying move on %v error", err)
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("move failed: %w", err)
|
||||||
|
}
|
||||||
|
fileInfo, ok := result.Metadata.(*files.FileMetadata)
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorNotAFile
|
||||||
|
}
|
||||||
|
err = dstObj.setMetadataFromEntry(fileInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("move failed: %w", err)
|
||||||
|
}
|
||||||
|
return dstObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
// PublicLink adds a "readable by anyone with link" permission on the given file or folder.
|
// PublicLink adds a "readable by anyone with link" permission on the given file or folder.
|
||||||
func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, unlink bool) (link string, err error) {
|
func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, unlink bool) (link string, err error) {
|
||||||
absPath := f.opt.Enc.FromStandardPath(path.Join(f.slashRoot, remote))
|
absPath := f.opt.Enc.FromStandardPath(path.Join(f.slashRoot, remote))
|
||||||
@@ -1445,6 +1823,11 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
|
|||||||
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
||||||
return fs.ErrorCantDirMove
|
return fs.ErrorCantDirMove
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.opt.SharedFolders {
|
||||||
|
return f.dirMoveSharedFolder(ctx, srcFs, srcRemote, dstRemote)
|
||||||
|
}
|
||||||
|
|
||||||
srcPath := path.Join(srcFs.slashRoot, srcRemote)
|
srcPath := path.Join(srcFs.slashRoot, srcRemote)
|
||||||
dstPath := path.Join(f.slashRoot, dstRemote)
|
dstPath := path.Join(f.slashRoot, dstRemote)
|
||||||
|
|
||||||
@@ -1477,6 +1860,57 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dirMoveSharedFolder moves a directory within shared folders.
|
||||||
|
// Both source and destination must be in the same shared folder namespace.
|
||||||
|
func (f *Fs) dirMoveSharedFolder(ctx context.Context, srcFs *Fs, srcRemote, dstRemote string) error {
|
||||||
|
srcFirstDir, srcSubPath, _ := strings.Cut(srcRemote, "/")
|
||||||
|
dstFirstDir, dstSubPath, _ := strings.Cut(dstRemote, "/")
|
||||||
|
if srcSubPath == "" || dstSubPath == "" {
|
||||||
|
return errors.New("can't move shared folder itself - only subdirectories within it")
|
||||||
|
}
|
||||||
|
srcNsID, err := srcFs.findSharedFolder(ctx, srcFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return fs.ErrorCantDirMove
|
||||||
|
}
|
||||||
|
dstNsID, err := f.findSharedFolder(ctx, dstFirstDir)
|
||||||
|
if err != nil {
|
||||||
|
return fs.ErrorCantDirMove
|
||||||
|
}
|
||||||
|
if srcNsID != dstNsID {
|
||||||
|
return fs.ErrorCantDirMove
|
||||||
|
}
|
||||||
|
srv := files.New(f.cfg.WithNamespaceID(srcNsID))
|
||||||
|
|
||||||
|
// Check if destination exists
|
||||||
|
dstAPIPath := f.opt.Enc.FromStandardPath("/" + dstSubPath)
|
||||||
|
var entry files.IsMetadata
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
entry, err = srv.GetMetadata(&files.GetMetadataArg{Path: dstAPIPath})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
if _, ok := entry.(*files.FolderMetadata); ok {
|
||||||
|
return fs.ErrorDirExists
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do the move
|
||||||
|
arg := files.RelocationArg{
|
||||||
|
RelocationPath: files.RelocationPath{
|
||||||
|
FromPath: f.opt.Enc.FromStandardPath("/" + srcSubPath),
|
||||||
|
ToPath: dstAPIPath,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
_, err = srv.MoveV2(&arg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("MoveDir failed: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// About gets quota information
|
// About gets quota information
|
||||||
func (f *Fs) About(ctx context.Context) (usage *fs.Usage, err error) {
|
func (f *Fs) About(ctx context.Context) (usage *fs.Usage, err error) {
|
||||||
var q *users.SpaceUsage
|
var q *users.SpaceUsage
|
||||||
@@ -1741,7 +2175,7 @@ func (o *Object) ID() string {
|
|||||||
|
|
||||||
// Hash returns the dropbox special hash
|
// Hash returns the dropbox special hash
|
||||||
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
|
func (o *Object) Hash(ctx context.Context, t hash.Type) (string, error) {
|
||||||
if o.fs.opt.SharedFiles || o.fs.opt.SharedFolders {
|
if o.fs.opt.SharedFiles {
|
||||||
return "", errNotSupportedInSharedMode
|
return "", errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
if t != DbHashType {
|
if t != DbHashType {
|
||||||
@@ -1802,6 +2236,29 @@ func (o *Object) setMetadataFromEntry(info *files.FileMetadata) error {
|
|||||||
|
|
||||||
// Reads the entry for a file from dropbox
|
// Reads the entry for a file from dropbox
|
||||||
func (o *Object) readEntry(ctx context.Context) (*files.FileMetadata, error) {
|
func (o *Object) readEntry(ctx context.Context) (*files.FileMetadata, error) {
|
||||||
|
if o.nsID != "" {
|
||||||
|
srv := o.fileSrv()
|
||||||
|
var entry files.IsMetadata
|
||||||
|
var err error
|
||||||
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
|
entry, err = srv.GetMetadata(&files.GetMetadataArg{Path: o.nsPath()})
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
switch e := err.(type) {
|
||||||
|
case files.GetMetadataAPIError:
|
||||||
|
if e.EndpointError != nil && e.EndpointError.Path != nil && e.EndpointError.Path.Tag == files.LookupErrorNotFound {
|
||||||
|
return nil, fs.ErrorObjectNotFound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fileInfo, ok := entry.(*files.FileMetadata)
|
||||||
|
if !ok {
|
||||||
|
return nil, fs.ErrorNotAFile
|
||||||
|
}
|
||||||
|
return fileInfo, nil
|
||||||
|
}
|
||||||
return o.fs.getFileMetadata(ctx, o.remotePath())
|
return o.fs.getFileMetadata(ctx, o.remotePath())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1868,8 +2325,9 @@ func (o *Object) export(ctx context.Context) (in io.ReadCloser, err error) {
|
|||||||
|
|
||||||
arg := files.ExportArg{Path: o.id, ExportFormat: string(o.exportAPIFormat)}
|
arg := files.ExportArg{Path: o.id, ExportFormat: string(o.exportAPIFormat)}
|
||||||
var exportResult *files.ExportResult
|
var exportResult *files.ExportResult
|
||||||
|
srv := o.fileSrv()
|
||||||
err = o.fs.pacer.Call(func() (bool, error) {
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
exportResult, in, err = o.fs.srv.Export(&arg)
|
exportResult, in, err = srv.Export(&arg)
|
||||||
return shouldRetry(ctx, err)
|
return shouldRetry(ctx, err)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1910,8 +2368,9 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
|
|||||||
Path: o.id,
|
Path: o.id,
|
||||||
ExtraHeaders: headers,
|
ExtraHeaders: headers,
|
||||||
}
|
}
|
||||||
|
srv := o.fileSrv()
|
||||||
err = o.fs.pacer.Call(func() (bool, error) {
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
_, in, err = o.fs.srv.Download(&arg)
|
_, in, err = srv.Download(&arg)
|
||||||
return shouldRetry(ctx, err)
|
return shouldRetry(ctx, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1931,11 +2390,11 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
|
|||||||
// Will introduce two additional network requests to start and finish the session.
|
// Will introduce two additional network requests to start and finish the session.
|
||||||
// If the size is unknown (i.e. -1) the method incurs one additional
|
// If the size is unknown (i.e. -1) the method incurs one additional
|
||||||
// request to the Dropbox API that does not carry a payload to close the append session.
|
// request to the Dropbox API that does not carry a payload to close the append session.
|
||||||
func (o *Object) uploadChunked(ctx context.Context, in0 io.Reader, commitInfo *files.CommitInfo, size int64) (entry *files.FileMetadata, err error) {
|
func (o *Object) uploadChunked(ctx context.Context, in0 io.Reader, srv files.Client, commitInfo *files.CommitInfo, size int64, useBatching bool) (entry *files.FileMetadata, err error) {
|
||||||
// start upload
|
// start upload
|
||||||
var res *files.UploadSessionStartResult
|
var res *files.UploadSessionStartResult
|
||||||
err = o.fs.pacer.Call(func() (bool, error) {
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
res, err = o.fs.srv.UploadSessionStart(&files.UploadSessionStartArg{}, nil)
|
res, err = srv.UploadSessionStart(&files.UploadSessionStartArg{}, nil)
|
||||||
return shouldRetry(ctx, err)
|
return shouldRetry(ctx, err)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1972,7 +2431,7 @@ func (o *Object) uploadChunked(ctx context.Context, in0 io.Reader, commitInfo *f
|
|||||||
if _, err = chunk.Seek(skip, io.SeekStart); err != nil {
|
if _, err = chunk.Seek(skip, io.SeekStart); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
err = o.fs.srv.UploadSessionAppendV2(&appendArg, chunk)
|
err = srv.UploadSessionAppendV2(&appendArg, chunk)
|
||||||
// after session is started, we retry everything
|
// after session is started, we retry everything
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check for incorrect offset error and retry with new offset
|
// Check for incorrect offset error and retry with new offset
|
||||||
@@ -2026,12 +2485,12 @@ func (o *Object) uploadChunked(ctx context.Context, in0 io.Reader, commitInfo *f
|
|||||||
}
|
}
|
||||||
// If we are batching then we should have written all the data now
|
// If we are batching then we should have written all the data now
|
||||||
// store the commit info now for a batch commit
|
// store the commit info now for a batch commit
|
||||||
if o.fs.batcher.Batching() {
|
if useBatching {
|
||||||
return o.fs.batcher.Commit(ctx, o.remote, args)
|
return o.fs.batcher.Commit(ctx, o.remote, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = o.fs.pacer.Call(func() (bool, error) {
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
entry, err = o.fs.srv.UploadSessionFinish(args, nil)
|
entry, err = srv.UploadSessionFinish(args, nil)
|
||||||
if retry, err := shouldRetryExclude(ctx, err); !retry {
|
if retry, err := shouldRetryExclude(ctx, err); !retry {
|
||||||
return retry, err
|
return retry, err
|
||||||
}
|
}
|
||||||
@@ -2075,14 +2534,15 @@ func checkPathLength(name string) (err error) {
|
|||||||
//
|
//
|
||||||
// The new object may have been created if an error is returned
|
// The new object may have been created if an error is returned
|
||||||
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
||||||
if o.fs.opt.SharedFiles || o.fs.opt.SharedFolders {
|
if o.fs.opt.SharedFiles {
|
||||||
return errNotSupportedInSharedMode
|
return errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
|
uploadPath := o.nsPath()
|
||||||
remote := o.remotePath()
|
remote := o.remotePath()
|
||||||
if ignoredFiles.MatchString(remote) {
|
if ignoredFiles.MatchString(remote) {
|
||||||
return fserrors.NoRetryError(fmt.Errorf("file name %q is disallowed - not uploading", path.Base(remote)))
|
return fserrors.NoRetryError(fmt.Errorf("file name %q is disallowed - not uploading", path.Base(remote)))
|
||||||
}
|
}
|
||||||
commitInfo := files.NewCommitInfo(o.fs.opt.Enc.FromStandardPath(o.remotePath()))
|
commitInfo := files.NewCommitInfo(uploadPath)
|
||||||
commitInfo.Mode.Tag = "overwrite"
|
commitInfo.Mode.Tag = "overwrite"
|
||||||
// The Dropbox API only accepts timestamps in UTC with second precision.
|
// The Dropbox API only accepts timestamps in UTC with second precision.
|
||||||
clientModified := src.ModTime(ctx).UTC().Round(time.Second)
|
clientModified := src.ModTime(ctx).UTC().Round(time.Second)
|
||||||
@@ -2092,14 +2552,17 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
|
|||||||
return cErr
|
return cErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srv := o.fileSrv()
|
||||||
size := src.Size()
|
size := src.Size()
|
||||||
|
// Disable batching for shared folder objects since the batch commit uses f.srv
|
||||||
|
useBatching := o.fs.batcher.Batching() && o.nsID == ""
|
||||||
var err error
|
var err error
|
||||||
var entry *files.FileMetadata
|
var entry *files.FileMetadata
|
||||||
if size > int64(o.fs.opt.ChunkSize) || size < 0 || o.fs.batcher.Batching() {
|
if size > int64(o.fs.opt.ChunkSize) || size < 0 || useBatching {
|
||||||
entry, err = o.uploadChunked(ctx, in, commitInfo, size)
|
entry, err = o.uploadChunked(ctx, in, srv, commitInfo, size, useBatching)
|
||||||
} else {
|
} else {
|
||||||
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
err = o.fs.pacer.CallNoRetry(func() (bool, error) {
|
||||||
entry, err = o.fs.srv.Upload(&files.UploadArg{CommitInfo: *commitInfo}, in)
|
entry, err = srv.Upload(&files.UploadArg{CommitInfo: *commitInfo}, in)
|
||||||
return shouldRetry(ctx, err)
|
return shouldRetry(ctx, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -2120,12 +2583,13 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
|
|||||||
|
|
||||||
// Remove an object
|
// Remove an object
|
||||||
func (o *Object) Remove(ctx context.Context) (err error) {
|
func (o *Object) Remove(ctx context.Context) (err error) {
|
||||||
if o.fs.opt.SharedFiles || o.fs.opt.SharedFolders {
|
if o.fs.opt.SharedFiles {
|
||||||
return errNotSupportedInSharedMode
|
return errNotSupportedInSharedMode
|
||||||
}
|
}
|
||||||
|
srv := o.fileSrv()
|
||||||
err = o.fs.pacer.Call(func() (bool, error) {
|
err = o.fs.pacer.Call(func() (bool, error) {
|
||||||
_, err = o.fs.srv.DeleteV2(&files.DeleteArg{
|
_, err = srv.DeleteV2(&files.DeleteArg{
|
||||||
Path: o.fs.opt.Enc.FromStandardPath(o.remotePath()),
|
Path: o.nsPath(),
|
||||||
})
|
})
|
||||||
return shouldRetry(ctx, err)
|
return shouldRetry(ctx, err)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
package dropbox
|
package dropbox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
|
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
|
||||||
|
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/async"
|
||||||
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
|
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
|
||||||
|
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/sharing"
|
||||||
|
"github.com/rclone/rclone/fs"
|
||||||
|
"github.com/rclone/rclone/fs/object"
|
||||||
"github.com/rclone/rclone/fstest/fstests"
|
"github.com/rclone/rclone/fstest/fstests"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -95,8 +101,310 @@ func (f *Fs) InternalTestPaperExport(t *testing.T) {
|
|||||||
require.Contains(t, text, excerpt)
|
require.Contains(t, text, excerpt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InternalTestSharedFolder tests both read and write operations on shared folders.
|
||||||
|
// It uses a single shared folder setup to avoid re-sharing between subtests
|
||||||
|
// (Dropbox has eventual consistency issues with share/unshare cycles).
|
||||||
|
func (f *Fs) InternalTestSharedFolder(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
testDir := f.slashRoot
|
||||||
|
|
||||||
|
// Share the test root folder
|
||||||
|
folderName := f.opt.Enc.ToStandardName(strings.TrimPrefix(testDir, "/"))
|
||||||
|
shareArg := sharing.NewShareFolderArg(testDir)
|
||||||
|
var sharedFolderID string
|
||||||
|
var shareLaunch *sharing.ShareFolderLaunch
|
||||||
|
var err error
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
shareLaunch, err = f.sharing.ShareFolder(shareArg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
sharedFolderID, err = f.findSharedFolder(ctx, folderName)
|
||||||
|
require.NoError(t, err, "ShareFolder failed and couldn't find existing share")
|
||||||
|
} else {
|
||||||
|
switch shareLaunch.Tag {
|
||||||
|
case sharing.ShareFolderLaunchComplete:
|
||||||
|
sharedFolderID = shareLaunch.Complete.SharedFolderId
|
||||||
|
case sharing.ShareFolderLaunchAsyncJobId:
|
||||||
|
pollArg := async.PollArg{AsyncJobId: shareLaunch.AsyncJobId}
|
||||||
|
for range 30 {
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
var status *sharing.ShareFolderJobStatus
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
status, err = f.sharing.CheckShareJobStatus(&pollArg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
if status.Tag == sharing.ShareFolderJobStatusComplete {
|
||||||
|
sharedFolderID = status.Complete.SharedFolderId
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.NotEmpty(t, sharedFolderID, "share folder job did not complete")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.NotEmpty(t, sharedFolderID)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
unshareArg := sharing.NewUnshareFolderArg(sharedFolderID)
|
||||||
|
unshareArg.LeaveACopy = true
|
||||||
|
_ = f.pacer.Call(func() (bool, error) {
|
||||||
|
_, err = f.sharing.UnshareFolder(unshareArg)
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
|
||||||
|
sf := *f //nolint:govet // intentional copy; mutex is zero-valued and unused before this point
|
||||||
|
sf.opt.SharedFolders = true
|
||||||
|
sf.features = (&fs.Features{
|
||||||
|
CaseInsensitive: true,
|
||||||
|
ReadMimeType: false,
|
||||||
|
CanHaveEmptyDirectories: true,
|
||||||
|
}).Fill(ctx, &sf)
|
||||||
|
sharedFs := &sf
|
||||||
|
|
||||||
|
// Upload a test file using the normal API for listing tests
|
||||||
|
testFileName := "shared-folder-test-file.txt"
|
||||||
|
testFilePath := testDir + "/" + testFileName
|
||||||
|
content := "hello shared folder"
|
||||||
|
uploadArg := files.NewUploadArg(testFilePath)
|
||||||
|
uploadArg.Mode = &files.WriteMode{Tagged: dropbox.Tagged{Tag: files.WriteModeOverwrite}}
|
||||||
|
err = f.pacer.Call(func() (bool, error) {
|
||||||
|
_, err = f.srv.Upload(uploadArg, strings.NewReader(content))
|
||||||
|
return shouldRetry(ctx, err)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
_, _ = f.srv.DeleteV2(files.NewDeleteArg(testFilePath))
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Run("List", func(t *testing.T) {
|
||||||
|
// listing root should include the shared folder
|
||||||
|
var rootEntries fs.DirEntries
|
||||||
|
err = sharedFs.ListP(ctx, "", func(entries fs.DirEntries) error {
|
||||||
|
rootEntries = append(rootEntries, entries...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, entry := range rootEntries {
|
||||||
|
if entry.Remote() == folderName {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found, "shared folder %q not found in root listing (have %v)", folderName, rootEntries)
|
||||||
|
|
||||||
|
// listing the shared folder should show its contents
|
||||||
|
var dirEntries fs.DirEntries
|
||||||
|
err = sharedFs.ListP(ctx, folderName, func(entries fs.DirEntries) error {
|
||||||
|
dirEntries = append(dirEntries, entries...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
foundFile := false
|
||||||
|
expectedRemote := folderName + "/" + testFileName
|
||||||
|
for _, entry := range dirEntries {
|
||||||
|
if entry.Remote() == expectedRemote {
|
||||||
|
foundFile = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, foundFile, "file %q not found in shared folder listing (have %v)", expectedRemote, dirEntries)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("NewObject", func(t *testing.T) {
|
||||||
|
expectedRemote := folderName + "/" + testFileName
|
||||||
|
obj, err := sharedFs.NewObject(ctx, expectedRemote)
|
||||||
|
require.NoError(t, err, "NewObject failed for shared folder file")
|
||||||
|
assert.Equal(t, expectedRemote, obj.Remote())
|
||||||
|
assert.Equal(t, int64(len(content)), obj.Size())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Open", func(t *testing.T) {
|
||||||
|
expectedRemote := folderName + "/" + testFileName
|
||||||
|
obj, err := sharedFs.NewObject(ctx, expectedRemote)
|
||||||
|
require.NoError(t, err)
|
||||||
|
rc, err := obj.Open(ctx)
|
||||||
|
require.NoError(t, err, "Open failed for shared folder file")
|
||||||
|
buf, err := io.ReadAll(rc)
|
||||||
|
require.NoError(t, rc.Close())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, content, string(buf))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Put", func(t *testing.T) {
|
||||||
|
// Upload a file via Put
|
||||||
|
content := "hello from shared folder put"
|
||||||
|
remote := folderName + "/test-put-file.txt"
|
||||||
|
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
obj, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, remote, obj.Remote())
|
||||||
|
assert.Equal(t, int64(len(content)), obj.Size())
|
||||||
|
|
||||||
|
// Verify we can read back the content
|
||||||
|
rc, err := obj.Open(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
buf, err := io.ReadAll(rc)
|
||||||
|
require.NoError(t, rc.Close())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, content, string(buf))
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
require.NoError(t, obj.Remove(ctx))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Hash", func(t *testing.T) {
|
||||||
|
// Upload a file and check its hash
|
||||||
|
content := "hash test content"
|
||||||
|
remote := folderName + "/test-hash-file.txt"
|
||||||
|
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
obj, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() { _ = obj.Remove(ctx) }()
|
||||||
|
|
||||||
|
hash, err := obj.Hash(ctx, DbHashType)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, hash, "hash should not be empty")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Mkdir", func(t *testing.T) {
|
||||||
|
// Create a subdirectory within the shared folder
|
||||||
|
subDir := folderName + "/test-mkdir-subdir"
|
||||||
|
err := sharedFs.Mkdir(ctx, subDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Verify it appears in the listing
|
||||||
|
var entries fs.DirEntries
|
||||||
|
err = sharedFs.ListP(ctx, folderName, func(e fs.DirEntries) error {
|
||||||
|
entries = append(entries, e...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
found := false
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.Remote() == subDir {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found, "subdirectory %q not found in listing", subDir)
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
require.NoError(t, sharedFs.Rmdir(ctx, subDir))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Rmdir", func(t *testing.T) {
|
||||||
|
// Create and then remove a subdirectory
|
||||||
|
subDir := folderName + "/test-rmdir-subdir"
|
||||||
|
require.NoError(t, sharedFs.Mkdir(ctx, subDir))
|
||||||
|
require.NoError(t, sharedFs.Rmdir(ctx, subDir))
|
||||||
|
|
||||||
|
// Verify it's gone
|
||||||
|
var entries fs.DirEntries
|
||||||
|
err := sharedFs.ListP(ctx, folderName, func(e fs.DirEntries) error {
|
||||||
|
entries = append(entries, e...)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, entry := range entries {
|
||||||
|
assert.NotEqual(t, subDir, entry.Remote(), "subdirectory should have been removed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("RmdirSharedFolderRoot", func(t *testing.T) {
|
||||||
|
// Removing a shared folder itself should fail
|
||||||
|
err := sharedFs.Rmdir(ctx, folderName)
|
||||||
|
assert.Error(t, err, "should not be able to remove shared folder itself")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Remove", func(t *testing.T) {
|
||||||
|
// Upload and then delete a file
|
||||||
|
content := "file to remove"
|
||||||
|
remote := folderName + "/test-remove-file.txt"
|
||||||
|
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
obj, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = obj.Remove(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Verify it's gone
|
||||||
|
_, err = sharedFs.NewObject(ctx, remote)
|
||||||
|
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Copy", func(t *testing.T) {
|
||||||
|
// Upload a file, then server-side copy it within the same shared folder
|
||||||
|
content := "file to copy"
|
||||||
|
srcRemote := folderName + "/test-copy-src.txt"
|
||||||
|
dstRemote := folderName + "/test-copy-dst.txt"
|
||||||
|
|
||||||
|
src := object.NewStaticObjectInfo(srcRemote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
srcObj, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() { _ = srcObj.Remove(ctx) }()
|
||||||
|
|
||||||
|
dstObj, err := sharedFs.Copy(ctx, srcObj, dstRemote)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() { _ = dstObj.Remove(ctx) }()
|
||||||
|
|
||||||
|
assert.Equal(t, dstRemote, dstObj.Remote())
|
||||||
|
assert.Equal(t, int64(len(content)), dstObj.Size())
|
||||||
|
|
||||||
|
// Verify content
|
||||||
|
rc, err := dstObj.Open(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
buf, err := io.ReadAll(rc)
|
||||||
|
require.NoError(t, rc.Close())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, content, string(buf))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Move", func(t *testing.T) {
|
||||||
|
// Upload a file, then server-side move it within the same shared folder
|
||||||
|
content := "file to move"
|
||||||
|
srcRemote := folderName + "/test-move-src.txt"
|
||||||
|
dstRemote := folderName + "/test-move-dst.txt"
|
||||||
|
|
||||||
|
src := object.NewStaticObjectInfo(srcRemote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
srcObj, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
dstObj, err := sharedFs.Move(ctx, srcObj, dstRemote)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() { _ = dstObj.Remove(ctx) }()
|
||||||
|
|
||||||
|
assert.Equal(t, dstRemote, dstObj.Remote())
|
||||||
|
|
||||||
|
// Source should be gone
|
||||||
|
_, err = sharedFs.NewObject(ctx, srcRemote)
|
||||||
|
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Purge", func(t *testing.T) {
|
||||||
|
// Create a subdir with a file, then purge
|
||||||
|
subDir := folderName + "/test-purge-subdir"
|
||||||
|
require.NoError(t, sharedFs.Mkdir(ctx, subDir))
|
||||||
|
|
||||||
|
content := "file in purge dir"
|
||||||
|
remote := subDir + "/file.txt"
|
||||||
|
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
||||||
|
_, err := sharedFs.Put(ctx, bytes.NewReader([]byte(content)), src)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = sharedFs.Purge(ctx, subDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fs) InternalTest(t *testing.T) {
|
func (f *Fs) InternalTest(t *testing.T) {
|
||||||
t.Run("PaperExport", f.InternalTestPaperExport)
|
t.Run("PaperExport", f.InternalTestPaperExport)
|
||||||
|
t.Run("SharedFolder", f.InternalTestSharedFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ fstests.InternalTester = (*Fs)(nil)
|
var _ fstests.InternalTester = (*Fs)(nil)
|
||||||
Reference in new issue
Block a user