2016-05-20 23:48:47 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
2016-05-24 16:35:43 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-05-20 23:48:47 -04:00
|
|
|
"time"
|
2016-05-24 16:35:43 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListMultipartUploads - list multipart uploads.
|
|
|
|
func (xl xlObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-05-30 19:51:59 -04:00
|
|
|
return xl.listMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// newMultipartUpload - initialize a new multipart.
|
|
|
|
func (xl xlObjects) newMultipartUpload(bucket string, object string, meta map[string]string) (uploadID string, err error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket name is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify if object name is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
// No metadata is set, allocate a new one.
|
|
|
|
if meta == nil {
|
|
|
|
meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
xlMeta := newXLMetaV1(xl.dataBlocks, xl.parityBlocks)
|
2016-05-20 23:48:47 -04:00
|
|
|
// If not set default to "application/octet-stream"
|
|
|
|
if meta["content-type"] == "" {
|
2016-05-24 16:35:43 -04:00
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(object); objectExt != "" {
|
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
meta["content-type"] = contentType
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-25 19:42:31 -04:00
|
|
|
xlMeta.Stat.ModTime = time.Now().UTC()
|
|
|
|
xlMeta.Stat.Version = 1
|
2016-05-20 23:48:47 -04:00
|
|
|
xlMeta.Meta = meta
|
|
|
|
|
|
|
|
// This lock needs to be held for any changes to the directory contents of ".minio/multipart/object/"
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
|
|
|
|
uploadID = getUUID()
|
|
|
|
initiated := time.Now().UTC()
|
|
|
|
// Create 'uploads.json'
|
|
|
|
if err = writeUploadJSON(bucket, object, uploadID, initiated, xl.storageDisks...); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
2016-05-29 03:42:09 -04:00
|
|
|
tempUploadIDPath := path.Join(tmpMetaPrefix, uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err = xl.writeXLMetadata(minioMetaBucket, tempUploadIDPath, xlMeta); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
2016-05-25 04:33:39 -04:00
|
|
|
rErr := xl.renameObject(minioMetaBucket, tempUploadIDPath, minioMetaBucket, uploadIDPath)
|
|
|
|
if rErr == nil {
|
|
|
|
// Return success.
|
|
|
|
return uploadID, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-25 04:33:39 -04:00
|
|
|
return "", toObjectErr(rErr, minioMetaBucket, uploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultipartUpload - initialize a new multipart upload, returns a unique id.
|
|
|
|
func (xl xlObjects) NewMultipartUpload(bucket, object string, meta map[string]string) (string, error) {
|
2016-05-30 19:51:59 -04:00
|
|
|
return xl.newMultipartUpload(bucket, object, meta)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// putObjectPart - put object part.
|
|
|
|
func (xl xlObjects) putObjectPart(bucket string, object string, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
uploadIDPath := pathJoin(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
nsMutex.Lock(minioMetaBucket, uploadIDPath)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, uploadIDPath)
|
2016-05-27 04:12:44 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
if !xl.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
|
|
|
partsMetadata, errs := xl.readAllXLMetadata(minioMetaBucket, uploadIDPath)
|
2016-05-30 14:26:10 -04:00
|
|
|
|
2016-05-25 19:42:31 -04:00
|
|
|
// List all online disks.
|
2016-05-31 23:23:31 -04:00
|
|
|
onlineDisks, higherVersion, err := xl.listOnlineDisks(partsMetadata, errs)
|
2016-05-25 19:42:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Pick one from the first valid metadata.
|
|
|
|
xlMeta := pickValidXLMeta(partsMetadata)
|
2016-05-27 04:12:44 -04:00
|
|
|
|
2016-05-26 22:55:48 -04:00
|
|
|
// Initialize a new erasure with online disks and new distribution.
|
|
|
|
erasure := newErasure(onlineDisks, xlMeta.Erasure.Distribution)
|
2016-05-25 19:42:31 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Initialize sha512 hash.
|
|
|
|
erasure.InitHash("sha512")
|
|
|
|
|
2016-05-25 00:24:20 -04:00
|
|
|
partSuffix := fmt.Sprintf("object%d", partID)
|
2016-05-29 03:42:09 -04:00
|
|
|
tmpPartPath := path.Join(tmpMetaPrefix, uploadID, partSuffix)
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
2016-05-29 18:38:14 -04:00
|
|
|
// Allocate blocksized buffer for reading.
|
|
|
|
buf := make([]byte, blockSizeV1)
|
|
|
|
|
|
|
|
// Read until io.EOF, fill the allocated buf.
|
2016-05-28 18:13:15 -04:00
|
|
|
for {
|
|
|
|
var n int
|
|
|
|
n, err = io.ReadFull(data, buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil && err != io.ErrUnexpectedEOF {
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
// Update md5 writer.
|
|
|
|
md5Writer.Write(buf[:n])
|
|
|
|
var m int64
|
|
|
|
m, err = erasure.AppendFile(minioMetaBucket, tmpPartPath, buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tmpPartPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
if m != int64(len(buf[:n])) {
|
|
|
|
return "", toObjectErr(errUnexpected, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-29 18:38:14 -04:00
|
|
|
|
|
|
|
// Calculate new md5sum.
|
2016-05-20 23:48:47 -04:00
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
|
|
|
return "", BadDigest{md5Hex, newMD5Hex}
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
|
2016-05-30 14:26:10 -04:00
|
|
|
if !xl.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
2016-05-29 18:38:14 -04:00
|
|
|
// Rename temporary part file to its final location.
|
2016-05-31 23:23:31 -04:00
|
|
|
partPath := path.Join(uploadIDPath, partSuffix)
|
2016-05-20 23:48:47 -04:00
|
|
|
err = xl.renameObject(minioMetaBucket, tmpPartPath, minioMetaBucket, partPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, partPath)
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
|
|
|
|
// Once part is successfully committed, proceed with updating XL metadata.
|
|
|
|
xlMeta.Stat.Version = higherVersion
|
2016-05-31 23:23:31 -04:00
|
|
|
// Add the current part.
|
2016-05-26 06:15:01 -04:00
|
|
|
xlMeta.AddObjectPart(partID, partSuffix, newMD5Hex, size)
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Get calculated hash checksums from erasure to save in `xl.json`.
|
|
|
|
hashChecksums := erasure.GetHashes()
|
|
|
|
|
|
|
|
checkSums := make([]checkSumInfo, len(xl.storageDisks))
|
|
|
|
for index := range xl.storageDisks {
|
|
|
|
blockIndex := xlMeta.Erasure.Distribution[index] - 1
|
|
|
|
checkSums[blockIndex] = checkSumInfo{
|
|
|
|
Name: partSuffix,
|
|
|
|
Algorithm: "sha512",
|
|
|
|
Hash: hashChecksums[blockIndex],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for index := range partsMetadata {
|
|
|
|
blockIndex := xlMeta.Erasure.Distribution[index] - 1
|
|
|
|
partsMetadata[index].Parts = xlMeta.Parts
|
|
|
|
partsMetadata[index].Erasure.Checksum = append(partsMetadata[index].Erasure.Checksum, checkSums[blockIndex])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all the checksum metadata.
|
2016-05-29 03:42:09 -04:00
|
|
|
tempUploadIDPath := path.Join(tmpMetaPrefix, uploadID)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Write unique `xl.json` each disk.
|
|
|
|
if err = xl.writeUniqueXLMetadata(minioMetaBucket, tempUploadIDPath, partsMetadata); err != nil {
|
2016-05-28 18:13:15 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
rErr := xl.renameXLMetadata(minioMetaBucket, tempUploadIDPath, minioMetaBucket, uploadIDPath)
|
|
|
|
if rErr != nil {
|
|
|
|
return "", toObjectErr(rErr, minioMetaBucket, uploadIDPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success.
|
2016-05-20 23:48:47 -04:00
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPart - writes the multipart upload chunks.
|
|
|
|
func (xl xlObjects) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
2016-05-30 19:51:59 -04:00
|
|
|
return xl.putObjectPart(bucket, object, uploadID, partID, size, data, md5Hex)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// ListObjectParts - list object parts.
|
|
|
|
func (xl xlObjects) listObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListPartsInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return ListPartsInfo{}, BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ListPartsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
// Hold lock so that there is no competing abort-multipart-upload or complete-multipart-upload.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
2016-05-28 16:23:08 -04:00
|
|
|
|
|
|
|
if !xl.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return ListPartsInfo{}, InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
result := ListPartsInfo{}
|
|
|
|
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-05-25 04:33:39 -04:00
|
|
|
xlMeta, err := xl.readXLMetadata(minioMetaBucket, uploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaBucket, uploadIDPath)
|
|
|
|
}
|
2016-05-25 00:24:20 -04:00
|
|
|
|
|
|
|
// Populate the result stub.
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
result.MaxParts = maxParts
|
|
|
|
|
|
|
|
// For empty number of parts or maxParts as zero, return right here.
|
|
|
|
if len(xlMeta.Parts) == 0 || maxParts == 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit output to maxPartsList.
|
|
|
|
if maxParts > maxPartsList {
|
|
|
|
maxParts = maxPartsList
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Only parts with higher part numbers will be listed.
|
2016-05-26 06:15:01 -04:00
|
|
|
partIdx := xlMeta.ObjectPartIndex(partNumberMarker)
|
2016-05-25 00:24:20 -04:00
|
|
|
parts := xlMeta.Parts
|
|
|
|
if partIdx != -1 {
|
|
|
|
parts = xlMeta.Parts[partIdx+1:]
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
count := maxParts
|
2016-05-25 00:24:20 -04:00
|
|
|
for _, part := range parts {
|
2016-05-20 23:48:47 -04:00
|
|
|
partNamePath := path.Join(mpartMetaPrefix, bucket, object, uploadID, part.Name)
|
2016-05-25 04:33:39 -04:00
|
|
|
var fi FileInfo
|
|
|
|
fi, err = xl.statPart(minioMetaBucket, partNamePath)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaBucket, partNamePath)
|
|
|
|
}
|
|
|
|
result.Parts = append(result.Parts, partInfo{
|
2016-05-25 00:24:20 -04:00
|
|
|
PartNumber: part.Number,
|
2016-05-20 23:48:47 -04:00
|
|
|
ETag: part.ETag,
|
|
|
|
LastModified: fi.ModTime,
|
2016-05-28 03:18:58 -04:00
|
|
|
Size: part.Size,
|
2016-05-20 23:48:47 -04:00
|
|
|
})
|
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If listed entries are more than maxParts, we set IsTruncated as true.
|
|
|
|
if len(parts) > len(result.Parts) {
|
|
|
|
result.IsTruncated = true
|
|
|
|
// Make sure to fill next part number marker if IsTruncated is
|
|
|
|
// true for subsequent listing.
|
|
|
|
nextPartNumberMarker := result.Parts[len(result.Parts)-1].PartNumber
|
|
|
|
result.NextPartNumberMarker = nextPartNumberMarker
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectParts - list object parts.
|
|
|
|
func (xl xlObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-05-30 19:51:59 -04:00
|
|
|
return xl.listObjectParts(bucket, object, uploadID, partNumberMarker, maxParts)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (xl xlObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Hold lock so that
|
|
|
|
// 1) no one aborts this multipart upload
|
|
|
|
// 2) no one does a parallel complete-multipart-upload on this multipart upload
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
|
2016-05-28 00:50:09 -04:00
|
|
|
if !xl.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
|
|
|
s3MD5, err := completeMultipartMD5(parts...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(mpartMetaPrefix, bucket, object, uploadID)
|
2016-05-25 00:24:20 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
|
|
|
partsMetadata, errs := xl.readAllXLMetadata(minioMetaBucket, uploadIDPath)
|
|
|
|
if err = xl.reduceError(errs); err != nil {
|
2016-05-25 00:24:20 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Calculate full object size.
|
2016-05-20 23:48:47 -04:00
|
|
|
var objectSize int64
|
2016-05-25 00:24:20 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Pick one from the first valid metadata.
|
|
|
|
xlMeta := pickValidXLMeta(partsMetadata)
|
|
|
|
|
2016-05-25 00:24:20 -04:00
|
|
|
// Save current xl meta for validation.
|
|
|
|
var currentXLMeta = xlMeta
|
|
|
|
|
|
|
|
// Allocate parts similar to incoming slice.
|
|
|
|
xlMeta.Parts = make([]objectPartInfo, len(parts))
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Loop through all parts, validate them and then commit to disk.
|
|
|
|
for i, part := range parts {
|
2016-05-26 06:15:01 -04:00
|
|
|
partIdx := currentXLMeta.ObjectPartIndex(part.PartNumber)
|
2016-05-25 00:24:20 -04:00
|
|
|
if partIdx == -1 {
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", InvalidPart{}
|
|
|
|
}
|
2016-05-25 00:24:20 -04:00
|
|
|
if currentXLMeta.Parts[partIdx].ETag != part.ETag {
|
|
|
|
return "", BadDigest{}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
// All parts except the last part has to be atleast 5MB.
|
2016-05-25 00:24:20 -04:00
|
|
|
if (i < len(parts)-1) && !isMinAllowedPartSize(currentXLMeta.Parts[partIdx].Size) {
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", PartTooSmall{}
|
|
|
|
}
|
2016-05-25 00:24:20 -04:00
|
|
|
|
|
|
|
// Save for total object size.
|
|
|
|
objectSize += currentXLMeta.Parts[partIdx].Size
|
|
|
|
|
|
|
|
// Add incoming parts.
|
|
|
|
xlMeta.Parts[i] = objectPartInfo{
|
|
|
|
Number: part.PartNumber,
|
|
|
|
ETag: part.ETag,
|
|
|
|
Size: currentXLMeta.Parts[partIdx].Size,
|
|
|
|
Name: fmt.Sprintf("object%d", part.PartNumber),
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an object is present as one of the parent dir.
|
|
|
|
if xl.parentDirIsObject(bucket, path.Dir(object)) {
|
|
|
|
return "", toObjectErr(errFileAccessDenied, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the final object size and modtime.
|
|
|
|
xlMeta.Stat.Size = objectSize
|
|
|
|
xlMeta.Stat.ModTime = time.Now().UTC()
|
|
|
|
|
|
|
|
// Save successfully calculated md5sum.
|
|
|
|
xlMeta.Meta["md5Sum"] = s3MD5
|
2016-05-28 18:13:15 -04:00
|
|
|
uploadIDPath = path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
2016-05-29 03:42:09 -04:00
|
|
|
tempUploadIDPath := path.Join(tmpMetaPrefix, uploadID)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Update all xl metadata, make sure to not modify fields like
|
|
|
|
// checksum which are different on each disks.
|
|
|
|
for index := range partsMetadata {
|
|
|
|
partsMetadata[index].Stat = xlMeta.Stat
|
|
|
|
partsMetadata[index].Meta = xlMeta.Meta
|
|
|
|
partsMetadata[index].Parts = xlMeta.Parts
|
|
|
|
}
|
|
|
|
// Write unique `xl.json` for each disk.
|
|
|
|
if err = xl.writeUniqueXLMetadata(minioMetaBucket, tempUploadIDPath, partsMetadata); err != nil {
|
2016-05-28 18:13:15 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
|
|
|
rErr := xl.renameXLMetadata(minioMetaBucket, tempUploadIDPath, minioMetaBucket, uploadIDPath)
|
|
|
|
if rErr != nil {
|
|
|
|
return "", toObjectErr(rErr, minioMetaBucket, uploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
// Hold write lock on the destination before rename
|
|
|
|
nsMutex.Lock(bucket, object)
|
|
|
|
defer nsMutex.Unlock(bucket, object)
|
|
|
|
|
2016-05-29 03:42:09 -04:00
|
|
|
// Rename if an object already exists to temporary location.
|
|
|
|
uniqueID := getUUID()
|
|
|
|
err = xl.renameObject(bucket, object, minioMetaBucket, path.Join(tmpMetaPrefix, uniqueID))
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-28 16:23:08 -04:00
|
|
|
// Remove parts that weren't present in CompleteMultipartUpload request
|
|
|
|
for _, curpart := range currentXLMeta.Parts {
|
|
|
|
if xlMeta.ObjectPartIndex(curpart.Number) == -1 {
|
|
|
|
// Delete the missing part files. e.g,
|
|
|
|
// Request 1: NewMultipart
|
|
|
|
// Request 2: PutObjectPart 1
|
|
|
|
// Request 3: PutObjectPart 2
|
|
|
|
// Request 4: CompleteMultipartUpload --part 2
|
|
|
|
// N.B. 1st part is not present. This part should be removed from the storage.
|
|
|
|
xl.removeObjectPart(bucket, object, uploadID, curpart.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 03:42:09 -04:00
|
|
|
// Rename the multipart object to final location.
|
2016-05-20 23:48:47 -04:00
|
|
|
if err = xl.renameObject(minioMetaBucket, uploadIDPath, bucket, object); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-29 03:42:09 -04:00
|
|
|
// Delete the previously successfully renamed object.
|
|
|
|
xl.deleteObject(minioMetaBucket, path.Join(tmpMetaPrefix, uniqueID))
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads do no
|
|
|
|
// leave a stale uploads.json behind.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
|
|
|
|
// Validate if there are other incomplete upload-id's present for
|
|
|
|
// the object, if yes do not attempt to delete 'uploads.json'.
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, xl.storageDisks...)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == nil {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadIDIdx := uploadsJSON.Index(uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
if uploadIDIdx != -1 {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON.Uploads = append(uploadsJSON.Uploads[:uploadIDIdx], uploadsJSON.Uploads[uploadIDIdx+1:]...)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 17:43:17 -04:00
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
|
|
|
if err = updateUploadsJSON(bucket, object, uploadsJSON, xl.storageDisks...); err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = xl.deleteObject(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return md5sum.
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// abortMultipartUpload - aborts a multipart upload.
|
|
|
|
func (xl xlObjects) abortMultipartUpload(bucket, object, uploadID string) error {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold lock so that there is no competing complete-multipart-upload or put-object-part.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
|
2016-05-28 16:23:08 -04:00
|
|
|
if !xl.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Cleanup all uploaded parts.
|
|
|
|
if err := cleanupUploadedParts(bucket, object, uploadID, xl.storageDisks...); err != nil {
|
2016-05-26 17:43:17 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 14:26:10 -04:00
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
2016-05-20 23:48:47 -04:00
|
|
|
// Validate if there are other incomplete upload-id's present for
|
|
|
|
// the object, if yes do not attempt to delete 'uploads.json'.
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, xl.storageDisks...)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == nil {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadIDIdx := uploadsJSON.Index(uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
if uploadIDIdx != -1 {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON.Uploads = append(uploadsJSON.Uploads[:uploadIDIdx], uploadsJSON.Uploads[uploadIDIdx+1:]...)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 17:43:17 -04:00
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
|
|
|
err = updateUploadsJSON(bucket, object, uploadsJSON, xl.storageDisks...)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
2016-05-25 07:39:06 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = xl.deleteObject(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object)); err != nil {
|
|
|
|
return toObjectErr(err, minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUpload - aborts a multipart upload.
|
|
|
|
func (xl xlObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
2016-05-30 19:51:59 -04:00
|
|
|
return xl.abortMultipartUpload(bucket, object, uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|