2015-05-14 22:31:36 -04:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-05-14 22:31:36 -04:00
|
|
|
*
|
|
|
|
* 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"
|
2015-07-09 22:01:15 -04:00
|
|
|
"encoding/xml"
|
2015-05-14 22:31:36 -04:00
|
|
|
"io"
|
2015-07-09 22:01:15 -04:00
|
|
|
"io/ioutil"
|
2015-05-14 22:31:36 -04:00
|
|
|
"math/rand"
|
|
|
|
"runtime/debug"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-07-09 22:01:15 -04:00
|
|
|
"github.com/minio/minio/pkg/crypto/sha256"
|
2015-07-08 23:18:32 -04:00
|
|
|
"github.com/minio/minio/pkg/donut/cache/data"
|
2015-08-03 19:17:21 -04:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-05-14 22:31:36 -04:00
|
|
|
)
|
|
|
|
|
2015-07-10 17:04:37 -04:00
|
|
|
/// V2 API functions
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// NewMultipartUpload - initiate a new multipart session
|
2015-08-03 19:17:21 -04:00
|
|
|
func (donut API) NewMultipartUpload(bucket, key, contentType string, signature *Signature) (string, *probe.Error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-17 12:31:28 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(key) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(ObjectNameInvalid{Object: key})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
2015-07-09 22:34:07 -04:00
|
|
|
if signature != nil {
|
|
|
|
ok, err := signature.DoesSignatureMatch("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return "", err.Trace()
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(SignatureDoesNotMatch{})
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
return donut.newMultipartUpload(bucket, key, contentType)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
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-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(ObjectExists{Object: key})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
id := []byte(strconv.Itoa(rand.Int()) + bucket + key + time.Now().UTC().String())
|
2015-05-14 22:31:36 -04:00
|
|
|
uploadIDSum := sha512.Sum512(id)
|
|
|
|
uploadID := base64.URLEncoding.EncodeToString(uploadIDSum[:])[:47]
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket.multiPartSession[key] = MultiPartSession{
|
2015-07-17 12:31:28 -04:00
|
|
|
UploadID: uploadID,
|
|
|
|
Initiated: time.Now().UTC(),
|
|
|
|
TotalParts: 0,
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-14 00:34:22 -04:00
|
|
|
storedBucket.partMetadata[key] = make(map[int]PartMetadata)
|
2015-07-08 23:18:32 -04:00
|
|
|
multiPartCache := data.NewCache(0)
|
|
|
|
multiPartCache.OnEvicted = donut.evictedPart
|
|
|
|
donut.multiPartObjects[uploadID] = multiPartCache
|
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-08-03 19:17:21 -04:00
|
|
|
func (donut API) AbortMultipartUpload(bucket, key, uploadID string, signature *Signature) *probe.Error {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-17 12:31:28 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(key) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return probe.NewError(ObjectNameInvalid{Object: key})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
2015-07-09 22:34:07 -04:00
|
|
|
if signature != nil {
|
|
|
|
ok, err := signature.DoesSignatureMatch("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return err.Trace()
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return probe.NewError(SignatureDoesNotMatch{})
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
return donut.abortMultipartUpload(bucket, key, uploadID)
|
2015-07-04 00:02:31 -04:00
|
|
|
}
|
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
2015-07-04 00:02:31 -04:00
|
|
|
}
|
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-07-17 12:31:28 -04:00
|
|
|
if storedBucket.multiPartSession[key].UploadID != uploadID {
|
2015-08-08 02:47:22 -04:00
|
|
|
return probe.NewError(InvalidUploadID{UploadID: uploadID})
|
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-08-03 19:17:21 -04:00
|
|
|
func (donut API) CreateObjectPart(bucket, key, uploadID string, partID int, contentType, expectedMD5Sum string, size int64, data io.Reader, signature *Signature) (string, *probe.Error) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-09 22:01:15 -04:00
|
|
|
etag, err := donut.createObjectPart(bucket, key, uploadID, partID, "", expectedMD5Sum, size, data, signature)
|
2015-07-14 00:34:22 -04:00
|
|
|
donut.lock.Unlock()
|
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-08-03 19:17:21 -04:00
|
|
|
return etag, err.Trace()
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// createObject - internal wrapper function called by CreateObjectPart
|
2015-08-03 19:17:21 -04:00
|
|
|
func (donut API) createObjectPart(bucket, key, uploadID string, partID int, contentType, expectedMD5Sum string, size int64, data io.Reader, signature *Signature) (string, *probe.Error) {
|
2015-07-06 01:17:56 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-07-06 01:17:56 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(key) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(ObjectNameInvalid{Object: key})
|
2015-07-06 01:17:56 -04:00
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
|
|
|
metadata["contentType"] = contentType
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum))
|
|
|
|
if err != nil {
|
|
|
|
// pro-actively close the connection
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(InvalidDigest{Md5: expectedMD5Sum})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
2015-07-25 16:47:19 -04:00
|
|
|
partMetadata, err := donut.putObjectPart(bucket, key, expectedMD5Sum, uploadID, partID, data, size, metadata, signature)
|
2015-07-17 12:31:28 -04:00
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return "", err.Trace()
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
return partMetadata.ETag, nil
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:17:56 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
2015-07-06 01:17:56 -04:00
|
|
|
}
|
2015-07-14 00:34:22 -04:00
|
|
|
strBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-07-04 00:02:31 -04:00
|
|
|
// Verify upload id
|
2015-07-17 12:31:28 -04:00
|
|
|
if strBucket.multiPartSession[key].UploadID != uploadID {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
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-14 00:34:22 -04:00
|
|
|
parts := strBucket.partMetadata[key]
|
|
|
|
if _, ok := parts[partID]; ok {
|
|
|
|
return parts[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-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(InvalidDigest{Md5: expectedMD5Sum})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate md5
|
|
|
|
hash := md5.New()
|
2015-07-09 22:01:15 -04:00
|
|
|
sha256hash := sha256.New()
|
2015-05-14 22:31:36 -04:00
|
|
|
|
2015-07-08 23:18:32 -04:00
|
|
|
var totalLength int64
|
2015-08-03 19:17:21 -04:00
|
|
|
var err error
|
2015-05-14 22:31:36 -04:00
|
|
|
for err == nil {
|
2015-07-08 23:18:32 -04:00
|
|
|
var length int
|
2015-05-14 22:31:36 -04:00
|
|
|
byteBuffer := make([]byte, 1024*1024)
|
2015-08-03 19:17:21 -04:00
|
|
|
length, err = data.Read(byteBuffer) // do not read error return error here, we will handle this error later
|
2015-05-14 22:31:36 -04:00
|
|
|
hash.Write(byteBuffer[0:length])
|
2015-07-09 22:01:15 -04:00
|
|
|
sha256hash.Write(byteBuffer[0:length])
|
2015-07-08 23:18:32 -04:00
|
|
|
ok := donut.multiPartObjects[uploadID].Append(partID, byteBuffer[0:length])
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(InternalError{})
|
2015-07-08 23:18:32 -04:00
|
|
|
}
|
|
|
|
totalLength += int64(length)
|
|
|
|
go debug.FreeOSMemory()
|
|
|
|
}
|
|
|
|
if totalLength != size {
|
|
|
|
donut.multiPartObjects[uploadID].Delete(partID)
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(IncompleteBody{Bucket: bucket, Object: key})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
if err != io.EOF {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(err)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 23:18:32 -04:00
|
|
|
md5SumBytes := hash.Sum(nil)
|
2015-05-14 22:31:36 -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-08-03 19:17:21 -04:00
|
|
|
return "", err.Trace()
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-09 22:01:15 -04:00
|
|
|
|
|
|
|
if signature != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
{
|
|
|
|
ok, err := signature.DoesSignatureMatch(hex.EncodeToString(sha256hash.Sum(nil)))
|
|
|
|
if err != nil {
|
|
|
|
return "", err.Trace()
|
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return "", probe.NewError(SignatureDoesNotMatch{})
|
2015-08-03 19:17:21 -04:00
|
|
|
}
|
2015-07-09 22:01:15 -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-14 00:34:22 -04:00
|
|
|
parts[partID] = newPart
|
|
|
|
strBucket.partMetadata[key] = parts
|
|
|
|
multiPartSession := strBucket.multiPartSession[key]
|
2015-07-17 12:31:28 -04:00
|
|
|
multiPartSession.TotalParts++
|
2015-07-14 00:34:22 -04:00
|
|
|
strBucket.multiPartSession[key] = multiPartSession
|
|
|
|
donut.storedBuckets.Set(bucket, strBucket)
|
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)
|
2015-07-17 12:31:28 -04:00
|
|
|
for i := 1; i <= storedBucket.multiPartSession[key].TotalParts; i++ {
|
2015-07-08 23:18:32 -04:00
|
|
|
donut.multiPartObjects[uploadID].Delete(i)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
delete(storedBucket.multiPartSession, key)
|
2015-07-14 00:34:22 -04:00
|
|
|
delete(storedBucket.partMetadata, key)
|
2015-07-04 00:02:31 -04:00
|
|
|
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-08-03 19:17:21 -04:00
|
|
|
func (donut API) CompleteMultipartUpload(bucket, key, uploadID string, data io.Reader, signature *Signature) (ObjectMetadata, *probe.Error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
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-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectNameInvalid{Object: key})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
donut.lock.Unlock()
|
|
|
|
return donut.completeMultipartUpload(bucket, key, uploadID, data, signature)
|
|
|
|
}
|
|
|
|
|
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-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
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-07-17 12:31:28 -04:00
|
|
|
if storedBucket.multiPartSession[key].UploadID != uploadID {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-09 22:01:15 -04:00
|
|
|
partBytes, err := ioutil.ReadAll(data)
|
|
|
|
if err != nil {
|
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(err)
|
2015-07-09 22:01:15 -04:00
|
|
|
}
|
|
|
|
if signature != nil {
|
|
|
|
ok, err := signature.DoesSignatureMatch(hex.EncodeToString(sha256.Sum256(partBytes)[:]))
|
|
|
|
if err != nil {
|
|
|
|
donut.lock.Unlock()
|
2015-08-03 19:17:21 -04:00
|
|
|
return ObjectMetadata{}, err.Trace()
|
2015-07-09 22:01:15 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(SignatureDoesNotMatch{})
|
2015-07-09 22:01:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
parts := &CompleteMultipartUpload{}
|
|
|
|
if err := xml.Unmarshal(partBytes, parts); err != nil {
|
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(MalformedXML{})
|
2015-07-09 22:01:15 -04:00
|
|
|
}
|
|
|
|
if !sort.IsSorted(completedParts(parts.Part)) {
|
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidPartOrder{})
|
2015-07-09 22:01:15 -04:00
|
|
|
}
|
2015-07-14 00:34:22 -04:00
|
|
|
|
2015-05-14 22:31:36 -04:00
|
|
|
var size int64
|
|
|
|
var fullObject bytes.Buffer
|
2015-07-13 17:20:26 -04:00
|
|
|
for i := 0; i < len(parts.Part); i++ {
|
|
|
|
recvMD5 := parts.Part[i].ETag
|
|
|
|
object, ok := donut.multiPartObjects[uploadID].Get(parts.Part[i].PartNumber)
|
2015-05-14 22:31:36 -04:00
|
|
|
if ok == false {
|
2015-07-02 23:31:22 -04:00
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidPart{})
|
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-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidDigest{Md5: recvMD5})
|
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-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(BadDigest{})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-08-03 19:17:21 -04:00
|
|
|
if _, err := io.Copy(&fullObject, bytes.NewBuffer(object)); err != nil {
|
2015-07-05 21:44:55 -04:00
|
|
|
donut.lock.Unlock()
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(err)
|
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-08-03 19:17:21 -04:00
|
|
|
{
|
|
|
|
objectMetadata, err := donut.CreateObject(bucket, key, md5sum, size, &fullObject, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
// No need to call internal cleanup functions here, caller should call AbortMultipartUpload()
|
|
|
|
// which would in-turn cleanup properly in accordance with S3 Spec
|
|
|
|
return ObjectMetadata{}, err.Trace()
|
|
|
|
}
|
|
|
|
fullObject.Reset()
|
2015-07-04 00:02:31 -04:00
|
|
|
|
2015-08-03 19:17:21 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
donut.cleanupMultipartSession(bucket, key, uploadID)
|
|
|
|
donut.lock.Unlock()
|
|
|
|
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-08-03 19:17:21 -04:00
|
|
|
func (donut API) ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata, signature *Signature) (BucketMultipartResourcesMetadata, *probe.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
|
|
|
|
2015-07-09 22:34:07 -04:00
|
|
|
if signature != nil {
|
|
|
|
ok, err := signature.DoesSignatureMatch("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, err.Trace()
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(SignatureDoesNotMatch{})
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:31:28 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
return donut.listMultipartUploads(bucket, resources)
|
|
|
|
}
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-09 22:34:07 -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
|
2015-07-17 12:31:28 -04:00
|
|
|
resources.NextUploadIDMarker = session.UploadID
|
2015-05-18 14:53:28 -04:00
|
|
|
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
|
2015-07-17 12:31:28 -04:00
|
|
|
upload.UploadID = session.UploadID
|
|
|
|
upload.Initiated = session.Initiated
|
2015-05-18 14:53:28 -04:00
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
case resources.KeyMarker != "" && resources.UploadIDMarker != "":
|
2015-07-17 12:31:28 -04:00
|
|
|
if session.UploadID > resources.UploadIDMarker {
|
2015-05-18 14:53:28 -04:00
|
|
|
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
|
2015-07-17 12:31:28 -04:00
|
|
|
upload.UploadID = session.UploadID
|
|
|
|
upload.Initiated = session.Initiated
|
2015-05-18 14:53:28 -04:00
|
|
|
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
|
2015-07-17 12:31:28 -04:00
|
|
|
upload.UploadID = session.UploadID
|
|
|
|
upload.Initiated = session.Initiated
|
2015-05-18 14:53:28 -04:00
|
|
|
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-08-03 19:17:21 -04:00
|
|
|
func (donut API) ListObjectParts(bucket, key string, resources ObjectResourcesMetadata, signature *Signature) (ObjectResourcesMetadata, *probe.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
|
|
|
|
2015-07-09 22:34:07 -04:00
|
|
|
if signature != nil {
|
|
|
|
ok, err := signature.DoesSignatureMatch("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
|
|
|
if err != nil {
|
2015-08-03 19:17:21 -04:00
|
|
|
return ObjectResourcesMetadata{}, err.Trace()
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(SignatureDoesNotMatch{})
|
2015-07-09 22:34:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:31:28 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(key) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(ObjectNameInvalid{Object: key})
|
2015-07-17 12:31:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
return donut.listObjectParts(bucket, key, resources)
|
|
|
|
}
|
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
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-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(ObjectNotFound{Object: key})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
if storedBucket.multiPartSession[key].UploadID != resources.UploadID {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(InvalidUploadID{UploadID: resources.UploadID})
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
2015-07-14 00:34:22 -04:00
|
|
|
storedParts := storedBucket.partMetadata[key]
|
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
|
|
|
|
}
|
2015-07-17 12:31:28 -04:00
|
|
|
for i := startPartNumber; i <= storedBucket.multiPartSession[key].TotalParts; i++ {
|
2015-05-14 22:31:36 -04:00
|
|
|
if len(parts) > objectResourcesMetadata.MaxParts {
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.IsTruncated = true
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
objectResourcesMetadata.NextPartNumberMarker = i
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
2015-07-14 00:34:22 -04:00
|
|
|
part, ok := storedParts[i]
|
2015-05-14 22:31:36 -04:00
|
|
|
if !ok {
|
2015-08-08 02:47:22 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(InvalidPart{})
|
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-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)
|
|
|
|
donut.storedBuckets.Set(bucketName, b)
|
2015-05-14 22:31:36 -04:00
|
|
|
}
|
|
|
|
debug.FreeOSMemory()
|
|
|
|
}
|