2015-04-08 20:13:25 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2015-05-07 22:55:30 -04:00
|
|
|
"crypto/sha512"
|
2015-04-24 18:19:29 -04:00
|
|
|
"encoding/base64"
|
2015-04-08 20:13:25 -04:00
|
|
|
"encoding/hex"
|
2015-04-24 18:19:29 -04:00
|
|
|
"errors"
|
2015-04-30 00:21:38 -04:00
|
|
|
"io"
|
2015-05-04 02:16:45 -04:00
|
|
|
"log"
|
2015-04-30 00:21:38 -04:00
|
|
|
"runtime/debug"
|
|
|
|
"sort"
|
2015-05-04 18:26:56 -04:00
|
|
|
"strconv"
|
2015-04-30 00:21:38 -04:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-04-22 17:44:59 -04:00
|
|
|
|
2015-05-07 22:55:30 -04:00
|
|
|
"math/rand"
|
|
|
|
|
2015-04-23 04:20:03 -04:00
|
|
|
"github.com/minio-io/minio/pkg/iodine"
|
|
|
|
"github.com/minio-io/minio/pkg/storage/drivers"
|
2015-04-08 20:13:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// memoryDriver - local variables
|
|
|
|
type memoryDriver struct {
|
2015-05-04 22:04:56 -04:00
|
|
|
storedBuckets map[string]storedBucket
|
|
|
|
lock *sync.RWMutex
|
|
|
|
objects *Intelligent
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type storedBucket struct {
|
2015-05-09 14:41:26 -04:00
|
|
|
bucketMetadata drivers.BucketMetadata
|
|
|
|
objectMetadata map[string]drivers.ObjectMetadata
|
|
|
|
multiPartSession map[string]multiPartSession
|
|
|
|
}
|
|
|
|
|
|
|
|
type multiPartSession struct {
|
|
|
|
totalParts int
|
|
|
|
uploadID string
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 00:15:43 -04:00
|
|
|
const (
|
|
|
|
totalBuckets = 100
|
|
|
|
)
|
|
|
|
|
2015-04-08 20:13:25 -04:00
|
|
|
// Start memory object server
|
2015-04-29 16:00:19 -04:00
|
|
|
func Start(maxSize uint64, expiration time.Duration) (chan<- string, <-chan error, drivers.Driver) {
|
2015-04-08 20:13:25 -04:00
|
|
|
ctrlChannel := make(chan string)
|
|
|
|
errorChannel := make(chan error)
|
|
|
|
|
2015-04-22 20:50:26 -04:00
|
|
|
var memory *memoryDriver
|
|
|
|
memory = new(memoryDriver)
|
2015-04-30 02:36:30 -04:00
|
|
|
memory.storedBuckets = make(map[string]storedBucket)
|
2015-05-04 22:04:56 -04:00
|
|
|
memory.objects = NewIntelligent(maxSize, expiration)
|
2015-04-08 20:13:25 -04:00
|
|
|
memory.lock = new(sync.RWMutex)
|
|
|
|
|
2015-05-08 15:00:02 -04:00
|
|
|
memory.objects.OnExpired = memory.expiredObject
|
2015-04-15 15:35:23 -04:00
|
|
|
|
2015-04-29 16:00:19 -04:00
|
|
|
// set up memory expiration
|
2015-05-05 17:27:18 -04:00
|
|
|
memory.objects.ExpireObjects(time.Second * 5)
|
2015-05-04 22:04:56 -04:00
|
|
|
|
2015-04-08 20:13:25 -04:00
|
|
|
go start(ctrlChannel, errorChannel)
|
|
|
|
return ctrlChannel, errorChannel, memory
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
|
|
|
|
close(errorChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject - GET object from memory buffer
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) GetObject(w io.Writer, bucket string, object string) (int64, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
2015-05-01 20:45:40 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return 0, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-05-01 23:29:01 -04:00
|
|
|
if !drivers.IsValidObjectName(object) {
|
2015-05-01 20:45:40 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return 0, iodine.New(drivers.ObjectNameInvalid{Object: object}, nil)
|
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-05-01 20:45:40 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return 0, iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + object
|
2015-05-04 05:31:18 -04:00
|
|
|
data, ok := memory.objects.Get(objectKey)
|
|
|
|
if !ok {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return 0, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: object}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
written, err := io.Copy(w, bytes.NewBuffer(data.([]byte)))
|
2015-05-04 18:26:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-05-04 05:31:18 -04:00
|
|
|
return written, iodine.New(err, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPartialObject - GET object from memory buffer range
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
|
2015-05-04 18:26:56 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"object": object,
|
|
|
|
"start": strconv.FormatInt(start, 10),
|
|
|
|
"length": strconv.FormatInt(length, 10),
|
|
|
|
}
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
2015-05-04 18:26:56 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
return 0, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, errParams)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-05-01 23:29:01 -04:00
|
|
|
if !drivers.IsValidObjectName(object) {
|
2015-05-04 18:26:56 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
return 0, iodine.New(drivers.ObjectNameInvalid{Object: object}, errParams)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-05-04 18:26:56 -04:00
|
|
|
if start < 0 {
|
|
|
|
return 0, iodine.New(drivers.InvalidRange{
|
|
|
|
Start: start,
|
|
|
|
Length: length,
|
|
|
|
}, errParams)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 18:26:56 -04:00
|
|
|
objectKey := bucket + "/" + object
|
|
|
|
data, ok := memory.objects.Get(objectKey)
|
|
|
|
if !ok {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return 0, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: object}, errParams)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
written, err := io.CopyN(w, bytes.NewBuffer(data.([]byte)[start:]), length)
|
2015-05-04 18:26:56 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
return written, iodine.New(err, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketMetadata -
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) GetBucketMetadata(bucket string) (drivers.BucketMetadata, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
defer memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
return drivers.BucketMetadata{}, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-04-23 04:20:03 -04:00
|
|
|
return drivers.BucketMetadata{}, iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
return memory.storedBuckets[bucket].bucketMetadata, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 06:04:29 -04:00
|
|
|
// SetBucketMetadata -
|
|
|
|
func (memory *memoryDriver) SetBucketMetadata(bucket, acl string) error {
|
|
|
|
memory.lock.RLock()
|
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-04-27 06:04:29 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
return iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(acl) == "" {
|
|
|
|
acl = "private"
|
|
|
|
}
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
memory.lock.Lock()
|
|
|
|
defer memory.lock.Unlock()
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
storedBucket.bucketMetadata.ACL = drivers.BucketACL(acl)
|
|
|
|
memory.storedBuckets[bucket] = storedBucket
|
2015-04-27 06:04:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:19:29 -04:00
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
|
|
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) error {
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" && strings.TrimSpace(actualMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
|
|
|
return iodine.New(errors.New("bad digest, md5sum mismatch"), nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return iodine.New(errors.New("invalid argument"), nil)
|
|
|
|
}
|
|
|
|
|
2015-05-04 02:16:45 -04:00
|
|
|
func (memory *memoryDriver) CreateObject(bucket, key, contentType, expectedMD5Sum string, size int64, data io.Reader) (string, error) {
|
2015-05-08 01:43:19 -04:00
|
|
|
md5sum, err := memory.createObject(bucket, key, contentType, expectedMD5Sum, size, data)
|
2015-05-04 22:04:56 -04:00
|
|
|
// free
|
|
|
|
debug.FreeOSMemory()
|
2015-05-08 01:43:19 -04:00
|
|
|
return md5sum, iodine.New(err, nil)
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getMD5AndData - this is written as a wrapper to capture md5sum and data in a more memory efficient way
|
|
|
|
func getMD5AndData(reader io.Reader) ([]byte, []byte, error) {
|
|
|
|
hash := md5.New()
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var length int
|
|
|
|
for err == nil {
|
|
|
|
byteBuffer := make([]byte, 1024*1024)
|
|
|
|
length, err = reader.Read(byteBuffer)
|
|
|
|
// While hash.Write() wouldn't mind a Nil byteBuffer
|
|
|
|
// It is necessary for us to verify this and break
|
|
|
|
if length == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
hash.Write(byteBuffer[0:length])
|
|
|
|
data = append(data, byteBuffer[0:length]...)
|
|
|
|
}
|
|
|
|
if err != io.EOF {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return hash.Sum(nil), data, nil
|
2015-04-30 15:38:12 -04:00
|
|
|
}
|
|
|
|
|
2015-05-07 22:55:30 -04:00
|
|
|
// createObject - PUT object to memory buffer
|
2015-05-04 02:16:45 -04:00
|
|
|
func (memory *memoryDriver) createObject(bucket, key, contentType, expectedMD5Sum string, size int64, data io.Reader) (string, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
memory.lock.RUnlock()
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-05-01 23:29:01 -04:00
|
|
|
if !drivers.IsValidObjectName(key) {
|
2015-04-23 04:20:03 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.ObjectNameInvalid{Object: key}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
// get object key
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + key
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.ObjectExists{Bucket: bucket, Object: key}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-08 20:13:25 -04:00
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
2015-04-24 18:19:29 -04:00
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum))
|
|
|
|
if err != nil {
|
|
|
|
// pro-actively close the connection
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.InvalidDigest{Md5: expectedMD5Sum}, nil)
|
2015-04-24 18:19:29 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
md5SumBytes, readBytes, err := getMD5AndData(data)
|
2015-05-04 02:16:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", iodine.New(err, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 22:04:56 -04:00
|
|
|
totalLength := len(readBytes)
|
|
|
|
memory.lock.Lock()
|
|
|
|
memory.objects.Set(objectKey, readBytes)
|
2015-05-04 02:16:45 -04:00
|
|
|
memory.lock.Unlock()
|
2015-05-04 22:04:56 -04:00
|
|
|
// de-allocating
|
|
|
|
readBytes = nil
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-04-24 18:19:29 -04:00
|
|
|
md5Sum := hex.EncodeToString(md5SumBytes)
|
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
if err := isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), md5Sum); err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(drivers.BadDigest{Md5: expectedMD5Sum, Bucket: bucket, Key: key}, nil)
|
2015-04-24 18:19:29 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-04-30 02:36:30 -04:00
|
|
|
newObject := drivers.ObjectMetadata{
|
2015-04-24 18:19:29 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Key: key,
|
|
|
|
|
|
|
|
ContentType: contentType,
|
2015-04-30 22:26:23 -04:00
|
|
|
Created: time.Now().UTC(),
|
2015-04-24 18:19:29 -04:00
|
|
|
Md5: md5Sum,
|
|
|
|
Size: int64(totalLength),
|
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-04-26 23:08:37 -04:00
|
|
|
memory.lock.Lock()
|
2015-04-30 02:36:30 -04:00
|
|
|
memoryObject := make(map[string]drivers.ObjectMetadata)
|
2015-05-04 02:16:45 -04:00
|
|
|
switch {
|
|
|
|
case len(memory.storedBuckets[bucket].objectMetadata) == 0:
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket.objectMetadata = memoryObject
|
|
|
|
storedBucket.objectMetadata[objectKey] = newObject
|
2015-05-04 02:16:45 -04:00
|
|
|
default:
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket.objectMetadata[objectKey] = newObject
|
|
|
|
}
|
|
|
|
memory.storedBuckets[bucket] = storedBucket
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.Unlock()
|
2015-04-30 06:38:11 -04:00
|
|
|
return newObject.Md5, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucket - create bucket in memory
|
2015-04-22 21:19:53 -04:00
|
|
|
func (memory *memoryDriver) CreateBucket(bucketName, acl string) error {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
2015-04-30 02:36:30 -04:00
|
|
|
if len(memory.storedBuckets) == totalBuckets {
|
2015-04-27 00:15:43 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
return iodine.New(drivers.TooManyBuckets{Bucket: bucketName}, nil)
|
|
|
|
}
|
2015-04-08 20:13:25 -04:00
|
|
|
if !drivers.IsValidBucket(bucketName) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return iodine.New(drivers.BucketNameInvalid{Bucket: bucketName}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-22 21:19:53 -04:00
|
|
|
if !drivers.IsValidBucketACL(acl) {
|
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return iodine.New(drivers.InvalidACL{ACL: acl}, nil)
|
2015-04-22 21:19:53 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucketName]; ok == true {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
return iodine.New(drivers.BucketExists{Bucket: bucketName}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RUnlock()
|
2015-04-08 20:13:25 -04:00
|
|
|
|
2015-04-27 05:04:05 -04:00
|
|
|
if strings.TrimSpace(acl) == "" {
|
|
|
|
// default is private
|
|
|
|
acl = "private"
|
|
|
|
}
|
2015-04-08 20:13:25 -04:00
|
|
|
var newBucket = storedBucket{}
|
2015-04-30 02:36:30 -04:00
|
|
|
newBucket.objectMetadata = make(map[string]drivers.ObjectMetadata)
|
|
|
|
newBucket.bucketMetadata = drivers.BucketMetadata{}
|
|
|
|
newBucket.bucketMetadata.Name = bucketName
|
2015-04-30 22:26:23 -04:00
|
|
|
newBucket.bucketMetadata.Created = time.Now().UTC()
|
2015-04-30 02:36:30 -04:00
|
|
|
newBucket.bucketMetadata.ACL = drivers.BucketACL(acl)
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.Lock()
|
|
|
|
defer memory.lock.Unlock()
|
2015-04-30 02:36:30 -04:00
|
|
|
memory.storedBuckets[bucketName] = newBucket
|
2015-04-08 20:13:25 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delimiter(object, delimiter string) string {
|
|
|
|
readBuffer := bytes.NewBufferString(object)
|
|
|
|
reader := bufio.NewReader(readBuffer)
|
|
|
|
stringReader := strings.NewReader(delimiter)
|
|
|
|
delimited, _ := stringReader.ReadByte()
|
|
|
|
delimitedStr, _ := reader.ReadString(delimited)
|
|
|
|
return delimitedStr
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendUniq(slice []string, i string) []string {
|
|
|
|
for _, ele := range slice {
|
|
|
|
if ele == i {
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(slice, i)
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:18 -04:00
|
|
|
func (memory *memoryDriver) filterDelimiterPrefix(keys []string, key, delim string, r drivers.BucketResourcesMetadata) ([]string, drivers.BucketResourcesMetadata) {
|
2015-04-08 20:13:25 -04:00
|
|
|
switch true {
|
2015-05-05 17:27:18 -04:00
|
|
|
case key == r.Prefix:
|
2015-04-08 20:13:25 -04:00
|
|
|
keys = appendUniq(keys, key)
|
2015-05-05 17:27:18 -04:00
|
|
|
// delim - requires r.Prefix as it was trimmed off earlier
|
|
|
|
case key == r.Prefix+delim:
|
2015-04-08 20:13:25 -04:00
|
|
|
keys = appendUniq(keys, key)
|
2015-05-05 17:27:18 -04:00
|
|
|
case delim != "":
|
|
|
|
r.CommonPrefixes = appendUniq(r.CommonPrefixes, r.Prefix+delim)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-05 17:27:18 -04:00
|
|
|
return keys, r
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 17:27:18 -04:00
|
|
|
func (memory *memoryDriver) listObjects(keys []string, key string, r drivers.BucketResourcesMetadata) ([]string, drivers.BucketResourcesMetadata) {
|
2015-04-23 04:20:03 -04:00
|
|
|
switch true {
|
|
|
|
// Prefix absent, delimit object key based on delimiter
|
2015-05-05 17:27:18 -04:00
|
|
|
case r.IsDelimiterSet():
|
|
|
|
delim := delimiter(key, r.Delimiter)
|
2015-04-23 04:20:03 -04:00
|
|
|
switch true {
|
2015-05-05 17:27:18 -04:00
|
|
|
case delim == "" || delim == key:
|
2015-04-23 04:20:03 -04:00
|
|
|
keys = appendUniq(keys, key)
|
2015-05-05 17:27:18 -04:00
|
|
|
case delim != "":
|
|
|
|
r.CommonPrefixes = appendUniq(r.CommonPrefixes, delim)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
|
|
|
// Prefix present, delimit object key with prefix key based on delimiter
|
2015-05-05 17:27:18 -04:00
|
|
|
case r.IsDelimiterPrefixSet():
|
|
|
|
if strings.HasPrefix(key, r.Prefix) {
|
|
|
|
trimmedName := strings.TrimPrefix(key, r.Prefix)
|
|
|
|
delim := delimiter(trimmedName, r.Delimiter)
|
|
|
|
keys, r = memory.filterDelimiterPrefix(keys, key, delim, r)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
|
|
|
// Prefix present, nothing to delimit
|
2015-05-05 17:27:18 -04:00
|
|
|
case r.IsPrefixSet():
|
2015-04-23 04:20:03 -04:00
|
|
|
keys = appendUniq(keys, key)
|
|
|
|
// Prefix and delimiter absent
|
2015-05-05 17:27:18 -04:00
|
|
|
case r.IsDefault():
|
2015-04-23 04:20:03 -04:00
|
|
|
keys = appendUniq(keys, key)
|
|
|
|
}
|
2015-05-05 17:27:18 -04:00
|
|
|
return keys, r
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 20:13:25 -04:00
|
|
|
// ListObjects - list objects from memory
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) ListObjects(bucket string, resources drivers.BucketResourcesMetadata) ([]drivers.ObjectMetadata, drivers.BucketResourcesMetadata, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
defer memory.lock.RUnlock()
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
return nil, drivers.BucketResourcesMetadata{IsTruncated: false}, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-05-01 23:29:01 -04:00
|
|
|
if !drivers.IsValidObjectName(resources.Prefix) {
|
2015-04-23 04:20:03 -04:00
|
|
|
return nil, drivers.BucketResourcesMetadata{IsTruncated: false}, iodine.New(drivers.ObjectNameInvalid{Object: resources.Prefix}, nil)
|
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-04-23 04:20:03 -04:00
|
|
|
return nil, drivers.BucketResourcesMetadata{IsTruncated: false}, iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
var results []drivers.ObjectMetadata
|
|
|
|
var keys []string
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
for key := range storedBucket.objectMetadata {
|
2015-04-15 15:35:23 -04:00
|
|
|
if strings.HasPrefix(key, bucket+"/") {
|
|
|
|
key = key[len(bucket)+1:]
|
2015-05-05 17:27:18 -04:00
|
|
|
keys, resources = memory.listObjects(keys, key, resources)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 17:27:18 -04:00
|
|
|
// Marker logic - TODO in-efficient right now fix it
|
|
|
|
var newKeys []string
|
|
|
|
switch {
|
|
|
|
case resources.Marker != "":
|
|
|
|
for _, key := range keys {
|
|
|
|
if key > resources.Marker {
|
|
|
|
newKeys = appendUniq(newKeys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
newKeys = keys
|
|
|
|
}
|
|
|
|
sort.Strings(newKeys)
|
|
|
|
for _, key := range newKeys {
|
2015-04-08 20:13:25 -04:00
|
|
|
if len(results) == resources.Maxkeys {
|
2015-04-29 19:45:57 -04:00
|
|
|
resources.IsTruncated = true
|
2015-05-05 17:27:18 -04:00
|
|
|
if resources.IsTruncated && resources.IsDelimiterSet() {
|
|
|
|
resources.NextMarker = results[len(results)-1].Key
|
|
|
|
}
|
2015-04-29 19:45:57 -04:00
|
|
|
return results, resources, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
object := storedBucket.objectMetadata[bucket+"/"+key]
|
|
|
|
results = append(results, object)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
return results, resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByBucketName is a type for sorting bucket metadata by bucket name
|
|
|
|
type ByBucketName []drivers.BucketMetadata
|
|
|
|
|
|
|
|
// Len of bucket name
|
|
|
|
func (b ByBucketName) Len() int { return len(b) }
|
|
|
|
|
|
|
|
// Swap bucket i, j
|
|
|
|
func (b ByBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
|
|
|
|
// Less
|
|
|
|
func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
|
|
|
|
|
|
|
|
// ListBuckets - List buckets from memory
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) ListBuckets() ([]drivers.BucketMetadata, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
defer memory.lock.RUnlock()
|
2015-04-08 20:13:25 -04:00
|
|
|
var results []drivers.BucketMetadata
|
2015-04-30 02:36:30 -04:00
|
|
|
for _, bucket := range memory.storedBuckets {
|
|
|
|
results = append(results, bucket.bucketMetadata)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
sort.Sort(ByBucketName(results))
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectMetadata - get object metadata from memory
|
2015-04-22 20:50:26 -04:00
|
|
|
func (memory *memoryDriver) GetObjectMetadata(bucket, key, prefix string) (drivers.ObjectMetadata, error) {
|
2015-04-15 14:48:56 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
defer memory.lock.RUnlock()
|
2015-04-08 20:13:25 -04:00
|
|
|
// check if bucket exists
|
2015-04-23 04:20:03 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
return drivers.ObjectMetadata{}, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-05-01 23:29:01 -04:00
|
|
|
if !drivers.IsValidObjectName(key) || !drivers.IsValidObjectName(prefix) {
|
2015-04-23 04:20:03 -04:00
|
|
|
return drivers.ObjectMetadata{}, iodine.New(drivers.ObjectNameInvalid{Object: key}, nil)
|
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
2015-04-23 04:20:03 -04:00
|
|
|
return drivers.ObjectMetadata{}, iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-30 02:36:30 -04:00
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + key
|
2015-04-30 02:36:30 -04:00
|
|
|
if object, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
|
|
|
return object, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
return drivers.ObjectMetadata{}, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: key}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 15:35:23 -04:00
|
|
|
|
2015-05-08 15:00:02 -04:00
|
|
|
func (memory *memoryDriver) expiredObject(a ...interface{}) {
|
2015-05-04 02:16:45 -04:00
|
|
|
cacheStats := memory.objects.Stats()
|
2015-05-08 15:00:02 -04:00
|
|
|
log.Printf("CurrentSize: %d, CurrentItems: %d, TotalExpirations: %d",
|
|
|
|
cacheStats.Bytes, cacheStats.Items, cacheStats.Expired)
|
2015-05-04 02:16:45 -04:00
|
|
|
key := a[0].(string)
|
2015-04-30 02:36:30 -04:00
|
|
|
// loop through all buckets
|
2015-05-08 05:23:34 -04:00
|
|
|
for bucket, storedBucket := range memory.storedBuckets {
|
2015-05-04 02:16:45 -04:00
|
|
|
delete(storedBucket.objectMetadata, key)
|
2015-04-30 02:36:30 -04:00
|
|
|
// remove bucket if no objects found anymore
|
|
|
|
if len(storedBucket.objectMetadata) == 0 {
|
2015-05-08 05:23:34 -04:00
|
|
|
delete(memory.storedBuckets, bucket)
|
2015-04-30 02:36:30 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
debug.FreeOSMemory()
|
2015-04-15 15:35:23 -04:00
|
|
|
}
|
2015-05-07 22:55:30 -04:00
|
|
|
|
|
|
|
func (memory *memoryDriver) NewMultipartUpload(bucket, key, contentType string) (string, error) {
|
2015-05-08 05:02:51 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
if !drivers.IsValidObjectName(key) {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.ObjectNameInvalid{Object: key}, nil)
|
|
|
|
}
|
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
objectKey := bucket + "/" + key
|
|
|
|
if _, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.ObjectExists{Bucket: bucket, Object: key}, nil)
|
|
|
|
}
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
|
2015-05-09 14:41:26 -04:00
|
|
|
memory.lock.Lock()
|
2015-05-07 22:55:30 -04:00
|
|
|
id := []byte(strconv.FormatInt(rand.Int63(), 10) + bucket + key + time.Now().String())
|
|
|
|
uploadIDSum := sha512.Sum512(id)
|
2015-05-09 21:28:19 -04:00
|
|
|
uploadID := base64.URLEncoding.EncodeToString(uploadIDSum[:])[:47]
|
2015-05-09 14:41:26 -04:00
|
|
|
|
|
|
|
storedBucket.multiPartSession = make(map[string]multiPartSession)
|
|
|
|
storedBucket.multiPartSession[key] = multiPartSession{
|
|
|
|
uploadID: uploadID,
|
|
|
|
totalParts: 0,
|
|
|
|
}
|
|
|
|
memory.storedBuckets[bucket] = storedBucket
|
|
|
|
memory.lock.Unlock()
|
|
|
|
|
|
|
|
return uploadID, nil
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 19:06:35 -04:00
|
|
|
func (memory *memoryDriver) AbortMultipartUpload(bucket, key, uploadID string) error {
|
|
|
|
memory.lock.RLock()
|
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return iodine.New(drivers.InvalidUploadID{UploadID: uploadID}, nil)
|
|
|
|
}
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
|
|
|
|
memory.cleanupMultiparts(bucket, key, uploadID)
|
|
|
|
memory.cleanupMultipartSession(bucket, key, uploadID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-07 22:55:30 -04:00
|
|
|
func getMultipartKey(key string, uploadID string, partNumber int) string {
|
|
|
|
return key + "?uploadId=" + uploadID + "&partNumber=" + strconv.Itoa(partNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (memory *memoryDriver) CreateObjectPart(bucket, key, uploadID string, partID int, contentType, expectedMD5Sum string, size int64, data io.Reader) (string, error) {
|
2015-05-08 05:02:51 -04:00
|
|
|
// Verify upload id
|
2015-05-09 14:41:26 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
|
|
|
memory.lock.RUnlock()
|
2015-05-08 05:02:51 -04:00
|
|
|
return "", iodine.New(drivers.InvalidUploadID{UploadID: uploadID}, nil)
|
|
|
|
}
|
2015-05-09 14:41:26 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
|
|
|
|
etag, err := memory.CreateObject(bucket, getMultipartKey(key, uploadID, partID), "", expectedMD5Sum, size, data)
|
|
|
|
if err != nil {
|
|
|
|
return "", iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
// once successful, update totalParts
|
|
|
|
multiPartSession := storedBucket.multiPartSession[key]
|
|
|
|
multiPartSession.totalParts++
|
|
|
|
storedBucket.multiPartSession[key] = multiPartSession
|
|
|
|
return etag, nil
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
|
|
|
|
2015-05-08 06:31:43 -04:00
|
|
|
func (memory *memoryDriver) cleanupMultipartSession(bucket, key, uploadID string) {
|
|
|
|
memory.lock.Lock()
|
|
|
|
defer memory.lock.Unlock()
|
2015-05-09 14:41:26 -04:00
|
|
|
delete(memory.storedBuckets[bucket].multiPartSession, key)
|
2015-05-08 06:31:43 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 19:06:35 -04:00
|
|
|
func (memory *memoryDriver) cleanupMultiparts(bucket, key, uploadID string) {
|
2015-05-08 06:31:43 -04:00
|
|
|
memory.lock.Lock()
|
|
|
|
defer memory.lock.Unlock()
|
2015-05-09 19:06:35 -04:00
|
|
|
for i := 1; i <= memory.storedBuckets[bucket].multiPartSession[key].totalParts; i++ {
|
2015-05-08 06:31:43 -04:00
|
|
|
objectKey := bucket + "/" + getMultipartKey(key, uploadID, i)
|
|
|
|
memory.objects.Delete(objectKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 01:43:19 -04:00
|
|
|
func (memory *memoryDriver) CompleteMultipartUpload(bucket, key, uploadID string, parts map[int]string) (string, error) {
|
2015-05-08 05:02:51 -04:00
|
|
|
if !drivers.IsValidBucket(bucket) {
|
|
|
|
return "", iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
if !drivers.IsValidObjectName(key) {
|
|
|
|
return "", iodine.New(drivers.ObjectNameInvalid{Object: key}, nil)
|
|
|
|
}
|
2015-05-09 14:41:26 -04:00
|
|
|
// Verify upload id
|
2015-05-08 05:02:51 -04:00
|
|
|
memory.lock.RLock()
|
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-05-09 14:41:26 -04:00
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
|
|
|
memory.lock.RUnlock()
|
|
|
|
return "", iodine.New(drivers.InvalidUploadID{UploadID: uploadID}, nil)
|
|
|
|
}
|
2015-05-08 05:02:51 -04:00
|
|
|
memory.lock.RUnlock()
|
|
|
|
|
2015-05-07 22:55:30 -04:00
|
|
|
memory.lock.Lock()
|
2015-05-08 05:02:51 -04:00
|
|
|
var size int64
|
2015-05-08 06:31:43 -04:00
|
|
|
for i := range parts {
|
2015-05-09 14:41:26 -04:00
|
|
|
object, ok := storedBucket.objectMetadata[bucket+"/"+getMultipartKey(key, uploadID, i)]
|
2015-05-08 06:31:43 -04:00
|
|
|
if !ok {
|
2015-05-07 22:55:30 -04:00
|
|
|
memory.lock.Unlock()
|
2015-05-08 01:43:19 -04:00
|
|
|
return "", iodine.New(errors.New("missing part: "+strconv.Itoa(i)), nil)
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
2015-05-08 06:31:43 -04:00
|
|
|
size += object.Size
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var fullObject bytes.Buffer
|
|
|
|
for i := 1; i <= len(parts); i++ {
|
2015-05-08 06:31:43 -04:00
|
|
|
recvMD5 := parts[i]
|
|
|
|
object, ok := memory.objects.Get(bucket + "/" + getMultipartKey(key, uploadID, i))
|
|
|
|
if ok == false {
|
2015-05-07 22:55:30 -04:00
|
|
|
memory.lock.Unlock()
|
2015-05-08 01:43:19 -04:00
|
|
|
return "", iodine.New(errors.New("missing part: "+strconv.Itoa(i)), nil)
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
2015-05-08 06:31:43 -04:00
|
|
|
obj := object.([]byte)
|
|
|
|
calcMD5Bytes := md5.Sum(obj)
|
|
|
|
// complete multi part request header md5sum per part is hex encoded
|
|
|
|
recvMD5Bytes, err := hex.DecodeString(strings.Trim(recvMD5, "\""))
|
|
|
|
if err != nil {
|
|
|
|
return "", iodine.New(drivers.InvalidDigest{Md5: recvMD5}, nil)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(recvMD5Bytes, calcMD5Bytes[:]) {
|
|
|
|
return "", iodine.New(drivers.BadDigest{Md5: recvMD5, Bucket: bucket, Key: getMultipartKey(key, uploadID, i)}, nil)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(&fullObject, bytes.NewBuffer(obj))
|
|
|
|
if err != nil {
|
|
|
|
return "", iodine.New(err, nil)
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
memory.lock.Unlock()
|
|
|
|
|
|
|
|
md5sumSlice := md5.Sum(fullObject.Bytes())
|
|
|
|
md5sum := base64.StdEncoding.EncodeToString(md5sumSlice[:])
|
2015-05-08 06:31:43 -04:00
|
|
|
etag, err := memory.CreateObject(bucket, key, "", md5sum, size, &fullObject)
|
|
|
|
if err != nil {
|
2015-05-09 19:06:35 -04:00
|
|
|
// No need to call internal cleanup functions here, caller will call AbortMultipartUpload()
|
|
|
|
// which would in-turn cleanup properly in accordance with S3 Spec
|
2015-05-08 06:31:43 -04:00
|
|
|
return "", iodine.New(err, nil)
|
|
|
|
}
|
2015-05-09 19:06:35 -04:00
|
|
|
memory.cleanupMultiparts(bucket, key, uploadID)
|
2015-05-08 06:31:43 -04:00
|
|
|
memory.cleanupMultipartSession(bucket, key, uploadID)
|
|
|
|
return etag, nil
|
2015-05-07 22:55:30 -04:00
|
|
|
}
|
2015-05-09 14:41:26 -04:00
|
|
|
|
|
|
|
// partNumber is a sortable interface for Part slice
|
|
|
|
type partNumber []*drivers.PartMetadata
|
|
|
|
|
|
|
|
func (a partNumber) Len() int { return len(a) }
|
|
|
|
func (a partNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a partNumber) Less(i, j int) bool { return a[i].PartNumber < a[j].PartNumber }
|
|
|
|
|
|
|
|
func (memory *memoryDriver) ListObjectParts(bucket, key, uploadID string) (drivers.ObjectResourcesMetadata, error) {
|
|
|
|
// Verify upload id
|
|
|
|
memory.lock.RLock()
|
|
|
|
defer memory.lock.RUnlock()
|
|
|
|
if _, ok := memory.storedBuckets[bucket]; ok == false {
|
|
|
|
return drivers.ObjectResourcesMetadata{}, iodine.New(drivers.BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
storedBucket := memory.storedBuckets[bucket]
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
|
|
|
return drivers.ObjectResourcesMetadata{}, iodine.New(drivers.InvalidUploadID{UploadID: uploadID}, nil)
|
|
|
|
}
|
|
|
|
// TODO support PartNumberMarker and NextPartNumberMarker
|
|
|
|
objectResourcesMetadata := drivers.ObjectResourcesMetadata{}
|
|
|
|
objectResourcesMetadata.UploadID = uploadID
|
|
|
|
objectResourcesMetadata.Bucket = bucket
|
|
|
|
objectResourcesMetadata.Key = key
|
|
|
|
objectResourcesMetadata.MaxParts = 1000
|
|
|
|
var parts []*drivers.PartMetadata
|
|
|
|
for i := 1; i <= storedBucket.multiPartSession[key].totalParts; i++ {
|
|
|
|
if len(parts) > objectResourcesMetadata.MaxParts {
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.IsTruncated = true
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
|
|
|
object, ok := storedBucket.objectMetadata[bucket+"/"+getMultipartKey(key, uploadID, i)]
|
|
|
|
if !ok {
|
|
|
|
return drivers.ObjectResourcesMetadata{}, iodine.New(errors.New("missing part: "+strconv.Itoa(i)), nil)
|
|
|
|
}
|
|
|
|
partMetadata := &drivers.PartMetadata{}
|
|
|
|
partMetadata.Size = object.Size
|
|
|
|
partMetadata.LastModified = object.Created
|
|
|
|
partMetadata.ETag = object.Md5
|
|
|
|
partMetadata.PartNumber = i
|
|
|
|
parts = append(parts, partMetadata)
|
|
|
|
}
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|