From ccb35f1672fd1177aa2b65290ce6d119c38d66c1 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 11 Mar 2021 08:12:04 +0100 Subject: [PATCH] mv: Fix panic and simply the code a little bit (#3640) Also, mv did not consider --fake flag, fix it as well. --- cmd/cp-main.go | 11 ++----- cmd/mv-main.go | 79 +++++--------------------------------------------- 2 files changed, 11 insertions(+), 79 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 36c29865..915ce03c 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -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 { diff --git a/cmd/mv-main.go b/cmd/mv-main.go index 78f9dec9..9a6a9b28 100644 --- a/cmd/mv-main.go +++ b/cmd/mv-main.go @@ -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.