Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findByPath: handle files clashing by path #356

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions src/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/odeke-em/drive/config"
Expand Down Expand Up @@ -130,17 +129,27 @@ func (g *Commands) byRemoteResolve(relToRoot, fsPath string, r *File, push bool)
}

func (g *Commands) changeListResolve(relToRoot, fsPath string, push bool) (cl, clashes []*Change, err error) {
var r *File
r, err = g.rem.FindByPath(relToRoot)
var rl []*File
rl, err = g.rem.FindByPath(relToRoot)
if err != nil && err != ErrPathNotExists {
return
}

if r != nil && anyMatch(g.opts.IgnoreRegexp, r.Name) {
return
for _, r := range rl {
if r != nil && anyMatch(g.opts.IgnoreRegexp, r.Name) {
return
}

ccl, cclashes, clErr := g.byRemoteResolve(relToRoot, fsPath, r, push)
if clErr != nil {
g.log.LogErrf("changeListResolve:: %s %v\n", relToRoot, clErr)
}

cl = append(cl, ccl...)
clashes = append(clashes, cclashes...)
}

return g.byRemoteResolve(relToRoot, fsPath, r, push)
return
}

func (g *Commands) doChangeListRecv(relToRoot, fsPath string, l, r *File, push bool) (cl, clashes []*Change, err error) {
Expand Down Expand Up @@ -337,8 +346,8 @@ func (g *Commands) resolveChangeListRecv(clr *changeListResolve) (cl, clashes []
chunkCount += 1
}

var wg sync.WaitGroup
wg.Add(chunkCount)
doneCount := uint64(0)
doneChan := make(chan bool)

clashesMap := make(map[int][]*Change)

Expand All @@ -348,11 +357,19 @@ func (g *Commands) resolveChangeListRecv(clr *changeListResolve) (cl, clashes []
end = srcLen
}

go g.changeSlice(clashesMap, j, &wg, clr.push, &cl, base, dirlist[i:end])
doneCount += 1

go func(clm map[int][]*Change, id int, push bool, cll *[]*Change, p string, dlist []*dirList) {
g.changeSlice(clashesMap, id, push, cll, p, dlist)
doneChan <- true
}(clashesMap, j, clr.push, &cl, base, dirlist[i:end])

i += chunkSize
}
wg.Wait()

for i := uint64(0); i < doneCount; i++ {
<-doneChan
}

for _, cclashes := range clashesMap {
clashes = append(clashes, cclashes...)
Expand All @@ -365,8 +382,7 @@ func (g *Commands) resolveChangeListRecv(clr *changeListResolve) (cl, clashes []
return cl, clashes, err
}

func (g *Commands) changeSlice(clashesMap map[int][]*Change, id int, wg *sync.WaitGroup, push bool, cl *[]*Change, p string, dlist []*dirList) {
defer wg.Done()
func (g *Commands) changeSlice(clashesMap map[int][]*Change, id int, push bool, cl *[]*Change, p string, dlist []*dirList) {
for _, l := range dlist {
// Avoiding path.Join which normalizes '/+' to '/'
var joined string
Expand All @@ -385,18 +401,17 @@ func (g *Commands) changeSlice(clashesMap map[int][]*Change, id int, wg *sync.Wa
}

childChanges, childClashes, cErr := g.resolveChangeListRecv(clr)
if cErr == nil {
*cl = append(*cl, childChanges...)
continue
}

if cErr == ErrClashesDetected {
if len(childClashes) >= 1 {
clashesMap[id] = childClashes
continue
} else if cErr != ErrPathNotExists {
}

if cErr != nil && cErr != ErrPathNotExists {
g.log.LogErrf("%s: %v\n", p, cErr)
break
continue
}

*cl = append(*cl, childChanges...)
}
}

Expand Down
90 changes: 65 additions & 25 deletions src/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,87 +42,126 @@ func (g *Commands) Copy(byId bool) error {
end := argc - 1
sources, dest := g.opts.Sources[:end], g.opts.Sources[end]

destFile, err := g.rem.FindByPath(dest)
destFiles, err := g.rem.FindByPath(dest)
if err != nil && err != ErrPathNotExists {
return fmt.Errorf("destination: %s err: %v", dest, err)
}

for _, destFile := range destFiles {
if destFile == nil {
continue
}

errs := copy_(g, dest, destFile, sources, byId)
for _, err := range errs {
g.log.LogErrf("copy_: %s %v\n", destFile.Id, err)
}
}

return nil
}

func copy_(g *Commands, dest string, destFile *File, sources []string, byId bool) (errs []error) {
multiPaths := len(sources) > 1
if multiPaths {
if destFile != nil && !destFile.IsDir {
return fmt.Errorf("%s: %v", dest, ErrPathNotDir)
errs = append(errs, fmt.Errorf("%s: %v", dest, ErrPathNotDir))
return
}
_, err := g.remoteMkdirAll(dest)
if err != nil {
return err
errs = append(errs, err)
return
}
}

srcResolver := g.rem.FindByPath
if byId {
srcResolver = g.rem.FindById
srcResolver = g.rem.FindByIdMulti
}

done := make(chan bool)
waitCount := uint64(0)

for _, srcPath := range sources {
srcFile, srcErr := srcResolver(srcPath)
srcFiles, srcErr := srcResolver(srcPath)
if srcErr != nil {
g.log.LogErrf("%s: %v\n", srcPath, srcErr)
continue
}

waitCount += 1
for _, srcFile := range srcFiles {
waitCount += 1

go func(fromPath, toPath string, fromFile *File) {
_, copyErr := g.copy(fromFile, toPath)
if copyErr != nil {
g.log.LogErrf("%s: %v\n", fromPath, copyErr)
}
done <- true
}(srcPath, dest, srcFile)
go func(fromPath, toPath string, fromFile *File) {
_, copyErr := g.copy(fromFile, toPath)
if copyErr != nil {
g.log.LogErrf("%s: %v\n", fromPath, copyErr)
}
done <- true
}(srcPath, dest, srcFile)
}
}

for i := uint64(0); i < waitCount; i += 1 {
<-done
}

return nil

}

func (g *Commands) copy(src *File, destPath string) (*File, error) {
func (g *Commands) copy(src *File, destPath string) (copies []*File, errs []error) {
if src == nil {
return nil, fmt.Errorf("non existant src")
errs = append(errs, fmt.Errorf("non existant src"))
return
}

if !src.IsDir {
if !src.Copyable {
return nil, fmt.Errorf("%s is non-copyable", src.Name)
errs = append(errs, fmt.Errorf("%s is non-copyable", src.Name))
return
}

destDir, destBase := g.pathSplitter(destPath)
destParent, destParErr := g.remoteMkdirAll(destDir)

if destParErr != nil {
return nil, destParErr
errs = append(errs, destParErr)
return
}

parentId := destParent.Id
destFile, destErr := g.rem.FindByPath(destPath)
destFiles, destErr := g.rem.FindByPath(destPath)
if destErr != nil && destErr != ErrPathNotExists {
return nil, destErr
errs = append(errs, destErr)
return
}
if destFile != nil && destFile.IsDir {
parentId = destFile.Id
destBase = src.Name

for _, destFile := range destFiles {
if destFile != nil && destFile.IsDir {
parentId = destFile.Id
destBase = src.Name
}

copy, cpErr := g.rem.copy(destBase, parentId, src)

if copy != nil {
copies = append(copies, copy)
}

if cpErr != nil {
errs = append(errs, cpErr)
}
}
return g.rem.copy(destBase, parentId, src)

return
}

destFile, destErr := g.remoteMkdirAll(destPath)
if destErr != nil {
return nil, destErr
errs = append(errs, destErr)
return
}

children := g.rem.findChildren(src.Id, false)
Expand All @@ -138,5 +177,6 @@ func (g *Commands) copy(src *File, destPath string) (*File, error) {
}
}

return destFile, nil
copies = append(copies, destFile)
return
}
46 changes: 24 additions & 22 deletions src/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,44 @@ func (g *Commands) List(byId bool) error {

resolver := g.rem.FindByPath
if byId {
resolver = g.rem.FindById
resolver = g.rem.FindByIdMulti
} else if g.opts.InTrash {
resolver = g.rem.FindByPathTrashed
}

mq := g.createMatchQuery(true)

for _, relPath := range g.opts.Sources {
r, rErr := resolver(relPath)
rl, rErr := resolver(relPath)
if rErr != nil && rErr != ErrPathNotExists {
return fmt.Errorf("%v: '%s'", rErr, relPath)
}

if r == nil {
g.log.LogErrf("remote: %s is nil\n", strconv.Quote(relPath))
continue
}
for _, r := range rl {
if r == nil {
g.log.LogErrf("remote: %s is nil\n", strconv.Quote(relPath))
continue
}

parentPath := ""
if !byId {
parentPath = g.parentPather(relPath)
} else {
parentPath = r.Id
}
parentPath := ""
if !byId {
parentPath = g.parentPather(relPath)
} else {
parentPath = r.Id
}

if remoteRootLike(parentPath) {
parentPath = ""
}
if remoteRootLike(r.Name) {
r.Name = ""
}
if rootLike(parentPath) {
parentPath = ""
}
if remoteRootLike(parentPath) {
parentPath = ""
}
if remoteRootLike(r.Name) {
r.Name = ""
}
if rootLike(parentPath) {
parentPath = ""
}

kvList = append(kvList, &keyValue{key: parentPath, value: r})
kvList = append(kvList, &keyValue{key: parentPath, value: r})
}
}

spin := g.playabler()
Expand Down
Loading