2015-04-08 19:28:14 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-04-07 00:42:54 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-05-11 19:23:10 -04:00
|
|
|
"github.com/minio/minio/pkg/iodine"
|
2015-04-07 00:42:54 -04:00
|
|
|
)
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// MakeBucket - make a new bucket
|
2015-04-22 21:19:53 -04:00
|
|
|
func (d donut) MakeBucket(bucket, acl string) error {
|
2015-04-07 00:42:54 -04:00
|
|
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return iodine.New(InvalidArgument{}, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-22 21:19:53 -04:00
|
|
|
return d.makeDonutBucket(bucket, acl)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetBucketMetadata - get bucket metadata
|
2015-04-07 00:42:54 -04:00
|
|
|
func (d donut) GetBucketMetadata(bucket string) (map[string]string, error) {
|
2015-04-08 19:28:14 -04:00
|
|
|
err := d.getDonutBuckets()
|
2015-04-07 03:44:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucket]; !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-07 03:44:01 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
metadata, err := d.getDonutBucketMetadata()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
return metadata[bucket], nil
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// SetBucketMetadata - set bucket metadata
|
2015-04-27 05:04:05 -04:00
|
|
|
func (d donut) SetBucketMetadata(bucket string, bucketMetadata map[string]string) error {
|
|
|
|
err := d.getDonutBuckets()
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
metadata, err := d.getDonutBucketMetadata()
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
2015-04-27 06:04:29 -04:00
|
|
|
oldBucketMetadata := metadata[bucket]
|
|
|
|
// TODO ignore rest of the keys for now, only mutable data is "acl"
|
|
|
|
oldBucketMetadata["acl"] = bucketMetadata["acl"]
|
|
|
|
metadata[bucket] = oldBucketMetadata
|
2015-04-27 05:04:05 -04:00
|
|
|
return d.setDonutBucketMetadata(metadata)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// ListBuckets - return list of buckets
|
2015-05-21 13:57:38 -04:00
|
|
|
func (d donut) ListBuckets() (metadata map[string]map[string]string, err error) {
|
2015-04-08 19:28:14 -04:00
|
|
|
err = d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
2015-05-21 13:57:38 -04:00
|
|
|
dummyMetadata := make(map[string]map[string]string)
|
|
|
|
metadata, err = d.getDonutBucketMetadata()
|
2015-04-27 05:04:05 -04:00
|
|
|
if err != nil {
|
2015-05-21 13:57:38 -04:00
|
|
|
// intentionally left out the error when Donut is empty
|
|
|
|
// but we need to revisit this area in future - since we need
|
|
|
|
// to figure out between acceptable and unacceptable errors
|
|
|
|
return dummyMetadata, nil
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-05-21 13:57:38 -04:00
|
|
|
return metadata, nil
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// ListObjects - return list of objects
|
2015-04-07 00:42:54 -04:00
|
|
|
func (d donut) ListObjects(bucket, prefix, marker, delimiter string, maxkeys int) ([]string, []string, bool, error) {
|
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"prefix": prefix,
|
|
|
|
"marker": marker,
|
|
|
|
"delimiter": delimiter,
|
|
|
|
"maxkeys": strconv.Itoa(maxkeys),
|
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
err := d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, false, iodine.New(err, errParams)
|
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucket]; !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, nil, false, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
objectList, err := d.buckets[bucket].ListObjects()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, false, iodine.New(err, errParams)
|
|
|
|
}
|
|
|
|
var donutObjects []string
|
|
|
|
for objectName := range objectList {
|
|
|
|
donutObjects = append(donutObjects, objectName)
|
|
|
|
}
|
|
|
|
if maxkeys <= 0 {
|
|
|
|
maxkeys = 1000
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(prefix) != "" {
|
|
|
|
donutObjects = filterPrefix(donutObjects, prefix)
|
|
|
|
donutObjects = removePrefix(donutObjects, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualObjects []string
|
2015-04-07 03:01:19 -04:00
|
|
|
var actualPrefixes []string
|
2015-04-07 00:42:54 -04:00
|
|
|
var isTruncated bool
|
|
|
|
if strings.TrimSpace(delimiter) != "" {
|
|
|
|
actualObjects = filterDelimited(donutObjects, delimiter)
|
2015-04-07 03:01:19 -04:00
|
|
|
actualPrefixes = filterNotDelimited(donutObjects, delimiter)
|
|
|
|
actualPrefixes = extractDir(actualPrefixes, delimiter)
|
|
|
|
actualPrefixes = uniqueObjects(actualPrefixes)
|
2015-04-07 00:42:54 -04:00
|
|
|
} else {
|
|
|
|
actualObjects = donutObjects
|
|
|
|
}
|
2015-04-07 03:01:19 -04:00
|
|
|
|
2015-05-06 01:18:24 -04:00
|
|
|
sort.Strings(actualObjects)
|
|
|
|
var newActualObjects []string
|
|
|
|
switch {
|
|
|
|
case marker != "":
|
|
|
|
for _, objectName := range actualObjects {
|
|
|
|
if objectName > marker {
|
|
|
|
newActualObjects = append(newActualObjects, objectName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
newActualObjects = actualObjects
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:42:54 -04:00
|
|
|
var results []string
|
2015-04-07 03:01:19 -04:00
|
|
|
var commonPrefixes []string
|
2015-05-06 01:18:24 -04:00
|
|
|
for _, objectName := range newActualObjects {
|
2015-04-07 00:42:54 -04:00
|
|
|
if len(results) >= maxkeys {
|
|
|
|
isTruncated = true
|
|
|
|
break
|
|
|
|
}
|
2015-04-07 03:01:19 -04:00
|
|
|
results = appendUniq(results, prefix+objectName)
|
|
|
|
}
|
|
|
|
for _, commonPrefix := range actualPrefixes {
|
|
|
|
commonPrefixes = appendUniq(commonPrefixes, prefix+commonPrefix)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
sort.Strings(results)
|
2015-04-07 03:01:19 -04:00
|
|
|
sort.Strings(commonPrefixes)
|
2015-04-07 00:42:54 -04:00
|
|
|
return results, commonPrefixes, isTruncated, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// PutObject - put object
|
2015-04-30 06:38:11 -04:00
|
|
|
func (d donut) PutObject(bucket, object, expectedMD5Sum string, reader io.ReadCloser, metadata map[string]string) (string, error) {
|
2015-04-07 00:42:54 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"object": object,
|
|
|
|
}
|
|
|
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return "", iodine.New(InvalidArgument{}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
if object == "" || strings.TrimSpace(object) == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return "", iodine.New(InvalidArgument{}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
err := d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucket]; !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return "", iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
objectList, err := d.buckets[bucket].ListObjects()
|
|
|
|
if err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-27 05:04:05 -04:00
|
|
|
}
|
|
|
|
for objectName := range objectList {
|
|
|
|
if objectName == object {
|
2015-06-23 14:44:32 -04:00
|
|
|
return "", iodine.New(ObjectExists{Object: object}, nil)
|
2015-04-27 05:04:05 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 06:38:11 -04:00
|
|
|
md5sum, err := d.buckets[bucket].PutObject(object, reader, expectedMD5Sum, metadata)
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-30 06:38:11 -04:00
|
|
|
return md5sum, nil
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetObject - get object
|
2015-04-07 00:42:54 -04:00
|
|
|
func (d donut) GetObject(bucket, object string) (reader io.ReadCloser, size int64, err error) {
|
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"object": object,
|
|
|
|
}
|
|
|
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, 0, iodine.New(InvalidArgument{}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
if object == "" || strings.TrimSpace(object) == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, 0, iodine.New(InvalidArgument{}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
err = d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucket]; !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, 0, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
objectList, err := d.buckets[bucket].ListObjects()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for objectName := range objectList {
|
|
|
|
if objectName == object {
|
|
|
|
return d.buckets[bucket].GetObject(object)
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, 0, iodine.New(ObjectNotFound{Object: object}, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// GetObjectMetadata - get object metadata
|
2015-04-07 00:42:54 -04:00
|
|
|
func (d donut) GetObjectMetadata(bucket, object string) (map[string]string, error) {
|
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"object": object,
|
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
err := d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, errParams)
|
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucket]; !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
objectList, err := d.buckets[bucket].ListObjects()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, errParams)
|
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
donutObject, ok := objectList[object]
|
2015-04-07 00:42:54 -04:00
|
|
|
if !ok {
|
2015-06-23 14:44:32 -04:00
|
|
|
return nil, iodine.New(ObjectNotFound{Object: object}, errParams)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
return donutObject.GetObjectMetadata()
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|