conditionally disallow list() when object does not exist (#5016)

allow users to avoid list() call when an object does not
exist when following flags are used with `mc stat`

```
--no-list (HEAD the latest version)
--version-id <vid>
```

Also avoid superflous url2Stat() calls while processing
the args, there is no real reason to do that.
This commit is contained in:
Harshavardhana
2024-08-12 01:52:27 -07:00
committed by GitHub
parent e7d1e97852
commit 32b7b72193
4 changed files with 76 additions and 17 deletions
+12 -2
View File
@@ -1736,7 +1736,10 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent,
}
if path == "" {
content, err := c.bucketStat(ctx, BucketStatOptions{bucket: bucket, ignoreBucketExists: opts.ignoreBucketExists})
content, err := c.bucketStat(ctx, BucketStatOptions{
bucket: bucket,
ignoreBucketExists: opts.ignoreBucketExists,
})
if err != nil {
return nil, err.Trace(bucket)
}
@@ -1771,13 +1774,20 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent,
if !errors.As(err.ToGoError(), &ObjectMissing{}) && !errors.As(err.ToGoError(), &ObjectIsDeleteMarker{}) {
return nil, err
}
// when versionID is specified we do not have to perform List() operation
if opts.versionID != "" && errors.As(err.ToGoError(), &ObjectMissing{}) || errors.As(err.ToGoError(), &ObjectIsDeleteMarker{}) {
return nil, probe.NewError(ObjectMissing{opts.timeRef})
}
// The object is not found, look for a directory marker or a prefix
path += string(c.targetURL.Separator)
}
nonRecursive := false
maxKeys := 1
for objectStat := range c.listObjectWrapper(ctx, bucket, path, nonRecursive, opts.timeRef, opts.includeVersions, opts.includeVersions, false, maxKeys, opts.isZip) {
for objectStat := range c.listObjectWrapper(ctx, bucket, path, nonRecursive, opts.timeRef,
opts.includeVersions, opts.includeVersions, false, maxKeys, opts.isZip) {
if objectStat.Err != nil {
return nil, probe.NewError(objectStat.Err)
}
+9 -1
View File
@@ -50,6 +50,7 @@ type url2StatOptions struct {
encKeyDB map[string][]prefixSSEPair
timeRef time.Time
isZip bool
headOnly bool
ignoreBucketExistsCheck bool
}
@@ -204,7 +205,14 @@ func url2Stat(ctx context.Context, opts url2StatOptions) (client Client, content
alias, _ := url2Alias(opts.urlStr)
sse := getSSE(opts.urlStr, opts.encKeyDB[alias])
content, err = client.Stat(ctx, StatOptions{preserve: opts.fileAttr, sse: sse, timeRef: opts.timeRef, versionID: opts.versionID, isZip: opts.isZip, ignoreBucketExists: opts.ignoreBucketExistsCheck})
content, err = client.Stat(ctx, StatOptions{
preserve: opts.fileAttr,
sse: sse,
timeRef: opts.timeRef,
versionID: opts.versionID,
isZip: opts.isZip,
ignoreBucketExists: opts.ignoreBucketExistsCheck,
})
if err != nil {
return nil, nil, err.Trace(opts.urlStr)
}
+15 -8
View File
@@ -49,7 +49,11 @@ var (
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "show verbose buckets information",
Usage: "show extended bucket(s) stat",
},
cli.BoolFlag{
Name: "no-list",
Usage: "disable all LIST operations for stat",
},
}
)
@@ -98,7 +102,7 @@ EXAMPLES:
}
// parseAndCheckStatSyntax - parse and validate all the passed arguments
func parseAndCheckStatSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB map[string][]prefixSSEPair) ([]string, bool, string, time.Time, bool) {
func parseAndCheckStatSyntax(ctx context.Context, cliCtx *cli.Context) ([]string, bool, string, time.Time, bool) {
if !cliCtx.Args().Present() {
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code
}
@@ -113,6 +117,7 @@ func parseAndCheckStatSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB
recursive := cliCtx.Bool("recursive")
versionID := cliCtx.String("version-id")
withVersions := cliCtx.Bool("versions")
headOnly := cliCtx.Bool("no-list")
rewind := parseRewindFlag(cliCtx.String("rewind"))
// extract URLs.
@@ -125,12 +130,13 @@ func parseAndCheckStatSyntax(ctx context.Context, cliCtx *cli.Context, encKeyDB
if versionID != "" && (recursive || withVersions || !rewind.IsZero()) {
fatalIf(errInvalidArgument().Trace(args...), "You cannot specify --version-id with either --rewind, --versions or --recursive.")
}
if (recursive || withVersions) && headOnly {
fatalIf(errInvalidArgument().Trace(args...), "You cannot specify --no-list with either --versions or --recursive.")
}
var targetUrls []string
for _, url := range URLs {
_, _, err := url2Stat(ctx, url2StatOptions{urlStr: url, versionID: versionID, fileAttr: false, encKeyDB: encKeyDB, timeRef: rewind, isZip: false, ignoreBucketExistsCheck: false})
if err != nil {
fatalIf(err.Trace(url), "Unable to stat `"+url+"`.")
}
_, path := url2Alias(url)
if path != "" || !cliCtx.Bool("verbose") {
targetUrls = append(targetUrls, url)
@@ -175,14 +181,15 @@ func mainStat(cliCtx *cli.Context) error {
fatalIf(err, "Unable to parse encryption keys.")
// check 'stat' cli arguments.
args, isRecursive, versionID, rewind, withVersions := parseAndCheckStatSyntax(ctx, cliCtx, encKeyDB)
args, isRecursive, versionID, rewind, withVersions := parseAndCheckStatSyntax(ctx, cliCtx)
// mimic operating system tool behavior.
if len(args) == 0 {
args = []string{"."}
}
headOnly := cliCtx.Bool("no-list")
for _, targetURL := range args {
fatalIf(statURL(ctx, targetURL, versionID, rewind, withVersions, false, isRecursive, encKeyDB), "Unable to stat `"+targetURL+"`.")
fatalIf(statURL(ctx, targetURL, versionID, rewind, withVersions, false, isRecursive, headOnly, encKeyDB), "Unable to stat `"+targetURL+"`.")
}
return nil
+40 -6
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2022 MinIO, Inc.
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
@@ -203,22 +203,45 @@ func getStandardizedURL(targetURL string) string {
// statURL - uses combination of GET listing and HEAD to fetch information of one or more objects
// HEAD can fail with 400 with an SSE-C encrypted object but we still return information gathered
// from GET listing.
func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time, includeOlderVersions, isIncomplete, isRecursive bool, encKeyDB map[string][]prefixSSEPair) *probe.Error {
func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time, includeOlderVersions, isIncomplete, isRecursive, headOnly bool, encKeyDB map[string][]prefixSSEPair) *probe.Error {
clnt, err := newClient(targetURL)
if err != nil {
return err
}
targetAlias, _, _ := mustExpandAlias(targetURL)
prefixPath := clnt.GetURL().Path
separator := string(clnt.GetURL().Separator)
prefixPath := clnt.GetURL().Path
hasTrailingSlash := strings.HasSuffix(prefixPath, separator)
if !hasTrailingSlash {
prefixPath = prefixPath[:strings.LastIndex(prefixPath, separator)+1]
}
if headOnly || versionID != "" {
url := getStandardizedURL(targetURL)
_, stat, err := url2Stat(ctx, url2StatOptions{
urlStr: url, versionID: versionID,
fileAttr: true, encKeyDB: encKeyDB,
timeRef: timeRef, isZip: false,
ignoreBucketExistsCheck: false,
headOnly: headOnly,
})
if err != nil {
return err
}
// Convert any os specific delimiters to "/".
contentURL := filepath.ToSlash(stat.URL.Path)
// Trim prefix path from the content path.
stat.URL.Path = strings.TrimPrefix(contentURL, filepath.ToSlash(prefixPath))
printMsg(parseStat(stat))
return nil
}
// if stat is on a bucket and non-recursive mode, serve the bucket metadata
if !isRecursive && !hasTrailingSlash {
bstat, err := clnt.GetBucketInfo(ctx)
@@ -271,6 +294,7 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time
}
var e error
var found int
for content := range clnt.List(ctx, lstOptions) {
if content.Err != nil {
switch content.Err.ToGoError().(type) {
@@ -292,6 +316,7 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time
e = exitStatus(globalErrorExitStatus) // Set the exit status.
continue
}
found++
if content.StorageClass == s3StorageClassGlacier {
continue
@@ -309,9 +334,14 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time
continue
}
}
_, stat, err := url2Stat(ctx, url2StatOptions{urlStr: url, versionID: content.VersionID, fileAttr: true, encKeyDB: encKeyDB, timeRef: timeRef, isZip: false, ignoreBucketExistsCheck: false})
_, stat, err := url2Stat(ctx, url2StatOptions{
urlStr: url, versionID: content.VersionID,
fileAttr: true, encKeyDB: encKeyDB,
timeRef: timeRef, isZip: false,
ignoreBucketExistsCheck: false,
})
if err != nil {
continue
return err.Trace(url)
}
// Convert any os specific delimiters to "/".
@@ -324,6 +354,10 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time
printMsg(parseStat(stat))
}
if found <= 0 {
return probe.NewError(ObjectMissing{timeRef})
}
return probe.NewError(e)
}