stat: Fix output when stat a prefix which has other prefixes (#4501)

This commit is contained in:
Anis Eleuch
2023-03-23 13:03:04 -07:00
committed by GitHub
parent 8d6afee8fc
commit 81453d7c8f
3 changed files with 57 additions and 40 deletions
+7
View File
@@ -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
+4 -1
View File
@@ -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)
+46 -39
View File
@@ -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)