From 9b07d32c02fbb52b5ee8add01d49a7709768e805 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 15 Mar 2017 20:55:05 +0000 Subject: [PATCH] onedrive, drive, amazonclouddrive: make sure we find the root This fixes copyto copying things to the wrong place - fixes #1231 --- amazonclouddrive/amazonclouddrive.go | 4 ++-- dircache/dircache.go | 11 +++++++++++ drive/drive.go | 10 ++-------- fs/operations_test.go | 2 -- onedrive/onedrive.go | 18 +----------------- 5 files changed, 16 insertions(+), 29 deletions(-) diff --git a/amazonclouddrive/amazonclouddrive.go b/amazonclouddrive/amazonclouddrive.go index 47f98f841..19656e9fa 100644 --- a/amazonclouddrive/amazonclouddrive.go +++ b/amazonclouddrive/amazonclouddrive.go @@ -553,7 +553,7 @@ func (f *Fs) Put(in io.Reader, src fs.ObjectInfo) (fs.Object, error) { return nil, err } // If not create it - leaf, directoryID, err := f.dirCache.FindPath(remote, true) + leaf, directoryID, err := f.dirCache.FindRootAndPath(remote, true) if err != nil { return nil, err } @@ -925,7 +925,7 @@ func (o *Object) readMetaData() (err error) { if o.info != nil { return nil } - leaf, directoryID, err := o.fs.dirCache.FindPath(o.remote, false) + leaf, directoryID, err := o.fs.dirCache.FindRootAndPath(o.remote, false) if err != nil { if err == fs.ErrorDirNotFound { return fs.ErrorObjectNotFound diff --git a/dircache/dircache.go b/dircache/dircache.go index 7bd971267..19fcd171f 100644 --- a/dircache/dircache.go +++ b/dircache/dircache.go @@ -243,6 +243,17 @@ func (dc *DirCache) FindRoot(create bool) error { return nil } +// FindRootAndPath finds the root first if not found then finds leaf and directoryID from a path +// +// If create is set parent directories will be created if they don't exist +func (dc *DirCache) FindRootAndPath(path string, create bool) (leaf, directoryID string, err error) { + err = dc.FindRoot(create) + if err != nil { + return + } + return dc.FindPath(path, create) +} + // FoundRoot returns whether the root directory has been found yet // // Call this from FindLeaf or CreateDir only diff --git a/drive/drive.go b/drive/drive.go index 326a0397d..c7be2921d 100644 --- a/drive/drive.go +++ b/drive/drive.go @@ -544,7 +544,7 @@ func (f *Fs) createFileInfo(remote string, modTime time.Time, size int64) (*Obje bytes: size, } - leaf, directoryID, err := f.dirCache.FindPath(remote, true) + leaf, directoryID, err := f.dirCache.FindRootAndPath(remote, true) if err != nil { return nil, nil, err } @@ -765,12 +765,6 @@ func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) { return nil, errors.New("can't move a Google document") } - // create the destination directory if necessary - err := f.dirCache.FindRoot(true) - if err != nil { - return nil, err - } - // Temporary Object under construction dstObj, dstInfo, err := f.createFileInfo(remote, srcObj.ModTime(), srcObj.bytes) if err != nil { @@ -949,7 +943,7 @@ func (o *Object) readMetaData() (err error) { return nil } - leaf, directoryID, err := o.fs.dirCache.FindPath(o.remote, false) + leaf, directoryID, err := o.fs.dirCache.FindRootAndPath(o.remote, false) if err != nil { if err == fs.ErrorDirNotFound { return fs.ErrorObjectNotFound diff --git a/fs/operations_test.go b/fs/operations_test.go index 4c8d792bd..40a1af16a 100644 --- a/fs/operations_test.go +++ b/fs/operations_test.go @@ -770,7 +770,6 @@ func TestMoveFile(t *testing.T) { r := NewRun(t) defer r.Finalise() - r.Mkdir(r.fremote) file1 := r.WriteFile("file1", "file1 contents", t1) fstest.CheckItems(t, r.flocal, file1) @@ -801,7 +800,6 @@ func TestCopyFile(t *testing.T) { file2 := file1 file2.Path = "sub/file2" - r.Mkdir(r.fremote) err := fs.CopyFile(r.fremote, r.flocal, file2.Path, file1.Path) require.NoError(t, err) fstest.CheckItems(t, r.flocal, file1) diff --git a/onedrive/onedrive.go b/onedrive/onedrive.go index 1d91adf33..acf3becd6 100644 --- a/onedrive/onedrive.go +++ b/onedrive/onedrive.go @@ -438,7 +438,7 @@ func (f *Fs) List(out fs.ListOpts, dir string) { // Used to create new objects func (f *Fs) createObject(remote string, modTime time.Time, size int64) (o *Object, leaf string, directoryID string, err error) { // Create the directory for the object if it doesn't exist - leaf, directoryID, err = f.dirCache.FindPath(remote, true) + leaf, directoryID, err = f.dirCache.FindRootAndPath(remote, true) if err != nil { return nil, leaf, directoryID, err } @@ -615,12 +615,6 @@ func (f *Fs) Copy(src fs.Object, remote string) (fs.Object, error) { return nil, errors.Errorf("can't copy %q -> %q as are same name when lowercase", srcPath, dstPath) } - // create the destination directory if necessary - err = f.dirCache.FindRoot(true) - if err != nil { - return nil, err - } - // Create temporary object dstObj, leaf, directoryID, err := f.createObject(remote, srcObj.modTime, srcObj.size) if err != nil { @@ -689,12 +683,6 @@ func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) { return nil, fs.ErrorCantMove } - // create the destination directory if necessary - err := f.dirCache.FindRoot(true) - if err != nil { - return nil, err - } - // Create temporary object dstObj, leaf, directoryID, err := f.createObject(remote, srcObj.modTime, srcObj.size) if err != nil { @@ -825,10 +813,6 @@ func (o *Object) readMetaData() (err error) { if o.hasMetaData { return nil } - // leaf, directoryID, err := o.fs.dirCache.FindPath(o.remote, false) - // if err != nil { - // return err - // } info, _, err := o.fs.readMetaDataForPath(o.srvPath()) if err != nil { if apiErr, ok := err.(*api.Error); ok {