diff --git a/pkg/client/client.go b/pkg/client/client.go index 71e753ff..bde8dfe3 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -18,6 +18,7 @@ package client import ( "io" + "regexp" "strconv" "time" ) @@ -55,15 +56,32 @@ type Prefix struct { Prefix string } -/// Collection of standard errors - -// InvalidBucketName - bucket name invalid -type InvalidBucketName struct { - Bucket string +// IsValidBucketName reports whether bucket is a valid bucket name, per Amazon's naming restrictions. +// See http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html +func IsValidBucketName(bucket string) bool { + if len(bucket) < 3 || len(bucket) > 63 { + return false + } + if bucket[0] == '.' || bucket[len(bucket)-1] == '.' { + return false + } + if match, _ := regexp.MatchString("\\.\\.", bucket); match == true { + return false + } + // We don't support buckets with '.' in them + match, _ := regexp.MatchString("^[a-zA-Z][a-zA-Z0-9\\-]+[a-zA-Z0-9]$", bucket) + return match } -func (e InvalidBucketName) Error() string { - return "Invalid bucketname " + e.Bucket +/// Collection of standard errors + +// APINotImplemented - api not implemented +type APINotImplemented struct { + API string +} + +func (e APINotImplemented) Error() string { + return "API not implemented: " + e.API } // InvalidArgument - bad arguments provided @@ -105,21 +123,55 @@ func (e InvalidRange) Error() string { return "invalid range offset: " + strconv.FormatInt(e.Offset, 10) } -// BucketNotFound - bucket requested does not exist -type BucketNotFound struct { +// GenericBucketError - generic bucket operations error +type GenericBucketError struct { Bucket string } +// BucketNotFound - bucket requested does not exist +type BucketNotFound GenericBucketError + func (e BucketNotFound) Error() string { return "bucket " + e.Bucket + " not found" } -// ObjectNotFound - object requested does not exist -type ObjectNotFound struct { +// BucketExists - bucket exists +type BucketExists GenericBucketError + +func (e BucketExists) Error() string { + return "bucket " + e.Bucket + " exists" +} + +// InvalidBucketName - bucket name invalid +type InvalidBucketName GenericBucketError + +func (e InvalidBucketName) Error() string { + return "Invalid bucketname " + e.Bucket +} + +// GenericObjectError - generic object operations error +type GenericObjectError struct { Bucket string Object string } +// ObjectNotFound - object requested does not exist +type ObjectNotFound GenericObjectError + func (e ObjectNotFound) Error() string { return "object " + e.Object + " not found in bucket " + e.Bucket } + +// InvalidObjectName - object requested is invalid +type InvalidObjectName GenericObjectError + +func (e InvalidObjectName) Error() string { + return "object " + e.Object + "at" + e.Bucket + "is invalid" +} + +// ObjectExists - object exists +type ObjectExists GenericObjectError + +func (e ObjectExists) Error() string { + return "object " + e.Object + " exists" +} diff --git a/pkg/client/file/.gitignore b/pkg/client/file/.gitignore new file mode 100644 index 00000000..daac6aea --- /dev/null +++ b/pkg/client/file/.gitignore @@ -0,0 +1 @@ +*# \ No newline at end of file diff --git a/pkg/client/file/file.go b/pkg/client/file/file.go new file mode 100644 index 00000000..b98f4303 --- /dev/null +++ b/pkg/client/file/file.go @@ -0,0 +1,186 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package file + +import ( + "io" + "io/ioutil" + "net/url" + "os" + "path" + "strings" + "time" + + "github.com/minio-io/mc/pkg/client" + "github.com/minio-io/minio/pkg/iodine" +) + +type fileClient struct { + // Supports URL in following formats + // - http://// + // - http://./ + *url.URL +} + +// GetNewClient - +func GetNewClient(path string) client.Client { + u, err := url.Parse(path) + if err != nil { + return nil + } + return &fileClient{u} +} + +/// Object operations + +// isValidObject - wrapper function for input validation +func isValidObject(bucket, object string) (string, os.FileInfo, error) { + if bucket == "" || object == "" { + return "", nil, iodine.New(client.InvalidArgument{}, nil) + } + objectPath := path.Join(bucket, object) + st, err := os.Stat(objectPath) + if os.IsNotExist(err) { + return "", nil, iodine.New(client.ObjectNotFound{Bucket: bucket, Object: object}, nil) + } + if st.IsDir() { + return "", nil, iodine.New(client.InvalidObjectName{Bucket: bucket, Object: object}, nil) + } + if err != nil { + return "", nil, iodine.New(err, nil) + } + return objectPath, st, nil +} + +// Put - upload new object to bucket +func (f *fileClient) Put(bucket, object, md5HexString string, size int64, contents io.Reader) error { + // handle md5HexString match internally + if bucket == "" || object == "" { + return iodine.New(client.InvalidArgument{}, nil) + } + objectPath := path.Join(bucket, object) + if size < 0 { + return iodine.New(client.InvalidArgument{}, nil) + } + file, err := os.OpenFile(objectPath, os.O_CREATE|os.O_EXCL, 0600) + if os.IsExist(err) { + return iodine.New(client.ObjectExists{Bucket: bucket, Object: object}, nil) + } + if err != nil { + return iodine.New(err, nil) + } + _, err = io.CopyN(file, contents, size) + if err != nil { + return iodine.New(err, nil) + } + return nil +} + +// Get - download an object from bucket +func (f *fileClient) Get(bucket, object string) (body io.ReadCloser, size int64, md5 string, err error) { + objectPath, st, err := isValidObject(bucket, object) + if err != nil { + return nil, 0, "", iodine.New(err, nil) + } + body, err = os.OpenFile(objectPath, os.O_RDONLY, 0600) + if err != nil { + return nil, 0, "", iodine.New(err, nil) + } + // TODO: support md5sum - there is no easier way to do it right now without temporary buffer + // so avoiding it to ensure no out of memory situations + return body, st.Size(), "", nil +} + +// GetPartial - download a partial object from bucket +func (f *fileClient) GetPartial(bucket, key string, offset, length int64) (body io.ReadCloser, size int64, md5 string, err error) { + if offset < 0 { + return nil, 0, "", iodine.New(client.InvalidRange{Offset: offset}, nil) + } + return nil, 0, "", iodine.New(client.APINotImplemented{API: "GetPartial"}, nil) +} + +// StatObject - +func (f *fileClient) StatObject(bucket, object string) (size int64, date time.Time, reterr error) { + _, st, err := isValidObject(bucket, object) + if size < 0 { + return 0, date, iodine.New(client.InvalidArgument{}, nil) + } + if err != nil { + return 0, date, iodine.New(err, nil) + } + return st.Size(), st.ModTime(), nil +} + +/// Bucket operations + +// ListBuckets - get list of buckets +func (f *fileClient) ListBuckets() ([]*client.Bucket, error) { + buckets, err := ioutil.ReadDir(f.Path) + if err != nil { + return nil, iodine.New(err, nil) + } + var results []*client.Bucket + for _, bucket := range buckets { + result := &client.Bucket{ + Name: bucket.Name(), + CreationDate: bucket.ModTime(), // no easier way on Linux + } + results = append(results, result) + } + return results, nil +} + +// ListObjects - get a list of objects +func (f *fileClient) ListObjects(bucket, keyPrefix string) (items []*client.Item, err error) { + return nil, iodine.New(client.APINotImplemented{API: "ListObjects"}, nil) +} + +// PutBucket - create a new bucket +func (f *fileClient) PutBucket(bucket string) error { + if bucket == "" || strings.TrimSpace(bucket) == "" { + return iodine.New(client.InvalidArgument{}, nil) + } + if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") { + return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) + } + err := os.Mkdir(bucket, os.ModeDir) + if os.IsExist(err) { + return iodine.New(client.BucketExists{Bucket: bucket}, nil) + } + if err != nil { + return iodine.New(err, nil) + } + return nil +} + +// StatBucket - +func (f *fileClient) StatBucket(bucket string) error { + if bucket == "" { + return iodine.New(client.InvalidArgument{}, nil) + } + if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") { + return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) + } + st, err := os.Stat(bucket) + if os.IsNotExist(err) { + return iodine.New(client.BucketNotFound{Bucket: bucket}, nil) + } + if !st.IsDir() { + return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) + } + return nil +} diff --git a/pkg/client/s3/bucket.go b/pkg/client/s3/bucket.go index 52dbc5e9..83d234d1 100644 --- a/pkg/client/s3/bucket.go +++ b/pkg/client/s3/bucket.go @@ -78,7 +78,7 @@ func (c *s3Client) ListBuckets() ([]*client.Bucket, error) { // PutBucket - create new bucket func (c *s3Client) PutBucket(bucket string) error { - if !IsValidBucketName(bucket) || strings.Contains(bucket, ".") { + if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") { return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) } u := fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket) @@ -102,7 +102,7 @@ func (c *s3Client) StatBucket(bucket string) error { if bucket == "" { return iodine.New(client.InvalidArgument{}, nil) } - if !IsValidBucketName(bucket) || strings.Contains(bucket, ".") { + if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") { return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) } u := fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket) diff --git a/pkg/client/s3/client.go b/pkg/client/s3/client.go index 8425fbfe..fd09c951 100644 --- a/pkg/client/s3/client.go +++ b/pkg/client/s3/client.go @@ -36,7 +36,6 @@ limitations under the License. * limitations under the License. */ -// Package s3 implements a generic Amazon S3 client package s3 import ( @@ -45,7 +44,6 @@ import ( "net" "net/http" "net/url" - "regexp" "strings" "encoding/xml" @@ -117,7 +115,7 @@ func (c *s3Client) bucketURL(bucket string) string { var url string // TODO: Bucket names can contain ".". This second check should be removed. // when minio server supports buckets with "." - if IsValidBucketName(bucket) && !strings.Contains(bucket, ".") { + if client.IsValidBucketName(bucket) && !strings.Contains(bucket, ".") { // if localhost use PathStyle if strings.Contains(c.Host, "localhost") || strings.Contains(c.Host, "127.0.0.1") { return fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket) @@ -169,20 +167,3 @@ func listAllMyBuckets(r io.Reader) ([]*client.Bucket, error) { } return res.Buckets.Bucket, nil } - -// IsValidBucketName reports whether bucket is a valid bucket name, per Amazon's naming restrictions. -// See http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html -func IsValidBucketName(bucket string) bool { - if len(bucket) < 3 || len(bucket) > 63 { - return false - } - if bucket[0] == '.' || bucket[len(bucket)-1] == '.' { - return false - } - if match, _ := regexp.MatchString("\\.\\.", bucket); match == true { - return false - } - // We don't support buckets with '.' in them - match, _ := regexp.MatchString("^[a-zA-Z][a-zA-Z0-9\\-]+[a-zA-Z0-9]$", bucket) - return match -}