mv: Fix panic and simply the code a little bit (#3640)

Also, mv did not consider --fake flag, fix it as well.
This commit is contained in:
Anis Elleuch
2021-03-10 23:12:04 -08:00
committed by GitHub
parent faa895dd75
commit ccb35f1672
2 changed files with 11 additions and 79 deletions
+3 -8
View File
@@ -258,23 +258,18 @@ func doCopy(ctx context.Context, cpURLs URLs, pg ProgressReader, encKeyDB map[st
urls := uploadSourceToTargetURL(ctx, cpURLs, pg, encKeyDB, preserve)
if isMvCmd && urls.Error == nil {
bgRemove(ctx, sourcePath)
rmManager.add(ctx, sourceAlias, sourceURL.String())
}
return urls
}
// doCopyFake - Perform a fake copy to update the progress bar appropriately.
func doCopyFake(ctx context.Context, cpURLs URLs, pg Progress, isMvCmd bool) URLs {
func doCopyFake(ctx context.Context, cpURLs URLs, pg Progress) URLs {
if progressReader, ok := pg.(*progressBar); ok {
progressReader.ProgressBar.Add64(cpURLs.SourceContent.Size)
}
if isMvCmd {
sourcePath := filepath.ToSlash(filepath.Join(cpURLs.SourceAlias, cpURLs.SourceContent.URL.Path))
bgRemove(ctx, sourcePath)
}
return cpURLs
}
@@ -534,7 +529,7 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
// Verify if previously copied, notify progress bar.
if isCopied != nil && isCopied(cpURLs.SourceContent.URL.String()) {
parallel.queueTask(func() URLs {
return doCopyFake(ctx, cpURLs, pg, isMvCmd)
return doCopyFake(ctx, cpURLs, pg)
})
} else {
parallel.queueTask(func() URLs {
+8 -71
View File
@@ -21,7 +21,6 @@ import (
"fmt"
"os"
"sync"
"sync/atomic"
"github.com/fatih/color"
"github.com/minio/cli"
@@ -153,8 +152,6 @@ type removeClientInfo struct {
type removeManager struct {
removeMap map[string]*removeClientInfo
removeMapMutex sync.RWMutex
doneCh chan struct{}
isClosed int32
wg sync.WaitGroup
}
@@ -162,35 +159,17 @@ func (rm *removeManager) readErrors(errorCh <-chan *probe.Error, targetURL strin
rm.wg.Add(1)
go func() {
defer rm.wg.Done()
var stop bool
for !stop {
select {
case pErr, ok := <-errorCh:
if ok {
errorIf(pErr.Trace(targetURL), "Failed to remove in`"+targetURL+"`.")
}
case <-rm.doneCh:
stop = true
}
}
for pErr := range errorCh {
if pErr != nil {
errorIf(pErr.Trace(targetURL), "Failed to remove in `"+targetURL+"`.")
}
errorIf(pErr.Trace(targetURL), "Failed to remove in`"+targetURL+"`.")
}
}()
}
// This function should be parallel-safe because it is executed by ParallelManager
// If targetAlias is empty, it means we will target local FS contents
func (rm *removeManager) add(ctx context.Context, targetAlias, targetURL string) {
if atomic.LoadInt32(&rm.isClosed) != 0 {
return
}
rm.removeMapMutex.RLock()
rm.removeMapMutex.Lock()
clientInfo := rm.removeMap[targetAlias]
rm.removeMapMutex.RUnlock()
if clientInfo == nil {
client, pErr := newClientFromAlias(targetAlias, targetURL)
if pErr != nil {
@@ -198,7 +177,7 @@ func (rm *removeManager) add(ctx context.Context, targetAlias, targetURL string)
return
}
contentCh := make(chan *ClientContent)
contentCh := make(chan *ClientContent, 10000)
errorCh := client.Remove(ctx, false, false, false, contentCh)
rm.readErrors(errorCh, targetURL)
@@ -208,66 +187,24 @@ func (rm *removeManager) add(ctx context.Context, targetAlias, targetURL string)
errorCh: errorCh,
}
rm.removeMapMutex.Lock()
rm.removeMap[targetAlias] = clientInfo
rm.removeMapMutex.Unlock()
}
rm.removeMapMutex.Unlock()
go func() {
clientInfo.contentCh <- &ClientContent{URL: *newClientURL(targetURL)}
}()
clientInfo.contentCh <- &ClientContent{URL: *newClientURL(targetURL)}
}
func (rm *removeManager) close() {
atomic.StoreInt32(&rm.isClosed, 1)
rm.removeMapMutex.Lock()
defer rm.removeMapMutex.Unlock()
for _, clientInfo := range rm.removeMap {
close(clientInfo.contentCh)
}
close(rm.doneCh)
// Wait until all on-going client.Remove() operations to finish
rm.wg.Wait()
}
var rmManager = &removeManager{
removeMap: make(map[string]*removeClientInfo),
doneCh: make(chan struct{}),
}
func bgRemove(ctx context.Context, url string) {
remove := func(targetAlias, targetURL string) {
clnt, pErr := newClientFromAlias(targetAlias, targetURL)
if pErr != nil {
errorIf(pErr.Trace(url), "Invalid argument `"+url+"`.")
}
contentCh := make(chan *ClientContent, 1)
contentCh <- &ClientContent{URL: *newClientURL(targetURL)}
close(contentCh)
errorCh := clnt.Remove(ctx, false, false, false, contentCh)
for pErr := range errorCh {
if pErr != nil {
errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.")
switch pErr.ToGoError().(type) {
case PathInsufficientPermission:
// Ignore Permission error.
continue
}
}
}
}
targetAlias, targetURL, _ := mustExpandAlias(url)
if targetAlias == "" {
// File system does not support batch deletion hence use individual deletion.
go remove(targetAlias, targetURL)
return
}
rmManager.add(ctx, targetAlias, targetURL)
}
// mainMove is the entry point for mv command.