2016-04-11 04:29:18 -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 (
|
2016-05-01 04:25:48 -04:00
|
|
|
"encoding/json"
|
2016-04-11 04:29:18 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
2016-05-06 13:19:09 -04:00
|
|
|
"strings"
|
2016-05-08 05:38:35 -04:00
|
|
|
"sync"
|
2016-05-04 18:28:58 -04:00
|
|
|
"time"
|
2016-04-11 04:29:18 -04:00
|
|
|
)
|
|
|
|
|
2016-05-01 20:52:16 -04:00
|
|
|
// MultipartPartInfo Info of each part kept in the multipart metadata file after
|
|
|
|
// CompleteMultipartUpload() is called.
|
|
|
|
type MultipartPartInfo struct {
|
|
|
|
PartNumber int
|
|
|
|
ETag string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultipartObjectInfo - contents of the multipart metadata file after
|
|
|
|
// CompleteMultipartUpload() is called.
|
2016-05-04 18:28:58 -04:00
|
|
|
type MultipartObjectInfo struct {
|
2016-05-19 20:10:08 -04:00
|
|
|
Parts []MultipartPartInfo
|
|
|
|
ModTime time.Time
|
|
|
|
Size int64
|
|
|
|
MD5Sum string
|
|
|
|
ContentType string
|
|
|
|
ContentEncoding string
|
2016-05-18 22:54:25 -04:00
|
|
|
// Add more fields here.
|
2016-05-01 20:52:16 -04:00
|
|
|
}
|
|
|
|
|
2016-05-06 13:19:09 -04:00
|
|
|
type byMultipartFiles []string
|
|
|
|
|
|
|
|
func (files byMultipartFiles) Len() int { return len(files) }
|
|
|
|
func (files byMultipartFiles) Less(i, j int) bool {
|
|
|
|
first := strings.TrimSuffix(files[i], multipartSuffix)
|
|
|
|
second := strings.TrimSuffix(files[j], multipartSuffix)
|
|
|
|
return first < second
|
|
|
|
}
|
|
|
|
func (files byMultipartFiles) Swap(i, j int) { files[i], files[j] = files[j], files[i] }
|
|
|
|
|
2016-05-01 20:52:16 -04:00
|
|
|
// GetPartNumberOffset - given an offset for the whole object, return the part and offset in that part.
|
|
|
|
func (m MultipartObjectInfo) GetPartNumberOffset(offset int64) (partIndex int, partOffset int64, err error) {
|
|
|
|
partOffset = offset
|
2016-05-04 18:28:58 -04:00
|
|
|
for i, part := range m.Parts {
|
2016-05-01 20:52:16 -04:00
|
|
|
partIndex = i
|
|
|
|
if partOffset < part.Size {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
partOffset -= part.Size
|
|
|
|
}
|
|
|
|
// Offset beyond the size of the object
|
|
|
|
err = errUnexpected
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-18 23:16:19 -04:00
|
|
|
// getMultipartObjectMeta - incomplete meta file and extract meta information if any.
|
2016-05-18 22:54:25 -04:00
|
|
|
func getMultipartObjectMeta(storage StorageAPI, metaFile string) (meta map[string]string, err error) {
|
|
|
|
meta = make(map[string]string)
|
|
|
|
offset := int64(0)
|
|
|
|
objMetaReader, err := storage.ReadFile(minioMetaBucket, metaFile, offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-18 23:16:19 -04:00
|
|
|
// Close the metadata reader.
|
|
|
|
defer objMetaReader.Close()
|
|
|
|
|
2016-05-18 22:54:25 -04:00
|
|
|
decoder := json.NewDecoder(objMetaReader)
|
|
|
|
err = decoder.Decode(&meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 20:52:16 -04:00
|
|
|
func partNumToPartFileName(partNum int) string {
|
|
|
|
return fmt.Sprintf("%.5d%s", partNum, multipartSuffix)
|
|
|
|
}
|
|
|
|
|
2016-04-11 04:29:18 -04:00
|
|
|
// ListMultipartUploads - list multipart uploads.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-05-05 15:51:56 -04:00
|
|
|
return listMultipartUploadsCommon(xl, bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:57:31 -04:00
|
|
|
// NewMultipartUpload - initialize a new multipart upload, returns a unique id.
|
2016-05-18 22:54:25 -04:00
|
|
|
func (xl xlObjects) NewMultipartUpload(bucket, object string, meta map[string]string) (string, error) {
|
|
|
|
return newMultipartUploadCommon(xl.storage, bucket, object, meta)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 13:39:28 -04:00
|
|
|
// PutObjectPart - writes the multipart upload chunks.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return putObjectPartCommon(xl.storage, bucket, object, uploadID, partID, size, data, md5Hex)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
// ListObjectParts - list object parts.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return listObjectPartsCommon(xl.storage, bucket, object, uploadID, partNumberMarker, maxParts)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-13 14:52:36 -04:00
|
|
|
// This function does the following check, suppose
|
|
|
|
// object is "a/b/c/d", stat makes sure that objects ""a/b/c""
|
|
|
|
// "a/b" and "a" do not exist.
|
|
|
|
func (xl xlObjects) parentDirIsObject(bucket, parent string) error {
|
|
|
|
var stat func(string) error
|
|
|
|
stat = func(p string) error {
|
|
|
|
if p == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := xl.getObjectInfo(bucket, p)
|
|
|
|
if err == nil {
|
|
|
|
// If there is already a file at prefix "p" return error.
|
|
|
|
return errFileAccessDenied
|
|
|
|
}
|
|
|
|
if err == errFileNotFound {
|
|
|
|
// Check if there is a file as one of the parent paths.
|
|
|
|
return stat(path.Dir(p))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return stat(parent)
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, error) {
|
2016-04-11 04:29:18 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !isBucketExist(xl.storage, bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-04-11 04:29:18 -04:00
|
|
|
if !IsValidObjectName(object) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", ObjectNameInvalid{
|
2016-04-16 15:48:41 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
if !isUploadIDExists(xl.storage, bucket, object, uploadID) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
2016-04-29 17:24:10 -04:00
|
|
|
}
|
2016-05-18 18:05:23 -04:00
|
|
|
// 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-08 05:38:35 -04:00
|
|
|
|
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
|
|
|
s3MD5, err := completeMultipartMD5(parts...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-05-04 18:28:58 -04:00
|
|
|
var metadata = MultipartObjectInfo{}
|
2016-05-08 05:38:35 -04:00
|
|
|
var errs = make([]error, len(parts))
|
|
|
|
|
2016-05-18 22:54:25 -04:00
|
|
|
uploadIDIncompletePath := path.Join(mpartMetaPrefix, bucket, object, uploadID, incompleteFile)
|
|
|
|
objMeta, err := getMultipartObjectMeta(xl.storage, uploadIDIncompletePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDIncompletePath)
|
|
|
|
}
|
|
|
|
|
2016-05-08 05:38:35 -04:00
|
|
|
// Waitgroup to wait for go-routines.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Loop through all parts, validate them and then commit to disk.
|
2016-05-09 02:49:49 -04:00
|
|
|
for i, part := range parts {
|
2016-05-03 19:10:24 -04:00
|
|
|
// Construct part suffix.
|
2016-05-07 05:08:03 -04:00
|
|
|
partSuffix := fmt.Sprintf("%.5d.%s", part.PartNumber, part.ETag)
|
|
|
|
multipartPartFile := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
2016-05-08 05:38:35 -04:00
|
|
|
var fi FileInfo
|
|
|
|
fi, err = xl.storage.StatFile(minioMetaBucket, multipartPartFile)
|
2016-05-01 20:52:16 -04:00
|
|
|
if err != nil {
|
2016-05-03 19:10:24 -04:00
|
|
|
if err == errFileNotFound {
|
|
|
|
return "", InvalidPart{}
|
|
|
|
}
|
2016-05-01 20:52:16 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2016-05-09 02:49:49 -04:00
|
|
|
// All parts except the last part has to be atleast 5MB.
|
|
|
|
if (i < len(parts)-1) && !isMinAllowedPartSize(fi.Size) {
|
2016-05-08 15:06:05 -04:00
|
|
|
return "", PartTooSmall{}
|
|
|
|
}
|
2016-05-04 18:28:58 -04:00
|
|
|
// Update metadata parts.
|
|
|
|
metadata.Parts = append(metadata.Parts, MultipartPartInfo{
|
|
|
|
PartNumber: part.PartNumber,
|
|
|
|
ETag: part.ETag,
|
|
|
|
Size: fi.Size,
|
|
|
|
})
|
|
|
|
metadata.Size += fi.Size
|
2016-05-08 05:38:35 -04:00
|
|
|
}
|
|
|
|
|
2016-05-13 14:52:36 -04:00
|
|
|
// check if an object is present as one of the parent dir.
|
|
|
|
if err = xl.parentDirIsObject(bucket, path.Dir(object)); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-08 05:38:35 -04:00
|
|
|
// Save successfully calculated md5sum.
|
2016-05-04 18:28:58 -04:00
|
|
|
metadata.MD5Sum = s3MD5
|
2016-05-18 22:54:25 -04:00
|
|
|
metadata.ContentType = objMeta["content-type"]
|
2016-05-19 20:10:08 -04:00
|
|
|
metadata.ContentEncoding = objMeta["content-encoding"]
|
2016-05-18 22:54:25 -04:00
|
|
|
|
2016-05-04 18:28:58 -04:00
|
|
|
// Save modTime as well as the current time.
|
|
|
|
metadata.ModTime = time.Now().UTC()
|
|
|
|
|
|
|
|
// Create temporary multipart meta file to write and then rename.
|
2016-05-16 18:40:57 -04:00
|
|
|
multipartMetaSuffix := fmt.Sprintf("%s.%s", uploadID, multipartMetaFile)
|
|
|
|
tempMultipartMetaFile := path.Join(tmpMetaPrefix, bucket, object, multipartMetaSuffix)
|
2016-05-03 19:10:24 -04:00
|
|
|
w, err := xl.storage.CreateFile(minioMetaBucket, tempMultipartMetaFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
err = encoder.Encode(&metadata)
|
|
|
|
if err != nil {
|
|
|
|
if err = safeCloseAndRemove(w); err != nil {
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-01 04:25:48 -04:00
|
|
|
}
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-01 04:25:48 -04:00
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
// Close the writer.
|
|
|
|
if err = w.Close(); err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if err = safeCloseAndRemove(w); err != nil {
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-05 23:28:22 -04:00
|
|
|
}
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-01 04:25:48 -04:00
|
|
|
}
|
2016-05-09 15:09:48 -04:00
|
|
|
|
|
|
|
// Attempt a Rename of multipart meta file to final namespace.
|
2016-05-13 14:52:36 -04:00
|
|
|
multipartObjFile := path.Join(mpartMetaPrefix, bucket, object, uploadID, multipartMetaFile)
|
|
|
|
err = xl.storage.RenameFile(minioMetaBucket, tempMultipartMetaFile, minioMetaBucket, multipartObjFile)
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if derr := xl.storage.DeleteFile(minioMetaBucket, tempMultipartMetaFile); derr != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempMultipartMetaFile)
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", toObjectErr(err, bucket, multipartObjFile)
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
|
2016-05-09 15:09:48 -04:00
|
|
|
// Loop through and atomically rename the parts to their actual location.
|
|
|
|
for index, part := range parts {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, part completePart) {
|
|
|
|
defer wg.Done()
|
|
|
|
partSuffix := fmt.Sprintf("%.5d.%s", part.PartNumber, part.ETag)
|
2016-05-13 14:52:36 -04:00
|
|
|
src := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
dst := path.Join(mpartMetaPrefix, bucket, object, uploadID, partNumToPartFileName(part.PartNumber))
|
|
|
|
errs[index] = xl.storage.RenameFile(minioMetaBucket, src, minioMetaBucket, dst)
|
2016-05-16 17:31:28 -04:00
|
|
|
errorIf(errs[index], "Unable to rename file %s to %s.", src, dst)
|
2016-05-09 15:09:48 -04:00
|
|
|
}(index, part)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the renames to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Loop through errs list and return first error.
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-05 23:28:22 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 15:09:48 -04:00
|
|
|
|
|
|
|
// Delete the incomplete file place holder.
|
2016-05-16 17:31:28 -04:00
|
|
|
err = xl.storage.DeleteFile(minioMetaBucket, uploadIDIncompletePath)
|
2016-05-05 23:28:22 -04:00
|
|
|
if err != nil {
|
2016-05-16 17:31:28 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDIncompletePath)
|
2016-05-05 23:28:22 -04:00
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
|
2016-05-18 18:05:23 -04:00
|
|
|
// Hold write lock on the destination before rename
|
|
|
|
nsMutex.Lock(bucket, object)
|
|
|
|
defer nsMutex.Unlock(bucket, object)
|
|
|
|
|
2016-05-13 14:52:36 -04:00
|
|
|
// Delete if an object already exists.
|
|
|
|
// FIXME: rename it to tmp file and delete only after
|
|
|
|
// the newly uploaded file is renamed from tmp location to
|
|
|
|
// the original location.
|
2016-05-19 20:10:33 -04:00
|
|
|
// Verify if the object is a multipart object.
|
|
|
|
if isMultipartObject(xl.storage, bucket, object) {
|
|
|
|
err = xl.deleteMultipartObject(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
2016-05-13 14:52:36 -04:00
|
|
|
err = xl.deleteObject(bucket, object)
|
2016-05-19 20:10:33 -04:00
|
|
|
if err != nil {
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:31:28 -04:00
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
if err = xl.storage.RenameFile(minioMetaBucket, uploadIDPath, bucket, object); err != nil {
|
2016-05-13 14:52:36 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-18 18:05:23 -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))
|
|
|
|
|
2016-05-09 15:09:48 -04:00
|
|
|
// Validate if there are other incomplete upload-id's present for
|
|
|
|
// the object, if yes do not attempt to delete 'uploads.json'.
|
|
|
|
var entries []string
|
|
|
|
if entries, err = xl.storage.ListDir(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object)); err == nil {
|
|
|
|
if len(entries) > 1 {
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 14:52:36 -04:00
|
|
|
|
2016-05-07 05:08:03 -04:00
|
|
|
uploadsJSONPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)
|
|
|
|
err = xl.storage.DeleteFile(minioMetaBucket, uploadsJSONPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadsJSONPath)
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Return md5sum.
|
2016-04-16 15:48:41 -04:00
|
|
|
return s3MD5, nil
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:57:31 -04:00
|
|
|
// AbortMultipartUpload - aborts a multipart upload.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
2016-05-02 19:57:31 -04:00
|
|
|
return abortMultipartUploadCommon(xl.storage, bucket, object, uploadID)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|