Validation optimization - reduce HEAD calls during cp and mv operations (#4710)

This commit is contained in:
Sveinn
2023-10-12 11:30:11 -07:00
committed by GitHub
parent 32311bb933
commit 87d6cd2d4f
10 changed files with 301 additions and 308 deletions
+6 -6
View File
@@ -108,12 +108,12 @@ func getEncKeys(ctx *cli.Context) (map[string][]prefixSSEPair, *probe.Error) {
// Check if the passed URL represents a folder. It may or may not exist yet.
// If it exists, we can easily check if it is a folder, if it doesn't exist,
// we can guess if the url is a folder from how it looks.
func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefixSSEPair, timeRef time.Time) bool {
func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefixSSEPair, timeRef time.Time) (bool, *ClientContent) {
// If the target url exists, check if it is a directory
// and return immediately.
_, targetContent, err := url2Stat(ctx, aliasURL, "", false, keys, timeRef, false)
if err == nil {
return targetContent.Type.IsDir()
return targetContent.Type.IsDir(), targetContent
}
_, expandedURL, _ := mustExpandAlias(aliasURL)
@@ -121,7 +121,7 @@ func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefi
// Check if targetURL is an FS or S3 aliased url
if expandedURL == aliasURL {
// This is an FS url, check if the url has a separator at the end
return strings.HasSuffix(aliasURL, string(filepath.Separator))
return strings.HasSuffix(aliasURL, string(filepath.Separator)), targetContent
}
// This is an S3 url, then:
@@ -134,14 +134,14 @@ func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefi
switch len(fields) {
// Nothing or alias format
case 0, 1:
return false
return false, targetContent
// alias/bucket format
case 2:
return true
return true, targetContent
} // default case..
// alias/bucket/prefix format
return strings.HasSuffix(pathURL, "/")
return strings.HasSuffix(pathURL, "/"), targetContent
}
// getSourceStreamMetadataFromURL gets a reader from URL.
+42 -29
View File
@@ -288,7 +288,7 @@ func doCopyFake(cpURLs URLs, pg Progress) URLs {
}
// doPrepareCopyURLs scans the source URL and prepares a list of objects for copying.
func doPrepareCopyURLs(ctx context.Context, session *sessionV8, cancelCopy context.CancelFunc) (totalBytes, totalObjects int64) {
func doPrepareCopyURLs(ctx context.Context, session *sessionV8, cancelCopy context.CancelFunc) (totalBytes, totalObjects int64, errSeen bool) {
// Separate source and target. 'cp' can take only one target,
// but any number of sources.
sourceURLs := session.Header.CommandArgs[:len(session.Header.CommandArgs)-1]
@@ -333,16 +333,10 @@ func doPrepareCopyURLs(ctx context.Context, session *sessionV8, cancelCopy conte
done = true
break
}
if cpURLs.Error != nil {
// Print in new line and adjust to top so that we don't print over the ongoing scan bar
if !globalQuiet && !globalJSON {
console.Eraseline()
}
if strings.Contains(cpURLs.Error.ToGoError().Error(), " is a folder.") {
errorIf(cpURLs.Error.Trace(), "Folder cannot be copied. Please use `...` suffix.")
} else {
errorIf(cpURLs.Error.Trace(), "Unable to prepare URL for copying.")
}
printCopyURLsError(&cpURLs)
errSeen = true
break
}
@@ -377,11 +371,29 @@ func doPrepareCopyURLs(ctx context.Context, session *sessionV8, cancelCopy conte
return
}
func printCopyURLsError(cpURLs *URLs) {
// Print in new line and adjust to top so that we
// don't print over the ongoing scan bar
if !globalQuiet && !globalJSON {
console.Eraseline()
}
if strings.Contains(cpURLs.Error.ToGoError().Error(),
" is a folder.") {
errorIf(cpURLs.Error.Trace(),
"Folder cannot be copied. Please use `...` suffix.")
} else {
errorIf(cpURLs.Error.Trace(),
"Unable to prepare URL for copying.")
}
}
func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.Context, session *sessionV8, encKeyDB map[string][]prefixSSEPair, isMvCmd bool) error {
var isCopied func(string) bool
var totalObjects, totalBytes int64
cpURLsCh := make(chan URLs, 10000)
errSeen := false
// Store a progress bar or an accounter
var pg ProgressReader
@@ -405,7 +417,7 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
isCopied = isLastFactory(session.Header.LastCopied)
if !session.HasData() {
totalBytes, totalObjects = doPrepareCopyURLs(ctx, session, cancelCopy)
totalBytes, totalObjects, errSeen = doPrepareCopyURLs(ctx, session, cancelCopy)
} else {
totalBytes, totalObjects = session.Header.TotalBytes, session.Header.TotalObjects
}
@@ -431,6 +443,7 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
cpURLsCh <- cpURLs
}
}()
} else {
// Access recursive flag inside the session header.
isRecursive := cli.Bool("recursive")
@@ -452,23 +465,14 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
versionID: versionID,
isZip: cli.Bool("zip"),
}
for cpURLs := range prepareCopyURLs(ctx, opts) {
if cpURLs.Error != nil {
// Print in new line and adjust to top so that we
// don't print over the ongoing scan bar
if !globalQuiet && !globalJSON {
console.Eraseline()
}
if strings.Contains(cpURLs.Error.ToGoError().Error(),
" is a folder.") {
errorIf(cpURLs.Error.Trace(),
"Folder cannot be copied. Please use `...` suffix.")
} else {
errorIf(cpURLs.Error.Trace(),
"Unable to start copying.")
}
errSeen = true
printCopyURLsError(&cpURLs)
break
}
totalBytes += cpURLs.SourceContent.Size
pg.SetTotal(totalBytes)
totalObjects++
@@ -570,7 +574,6 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
}()
var retErr error
errSeen := false
cpAllFilesErr := true
loop:
@@ -639,14 +642,24 @@ loop:
}
if progressReader, ok := pg.(*progressBar); ok {
if (errSeen && totalObjects == 1) || (cpAllFilesErr && totalObjects > 1) {
console.Eraseline()
if errSeen || (cpAllFilesErr && totalObjects > 0) {
// We only erase a line if we are displaying a progress bar
if !globalQuiet && !globalJSON {
console.Eraseline()
}
} else if progressReader.ProgressBar.Get() > 0 {
progressReader.ProgressBar.Finish()
}
} else {
if accntReader, ok := pg.(*accounter); ok {
printMsg(accntReader.Stat())
if errSeen || (cpAllFilesErr && totalObjects > 0) {
// We only erase a line if we are displaying a progress bar
if !globalQuiet && !globalJSON {
console.Eraseline()
}
} else {
printMsg(accntReader.Stat())
}
}
}
@@ -670,7 +683,7 @@ func mainCopy(cliCtx *cli.Context) error {
}
// check 'copy' cli arguments.
checkCopySyntax(ctx, cliCtx, encKeyDB, false)
checkCopySyntax(cliCtx)
// Additional command specific theme customization.
console.SetColor("Copy", color.New(color.FgGreen, color.Bold))
+1 -149
View File
@@ -18,21 +18,14 @@
package cmd
import (
"context"
"fmt"
"runtime"
"time"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v2/console"
)
func checkCopySyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string][]prefixSSEPair, isMvCmd bool) {
func checkCopySyntax(cliCtx *cli.Context) {
if len(cliCtx.Args()) < 2 {
if isMvCmd {
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code.
}
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code.
}
@@ -44,9 +37,7 @@ func checkCopySyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[stri
srcURLs := URLs[:len(URLs)-1]
tgtURL := URLs[len(URLs)-1]
isRecursive := cliCtx.Bool("recursive")
isZip := cliCtx.Bool("zip")
timeRef := parseRewindFlag(cliCtx.String("rewind"))
versionID := cliCtx.String("version-id")
if versionID != "" && len(srcURLs) > 1 {
@@ -57,24 +48,6 @@ func checkCopySyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[stri
fatalIf(errDummy().Trace(cliCtx.Args()...), "--zip and --rewind cannot be used together")
}
// Verify if source(s) exists.
for _, srcURL := range srcURLs {
var err *probe.Error
if !isRecursive {
_, _, err = url2Stat(ctx, srcURL, versionID, false, encKeyDB, timeRef, isZip)
} else {
_, _, err = firstURL2Stat(ctx, srcURL, timeRef, isZip)
}
if err != nil {
msg := "Unable to validate source `" + srcURL + "`"
if versionID != "" {
msg += " (" + versionID + ")"
}
msg += ": " + err.ToGoError().Error()
console.Fatalln(msg)
}
}
// Check if bucket name is passed for URL type arguments.
url := newClientURL(tgtURL)
if url.Host != "" {
@@ -91,129 +64,8 @@ func checkCopySyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[stri
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Both object retention flags `--%s` and `--%s` are required.\n", rdFlag, rmFlag))
}
operation := "copy"
if isMvCmd {
operation = "move"
}
// Guess CopyURLsType based on source and target URLs.
opts := prepareCopyURLsOpts{
sourceURLs: srcURLs,
targetURL: tgtURL,
isRecursive: isRecursive,
encKeyDB: encKeyDB,
olderThan: "",
newerThan: "",
timeRef: timeRef,
versionID: versionID,
isZip: isZip,
}
copyURLsType, _, err := guessCopyURLType(ctx, opts)
if err != nil {
fatalIf(errInvalidArgument().Trace(), "Unable to guess the type of "+operation+" operation.")
}
switch copyURLsType {
case copyURLsTypeA: // File -> File.
// Check source.
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments.")
}
checkCopySyntaxTypeA(ctx, srcURLs[0], versionID, encKeyDB, isZip, timeRef)
case copyURLsTypeB: // File -> Folder.
// Check source.
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments.")
}
checkCopySyntaxTypeB(ctx, srcURLs[0], versionID, tgtURL, encKeyDB, isZip, timeRef)
case copyURLsTypeC: // Folder... -> Folder.
checkCopySyntaxTypeC(ctx, srcURLs, tgtURL, isRecursive, isZip, encKeyDB, isMvCmd, timeRef)
case copyURLsTypeD: // File1...FileN -> Folder.
checkCopySyntaxTypeD(ctx, tgtURL, encKeyDB, timeRef)
default:
fatalIf(errInvalidArgument().Trace(), "Unable to guess the type of "+operation+" operation.")
}
// Preserve functionality not supported for windows
if cliCtx.Bool("preserve") && runtime.GOOS == "windows" {
fatalIf(errInvalidArgument().Trace(), "Permissions are not preserved on windows platform.")
}
}
// checkCopySyntaxTypeA verifies if the source and target are valid file arguments.
func checkCopySyntaxTypeA(ctx context.Context, srcURL, versionID string, keys map[string][]prefixSSEPair, isZip bool, timeRef time.Time) {
_, srcContent, err := url2Stat(ctx, srcURL, versionID, false, keys, timeRef, isZip)
fatalIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.")
if !srcContent.Type.IsRegular() {
fatalIf(errInvalidArgument().Trace(), "Source `"+srcURL+"` is not a file.")
}
}
// checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid folder.
func checkCopySyntaxTypeB(ctx context.Context, srcURL, versionID, tgtURL string, keys map[string][]prefixSSEPair, isZip bool, timeRef time.Time) {
_, srcContent, err := url2Stat(ctx, srcURL, versionID, false, keys, timeRef, isZip)
fatalIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.")
if !srcContent.Type.IsRegular() {
fatalIf(errInvalidArgument().Trace(srcURL), "Source `"+srcURL+"` is not a file.")
}
// Check target.
if _, tgtContent, err := url2Stat(ctx, tgtURL, "", false, keys, timeRef, false); err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument().Trace(tgtURL), "Target `"+tgtURL+"` is not a folder.")
}
}
}
// checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid folder.
func checkCopySyntaxTypeC(ctx context.Context, srcURLs []string, tgtURL string, isRecursive, isZip bool, keys map[string][]prefixSSEPair, isMvCmd bool, timeRef time.Time) {
// Check source.
if len(srcURLs) != 1 {
fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments.")
}
// Check target.
if _, tgtContent, err := url2Stat(ctx, tgtURL, "", false, keys, timeRef, false); err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument().Trace(tgtURL), "Target `"+tgtURL+"` is not a folder.")
}
}
for _, srcURL := range srcURLs {
c, srcContent, err := url2Stat(ctx, srcURL, "", false, keys, timeRef, isZip)
fatalIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.")
if srcContent.Type.IsDir() {
// Require --recursive flag if we are copying a directory
if !isRecursive {
operation := "copy"
if isMvCmd {
operation = "move"
}
fatalIf(errInvalidArgument().Trace(srcURL), fmt.Sprintf("To %v a folder requires --recursive flag.", operation))
}
// Check if we are going to copy a directory into itself
if isURLContains(srcURL, tgtURL, string(c.GetURL().Separator)) {
operation := "Copying"
if isMvCmd {
operation = "Moving"
}
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("%v a folder into itself is not allowed.", operation))
}
}
}
}
// checkCopySyntaxTypeD verifies if the source is a valid list of files and target is a valid folder.
func checkCopySyntaxTypeD(ctx context.Context, tgtURL string, keys map[string][]prefixSSEPair, timeRef time.Time) {
// Source can be anything: file, dir, dir...
// Check target if it is a dir
if _, tgtContent, err := url2Stat(ctx, tgtURL, "", false, keys, timeRef, false); err == nil {
if !tgtContent.Type.IsDir() {
fatalIf(errInvalidArgument().Trace(tgtURL), "Target `"+tgtURL+"` is not a folder.")
}
}
}
+167 -86
View File
@@ -53,127 +53,182 @@ const (
// guessCopyURLType guesses the type of clientURL. This approach all allows prepareURL
// functions to accurately report failure causes.
func guessCopyURLType(ctx context.Context, o prepareCopyURLsOpts) (copyURLsType, string, *probe.Error) {
func guessCopyURLType(ctx context.Context, o prepareCopyURLsOpts) (*copyURLsContent, *probe.Error) {
cc := new(copyURLsContent)
// Extract alias before fiddling with the clientURL.
cc.sourceURL = o.sourceURLs[0]
cc.sourceAlias, _, _ = mustExpandAlias(cc.sourceURL)
// Find alias and expanded clientURL.
cc.targetAlias, cc.targetURL, _ = mustExpandAlias(o.targetURL)
if len(o.sourceURLs) == 1 { // 1 Source, 1 Target
var err *probe.Error
var sourceContent *ClientContent
sourceURL := o.sourceURLs[0]
if !o.isRecursive {
_, sourceContent, err = url2Stat(ctx, sourceURL, o.versionID, false, o.encKeyDB, o.timeRef, o.isZip)
_, cc.sourceContent, err = url2Stat(ctx, cc.sourceURL, o.versionID, false, o.encKeyDB, o.timeRef, o.isZip)
} else {
_, sourceContent, err = firstURL2Stat(ctx, sourceURL, o.timeRef, o.isZip)
_, cc.sourceContent, err = firstURL2Stat(ctx, cc.sourceURL, o.timeRef, o.isZip)
}
if err != nil {
return copyURLsTypeInvalid, "", err
cc.copyType = copyURLsTypeInvalid
return cc, err
}
// If recursion is ON, it is type C.
// If source is a folder, it is Type C.
if sourceContent.Type.IsDir() || o.isRecursive {
return copyURLsTypeC, "", nil
if cc.sourceContent.Type.IsDir() || o.isRecursive {
cc.copyType = copyURLsTypeC
return cc, nil
}
// If target is a folder, it is Type B.
if isAliasURLDir(ctx, o.targetURL, o.encKeyDB, o.timeRef) {
return copyURLsTypeB, sourceContent.VersionID, nil
var isDir bool
isDir, cc.targetContent = isAliasURLDir(ctx, o.targetURL, o.encKeyDB, o.timeRef)
if isDir {
cc.copyType = copyURLsTypeB
cc.sourceVersionID = cc.sourceContent.VersionID
return cc, nil
}
// else Type A.
return copyURLsTypeA, sourceContent.VersionID, nil
cc.copyType = copyURLsTypeA
cc.sourceVersionID = cc.sourceContent.VersionID
return cc, nil
}
var isDir bool
// Multiple source args and target is a folder. It is Type D.
if isAliasURLDir(ctx, o.targetURL, o.encKeyDB, o.timeRef) {
return copyURLsTypeD, "", nil
isDir, cc.targetContent = isAliasURLDir(ctx, o.targetURL, o.encKeyDB, o.timeRef)
if isDir {
cc.copyType = copyURLsTypeD
return cc, nil
}
return copyURLsTypeInvalid, "", errInvalidArgument().Trace()
cc.copyType = copyURLsTypeInvalid
return cc, errInvalidArgument().Trace()
}
// SINGLE SOURCE - Type A: copy(f, f) -> copy(f, f)
// prepareCopyURLsTypeA - prepares target and source clientURLs for copying.
func prepareCopyURLsTypeA(ctx context.Context, sourceURL, sourceVersion, targetURL string, encKeyDB map[string][]prefixSSEPair, isZip bool) URLs {
// Extract alias before fiddling with the clientURL.
sourceAlias, _, _ := mustExpandAlias(sourceURL)
// Find alias and expanded clientURL.
targetAlias, targetURL, _ := mustExpandAlias(targetURL)
_, sourceContent, err := url2Stat(ctx, sourceURL, sourceVersion, false, encKeyDB, time.Time{}, isZip)
if err != nil {
// Source does not exist or insufficient privileges.
return URLs{Error: err.Trace(sourceURL)}
func prepareCopyURLsTypeA(ctx context.Context, cc copyURLsContent, o prepareCopyURLsOpts) URLs {
var err *probe.Error
if cc.sourceContent == nil {
_, cc.sourceContent, err = url2Stat(ctx, cc.sourceURL, cc.sourceVersionID, false, o.encKeyDB, time.Time{}, o.isZip)
if err != nil {
// Source does not exist or insufficient privileges.
return URLs{Error: err.Trace(cc.sourceURL)}
}
}
if !sourceContent.Type.IsRegular() {
if !cc.sourceContent.Type.IsRegular() {
// Source is not a regular file
return URLs{Error: errInvalidSource(sourceURL).Trace(sourceURL)}
return URLs{Error: errInvalidSource(cc.sourceURL).Trace(cc.sourceURL)}
}
// All OK.. We can proceed. Type A
return makeCopyContentTypeA(sourceAlias, sourceContent, targetAlias, targetURL)
return makeCopyContentTypeA(cc)
}
// prepareCopyContentTypeA - makes CopyURLs content for copying.
func makeCopyContentTypeA(sourceAlias string, sourceContent *ClientContent, targetAlias, targetURL string) URLs {
targetContent := ClientContent{URL: *newClientURL(targetURL)}
func makeCopyContentTypeA(cc copyURLsContent) URLs {
targetContent := ClientContent{URL: *newClientURL(cc.targetURL)}
return URLs{
SourceAlias: sourceAlias,
SourceContent: sourceContent,
TargetAlias: targetAlias,
SourceAlias: cc.sourceAlias,
SourceContent: cc.sourceContent,
TargetAlias: cc.targetAlias,
TargetContent: &targetContent,
}
}
// SINGLE SOURCE - Type B: copy(f, d) -> copy(f, d/f) -> A
// prepareCopyURLsTypeB - prepares target and source clientURLs for copying.
func prepareCopyURLsTypeB(ctx context.Context, sourceURL, sourceVersion, targetURL string, encKeyDB map[string][]prefixSSEPair, isZip bool) URLs {
// Extract alias before fiddling with the clientURL.
sourceAlias, _, _ := mustExpandAlias(sourceURL)
// Find alias and expanded clientURL.
targetAlias, targetURL, _ := mustExpandAlias(targetURL)
_, sourceContent, err := url2Stat(ctx, sourceURL, sourceVersion, false, encKeyDB, time.Time{}, isZip)
if err != nil {
// Source does not exist or insufficient privileges.
return URLs{Error: err.Trace(sourceURL)}
func prepareCopyURLsTypeB(ctx context.Context, cc copyURLsContent, o prepareCopyURLsOpts) URLs {
var err *probe.Error
if cc.sourceContent == nil {
_, cc.sourceContent, err = url2Stat(ctx, cc.sourceURL, cc.sourceVersionID, false, o.encKeyDB, time.Time{}, o.isZip)
if err != nil {
// Source does not exist or insufficient privileges.
return URLs{Error: err.Trace(cc.sourceURL)}
}
}
if !sourceContent.Type.IsRegular() {
if sourceContent.Type.IsDir() {
return URLs{Error: errSourceIsDir(sourceURL).Trace(sourceURL)}
if !cc.sourceContent.Type.IsRegular() {
if cc.sourceContent.Type.IsDir() {
return URLs{Error: errSourceIsDir(cc.sourceURL).Trace(cc.sourceURL)}
}
// Source is not a regular file.
return URLs{Error: errInvalidSource(sourceURL).Trace(sourceURL)}
return URLs{Error: errInvalidSource(cc.sourceURL).Trace(cc.sourceURL)}
}
if cc.targetContent == nil {
_, cc.targetContent, err = url2Stat(ctx, cc.targetURL, "", false, o.encKeyDB, time.Time{}, false)
if err == nil {
if !cc.targetContent.Type.IsDir() {
return URLs{Error: errInvalidTarget(cc.targetURL).Trace(cc.targetURL)}
}
}
}
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
return makeCopyContentTypeB(sourceAlias, sourceContent, targetAlias, targetURL)
return makeCopyContentTypeB(cc)
}
// makeCopyContentTypeB - CopyURLs content for copying.
func makeCopyContentTypeB(sourceAlias string, sourceContent *ClientContent, targetAlias, targetURL string) URLs {
func makeCopyContentTypeB(cc copyURLsContent) URLs {
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
targetURLParse := newClientURL(targetURL)
targetURLParse.Path = filepath.ToSlash(filepath.Join(targetURLParse.Path, filepath.Base(sourceContent.URL.Path)))
return makeCopyContentTypeA(sourceAlias, sourceContent, targetAlias, targetURLParse.String())
targetURLParse := newClientURL(cc.targetURL)
targetURLParse.Path = filepath.ToSlash(filepath.Join(targetURLParse.Path, filepath.Base(cc.sourceContent.URL.Path)))
cc.targetURL = targetURLParse.String()
return makeCopyContentTypeA(cc)
}
// SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A
// prepareCopyRecursiveURLTypeC - prepares target and source clientURLs for copying.
func prepareCopyURLsTypeC(ctx context.Context, sourceURL, targetURL string, isRecursive, isZip bool, timeRef time.Time) <-chan URLs {
// Extract alias before fiddling with the clientURL.
sourceAlias, _, _ := mustExpandAlias(sourceURL)
// Find alias and expanded clientURL.
targetAlias, targetURL, _ := mustExpandAlias(targetURL)
copyURLsCh := make(chan URLs)
go func(sourceURL, targetURL string, copyURLsCh chan URLs) {
defer close(copyURLsCh)
sourceClient, err := newClient(sourceURL)
func prepareCopyURLsTypeC(ctx context.Context, cc copyURLsContent, o prepareCopyURLsOpts) <-chan URLs {
copyURLsCh := make(chan URLs, 1)
returnErrorAndCloseChannel := func(err *probe.Error) chan URLs {
copyURLsCh <- URLs{Error: err}
close(copyURLsCh)
return copyURLsCh
}
c, err := newClient(cc.sourceURL)
if err != nil {
return returnErrorAndCloseChannel(err.Trace(cc.sourceURL))
}
if cc.targetContent == nil {
_, cc.targetContent, err = url2Stat(ctx, cc.targetURL, "", false, o.encKeyDB, time.Time{}, o.isZip)
if err == nil {
if !cc.targetContent.Type.IsDir() {
return returnErrorAndCloseChannel(errTargetIsNotDir(cc.targetURL).Trace(cc.targetURL))
}
}
}
if cc.sourceContent == nil {
_, cc.sourceContent, err = url2Stat(ctx, cc.sourceURL, "", false, o.encKeyDB, time.Time{}, o.isZip)
if err != nil {
// Source initialization failed.
copyURLsCh <- URLs{Error: err.Trace(sourceURL)}
return
return returnErrorAndCloseChannel(err.Trace(cc.sourceURL))
}
}
if cc.sourceContent.Type.IsDir() {
// Require --recursive flag if we are copying a directory
if !o.isRecursive {
return returnErrorAndCloseChannel(errRequiresRecursive(cc.sourceURL).Trace(cc.sourceURL))
}
for sourceContent := range sourceClient.List(ctx, ListOptions{Recursive: isRecursive, TimeRef: timeRef, ShowDir: DirNone, ListZip: isZip}) {
// Check if we are going to copy a directory into itself
if isURLContains(cc.sourceURL, cc.targetURL, string(c.GetURL().Separator)) {
return returnErrorAndCloseChannel(errCopyIntoSelf(cc.sourceURL).Trace(cc.targetURL))
}
}
go func(sourceClient Client, cc copyURLsContent, o prepareCopyURLsOpts, copyURLsCh chan URLs) {
defer close(copyURLsCh)
for sourceContent := range sourceClient.List(ctx, ListOptions{Recursive: o.isRecursive, TimeRef: o.timeRef, ShowDir: DirNone, ListZip: o.isZip}) {
if sourceContent.Err != nil {
// Listing failed.
copyURLsCh <- URLs{Error: sourceContent.Err.Trace(sourceClient.GetURL().String())}
@@ -185,38 +240,50 @@ func prepareCopyURLsTypeC(ctx context.Context, sourceURL, targetURL string, isRe
continue
}
// Clone cc
newCC := cc
newCC.sourceContent = sourceContent
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
copyURLsCh <- makeCopyContentTypeC(sourceAlias, sourceClient.GetURL(), sourceContent, targetAlias, targetURL)
copyURLsCh <- makeCopyContentTypeC(newCC, sourceClient.GetURL())
}
}(sourceURL, targetURL, copyURLsCh)
}(c, cc, o, copyURLsCh)
return copyURLsCh
}
// makeCopyContentTypeC - CopyURLs content for copying.
func makeCopyContentTypeC(sourceAlias string, sourceURL ClientURL, sourceContent *ClientContent, targetAlias, targetURL string) URLs {
newSourceURL := sourceContent.URL
pathSeparatorIndex := strings.LastIndex(sourceURL.Path, string(sourceURL.Separator))
func makeCopyContentTypeC(cc copyURLsContent, sourceClientURL ClientURL) URLs {
newSourceURL := cc.sourceContent.URL
pathSeparatorIndex := strings.LastIndex(sourceClientURL.Path, string(sourceClientURL.Separator))
newSourceSuffix := filepath.ToSlash(newSourceURL.Path)
if pathSeparatorIndex > 1 {
sourcePrefix := filepath.ToSlash(sourceURL.Path[:pathSeparatorIndex])
sourcePrefix := filepath.ToSlash(sourceClientURL.Path[:pathSeparatorIndex])
newSourceSuffix = strings.TrimPrefix(newSourceSuffix, sourcePrefix)
}
newTargetURL := urlJoinPath(targetURL, newSourceSuffix)
return makeCopyContentTypeA(sourceAlias, sourceContent, targetAlias, newTargetURL)
newTargetURL := urlJoinPath(cc.targetURL, newSourceSuffix)
cc.targetURL = newTargetURL
return makeCopyContentTypeA(cc)
}
// MULTI-SOURCE - Type D: copy([](f|d...), d) -> []B
// prepareCopyURLsTypeE - prepares target and source clientURLs for copying.
func prepareCopyURLsTypeD(ctx context.Context, sourceURLs []string, targetURL string, isRecursive bool, timeRef time.Time) <-chan URLs {
copyURLsCh := make(chan URLs)
go func(sourceURLs []string, targetURL string, copyURLsCh chan URLs) {
func prepareCopyURLsTypeD(ctx context.Context, cc copyURLsContent, o prepareCopyURLsOpts) <-chan URLs {
copyURLsCh := make(chan URLs, 1)
go func(ctx context.Context, cc copyURLsContent, o prepareCopyURLsOpts) {
defer close(copyURLsCh)
for _, sourceURL := range sourceURLs {
for cpURLs := range prepareCopyURLsTypeC(ctx, sourceURL, targetURL, isRecursive, false, timeRef) {
for _, sourceURL := range o.sourceURLs {
// Clone CC
newCC := cc
newCC.sourceURL = sourceURL
for cpURLs := range prepareCopyURLsTypeC(ctx, newCC, o) {
copyURLsCh <- cpURLs
}
}
}(sourceURLs, targetURL, copyURLsCh)
}(ctx, cc, o)
return copyURLsCh
}
@@ -231,25 +298,39 @@ type prepareCopyURLsOpts struct {
isZip bool
}
type copyURLsContent struct {
targetContent *ClientContent
targetAlias string
targetURL string
sourceContent *ClientContent
sourceAlias string
sourceURL string
copyType copyURLsType
sourceVersionID string
}
// prepareCopyURLs - prepares target and source clientURLs for copying.
func prepareCopyURLs(ctx context.Context, o prepareCopyURLsOpts) chan URLs {
copyURLsCh := make(chan URLs)
go func(o prepareCopyURLsOpts) {
defer close(copyURLsCh)
cpType, cpVersion, err := guessCopyURLType(ctx, o)
fatalIf(err.Trace(), "Unable to guess the type of copy operation.")
copyURLsContent, err := guessCopyURLType(ctx, o)
if err != nil {
copyURLsCh <- URLs{Error: errUnableToGuess().Trace(o.sourceURLs...)}
return
}
switch cpType {
switch copyURLsContent.copyType {
case copyURLsTypeA:
copyURLsCh <- prepareCopyURLsTypeA(ctx, o.sourceURLs[0], cpVersion, o.targetURL, o.encKeyDB, o.isZip)
copyURLsCh <- prepareCopyURLsTypeA(ctx, *copyURLsContent, o)
case copyURLsTypeB:
copyURLsCh <- prepareCopyURLsTypeB(ctx, o.sourceURLs[0], cpVersion, o.targetURL, o.encKeyDB, o.isZip)
copyURLsCh <- prepareCopyURLsTypeB(ctx, *copyURLsContent, o)
case copyURLsTypeC:
for cURLs := range prepareCopyURLsTypeC(ctx, o.sourceURLs[0], o.targetURL, o.isRecursive, o.isZip, o.timeRef) {
for cURLs := range prepareCopyURLsTypeC(ctx, *copyURLsContent, o) {
copyURLsCh <- cURLs
}
case copyURLsTypeD:
for cURLs := range prepareCopyURLsTypeD(ctx, o.sourceURLs, o.targetURL, o.isRecursive, o.timeRef) {
for cURLs := range prepareCopyURLsTypeD(ctx, *copyURLsContent, o) {
copyURLsCh <- cURLs
}
default:
+3 -1
View File
@@ -243,8 +243,10 @@ func mainDu(cliCtx *cli.Context) error {
timeRef := parseRewindFlag(cliCtx.String("rewind"))
var duErr error
var isDir bool
for _, urlStr := range cliCtx.Args() {
if !isAliasURLDir(ctx, urlStr, nil, time.Time{}) {
isDir, _ = isAliasURLDir(ctx, urlStr, nil, time.Time{})
if !isDir {
fatalIf(errInvalidArgument().Trace(urlStr), fmt.Sprintf("Source `%s` is not a folder. Only folders are supported by 'du' command.", urlStr))
}
+1 -1
View File
@@ -227,7 +227,7 @@ func mainMove(cliCtx *cli.Context) error {
}
// check 'copy' cli arguments.
checkCopySyntax(ctx, cliCtx, encKeyDB, true)
checkCopySyntax(cliCtx)
if cliCtx.NArg() == 2 {
args := cliCtx.Args()
+3 -22
View File
@@ -105,13 +105,13 @@ func getOdUrls(ctx context.Context, args argKVS) (odURLs URLs, e error) {
sourceURLs: []string{inFile},
targetURL: outFile,
}
odType, _, err := guessCopyURLType(ctx, opts)
copyURLsContent, err := guessCopyURLType(ctx, opts)
fatalIf(err, "Unable to guess copy URL type")
// Get content of inFile, set up URLs.
switch odType {
switch copyURLsContent.copyType {
case copyURLsTypeA:
odURLs = prepareOdUrls(ctx, inFile, "", outFile)
odURLs = makeCopyContentTypeA(*copyURLsContent)
case copyURLsTypeB:
return URLs{}, fmt.Errorf("invalid source path %s, destination cannot be a directory", outFile)
default:
@@ -121,25 +121,6 @@ func getOdUrls(ctx context.Context, args argKVS) (odURLs URLs, e error) {
return odURLs, nil
}
func prepareOdUrls(ctx context.Context, sourceURL, sourceVersion, targetURL string) URLs {
// Extract alias before fiddling with the clientURL.
sourceAlias, _, _ := mustExpandAlias(sourceURL)
// Find alias and expanded clientURL.
targetAlias, targetURL, _ := mustExpandAlias(targetURL)
// Placeholder encryption key database
var encKeyDB map[string][]prefixSSEPair
_, sourceContent, err := url2Stat(ctx, sourceURL, sourceVersion, false, encKeyDB, time.Time{}, false)
if err != nil {
// Source does not exist or insufficient privileges.
return URLs{Error: err}
}
// All OK.. We can proceed. Type A
return makeCopyContentTypeA(sourceAlias, sourceContent, targetAlias, targetURL)
}
// odCheckType checks if request is a download or upload and calls the appropriate function
func odCheckType(ctx context.Context, odURLs URLs, args argKVS) (message, error) {
if odURLs.SourceAlias != "" && odURLs.TargetAlias == "" {
+1 -1
View File
@@ -253,7 +253,7 @@ func checkRmSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string
// Note: UNC path using / works properly in go 1.9.2 even though it breaks the UNC specification.
url = filepath.ToSlash(filepath.Clean(url))
// namespace removal applies only for non FS. So filter out if passed url represents a directory
dir := isAliasURLDir(ctx, url, encKeyDB, time.Time{})
dir, _ := isAliasURLDir(ctx, url, encKeyDB, time.Time{})
if dir {
_, path := url2Alias(url)
isNamespaceRemoval = (path == "")
+28
View File
@@ -39,6 +39,13 @@ var errInvalidArgument = func() *probe.Error {
return probe.NewError(invalidArgumentErr(errors.New(msg))).Untrace()
}
type unableToGuessErr error
var errUnableToGuess = func() *probe.Error {
msg := "Unable to guess the type of copy operation."
return probe.NewError(unableToGuessErr(errors.New(msg)))
}
type unrecognizedDiffTypeErr error
var errUnrecognizedDiffType = func(diff differType) *probe.Error {
@@ -97,6 +104,20 @@ var errInvalidTarget = func(URL string) *probe.Error {
return probe.NewError(invalidTargetErr(errors.New(msg))).Untrace()
}
type requiresRecuriveErr error
var errRequiresRecursive = func(URL string) *probe.Error {
msg := "To copy or move '" + URL + "' the --recursive flag is required."
return probe.NewError(requiresRecuriveErr(errors.New(msg))).Untrace()
}
type copyIntoSelfErr error
var errCopyIntoSelf = func(URL string) *probe.Error {
msg := "Copying or moving '" + URL + "' into itself is not allowed."
return probe.NewError(copyIntoSelfErr(errors.New(msg))).Untrace()
}
type targetNotFoundErr error
var errTargetNotFound = func(URL string) *probe.Error {
@@ -113,6 +134,13 @@ var errOverWriteNotAllowed = func(URL string) *probe.Error {
return probe.NewError(overwriteNotAllowedErr{errors.New(msg)})
}
type targetIsNotDirErr error
var errTargetIsNotDir = func(URL string) *probe.Error {
msg := "Target `" + URL + "` is not a folder."
return probe.NewError(targetIsNotDirErr(errors.New(msg))).Untrace()
}
type sourceIsDirErr error
var errSourceIsDir = func(URL string) *probe.Error {
+49 -13
View File
@@ -315,6 +315,16 @@ function test_list_dir()
log_success "$start_time" "${FUNCNAME[0]}"
}
function test_od_object() {
show "${FUNCNAME[0]}"
start_time=$(get_time)
object_name="mc-test-object-$RANDOM"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd od if="${FILE_1_MB}" of="${SERVER_ALIAS}/${BUCKET_NAME}/${object_name}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd od of="${FILE_1_MB}" if="${SERVER_ALIAS}/${BUCKET_NAME}/${object_name}"
log_success "$start_time" "${FUNCNAME[0]}"
}
function test_put_object()
{
@@ -983,6 +993,8 @@ function test_admin_users()
# create a user
username=foo
password=foobar12345
test_alias="aliasx"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin user add "$SERVER_ALIAS" "$username" "$password"
# check that user appears in the user list
@@ -993,49 +1005,60 @@ function test_admin_users()
# setup temporary alias to make requests as the created user.
scheme="https"
if [ "$ENABLE_HTTPS" != "1" ]; then
scheme="http"
scheme="http"
fi
object1_name="mc-test-object-$RANDOM"
object2_name="mc-test-object-$RANDOM"
export MC_HOST_foo="${scheme}://${username}:${password}@${SERVER_ENDPOINT}"
# Adding an alias for the $test_alias
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd alias set $test_alias "${scheme}://${SERVER_ENDPOINT}" ${username} ${password}
# check that alias appears in the alias list
"${MC_CMD[@]}" --json alias list | jq -r '.alias' | grep --quiet "^${test_alias}$"
rv=$?
assert_success "$start_time" "${FUNCNAME[0]}" show_on_failure ${rv} "alias ${test_alias} did NOT appear in the list of aliases returned by server"
# check that the user can write objects with readwrite policy
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "foo/${BUCKET_NAME}/${object1_name}"
# Validate that the correct policy has been added to the user
"${MC_CMD[@]}" --json admin user list "${SERVER_ALIAS}" | jq -r '.policyName' | grep --quiet "^readwrite$"
rv=$?
assert_success "$start_time" "${FUNCNAME[0]}" show_on_failure ${rv} "user ${username} did NOT have the readwrite policy attached"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that the user cannot write objects with readonly policy
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy detach "$SERVER_ALIAS" readwrite --user="${username}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readonly --user="${username}"
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "foo/${BUCKET_NAME}/${object2_name}"
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "${test_alias}/${BUCKET_NAME}/${object2_name}"
# check that the user can read with readonly policy
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cat "foo/${BUCKET_NAME}/${object1_name}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cat "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that user can delete with readwrite policy
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin policy attach "$SERVER_ALIAS" readwrite --user="${username}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rm "foo/${BUCKET_NAME}/${object1_name}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rm "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that user cannot perform admin actions with readwrite policy
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd admin info "foo"
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd admin info $test_alias
# create object1_name for subsequent tests.
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "foo/${BUCKET_NAME}/${object1_name}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "$FILE_1_MB" "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that user can be disabled
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin user disable "$SERVER_ALIAS" "$username"
# check that disabled cannot perform any action
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cat "foo/${BUCKET_NAME}/${object1_name}"
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cat "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that user can be enabled and can then perform an allowed action
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin user enable "$SERVER_ALIAS" "$username"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cat "foo/${BUCKET_NAME}/${object1_name}"
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cat "${test_alias}/${BUCKET_NAME}/${object1_name}"
# check that user can be removed, and then is no longer available
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd admin user remove "$SERVER_ALIAS" "$username"
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cat "foo/${BUCKET_NAME}/${object1_name}"
unset MC_HOST_foo
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd cat "${test_alias}/${BUCKET_NAME}/${object1_name}"
log_success "$start_time" "${FUNCNAME[0]}"
}
@@ -1057,6 +1080,7 @@ function run_test()
test_put_object_multipart
test_get_object
test_get_object_multipart
test_od_object
test_mv_object
test_presigned_post_policy_error
test_presigned_put_object
@@ -1162,8 +1186,20 @@ function __init__()
set +e
}
function validate_dependencies() {
jqVersion=$(jq --version)
if [[ $jqVersion == *"jq"* ]]; then
echo "Dependency validation complete"
else
echo "jq is missing, please install: 'sudo apt install jq'"
exit 1
fi
}
function main()
{
validate_dependencies
( run_test )
rv=$?