From 19952de324d2e3ee195f65b3490bdfdf592e4a85 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 28 Apr 2022 20:50:01 +0100 Subject: [PATCH] Fix client-s3 Stat() for prefix with parallel lower prefixes (#4069) client-s3.Stat() does not properly detect a prefix when there is another lower one, e.g: Stat() of 'dir/' fails when there is 'dir-name/' in the same parent prefix. --- cmd/client-s3.go | 47 +++++++++++++++++++++++++++++++++------------ functional-tests.sh | 20 +++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index f33db51d..5eaeee6a 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1511,7 +1511,7 @@ func (c *S3Client) statIncompleteUpload(ctx context.Context, bucket, object stri func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, *probe.Error) { c.Lock() defer c.Unlock() - bucket, object := c.url2BucketAndObject() + bucket, path := c.url2BucketAndObject() // Bucket name cannot be empty, stat on URL has no meaning. if bucket == "" { @@ -1525,7 +1525,7 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, }, nil } - if object == "" { + if path == "" { content, err := c.bucketStat(ctx, bucket) if err != nil { return nil, err.Trace(bucket) @@ -1535,7 +1535,7 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, // If the request is for incomplete upload stat, handle it here. if opts.incomplete { - return c.statIncompleteUpload(ctx, bucket, object) + return c.statIncompleteUpload(ctx, bucket, path) } // The following code tries to calculate if a given prefix/object does really exist @@ -1550,14 +1550,14 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, // because the list could be very large. At the same time, the HEAD call is avoided if the // object already contains a trailing prefix or we passed rewind flag to know the object version // created just before the rewind parameter. - if !strings.HasSuffix(object, string(c.targetURL.Separator)) && opts.timeRef.IsZero() { + if !strings.HasSuffix(path, string(c.targetURL.Separator)) && opts.timeRef.IsZero() { // Issue HEAD request first but ignore no such key error // so we can check if there is such prefix which exists o := minio.StatObjectOptions{ServerSideEncryption: opts.sse, VersionID: opts.versionID} if opts.isZip { o.Set("x-minio-extract", "true") } - ctnt, err := c.getObjectStat(ctx, bucket, object, o) + ctnt, err := c.getObjectStat(ctx, bucket, path, o) if err == nil { return ctnt, nil } @@ -1568,19 +1568,26 @@ func (c *S3Client) Stat(ctx context.Context, opts StatOptions) (*ClientContent, } } - nonRecursive := false - // Prefix to pass to minio-go listing in order to fetch if a prefix exists - prefix := strings.TrimRight(object, string(c.targetURL.Separator)) + // No object found, start looking for a prefix with the same name + // or a directory marker. Add a trailing slash if it is not in the path + if !strings.HasSuffix(path, string(c.targetURL.Separator)) { + path += string(c.targetURL.Separator) + } - for objectStat := range c.listObjectWrapper(ctx, bucket, prefix, nonRecursive, opts.timeRef, false, false, false, 1, opts.isZip) { + nonRecursive := false + maxKeys := 1 + for objectStat := range c.listObjectWrapper(ctx, bucket, path, nonRecursive, opts.timeRef, false, false, false, maxKeys, opts.isZip) { if objectStat.Err != nil { return nil, probe.NewError(objectStat.Err) } - - if object == objectStat.Key || object == strings.TrimSuffix(objectStat.Key, string(c.targetURL.Separator)) { + // In case of a directory marker + if path == objectStat.Key { return c.objectInfo2ClientContent(bucket, objectStat), nil } - break + if strings.HasPrefix(objectStat.Key, path) { + // An object inside the prefix is found, then the prefix exists. + return c.prefixInfo2ClientContent(bucket, path), nil + } } return nil, probe.NewError(ObjectMissing{opts.timeRef}) @@ -2002,6 +2009,22 @@ func (c *S3Client) bucketInfo2ClientContent(bucket minio.BucketInfo) *ClientCont return content } +// Convert objectInfo to ClientContent +func (c *S3Client) prefixInfo2ClientContent(bucket string, prefix string) *ClientContent { + // Join bucket and incoming object key. + if bucket == "" { + panic("should never happen, bucket cannot be empty") + } + content := &ClientContent{} + url := c.targetURL.Clone() + url.Path = c.joinPath(bucket, prefix) + content.URL = url + content.BucketName = bucket + content.Type = os.ModeDir + content.Time = time.Now() + return content +} + // Convert objectInfo to ClientContent func (c *S3Client) objectInfo2ClientContent(bucket string, entry minio.ObjectInfo) *ClientContent { content := &ClientContent{} diff --git a/functional-tests.sh b/functional-tests.sh index 6e0cb7e9..d94a59fb 100755 --- a/functional-tests.sh +++ b/functional-tests.sh @@ -297,6 +297,25 @@ function teardown() assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rb --force "${SERVER_ALIAS}/${BUCKET_NAME}" } +# Test mc ls on a S3 prefix where a lower similar prefix exists as well e.g. dir-foo/ and dir/ +function test_list_dir() +{ + show "${FUNCNAME[0]}" + + start_time=$(get_time) + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/dir-foo/object1" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/dir/object2" + diff -bB <(echo "object2") <("${MC_CMD[@]}" --json ls "${SERVER_ALIAS}/${BUCKET_NAME}/dir" | jq -r '.key') >/dev/null 2>&1 + assert_success "$start_time" "${FUNCNAME[0]}" show_on_failure $? "unexpected listing dir" + + # Cleanup + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/dir-foo/${object_name}" + assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "${FILE_1_MB}" "${SERVER_ALIAS}/${BUCKET_NAME}/dir/${object_name}" + + log_success "$start_time" "${FUNCNAME[0]}" +} + + function test_put_object() { show "${FUNCNAME[0]}" @@ -1024,6 +1043,7 @@ function run_test() test_rb setup + test_list_dir test_put_object test_put_object_error test_put_object_0byte