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
This commit is contained in:
Harshavardhana
2016-03-30 18:46:20 -07:00
parent aeb6de996c
commit 1e1516c968
4 changed files with 56 additions and 54 deletions
+9
View File
@@ -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
+44 -43
View File
@@ -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 {
+2 -10
View File
@@ -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
+1 -1
View File
@@ -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))
}
}