2016-05-03 01:57:31 +02: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.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-05-03 01:57:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2016-10-03 00:51:49 +02:00
|
|
|
"crypto/sha256"
|
2016-05-03 01:57:31 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-10-03 00:51:49 +02:00
|
|
|
"hash"
|
2016-05-03 01:57:31 +02:00
|
|
|
"io"
|
|
|
|
"path"
|
2016-05-04 01:10:24 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-05-21 05:48:47 +02:00
|
|
|
"time"
|
2016-05-03 01:57:31 +02:00
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
2016-05-31 01:51:59 +02:00
|
|
|
// listMultipartUploads - lists all multipart uploads.
|
|
|
|
func (fs fsObjects) listMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-05-04 01:10:24 +02:00
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
recursive := true
|
|
|
|
if delimiter == slashSeparator {
|
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
result.IsTruncated = true
|
|
|
|
result.MaxUploads = maxUploads
|
2016-05-26 23:43:17 +02:00
|
|
|
result.KeyMarker = keyMarker
|
|
|
|
result.Prefix = prefix
|
|
|
|
result.Delimiter = delimiter
|
2016-05-04 01:10:24 +02:00
|
|
|
|
|
|
|
// Not using path.Join() as it strips off the trailing '/'.
|
2016-05-26 23:43:17 +02:00
|
|
|
multipartPrefixPath := pathJoin(mpartMetaPrefix, bucket, prefix)
|
2016-05-17 20:44:32 +02:00
|
|
|
if prefix == "" {
|
|
|
|
// Should have a trailing "/" if prefix is ""
|
|
|
|
// For ex. multipartPrefixPath should be "multipart/bucket/" if prefix is ""
|
|
|
|
multipartPrefixPath += slashSeparator
|
|
|
|
}
|
2016-05-04 01:10:24 +02:00
|
|
|
multipartMarkerPath := ""
|
|
|
|
if keyMarker != "" {
|
2016-05-26 23:43:17 +02:00
|
|
|
multipartMarkerPath = pathJoin(mpartMetaPrefix, bucket, keyMarker)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
2016-05-26 23:43:17 +02:00
|
|
|
var uploads []uploadMetadata
|
|
|
|
var err error
|
|
|
|
var eof bool
|
|
|
|
if uploadIDMarker != "" {
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
nsMutex.RLock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, keyMarker), opsID)
|
2016-05-26 23:43:17 +02:00
|
|
|
uploads, _, err = listMultipartUploadIDs(bucket, keyMarker, uploadIDMarker, maxUploads, fs.storage)
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.RUnlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, keyMarker), opsID)
|
2016-05-26 23:43:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
maxUploads = maxUploads - len(uploads)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
2016-07-07 18:06:35 +02:00
|
|
|
var walkResultCh chan treeWalkResult
|
|
|
|
var endWalkCh chan struct{}
|
2016-08-17 20:36:33 +02:00
|
|
|
heal := false // true only for xl.ListObjectsHeal()
|
2016-05-26 23:43:17 +02:00
|
|
|
if maxUploads > 0 {
|
2016-08-17 20:36:33 +02:00
|
|
|
walkResultCh, endWalkCh = fs.listPool.Release(listParams{minioMetaBucket, recursive, multipartMarkerPath, multipartPrefixPath, heal})
|
2016-06-25 01:41:57 +02:00
|
|
|
if walkResultCh == nil {
|
|
|
|
endWalkCh = make(chan struct{})
|
2016-07-18 00:16:52 +02:00
|
|
|
isLeaf := fs.isMultipartUpload
|
2016-09-15 22:43:40 +02:00
|
|
|
listDir := listDirFactory(isLeaf, fsTreeWalkIgnoredErrs, fs.storage)
|
2016-07-18 00:16:52 +02:00
|
|
|
walkResultCh = startTreeWalk(minioMetaBucket, multipartPrefixPath, multipartMarkerPath, recursive, listDir, isLeaf, endWalkCh)
|
2016-05-26 23:43:17 +02:00
|
|
|
}
|
|
|
|
for maxUploads > 0 {
|
2016-06-25 01:41:57 +02:00
|
|
|
walkResult, ok := <-walkResultCh
|
2016-05-26 23:43:17 +02:00
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
|
|
|
// File not found or Disk not found is a valid case.
|
2016-09-15 22:43:40 +02:00
|
|
|
if isErrIgnored(walkResult.err, fsTreeWalkIgnoredErrs) {
|
2016-05-26 23:43:17 +02:00
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, walkResult.err
|
2016-05-26 23:43:17 +02:00
|
|
|
}
|
|
|
|
entry := strings.TrimPrefix(walkResult.entry, retainSlash(pathJoin(mpartMetaPrefix, bucket)))
|
|
|
|
if strings.HasSuffix(walkResult.entry, slashSeparator) {
|
|
|
|
uploads = append(uploads, uploadMetadata{
|
|
|
|
Object: entry,
|
|
|
|
})
|
|
|
|
maxUploads--
|
|
|
|
if maxUploads == 0 {
|
|
|
|
if walkResult.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var tmpUploads []uploadMetadata
|
|
|
|
var end bool
|
|
|
|
uploadIDMarker = ""
|
2016-08-31 20:39:08 +02:00
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
|
|
|
nsMutex.RLock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, entry), opsID)
|
2016-05-26 23:43:17 +02:00
|
|
|
tmpUploads, end, err = listMultipartUploadIDs(bucket, entry, uploadIDMarker, maxUploads, fs.storage)
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.RUnlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, entry), opsID)
|
2016-05-26 23:43:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
uploads = append(uploads, tmpUploads...)
|
|
|
|
maxUploads -= len(tmpUploads)
|
|
|
|
if walkResult.end && end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Loop through all the received uploads fill in the multiparts result.
|
|
|
|
for _, upload := range uploads {
|
2016-05-04 01:10:24 +02:00
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
2016-05-26 23:43:17 +02:00
|
|
|
if strings.HasSuffix(upload.Object, slashSeparator) {
|
2016-05-04 01:10:24 +02:00
|
|
|
// All directory entries are common prefixes.
|
|
|
|
uploadID = "" // Upload ids are empty for CommonPrefixes.
|
2016-05-26 23:43:17 +02:00
|
|
|
objectName = upload.Object
|
2016-05-04 01:10:24 +02:00
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objectName)
|
|
|
|
} else {
|
2016-05-26 23:43:17 +02:00
|
|
|
uploadID = upload.UploadID
|
|
|
|
objectName = upload.Object
|
|
|
|
result.Uploads = append(result.Uploads, upload)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
|
|
|
result.NextKeyMarker = objectName
|
|
|
|
result.NextUploadIDMarker = uploadID
|
|
|
|
}
|
2016-07-07 18:06:35 +02:00
|
|
|
|
|
|
|
if !eof {
|
|
|
|
// Save the go-routine state in the pool so that it can continue from where it left off on
|
|
|
|
// the next request.
|
2016-08-17 20:36:33 +02:00
|
|
|
fs.listPool.Set(listParams{bucket, recursive, result.NextKeyMarker, prefix, heal}, walkResultCh, endWalkCh)
|
2016-07-07 18:06:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:10:24 +02:00
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// ListMultipartUploads - lists all the pending multipart uploads on a
|
|
|
|
// bucket. Additionally takes 'prefix, keyMarker, uploadIDmarker and a
|
|
|
|
// delimiter' which allows us to list uploads match a particular
|
|
|
|
// prefix or lexically starting from 'keyMarker' or delimiting the
|
|
|
|
// output to get a directory like listing.
|
|
|
|
//
|
|
|
|
// Implements S3 compatible ListMultipartUploads API. The resulting
|
|
|
|
// ListMultipartsInfo structure is unmarshalled directly into XML and
|
|
|
|
// replied back to the client.
|
2016-05-21 05:48:47 +02:00
|
|
|
func (fs fsObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-06-02 21:18:56 +02:00
|
|
|
// Validate input arguments.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(BucketNameInvalid{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(BucketNotFound{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectPrefix(prefix) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(ObjectNameInvalid{Bucket: bucket, Object: prefix})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(UnsupportedDelimiter{
|
2016-06-02 21:18:56 +02:00
|
|
|
Delimiter: delimiter,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, prefix) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(InvalidMarkerPrefixCombination{
|
2016-06-02 21:18:56 +02:00
|
|
|
Marker: keyMarker,
|
|
|
|
Prefix: prefix,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
if strings.HasSuffix(keyMarker, slashSeparator) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(InvalidUploadIDKeyCombination{
|
2016-06-02 21:18:56 +02:00
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
KeyMarker: keyMarker,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
id, err := uuid.Parse(uploadIDMarker)
|
|
|
|
if err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(err)
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if id.IsZero() {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListMultipartsInfo{}, traceError(MalformedUploadID{
|
2016-06-02 21:18:56 +02:00
|
|
|
UploadID: uploadIDMarker,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-31 01:51:59 +02:00
|
|
|
return fs.listMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// newMultipartUpload - wrapper for initializing a new multipart
|
|
|
|
// request, returns back a unique upload id.
|
|
|
|
//
|
|
|
|
// Internally this function creates 'uploads.json' associated for the
|
2016-09-07 05:31:50 +02:00
|
|
|
// incoming object at '.minio.sys/multipart/bucket/object/uploads.json' on
|
2016-06-02 21:18:56 +02:00
|
|
|
// all the disks. `uploads.json` carries metadata regarding on going
|
|
|
|
// multipart operation on the object.
|
|
|
|
func (fs fsObjects) newMultipartUpload(bucket string, object string, meta map[string]string) (uploadID string, err error) {
|
|
|
|
// Initialize `fs.json` values.
|
|
|
|
fsMeta := newFSMetaV1()
|
2016-08-17 22:26:08 +02:00
|
|
|
|
2016-07-22 02:31:14 +02:00
|
|
|
// Save additional metadata only if extended headers such as "X-Amz-Meta-" are set.
|
|
|
|
if hasExtendedHeader(meta) {
|
|
|
|
fsMeta.Meta = meta
|
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
2016-09-07 05:31:50 +02:00
|
|
|
// This lock needs to be held for any changes to the directory contents of ".minio.sys/multipart/object/"
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object), opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object), opsID)
|
2016-06-02 21:18:56 +02:00
|
|
|
|
|
|
|
uploadID = getUUID()
|
|
|
|
initiated := time.Now().UTC()
|
|
|
|
// Create 'uploads.json'
|
2016-06-29 11:10:40 +02:00
|
|
|
if err = fs.writeUploadJSON(bucket, object, uploadID, initiated); err != nil {
|
2016-06-02 21:18:56 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
if err = writeFSMetadata(fs.storage, minioMetaBucket, path.Join(uploadIDPath, fsMetaJSONFile), fsMeta); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
// Return success.
|
|
|
|
return uploadID, nil
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// NewMultipartUpload - initialize a new multipart upload, returns a
|
|
|
|
// unique id. The unique id returned here is of UUID form, for each
|
|
|
|
// subsequent request each UUID is unique.
|
|
|
|
//
|
|
|
|
// Implements S3 compatible initiate multipart API.
|
|
|
|
func (fs fsObjects) NewMultipartUpload(bucket, object string, meta map[string]string) (string, error) {
|
|
|
|
// Verify if bucket name is valid.
|
2016-05-21 05:48:47 +02:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNameInvalid{Bucket: bucket})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNotFound{Bucket: bucket})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
// Verify if object name is valid.
|
2016-05-21 05:48:47 +02:00
|
|
|
if !IsValidObjectName(object) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
return fs.newMultipartUpload(bucket, object, meta)
|
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// Returns if a new part can be appended to fsAppendDataFile.
|
|
|
|
func partToAppend(fsMeta fsMetaV1, fsAppendMeta fsMetaV1) (part objectPartInfo, appendNeeded bool) {
|
|
|
|
if len(fsMeta.Parts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// As fsAppendMeta.Parts will be sorted len(fsAppendMeta.Parts) will naturally be the next part number
|
|
|
|
nextPartNum := len(fsAppendMeta.Parts) + 1
|
|
|
|
nextPartIndex := fsMeta.ObjectPartIndex(nextPartNum)
|
|
|
|
if nextPartIndex == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return fsMeta.Parts[nextPartIndex], true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns metadata path for the file holding info about the parts that
|
|
|
|
// have been appended to the "append-file"
|
|
|
|
func getFSAppendMetaPath(uploadID string) string {
|
|
|
|
return path.Join(tmpMetaPrefix, uploadID+".json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns path for the append-file.
|
|
|
|
func getFSAppendDataPath(uploadID string) string {
|
|
|
|
return path.Join(tmpMetaPrefix, uploadID+".data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append parts to fsAppendDataFile.
|
2016-08-31 20:39:08 +02:00
|
|
|
func appendParts(disk StorageAPI, bucket, object, uploadID, opsID string) {
|
2016-09-02 21:17:32 +02:00
|
|
|
cleanupAppendPaths := func() {
|
|
|
|
// In case of any error, cleanup the append data and json files
|
|
|
|
// from the tmp so that we do not have any inconsistent append
|
|
|
|
// data/json files.
|
|
|
|
disk.DeleteFile(bucket, getFSAppendDataPath(uploadID))
|
|
|
|
disk.DeleteFile(bucket, getFSAppendMetaPath(uploadID))
|
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
// fs-append.json path
|
|
|
|
fsAppendMetaPath := getFSAppendMetaPath(uploadID)
|
|
|
|
// fs.json path
|
|
|
|
fsMetaPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, fsMetaJSONFile)
|
|
|
|
|
|
|
|
// Lock the uploadID so that no one modifies fs.json
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.RLock(minioMetaBucket, uploadIDPath, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
fsMeta, err := readFSMetadata(disk, minioMetaBucket, fsMetaPath)
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.RUnlock(minioMetaBucket, uploadIDPath, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock fs-append.json so that there is no parallel append to the file.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, fsAppendMetaPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, fsAppendMetaPath, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
|
|
|
|
fsAppendMeta, err := readFSMetadata(disk, minioMetaBucket, fsAppendMetaPath)
|
|
|
|
if err != nil {
|
2016-09-02 12:40:12 +02:00
|
|
|
if errorCause(err) != errFileNotFound {
|
2016-09-02 21:17:32 +02:00
|
|
|
cleanupAppendPaths()
|
2016-08-31 22:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fsAppendMeta = fsMeta
|
|
|
|
fsAppendMeta.Parts = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a part needs to be appended to
|
|
|
|
part, appendNeeded := partToAppend(fsMeta, fsAppendMeta)
|
|
|
|
if !appendNeeded {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Hold write lock on the part so that there is no parallel upload on the part.
|
2016-08-31 20:39:08 +02:00
|
|
|
partPath := pathJoin(mpartMetaPrefix, bucket, object, uploadID, strconv.Itoa(part.Number))
|
|
|
|
nsMutex.Lock(minioMetaBucket, partPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, partPath, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
|
|
|
|
// Proceed to append "part"
|
|
|
|
fsAppendDataPath := getFSAppendDataPath(uploadID)
|
|
|
|
// Path to the part that needs to be appended.
|
2016-09-02 12:40:12 +02:00
|
|
|
partPath = path.Join(mpartMetaPrefix, bucket, object, uploadID, part.Name)
|
2016-08-31 22:42:57 +02:00
|
|
|
offset := int64(0)
|
|
|
|
totalLeft := part.Size
|
|
|
|
buf := make([]byte, readSizeV1)
|
|
|
|
for totalLeft > 0 {
|
|
|
|
curLeft := int64(readSizeV1)
|
|
|
|
if totalLeft < readSizeV1 {
|
|
|
|
curLeft = totalLeft
|
|
|
|
}
|
|
|
|
var n int64
|
|
|
|
n, err = disk.ReadFile(minioMetaBucket, partPath, offset, buf[:curLeft])
|
|
|
|
if n > 0 {
|
2016-09-02 21:17:32 +02:00
|
|
|
if err = disk.AppendFile(minioMetaBucket, fsAppendDataPath, buf[:n]); err != nil {
|
|
|
|
cleanupAppendPaths()
|
2016-08-31 22:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
break
|
|
|
|
}
|
2016-09-02 21:17:32 +02:00
|
|
|
cleanupAppendPaths()
|
2016-08-31 22:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
offset += n
|
|
|
|
totalLeft -= n
|
|
|
|
}
|
|
|
|
fsAppendMeta.AddObjectPart(part.Number, part.Name, part.ETag, part.Size)
|
2016-09-02 21:17:32 +02:00
|
|
|
// Overwrite previous fs-append.json
|
2016-08-31 22:42:57 +02:00
|
|
|
if err = writeFSMetadata(disk, minioMetaBucket, fsAppendMetaPath, fsAppendMeta); err != nil {
|
2016-09-02 21:17:32 +02:00
|
|
|
cleanupAppendPaths()
|
2016-08-31 22:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// If there are more parts that need to be appended to fsAppendDataFile
|
|
|
|
_, appendNeeded = partToAppend(fsMeta, fsAppendMeta)
|
|
|
|
if appendNeeded {
|
2016-08-31 20:39:08 +02:00
|
|
|
go appendParts(disk, bucket, object, uploadID, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 00:19:13 +02:00
|
|
|
// PutObjectPart - reads incoming data until EOF for the part file on
|
2016-06-02 21:18:56 +02:00
|
|
|
// an ongoing multipart transaction. Internally incoming data is
|
2016-09-07 05:31:50 +02:00
|
|
|
// written to '.minio.sys/tmp' location and safely renamed to
|
|
|
|
// '.minio.sys/multipart' for reach parts.
|
2016-10-03 00:51:49 +02:00
|
|
|
func (fs fsObjects) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string, sha256sum string) (string, error) {
|
2016-06-03 00:19:13 +02:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNameInvalid{Bucket: bucket})
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNotFound{Bucket: bucket})
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
|
|
|
nsMutex.RLock(minioMetaBucket, uploadIDPath, opsID)
|
2016-06-03 00:19:13 +02:00
|
|
|
// Just check if the uploadID exists to avoid copy if it doesn't.
|
|
|
|
uploadIDExists := fs.isUploadIDExists(bucket, object, uploadID)
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.RUnlock(minioMetaBucket, uploadIDPath, opsID)
|
2016-06-03 00:19:13 +02:00
|
|
|
if !uploadIDExists {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(InvalidUploadID{UploadID: uploadID})
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
partSuffix := fmt.Sprintf("object%d", partID)
|
2016-09-01 01:01:32 +02:00
|
|
|
tmpPartPath := path.Join(tmpMetaPrefix, uploadID+"."+getUUID()+"."+partSuffix)
|
2016-05-21 05:48:47 +02:00
|
|
|
|
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
2016-10-03 00:51:49 +02:00
|
|
|
hashWriters := []io.Writer{md5Writer}
|
|
|
|
|
|
|
|
var sha256Writer hash.Hash
|
|
|
|
if sha256sum != "" {
|
|
|
|
sha256Writer = sha256.New()
|
|
|
|
hashWriters = append(hashWriters, sha256Writer)
|
|
|
|
}
|
|
|
|
multiWriter := io.MultiWriter(hashWriters...)
|
2016-07-05 10:04:50 +02:00
|
|
|
// Limit the reader to its provided size if specified.
|
|
|
|
var limitDataReader io.Reader
|
|
|
|
if size > 0 {
|
|
|
|
// This is done so that we can avoid erroneous clients sending more data than the set content size.
|
|
|
|
limitDataReader = io.LimitReader(data, size)
|
|
|
|
} else {
|
|
|
|
// else we read till EOF.
|
|
|
|
limitDataReader = data
|
|
|
|
}
|
|
|
|
|
2016-10-03 00:51:49 +02:00
|
|
|
teeReader := io.TeeReader(limitDataReader, multiWriter)
|
2016-07-06 05:59:54 +02:00
|
|
|
bufSize := int64(readSizeV1)
|
|
|
|
if size > 0 && bufSize > size {
|
|
|
|
bufSize = size
|
|
|
|
}
|
2016-07-19 04:06:48 +02:00
|
|
|
buf := make([]byte, int(bufSize))
|
|
|
|
bytesWritten, cErr := fsCreateFile(fs.storage, teeReader, buf, minioMetaBucket, tmpPartPath)
|
|
|
|
if cErr != nil {
|
|
|
|
fs.storage.DeleteFile(minioMetaBucket, tmpPartPath)
|
|
|
|
return "", toObjectErr(cErr, minioMetaBucket, tmpPartPath)
|
|
|
|
}
|
|
|
|
// Should return IncompleteBody{} error when reader has fewer
|
|
|
|
// bytes than specified in request header.
|
|
|
|
if bytesWritten < size {
|
|
|
|
fs.storage.DeleteFile(minioMetaBucket, tmpPartPath)
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(IncompleteBody{})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 07:52:03 +02:00
|
|
|
// Delete temporary part in case of failure. If
|
|
|
|
// PutObjectPart succeeds then there would be nothing to
|
|
|
|
// delete.
|
|
|
|
defer fs.storage.DeleteFile(minioMetaBucket, tmpPartPath)
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BadDigest{md5Hex, newMD5Hex})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 00:51:49 +02:00
|
|
|
if sha256sum != "" {
|
|
|
|
newSHA256sum := hex.EncodeToString(sha256Writer.Sum(nil))
|
|
|
|
if newSHA256sum != sha256sum {
|
|
|
|
return "", traceError(SHA256Mismatch{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-10-03 00:51:49 +02:00
|
|
|
// generates random string on setting MINIO_DEBUG=lock, else returns empty string.
|
|
|
|
// used for instrumentation on locks.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID = getOpsID()
|
|
|
|
|
2016-06-03 00:19:13 +02:00
|
|
|
// Hold write lock as we are updating fs.json
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, uploadIDPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, uploadIDPath, opsID)
|
2016-06-03 00:19:13 +02:00
|
|
|
|
|
|
|
// Just check if the uploadID exists to avoid copy if it doesn't.
|
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(InvalidUploadID{UploadID: uploadID})
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
fsMetaPath := pathJoin(uploadIDPath, fsMetaJSONFile)
|
|
|
|
fsMeta, err := readFSMetadata(fs.storage, minioMetaBucket, fsMetaPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
if err != nil {
|
2016-08-31 22:42:57 +02:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, fsMetaPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-05-25 06:24:20 +02:00
|
|
|
fsMeta.AddObjectPart(partID, partSuffix, newMD5Hex, size)
|
2016-05-21 05:48:47 +02:00
|
|
|
|
|
|
|
partPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
err = fs.storage.RenameFile(minioMetaBucket, tmpPartPath, minioMetaBucket, partPath)
|
|
|
|
if err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), minioMetaBucket, partPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
uploadIDPath = path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
if err = writeFSMetadata(fs.storage, minioMetaBucket, path.Join(uploadIDPath, fsMetaJSONFile), fsMeta); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-08-31 20:39:08 +02:00
|
|
|
go appendParts(fs.storage, bucket, object, uploadID, opsID)
|
2016-05-21 05:48:47 +02:00
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// listObjectParts - wrapper scanning through
|
2016-09-07 05:31:50 +02:00
|
|
|
// '.minio.sys/multipart/bucket/object/UPLOADID'. Lists all the parts
|
|
|
|
// saved inside '.minio.sys/multipart/bucket/object/UPLOADID'.
|
2016-06-02 21:18:56 +02:00
|
|
|
func (fs fsObjects) listObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-05-04 01:10:24 +02:00
|
|
|
result := ListPartsInfo{}
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
fsMetaPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, fsMetaJSONFile)
|
|
|
|
fsMeta, err := readFSMetadata(fs.storage, minioMetaBucket, fsMetaPath)
|
2016-05-04 01:10:24 +02:00
|
|
|
if err != nil {
|
2016-08-31 22:42:57 +02:00
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaBucket, fsMetaPath)
|
2016-05-05 21:51:56 +02:00
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
// Only parts with higher part numbers will be listed.
|
2016-05-26 12:15:01 +02:00
|
|
|
partIdx := fsMeta.ObjectPartIndex(partNumberMarker)
|
2016-05-25 06:24:20 +02:00
|
|
|
parts := fsMeta.Parts
|
|
|
|
if partIdx != -1 {
|
|
|
|
parts = fsMeta.Parts[partIdx+1:]
|
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
count := maxParts
|
2016-05-25 06:24:20 +02:00
|
|
|
for _, part := range parts {
|
2016-05-21 05:48:47 +02:00
|
|
|
var fi FileInfo
|
|
|
|
partNamePath := path.Join(mpartMetaPrefix, bucket, object, uploadID, part.Name)
|
|
|
|
fi, err = fs.storage.StatFile(minioMetaBucket, partNamePath)
|
2016-05-04 01:10:24 +02:00
|
|
|
if err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListPartsInfo{}, toObjectErr(traceError(err), minioMetaBucket, partNamePath)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
|
|
|
result.Parts = append(result.Parts, partInfo{
|
2016-05-25 06:24:20 +02:00
|
|
|
PartNumber: part.Number,
|
2016-05-21 05:48:47 +02:00
|
|
|
ETag: part.ETag,
|
2016-05-05 21:51:56 +02:00
|
|
|
LastModified: fi.ModTime,
|
|
|
|
Size: fi.Size,
|
2016-05-04 01:10:24 +02:00
|
|
|
})
|
2016-05-05 21:51:56 +02:00
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 11:21:12 +02:00
|
|
|
// If listed entries are more than maxParts, we set IsTruncated as true.
|
2016-05-21 05:48:47 +02:00
|
|
|
if len(parts) > len(result.Parts) {
|
2016-05-05 21:51:56 +02:00
|
|
|
result.IsTruncated = true
|
2016-05-08 11:21:12 +02:00
|
|
|
// 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
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
result.MaxParts = maxParts
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// ListObjectParts - lists all previously uploaded parts for a given
|
|
|
|
// object and uploadID. Takes additional input of part-number-marker
|
|
|
|
// to indicate where the listing should begin from.
|
|
|
|
//
|
|
|
|
// Implements S3 compatible ListObjectParts API. The resulting
|
|
|
|
// ListPartsInfo structure is unmarshalled directly into XML and
|
|
|
|
// replied back to the client.
|
2016-05-21 05:48:47 +02:00
|
|
|
func (fs fsObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-06-02 21:18:56 +02:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListPartsInfo{}, traceError(BucketNameInvalid{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListPartsInfo{}, traceError(BucketNotFound{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListPartsInfo{}, traceError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
2016-08-31 20:39:08 +02:00
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// Hold lock so that there is no competing abort-multipart-upload or complete-multipart-upload.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID), opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID), opsID)
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return ListPartsInfo{}, traceError(InvalidUploadID{UploadID: uploadID})
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
return fs.listObjectParts(bucket, object, uploadID, partNumberMarker, maxParts)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// CompleteMultipartUpload - completes an ongoing multipart
|
|
|
|
// transaction after receiving all the parts indicated by the client.
|
|
|
|
// Returns an md5sum calculated by concatenating all the individual
|
|
|
|
// md5sums of all the parts.
|
|
|
|
//
|
|
|
|
// Implements S3 compatible Complete multipart API.
|
2016-05-21 05:48:47 +02:00
|
|
|
func (fs fsObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNameInvalid{Bucket: bucket})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BucketNotFound{Bucket: bucket})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(ObjectNameInvalid{
|
2016-05-21 05:48:47 +02:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
2016-06-02 21:18:56 +02: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
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, uploadIDPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, uploadIDPath, opsID)
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(InvalidUploadID{UploadID: uploadID})
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// fs-append.json path
|
|
|
|
fsAppendMetaPath := getFSAppendMetaPath(uploadID)
|
|
|
|
// Lock fs-append.json so that no parallel appendParts() is being done.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, fsAppendMetaPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, fsAppendMetaPath, opsID)
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
2016-10-24 22:56:13 +02:00
|
|
|
s3MD5, err := getCompleteMultipartMD5(parts...)
|
2016-05-21 05:48:47 +02:00
|
|
|
if err != nil {
|
2016-09-15 06:24:54 +02:00
|
|
|
return "", err
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// Read saved fs metadata for ongoing multipart.
|
|
|
|
fsMetaPath := pathJoin(uploadIDPath, fsMetaJSONFile)
|
|
|
|
fsMeta, err := readFSMetadata(fs.storage, minioMetaBucket, fsMetaPath)
|
|
|
|
if err != nil {
|
2016-09-15 06:24:54 +02:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, fsMetaPath)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
fsAppendMeta, err := readFSMetadata(fs.storage, minioMetaBucket, fsAppendMetaPath)
|
|
|
|
if err == nil && isPartsSame(fsAppendMeta.Parts, parts) {
|
|
|
|
fsAppendDataPath := getFSAppendDataPath(uploadID)
|
|
|
|
if err = fs.storage.RenameFile(minioMetaBucket, fsAppendDataPath, bucket, object); err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), minioMetaBucket, fsAppendDataPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
// Remove the append-file metadata file in tmp location as we no longer need it.
|
|
|
|
fs.storage.DeleteFile(minioMetaBucket, fsAppendMetaPath)
|
|
|
|
} else {
|
|
|
|
tempObj := path.Join(tmpMetaPrefix, uploadID+"-"+"part.1")
|
|
|
|
|
|
|
|
// Allocate staging buffer.
|
|
|
|
var buf = make([]byte, readSizeV1)
|
|
|
|
|
|
|
|
// Loop through all parts, validate them and then commit to disk.
|
|
|
|
for i, part := range parts {
|
|
|
|
partIdx := fsMeta.ObjectPartIndex(part.PartNumber)
|
|
|
|
if partIdx == -1 {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(InvalidPart{})
|
2016-06-28 23:51:49 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
if fsMeta.Parts[partIdx].ETag != part.ETag {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(BadDigest{})
|
2016-06-25 12:03:27 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
// All parts except the last part has to be atleast 5MB.
|
|
|
|
if (i < len(parts)-1) && !isMinAllowedPartSize(fsMeta.Parts[partIdx].Size) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(PartTooSmall{
|
2016-08-31 22:42:57 +02:00
|
|
|
PartNumber: part.PartNumber,
|
|
|
|
PartSize: fsMeta.Parts[partIdx].Size,
|
|
|
|
PartETag: part.ETag,
|
2016-08-25 18:39:01 +02:00
|
|
|
})
|
2016-06-24 11:06:23 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
// Construct part suffix.
|
|
|
|
partSuffix := fmt.Sprintf("object%d", part.PartNumber)
|
|
|
|
multipartPartFile := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
offset := int64(0)
|
|
|
|
totalLeft := fsMeta.Parts[partIdx].Size
|
|
|
|
for totalLeft > 0 {
|
|
|
|
curLeft := int64(readSizeV1)
|
|
|
|
if totalLeft < readSizeV1 {
|
|
|
|
curLeft = totalLeft
|
|
|
|
}
|
|
|
|
var n int64
|
|
|
|
n, err = fs.storage.ReadFile(minioMetaBucket, multipartPartFile, offset, buf[:curLeft])
|
|
|
|
if n > 0 {
|
|
|
|
if err = fs.storage.AppendFile(minioMetaBucket, tempObj, buf[:n]); err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), minioMetaBucket, tempObj)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2016-06-24 11:06:23 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err == errFileNotFound {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", traceError(InvalidPart{})
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), minioMetaBucket, multipartPartFile)
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
2016-08-31 22:42:57 +02:00
|
|
|
offset += n
|
|
|
|
totalLeft -= n
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// Rename the file back to original location, if not delete the temporary object.
|
|
|
|
err = fs.storage.RenameFile(minioMetaBucket, tempObj, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
if dErr := fs.storage.DeleteFile(minioMetaBucket, tempObj); dErr != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(dErr), minioMetaBucket, tempObj)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), bucket, object)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 02:31:14 +02:00
|
|
|
// No need to save part info, since we have concatenated all parts.
|
|
|
|
fsMeta.Parts = nil
|
|
|
|
|
|
|
|
// Save additional metadata only if extended headers such as "X-Amz-Meta-" are set.
|
|
|
|
if hasExtendedHeader(fsMeta.Meta) {
|
2016-08-16 01:09:08 +02:00
|
|
|
if len(fsMeta.Meta) == 0 {
|
|
|
|
fsMeta.Meta = make(map[string]string)
|
|
|
|
}
|
2016-07-22 02:31:14 +02:00
|
|
|
fsMeta.Meta["md5Sum"] = s3MD5
|
2016-08-16 00:02:19 +02:00
|
|
|
|
2016-08-25 18:39:01 +02:00
|
|
|
fsMetaPath := path.Join(bucketMetaPrefix, bucket, object, fsMetaJSONFile)
|
|
|
|
// Write the metadata to a temp file and rename it to the actual location.
|
2016-08-31 22:42:57 +02:00
|
|
|
if err = writeFSMetadata(fs.storage, minioMetaBucket, fsMetaPath, fsMeta); err != nil {
|
2016-07-22 02:31:14 +02:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
// Cleanup all the parts if everything else has been safely committed.
|
|
|
|
if err = cleanupUploadedParts(bucket, object, uploadID, fs.storage); err != nil {
|
2016-09-15 06:24:54 +02:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID = getOpsID()
|
|
|
|
|
2016-06-03 00:54:00 +02:00
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads do not
|
|
|
|
// leave a stale uploads.json behind.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object), opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object), opsID)
|
2016-06-03 00:54:00 +02:00
|
|
|
|
|
|
|
// Validate if there are other incomplete upload-id's present for
|
|
|
|
// the object, if yes do not attempt to delete 'uploads.json'.
|
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, fs.storage)
|
|
|
|
if err != nil {
|
2016-09-15 06:24:54 +02:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, object)
|
2016-06-03 00:54:00 +02:00
|
|
|
}
|
|
|
|
// If we have successfully read `uploads.json`, then we proceed to
|
|
|
|
// purge or update `uploads.json`.
|
|
|
|
uploadIDIdx := uploadsJSON.Index(uploadID)
|
|
|
|
if uploadIDIdx != -1 {
|
|
|
|
uploadsJSON.Uploads = append(uploadsJSON.Uploads[:uploadIDIdx], uploadsJSON.Uploads[uploadIDIdx+1:]...)
|
|
|
|
}
|
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
2016-06-29 11:10:40 +02:00
|
|
|
if err = fs.updateUploadsJSON(bucket, object, uploadsJSON); err != nil {
|
2016-09-15 06:24:54 +02:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
2016-06-03 00:54:00 +02:00
|
|
|
}
|
|
|
|
// Return success.
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fs.storage.DeleteFile(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)); err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return "", toObjectErr(traceError(err), minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
2016-06-03 00:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
// Return md5sum.
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// abortMultipartUpload - wrapper for purging an ongoing multipart
|
|
|
|
// transaction, deletes uploadID entry from `uploads.json` and purges
|
2016-09-07 05:31:50 +02:00
|
|
|
// the directory at '.minio.sys/multipart/bucket/object/uploadID' holding
|
2016-06-02 21:18:56 +02:00
|
|
|
// all the upload parts.
|
2016-05-31 01:51:59 +02:00
|
|
|
func (fs fsObjects) abortMultipartUpload(bucket, object, uploadID string) error {
|
2016-05-21 05:48:47 +02:00
|
|
|
// Cleanup all uploaded parts.
|
|
|
|
if err := cleanupUploadedParts(bucket, object, uploadID, fs.storage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 23:43:17 +02:00
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, fs.storage)
|
2016-05-21 05:48:47 +02:00
|
|
|
if err == nil {
|
2016-05-26 23:43:17 +02:00
|
|
|
uploadIDIdx := uploadsJSON.Index(uploadID)
|
2016-05-21 05:48:47 +02:00
|
|
|
if uploadIDIdx != -1 {
|
2016-05-26 23:43:17 +02:00
|
|
|
uploadsJSON.Uploads = append(uploadsJSON.Uploads[:uploadIDIdx], uploadsJSON.Uploads[uploadIDIdx+1:]...)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
// There are pending uploads for the same object, preserve
|
|
|
|
// them update 'uploads.json' in-place.
|
2016-05-26 23:43:17 +02:00
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
2016-06-29 11:10:40 +02:00
|
|
|
err = fs.updateUploadsJSON(bucket, object, uploadsJSON)
|
2016-05-26 23:43:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
} // No more pending uploads for the object, we purge the entire
|
2016-09-07 05:31:50 +02:00
|
|
|
// entry at '.minio.sys/multipart/bucket/object'.
|
2016-05-21 05:48:47 +02:00
|
|
|
if err = fs.storage.DeleteFile(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)); err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return toObjectErr(traceError(err), minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// AbortMultipartUpload - aborts an ongoing multipart operation
|
|
|
|
// signified by the input uploadID. This is an atomic operation
|
|
|
|
// doesn't require clients to initiate multiple such requests.
|
|
|
|
//
|
|
|
|
// All parts are purged from all disks and reference to the uploadID
|
|
|
|
// would be removed from the system, rollback is not possible on this
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
// Implements S3 compatible Abort multipart API, slight difference is
|
|
|
|
// that this is an atomic idempotent operation. Subsequent calls have
|
|
|
|
// no affect and further requests to the same uploadID would not be
|
|
|
|
// honored.
|
2016-05-21 05:48:47 +02:00
|
|
|
func (fs fsObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
2016-06-02 21:18:56 +02:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return traceError(BucketNameInvalid{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return traceError(BucketNotFound{Bucket: bucket})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return traceError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 03:28:46 +02:00
|
|
|
// get a random ID for lock instrumentation.
|
2016-08-31 20:39:08 +02:00
|
|
|
opsID := getOpsID()
|
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
// Hold lock so that there is no competing complete-multipart-upload or put-object-part.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID), opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID), opsID)
|
2016-06-02 21:18:56 +02:00
|
|
|
|
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return traceError(InvalidUploadID{UploadID: uploadID})
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
fsAppendMetaPath := getFSAppendMetaPath(uploadID)
|
|
|
|
// Lock fs-append.json so that no parallel appendParts() is being done.
|
2016-08-31 20:39:08 +02:00
|
|
|
nsMutex.Lock(minioMetaBucket, fsAppendMetaPath, opsID)
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, fsAppendMetaPath, opsID)
|
2016-08-31 22:42:57 +02:00
|
|
|
|
2016-06-02 21:18:56 +02:00
|
|
|
err := fs.abortMultipartUpload(bucket, object, uploadID)
|
|
|
|
return err
|
2016-05-03 01:57:31 +02:00
|
|
|
}
|