From 1e1516c968dda42e1e784d0e34bb1947cdcc0674 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 30 Mar 2016 18:44:01 -0700 Subject: [PATCH] stat: Print proper error messages when bucket name is empty. Handle cases for situations for all commands where target bucket name is not specified, indicate proper errors in this situation. Fixes #1629 --- client-errors.go | 9 +++++ client-s3.go | 87 ++++++++++++++++++++++++------------------------ mirror-main.go | 12 ++----- mirror-url.go | 2 +- 4 files changed, 56 insertions(+), 54 deletions(-) diff --git a/client-errors.go b/client-errors.go index bf244016..81f25de0 100644 --- a/client-errors.go +++ b/client-errors.go @@ -56,6 +56,15 @@ func (e BucketNameEmpty) Error() string { return "Bucket name cannot be empty." } +// BucketInvalid - bucket name invalid. +type BucketInvalid struct { + Bucket string +} + +func (e BucketInvalid) Error() string { + return "Bucket name " + e.Bucket + " not valid." +} + // ObjectAlreadyExists - typed return for MethodNotAllowed type ObjectAlreadyExists struct { Object string diff --git a/client-s3.go b/client-s3.go index d1243304..10605983 100644 --- a/client-s3.go +++ b/client-s3.go @@ -161,6 +161,9 @@ func (c *s3Client) Put(reader io.Reader, size int64, contentType string, progres if contentType == "" { contentType = "application/octet-stream" } + if bucket == "" { + return 0, probe.NewError(BucketNameEmpty{}) + } n, e := c.api.PutObjectWithProgress(bucket, object, reader, contentType, progress) if e != nil { errResponse := minio.ToErrorResponse(e) @@ -185,7 +188,12 @@ func (c *s3Client) Put(reader io.Reader, size int64, contentType string, progres Bucket: bucket, }) } - if errResponse.Code == "InvalidArgument" { + if errResponse.Code == "InvalidBucketName" { + return n, probe.NewError(BucketInvalid{ + Bucket: bucket, + }) + } + if errResponse.Code == "NoSuchKey" || errResponse.Code == "InvalidArgument" { return n, probe.NewError(ObjectMissing{}) } return n, probe.NewError(e) @@ -276,59 +284,52 @@ func (c *s3Client) Stat() (*clientContent, *probe.Error) { c.mutex.Lock() objectMetadata := &clientContent{} bucket, object := c.url2BucketAndObject() - switch { - // valid case for 'ls -r s3/' - case bucket == "" && object == "": - _, e := c.api.ListBuckets() + // Bucket name cannot be empty, stat on URL has no meaning. + if bucket == "" { + c.mutex.Unlock() + return nil, probe.NewError(BucketNameEmpty{}) + } else if object == "" { + e := c.api.BucketExists(bucket) if e != nil { c.mutex.Unlock() return nil, probe.NewError(e) } + bucketMetadata := &clientContent{} + bucketMetadata.URL = *c.targetURL + bucketMetadata.Type = os.ModeDir c.mutex.Unlock() - return &clientContent{URL: *c.targetURL, Type: os.ModeDir}, nil + return bucketMetadata, nil } - if object != "" { - metadata, e := c.api.StatObject(bucket, object) - if e != nil { - c.mutex.Unlock() - errResponse := minio.ToErrorResponse(e) - if errResponse.Code == "NoSuchKey" { - // Append "/" to the object name proactively and see if the Listing - // produces an output. If yes, then we treat it as a directory. - prefixName := object - // Trim any trailing separators and add it. - prefixName = strings.TrimSuffix(prefixName, string(c.targetURL.Separator)) + string(c.targetURL.Separator) - isRecursive := false - for objectStat := range c.api.ListObjects(bucket, prefixName, isRecursive, nil) { - if objectStat.Err != nil { - return nil, probe.NewError(objectStat.Err) - } - content := clientContent{} - content.URL = *c.targetURL - content.Type = os.ModeDir - return &content, nil - } - return nil, probe.NewError(PathNotFound{Path: c.targetURL.Path}) - } - return nil, probe.NewError(e) - } - objectMetadata.URL = *c.targetURL - objectMetadata.Time = metadata.LastModified - objectMetadata.Size = metadata.Size - objectMetadata.Type = os.FileMode(0664) - c.mutex.Unlock() - return objectMetadata, nil - } - e := c.api.BucketExists(bucket) + metadata, e := c.api.StatObject(bucket, object) if e != nil { c.mutex.Unlock() + errResponse := minio.ToErrorResponse(e) + if errResponse.Code == "NoSuchKey" { + // Append "/" to the object name proactively and see if the Listing + // produces an output. If yes, then we treat it as a directory. + prefixName := object + // Trim any trailing separators and add it. + prefixName = strings.TrimSuffix(prefixName, string(c.targetURL.Separator)) + string(c.targetURL.Separator) + isRecursive := false + for objectStat := range c.api.ListObjects(bucket, prefixName, isRecursive, nil) { + if objectStat.Err != nil { + return nil, probe.NewError(objectStat.Err) + } + content := clientContent{} + content.URL = *c.targetURL + content.Type = os.ModeDir + return &content, nil + } + return nil, probe.NewError(PathNotFound{Path: c.targetURL.Path}) + } return nil, probe.NewError(e) } - bucketMetadata := &clientContent{} - bucketMetadata.URL = *c.targetURL - bucketMetadata.Type = os.ModeDir + objectMetadata.URL = *c.targetURL + objectMetadata.Time = metadata.LastModified + objectMetadata.Size = metadata.Size + objectMetadata.Type = os.FileMode(0664) c.mutex.Unlock() - return bucketMetadata, nil + return objectMetadata, nil } func isAmazon(host string) bool { diff --git a/mirror-main.go b/mirror-main.go index b252e328..aefcbff9 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -309,17 +309,9 @@ func doMirrorSession(session *sessionV6) { // remaining files. switch sURLs.Error.ToGoError().(type) { // Handle this specifically for filesystem related errors. - case BrokenSymlink: + case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: continue - case TooManyLevelsSymlink: - continue - case PathNotFound: - continue - case PathInsufficientPermission: - continue - case ObjectAlreadyExists: - continue - case BucketDoesNotExist: + case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists, BucketDoesNotExist, BucketInvalid: continue } // For critical errors we should exit. Session diff --git a/mirror-url.go b/mirror-url.go index 49fa7e73..24290a74 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -86,7 +86,7 @@ func checkMirrorSyntax(ctx *cli.Context) { _, _, err = url2Stat(tgtURL) // we die on any error other than PathNotFound - destination directory need not exist. if _, ok := err.ToGoError().(PathNotFound); !ok { - fatalIf(err.Trace(tgtURL), fmt.Sprintf("Unable to stat %s", tgtURL)) + fatalIf(err.Trace(tgtURL), fmt.Sprintf("Unable to stat target ā€˜%s’.", tgtURL)) } }