2015-05-14 22:31:36 -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-06-30 19:30:02 -04:00
|
|
|
package donut
|
2015-05-14 22:31:36 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"runtime/debug"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/iodine"
|
|
|
|
)
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// NewMultipartUpload - initiate a new multipart session
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) NewMultipartUpload(bucket, key, contentType string) (string, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return "", iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(key) {
|
|
|
|
return "", iodine.New(ObjectNameInvalid{Object: key}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return "", iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
objectKey := bucket + "/" + key
|
|
|
|
if _, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
2015-06-30 20:08:18 -04:00
|
|
|
return "", iodine.New(ObjectExists{Object: key}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
id := []byte(strconv.FormatInt(rand.Int63(), 10) + bucket + key + time.Now().String())
|
|
|
|
uploadIDSum := sha512.Sum512(id)
|
|
|
|
uploadID := base64.URLEncoding.EncodeToString(uploadIDSum[:])[:47]
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket.multiPartSession[key] = MultiPartSession{
|
2015-05-14 22:31:36 -04:00
|
|
|
uploadID: uploadID,
|
|
|
|
initiated: time.Now(),
|
|
|
|
totalParts: 0,
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
return uploadID, nil
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// AbortMultipartUpload - abort an incomplete multipart session
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) AbortMultipartUpload(bucket, key, uploadID string) error {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
|
|
|
return iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
2015-06-30 19:30:02 -04:00
|
|
|
return iodine.New(InvalidUploadID{UploadID: uploadID}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
donut.cleanupMultipartSession(bucket, key, uploadID)
|
2015-05-14 22:31:36 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// CreateObjectPart - create a part in a multipart session
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) CreateObjectPart(bucket, key, uploadID string, partID int, contentType, expectedMD5Sum string, size int64, data io.Reader) (string, error) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-06 01:17:56 -04:00
|
|
|
defer donut.lock.Unlock()
|
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
etag, err := donut.createObjectPart(bucket, key, uploadID, partID, "", expectedMD5Sum, size, data)
|
2015-07-04 00:02:31 -04:00
|
|
|
// possible free
|
2015-05-14 22:31:36 -04:00
|
|
|
debug.FreeOSMemory()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
return etag, iodine.New(err, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// createObject - internal wrapper function called by CreateObjectPart
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) createObjectPart(bucket, key, uploadID string, partID int, contentType, expectedMD5Sum string, size int64, data io.Reader) (string, error) {
|
2015-07-06 01:17:56 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return "", iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(key) {
|
|
|
|
return "", iodine.New(ObjectNameInvalid{Object: key}, nil)
|
|
|
|
}
|
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
|
|
|
return "", iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
|
|
|
// Verify upload id
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
|
|
|
return "", iodine.New(InvalidUploadID{UploadID: uploadID}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
|
2015-05-14 22:31:36 -04:00
|
|
|
// get object key
|
2015-07-05 21:44:55 -04:00
|
|
|
if _, ok := storedBucket.partMetadata[partID]; ok {
|
|
|
|
return storedBucket.partMetadata[partID].ETag, nil
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum))
|
|
|
|
if err != nil {
|
|
|
|
// pro-actively close the connection
|
2015-06-30 19:30:02 -04:00
|
|
|
return "", iodine.New(InvalidDigest{Md5: expectedMD5Sum}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate md5
|
|
|
|
hash := md5.New()
|
|
|
|
var readBytes []byte
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var length int
|
|
|
|
for err == nil {
|
|
|
|
byteBuffer := make([]byte, 1024*1024)
|
|
|
|
length, err = data.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])
|
|
|
|
readBytes = append(readBytes, byteBuffer[0:length]...)
|
|
|
|
}
|
|
|
|
if err != io.EOF {
|
|
|
|
return "", iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
go debug.FreeOSMemory()
|
|
|
|
md5SumBytes := hash.Sum(nil)
|
|
|
|
totalLength := int64(len(readBytes))
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.multiPartObjects.Set(partID, readBytes)
|
2015-05-14 22:31:36 -04:00
|
|
|
// setting up for de-allocation
|
|
|
|
readBytes = nil
|
|
|
|
|
|
|
|
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-06-30 20:08:18 -04:00
|
|
|
return "", iodine.New(BadDigest{}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
newPart := PartMetadata{
|
2015-05-14 22:31:36 -04:00
|
|
|
PartNumber: partID,
|
|
|
|
LastModified: time.Now().UTC(),
|
|
|
|
ETag: md5Sum,
|
|
|
|
Size: totalLength,
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
storedBucket.partMetadata[partID] = newPart
|
2015-05-18 14:53:28 -04:00
|
|
|
multiPartSession := storedBucket.multiPartSession[key]
|
|
|
|
multiPartSession.totalParts++
|
|
|
|
storedBucket.multiPartSession[key] = multiPartSession
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
return md5Sum, nil
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// cleanupMultipartSession invoked during an abort or complete multipart session to cleanup session from memory
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) cleanupMultipartSession(bucket, key, uploadID string) {
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
|
|
|
for i := 1; i <= storedBucket.multiPartSession[key].totalParts; i++ {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.multiPartObjects.Delete(i)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
delete(storedBucket.multiPartSession, key)
|
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// CompleteMultipartUpload - complete a multipart upload and persist the data
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) CompleteMultipartUpload(bucket, key, uploadID string, parts map[int]string) (ObjectMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-06 01:17:56 -04:00
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(key) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(ObjectNameInvalid{Object: key}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-07-05 21:44:55 -04:00
|
|
|
// Verify upload id
|
2015-05-14 22:31:36 -04:00
|
|
|
if storedBucket.multiPartSession[key].uploadID != uploadID {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(InvalidUploadID{UploadID: uploadID}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
var size int64
|
|
|
|
var fullObject bytes.Buffer
|
|
|
|
for i := 1; i <= len(parts); i++ {
|
|
|
|
recvMD5 := parts[i]
|
2015-07-05 21:44:55 -04:00
|
|
|
object, ok := donut.multiPartObjects.Get(i)
|
2015-05-14 22:31:36 -04:00
|
|
|
if ok == false {
|
2015-07-02 23:31:22 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(errors.New("missing part: "+strconv.Itoa(i)), nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-11 17:22:04 -04:00
|
|
|
size += int64(len(object))
|
|
|
|
calcMD5Bytes := md5.Sum(object)
|
2015-05-14 22:31:36 -04:00
|
|
|
// complete multi part request header md5sum per part is hex encoded
|
|
|
|
recvMD5Bytes, err := hex.DecodeString(strings.Trim(recvMD5, "\""))
|
|
|
|
if err != nil {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(InvalidDigest{Md5: recvMD5}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
if !bytes.Equal(recvMD5Bytes, calcMD5Bytes[:]) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BadDigest{}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-11 17:22:04 -04:00
|
|
|
_, err = io.Copy(&fullObject, bytes.NewBuffer(object))
|
2015-05-14 22:31:36 -04:00
|
|
|
if err != nil {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-11 17:22:04 -04:00
|
|
|
object = nil
|
|
|
|
go debug.FreeOSMemory()
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
md5sumSlice := md5.Sum(fullObject.Bytes())
|
2015-05-31 04:17:28 -04:00
|
|
|
// this is needed for final verification inside CreateObject, do not convert this to hex
|
2015-05-14 22:31:36 -04:00
|
|
|
md5sum := base64.StdEncoding.EncodeToString(md5sumSlice[:])
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-02 23:31:22 -04:00
|
|
|
objectMetadata, err := donut.CreateObject(bucket, key, md5sum, size, &fullObject, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
if err != nil {
|
|
|
|
// No need to call internal cleanup functions here, caller will call AbortMultipartUpload()
|
|
|
|
// which would in-turn cleanup properly in accordance with S3 Spec
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-06-11 17:22:04 -04:00
|
|
|
fullObject.Reset()
|
2015-07-04 00:02:31 -04:00
|
|
|
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-02 23:31:22 -04:00
|
|
|
donut.cleanupMultipartSession(bucket, key, uploadID)
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Unlock()
|
2015-07-01 21:09:44 -04:00
|
|
|
return objectMetadata, nil
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// byKey is a sortable interface for UploadMetadata slice
|
2015-06-30 19:30:02 -04:00
|
|
|
type byKey []*UploadMetadata
|
2015-05-14 22:31:36 -04:00
|
|
|
|
|
|
|
func (a byKey) Len() int { return len(a) }
|
|
|
|
func (a byKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byKey) Less(i, j int) bool { return a[i].Key < a[j].Key }
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// ListMultipartUploads - list incomplete multipart sessions for a given bucket
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, error) {
|
2015-05-18 14:53:28 -04:00
|
|
|
// TODO handle delimiter
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-06-30 19:30:02 -04:00
|
|
|
var uploads []*UploadMetadata
|
2015-05-14 22:31:36 -04:00
|
|
|
|
|
|
|
for key, session := range storedBucket.multiPartSession {
|
2015-05-18 14:53:28 -04:00
|
|
|
if strings.HasPrefix(key, resources.Prefix) {
|
|
|
|
if len(uploads) > resources.MaxUploads {
|
|
|
|
sort.Sort(byKey(uploads))
|
|
|
|
resources.Upload = uploads
|
|
|
|
resources.NextKeyMarker = key
|
|
|
|
resources.NextUploadIDMarker = session.uploadID
|
|
|
|
resources.IsTruncated = true
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
// uploadIDMarker is ignored if KeyMarker is empty
|
|
|
|
switch {
|
|
|
|
case resources.KeyMarker != "" && resources.UploadIDMarker == "":
|
|
|
|
if key > resources.KeyMarker {
|
2015-06-30 19:30:02 -04:00
|
|
|
upload := new(UploadMetadata)
|
2015-05-18 14:53:28 -04:00
|
|
|
upload.Key = key
|
|
|
|
upload.UploadID = session.uploadID
|
|
|
|
upload.Initiated = session.initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
case resources.KeyMarker != "" && resources.UploadIDMarker != "":
|
|
|
|
if session.uploadID > resources.UploadIDMarker {
|
|
|
|
if key >= resources.KeyMarker {
|
2015-06-30 19:30:02 -04:00
|
|
|
upload := new(UploadMetadata)
|
2015-05-18 14:53:28 -04:00
|
|
|
upload.Key = key
|
|
|
|
upload.UploadID = session.uploadID
|
|
|
|
upload.Initiated = session.initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2015-06-30 19:30:02 -04:00
|
|
|
upload := new(UploadMetadata)
|
2015-05-18 14:53:28 -04:00
|
|
|
upload.Key = key
|
|
|
|
upload.UploadID = session.uploadID
|
|
|
|
upload.Initiated = session.initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(byKey(uploads))
|
|
|
|
resources.Upload = uploads
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// partNumber is a sortable interface for Part slice
|
2015-06-30 19:30:02 -04:00
|
|
|
type partNumber []*PartMetadata
|
2015-05-14 22:31:36 -04:00
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// ListObjectParts - list parts from incomplete multipart session for a given object
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) ListObjectParts(bucket, key string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, error) {
|
2015-05-14 22:31:36 -04:00
|
|
|
// Verify upload id
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
// update Config if possible
|
|
|
|
donut.updateConfig()
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return ObjectResourcesMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-05-14 22:31:36 -04:00
|
|
|
if _, ok := storedBucket.multiPartSession[key]; ok == false {
|
2015-06-30 20:08:18 -04:00
|
|
|
return ObjectResourcesMetadata{}, iodine.New(ObjectNotFound{Object: key}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
if storedBucket.multiPartSession[key].uploadID != resources.UploadID {
|
2015-06-30 19:30:02 -04:00
|
|
|
return ObjectResourcesMetadata{}, iodine.New(InvalidUploadID{UploadID: resources.UploadID}, nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
objectResourcesMetadata := resources
|
|
|
|
objectResourcesMetadata.Bucket = bucket
|
|
|
|
objectResourcesMetadata.Key = key
|
2015-06-30 19:30:02 -04:00
|
|
|
var parts []*PartMetadata
|
2015-05-14 22:31:36 -04:00
|
|
|
var startPartNumber int
|
|
|
|
switch {
|
|
|
|
case objectResourcesMetadata.PartNumberMarker == 0:
|
|
|
|
startPartNumber = 1
|
|
|
|
default:
|
|
|
|
startPartNumber = objectResourcesMetadata.PartNumberMarker
|
|
|
|
}
|
|
|
|
for i := startPartNumber; i <= storedBucket.multiPartSession[key].totalParts; i++ {
|
|
|
|
if len(parts) > objectResourcesMetadata.MaxParts {
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.IsTruncated = true
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
objectResourcesMetadata.NextPartNumberMarker = i
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
2015-07-05 21:44:55 -04:00
|
|
|
part, ok := storedBucket.partMetadata[i]
|
2015-05-14 22:31:36 -04:00
|
|
|
if !ok {
|
2015-06-30 19:30:02 -04:00
|
|
|
return ObjectResourcesMetadata{}, iodine.New(errors.New("missing part: "+strconv.Itoa(i)), nil)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
parts = append(parts, &part)
|
|
|
|
}
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// evictedPart - call back function called by caching module during individual cache evictions
|
2015-07-04 20:08:23 -04:00
|
|
|
func (donut API) evictedPart(a ...interface{}) {
|
2015-07-05 21:44:55 -04:00
|
|
|
key := a[0].(int)
|
2015-05-14 22:31:36 -04:00
|
|
|
// loop through all buckets
|
2015-07-04 00:02:31 -04:00
|
|
|
buckets := donut.storedBuckets.GetAll()
|
|
|
|
for bucketName, bucket := range buckets {
|
|
|
|
b := bucket.(storedBucket)
|
|
|
|
delete(b.partMetadata, key)
|
|
|
|
donut.storedBuckets.Set(bucketName, b)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
debug.FreeOSMemory()
|
|
|
|
}
|