onedrive, drive, amazonclouddrive: make sure we find the root

This fixes copyto copying things to the wrong place - fixes #1231
This commit is contained in:
Nick Craig-Wood 2017-03-15 20:55:05 +00:00
parent 986a2851bf
commit 9b07d32c02
5 changed files with 16 additions and 29 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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 {