avoid stat and listing with --purge (#4869)
This commit is contained in:
@@ -106,7 +106,15 @@ func getEncKeys(ctx *cli.Context) (map[string][]prefixSSEPair, *probe.Error) {
|
||||
func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefixSSEPair, timeRef time.Time, ignoreBucketExists bool) (bool, *ClientContent) {
|
||||
// If the target url exists, check if it is a directory
|
||||
// and return immediately.
|
||||
_, targetContent, err := url2Stat(ctx, url2StatOptions{urlStr: aliasURL, versionID: "", fileAttr: false, encKeyDB: keys, timeRef: timeRef, isZip: false, ignoreBucketExistsCheck: ignoreBucketExists})
|
||||
_, targetContent, err := url2Stat(ctx, url2StatOptions{
|
||||
urlStr: aliasURL,
|
||||
versionID: "",
|
||||
fileAttr: false,
|
||||
encKeyDB: keys,
|
||||
timeRef: timeRef,
|
||||
isZip: false,
|
||||
ignoreBucketExistsCheck: ignoreBucketExists,
|
||||
})
|
||||
if err == nil {
|
||||
return targetContent.Type.IsDir(), targetContent
|
||||
}
|
||||
|
||||
+102
-87
@@ -248,26 +248,30 @@ func checkRmSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string
|
||||
fatalIf(errDummy().Trace(),
|
||||
"You cannot specify --purge flag with any flag(s) other than --force.")
|
||||
}
|
||||
for _, url := range cliCtx.Args() {
|
||||
// clean path for aliases like s3/.
|
||||
// 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{}, false)
|
||||
if dir {
|
||||
_, path := url2Alias(url)
|
||||
isNamespaceRemoval = (path == "")
|
||||
break
|
||||
}
|
||||
if dir && isRecursive && !isForce {
|
||||
fatalIf(errDummy().Trace(),
|
||||
"Removal requires --force flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.")
|
||||
}
|
||||
if dir && !isRecursive {
|
||||
fatalIf(errDummy().Trace(),
|
||||
"Removal requires --recursive flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.")
|
||||
|
||||
if !isForceDel {
|
||||
for _, url := range cliCtx.Args() {
|
||||
// clean path for aliases like s3/.
|
||||
// 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{}, false)
|
||||
if dir {
|
||||
_, path := url2Alias(url)
|
||||
isNamespaceRemoval = (path == "")
|
||||
break
|
||||
}
|
||||
if dir && isRecursive && !isForce {
|
||||
fatalIf(errDummy().Trace(),
|
||||
"Removal requires --force flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.")
|
||||
}
|
||||
if dir && !isRecursive {
|
||||
fatalIf(errDummy().Trace(),
|
||||
"Removal requires --recursive flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !cliCtx.Args().Present() && !isStdin {
|
||||
exitCode := 1
|
||||
showCommandHelpAndExit(cliCtx, exitCode)
|
||||
@@ -302,80 +306,91 @@ func removeSingle(url, versionID string, opts removeOpts) error {
|
||||
modTime time.Time
|
||||
)
|
||||
|
||||
_, content, pErr := url2Stat(ctx, url2StatOptions{urlStr: url, versionID: versionID, fileAttr: false, encKeyDB: opts.encKeyDB, timeRef: time.Time{}, isZip: false, ignoreBucketExistsCheck: false})
|
||||
if pErr != nil {
|
||||
switch st := minio.ToErrorResponse(pErr.ToGoError()).StatusCode; st {
|
||||
case http.StatusBadRequest, http.StatusMethodNotAllowed:
|
||||
ignoreStatError = true
|
||||
default:
|
||||
_, ok := pErr.ToGoError().(ObjectMissing)
|
||||
ignoreStatError = (st == http.StatusServiceUnavailable || ok || st == http.StatusNotFound) && (opts.isForce && opts.isForceDel)
|
||||
if !ignoreStatError {
|
||||
errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
isDir = content.Type.IsDir()
|
||||
modTime = content.Time
|
||||
}
|
||||
|
||||
// We should not proceed
|
||||
if ignoreStatError && opts.olderThan != "" || opts.newerThan != "" {
|
||||
errorIf(pErr.Trace(url), "Unable to stat `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
|
||||
// Skip objects older than older--than parameter if specified
|
||||
if opts.olderThan != "" && isOlder(modTime, opts.olderThan) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip objects older than older--than parameter if specified
|
||||
if opts.newerThan != "" && isNewer(modTime, opts.newerThan) {
|
||||
return nil
|
||||
}
|
||||
|
||||
targetAlias, targetURL, _ := mustExpandAlias(url)
|
||||
if !opts.isFake {
|
||||
clnt, pErr := newClientFromAlias(targetAlias, targetURL)
|
||||
if !opts.isForceDel {
|
||||
_, content, pErr := url2Stat(ctx, url2StatOptions{
|
||||
urlStr: url,
|
||||
versionID: versionID,
|
||||
fileAttr: false,
|
||||
encKeyDB: opts.encKeyDB,
|
||||
timeRef: time.Time{},
|
||||
isZip: false,
|
||||
ignoreBucketExistsCheck: false,
|
||||
})
|
||||
if pErr != nil {
|
||||
errorIf(pErr.Trace(url), "Invalid argument `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus) // End of journey.
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(targetURL, string(clnt.GetURL().Separator)) && isDir {
|
||||
targetURL = targetURL + string(clnt.GetURL().Separator)
|
||||
}
|
||||
|
||||
contentCh := make(chan *ClientContent, 1)
|
||||
contentURL := *newClientURL(targetURL)
|
||||
contentCh <- &ClientContent{URL: contentURL, VersionID: versionID}
|
||||
close(contentCh)
|
||||
isRemoveBucket := false
|
||||
resultCh := clnt.Remove(ctx, opts.isIncomplete, isRemoveBucket, opts.isBypass, opts.isForce && opts.isForceDel, contentCh)
|
||||
for result := range resultCh {
|
||||
if result.Err != nil {
|
||||
errorIf(result.Err.Trace(url), "Failed to remove `"+url+"`.")
|
||||
switch result.Err.ToGoError().(type) {
|
||||
case PathInsufficientPermission:
|
||||
// Ignore Permission error.
|
||||
continue
|
||||
switch st := minio.ToErrorResponse(pErr.ToGoError()).StatusCode; st {
|
||||
case http.StatusBadRequest, http.StatusMethodNotAllowed:
|
||||
ignoreStatError = true
|
||||
default:
|
||||
_, ok := pErr.ToGoError().(ObjectMissing)
|
||||
ignoreStatError = (st == http.StatusServiceUnavailable || ok || st == http.StatusNotFound) && (opts.isForce && opts.isForceDel)
|
||||
if !ignoreStatError {
|
||||
errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
msg := rmMessage{
|
||||
Key: path.Join(targetAlias, result.BucketName, result.ObjectName),
|
||||
VersionID: result.ObjectVersionID,
|
||||
}
|
||||
if result.DeleteMarker {
|
||||
msg.DeleteMarker = true
|
||||
msg.VersionID = result.DeleteMarkerVersionID
|
||||
}
|
||||
printMsg(msg)
|
||||
} else {
|
||||
isDir = content.Type.IsDir()
|
||||
modTime = content.Time
|
||||
}
|
||||
} else {
|
||||
printDryRunMsg(targetAlias, content, opts.withVersions)
|
||||
|
||||
// We should not proceed
|
||||
if ignoreStatError && opts.olderThan != "" || opts.newerThan != "" {
|
||||
errorIf(pErr.Trace(url), "Unable to stat `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
|
||||
// Skip objects older than older--than parameter if specified
|
||||
if opts.olderThan != "" && isOlder(modTime, opts.olderThan) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip objects older than older--than parameter if specified
|
||||
if opts.newerThan != "" && isNewer(modTime, opts.newerThan) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if opts.isFake {
|
||||
printDryRunMsg(targetAlias, content, opts.withVersions)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
clnt, pErr := newClientFromAlias(targetAlias, targetURL)
|
||||
if pErr != nil {
|
||||
errorIf(pErr.Trace(url), "Invalid argument `"+url+"`.")
|
||||
return exitStatus(globalErrorExitStatus) // End of journey.
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(targetURL, string(clnt.GetURL().Separator)) && isDir {
|
||||
targetURL = targetURL + string(clnt.GetURL().Separator)
|
||||
}
|
||||
|
||||
contentCh := make(chan *ClientContent, 1)
|
||||
contentURL := *newClientURL(targetURL)
|
||||
contentCh <- &ClientContent{URL: contentURL, VersionID: versionID}
|
||||
close(contentCh)
|
||||
isRemoveBucket := false
|
||||
resultCh := clnt.Remove(ctx, opts.isIncomplete, isRemoveBucket, opts.isBypass, opts.isForce && opts.isForceDel, contentCh)
|
||||
for result := range resultCh {
|
||||
if result.Err != nil {
|
||||
errorIf(result.Err.Trace(url), "Failed to remove `"+url+"`.")
|
||||
switch result.Err.ToGoError().(type) {
|
||||
case PathInsufficientPermission:
|
||||
// Ignore Permission error.
|
||||
continue
|
||||
}
|
||||
return exitStatus(globalErrorExitStatus)
|
||||
}
|
||||
msg := rmMessage{
|
||||
Key: path.Join(targetAlias, result.BucketName, result.ObjectName),
|
||||
VersionID: result.ObjectVersionID,
|
||||
}
|
||||
if result.DeleteMarker {
|
||||
msg.DeleteMarker = true
|
||||
msg.VersionID = result.DeleteMarkerVersionID
|
||||
}
|
||||
printMsg(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user