diff --git a/cmd/client-errors.go b/cmd/client-errors.go index 15abedc7..1c726fcc 100644 --- a/cmd/client-errors.go +++ b/cmd/client-errors.go @@ -34,6 +34,13 @@ func (e APINotImplemented) Error() string { return "`" + e.API + "` is not supported for `" + e.APIType + "`." } +// InvalidArgument - passed argument is invalid for this operation +type InvalidArgument struct{} + +func (e InvalidArgument) Error() string { + return "invalid argument" +} + // GenericBucketError - generic bucket operations error type GenericBucketError struct { Bucket string diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 8a72e8bb..2b723a4d 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -2836,10 +2836,13 @@ func (c *S3Client) DeleteEncryption(ctx context.Context) *probe.Error { // GetBucketInfo gets info about a bucket func (c *S3Client) GetBucketInfo(ctx context.Context) (BucketInfo, *probe.Error) { var b BucketInfo - bucket, _ := c.url2BucketAndObject() + bucket, object := c.url2BucketAndObject() if bucket == "" { return b, probe.NewError(BucketNameEmpty{}) } + if object != "" { + return b, probe.NewError(InvalidArgument{}) + } content, err := c.bucketStat(ctx, bucket) if err != nil { return b, err.Trace(bucket) diff --git a/cmd/stat.go b/cmd/stat.go index c94630fc..c3496de0 100644 --- a/cmd/stat.go +++ b/cmd/stat.go @@ -180,9 +180,53 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time targetAlias, _, _ := mustExpandAlias(targetURL) prefixPath := clnt.GetURL().Path separator := string(clnt.GetURL().Separator) - if !strings.HasSuffix(prefixPath, separator) { + + hasTrailingSlash := strings.HasSuffix(prefixPath, separator) + + if !hasTrailingSlash { prefixPath = prefixPath[:strings.LastIndex(prefixPath, separator)+1] } + + // if stat is on a bucket and non-recursive mode, serve the bucket metadata + if !isRecursive && !hasTrailingSlash { + bstat, err := clnt.GetBucketInfo(ctx) + if err == nil { + // Convert any os specific delimiters to "/". + contentURL := filepath.ToSlash(bstat.URL.Path) + prefixPath = filepath.ToSlash(prefixPath) + // Trim prefix path from the content path. + contentURL = strings.TrimPrefix(contentURL, prefixPath) + bstat.URL.Path = contentURL + + if bstat.Date.IsZero() || bstat.Date.Equal(timeSentinel) { + bstat.Date = time.Now() + } + + var bu madmin.BucketUsageInfo + + adminClient, _ := newAdminClient(targetURL) + if adminClient != nil { + // Create a new MinIO Admin Client + duinfo, e := adminClient.DataUsageInfo(ctx) + if e == nil { + bu = duinfo.BucketsUsage[bstat.Key] + } + } + + if prefixPath != "/" { + bstat.Prefix = true + } + + printMsg(bucketInfoMessage{ + Status: "success", + BucketInfo: bstat, + Usage: bu, + }) + + return nil + } + } + lstOptions := ListOptions{Recursive: isRecursive, Incomplete: isIncomplete, ShowDir: DirNone} switch { case versionID != "": @@ -193,7 +237,6 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time lstOptions.WithDeleteMarkers = true lstOptions.TimeRef = timeRef } - adminClient, _ := newAdminClient(targetURL) var e error for content := range clnt.List(ctx, lstOptions) { @@ -234,47 +277,11 @@ func statURL(ctx context.Context, targetURL, versionID string, timeRef time.Time continue } } - clnt, stat, err := url2Stat(ctx, url, content.VersionID, true, encKeyDB, timeRef, false) + _, stat, err := url2Stat(ctx, url, content.VersionID, true, encKeyDB, timeRef, false) if err != nil { continue } - // if stat is on a bucket and non-recursive mode, serve the bucket metadata - if clnt != nil && !isRecursive && stat.Type.IsDir() { - bstat, err := clnt.GetBucketInfo(ctx) - if err == nil { - // Convert any os specific delimiters to "/". - contentURL := filepath.ToSlash(bstat.URL.Path) - prefixPath = filepath.ToSlash(prefixPath) - // Trim prefix path from the content path. - contentURL = strings.TrimPrefix(contentURL, prefixPath) - bstat.URL.Path = contentURL - - if bstat.Date.IsZero() || bstat.Date.Equal(timeSentinel) { - bstat.Date = content.Time - } - var bu madmin.BucketUsageInfo - if adminClient != nil { - // Create a new MinIO Admin Client - duinfo, e := adminClient.DataUsageInfo(globalContext) - if e == nil { - bu = duinfo.BucketsUsage[stat.BucketName] - } - } - - if prefixPath != "/" { - bstat.Prefix = true - } - - printMsg(bucketInfoMessage{ - Status: "success", - BucketInfo: bstat, - Usage: bu, - }) - continue - } - } - // Convert any os specific delimiters to "/". contentURL := filepath.ToSlash(stat.URL.Path) prefixPath = filepath.ToSlash(prefixPath)