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"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-10-03 00:51:49 +02:00
|
|
|
"hash"
|
2016-05-03 01:57:31 +02:00
|
|
|
"io"
|
2017-01-17 02:05:00 +01:00
|
|
|
"os"
|
|
|
|
pathutil "path"
|
2017-01-26 21:51:12 +01:00
|
|
|
"runtime"
|
2016-05-04 01:10:24 +02:00
|
|
|
"strings"
|
2016-05-21 05:48:47 +02:00
|
|
|
"time"
|
2016-12-20 04:32:55 +01:00
|
|
|
|
2017-01-26 21:51:12 +01:00
|
|
|
"github.com/minio/minio/pkg/lock"
|
2016-12-20 04:32:55 +01:00
|
|
|
"github.com/minio/sha256-simd"
|
2016-05-03 01:57:31 +02:00
|
|
|
)
|
|
|
|
|
2017-01-26 21:51:12 +01:00
|
|
|
// Returns if the prefix is a multipart upload.
|
|
|
|
func (fs fsObjects) isMultipartUpload(bucket, prefix string) bool {
|
|
|
|
uploadsIDPath := pathJoin(fs.fsPath, bucket, prefix, uploadsJSONFile)
|
|
|
|
_, err := fsStatFile(uploadsIDPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
errorIf(err, "Unable to access uploads.json "+uploadsIDPath)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete uploads.json file wrapper handling a tricky case on windows.
|
|
|
|
func (fs fsObjects) deleteUploadsJSON(bucket, object, uploadID string) error {
|
|
|
|
timeID := fmt.Sprintf("%X", time.Now().UTC().UnixNano())
|
|
|
|
tmpPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, uploadID+"+"+timeID)
|
|
|
|
|
|
|
|
multipartBucketPath := pathJoin(fs.fsPath, minioMetaMultipartBucket)
|
|
|
|
uploadPath := pathJoin(multipartBucketPath, bucket, object)
|
|
|
|
uploadsMetaPath := pathJoin(uploadPath, uploadsJSONFile)
|
|
|
|
|
|
|
|
// Special case for windows please read through.
|
|
|
|
if runtime.GOOS == globalWindowsOSName {
|
|
|
|
// Ordinarily windows does not permit deletion or renaming of files still
|
|
|
|
// in use, but if all open handles to that file were opened with FILE_SHARE_DELETE
|
|
|
|
// then it can permit renames and deletions of open files.
|
|
|
|
//
|
|
|
|
// There are however some gotchas with this, and it is worth listing them here.
|
|
|
|
// Firstly, Windows never allows you to really delete an open file, rather it is
|
|
|
|
// flagged as delete pending and its entry in its directory remains visible
|
|
|
|
// (though no new file handles may be opened to it) and when the very last
|
|
|
|
// open handle to the file in the system is closed, only then is it truly
|
|
|
|
// deleted. Well, actually only sort of truly deleted, because Windows only
|
|
|
|
// appears to remove the file entry from the directory, but in fact that
|
|
|
|
// entry is merely hidden and actually still exists and attempting to create
|
|
|
|
// a file with the same name will return an access denied error. How long it
|
|
|
|
// silently exists for depends on a range of factors, but put it this way:
|
|
|
|
// if your code loops creating and deleting the same file name as you might
|
|
|
|
// when operating a lock file, you're going to see lots of random spurious
|
|
|
|
// access denied errors and truly dismal lock file performance compared to POSIX.
|
|
|
|
//
|
|
|
|
// We work-around these un-POSIX file semantics by taking a dual step to
|
|
|
|
// deleting files. Firstly, it renames the file to tmp location into multipartTmpBucket
|
|
|
|
// We always open files with FILE_SHARE_DELETE permission enabled, with that
|
|
|
|
// flag Windows permits renaming and deletion, and because the name was changed
|
|
|
|
// to a very random name somewhere not in its origin directory before deletion,
|
|
|
|
// you don't see those unexpected random errors when creating files with the
|
|
|
|
// same name as a recently deleted file as you do anywhere else on Windows.
|
|
|
|
// Because the file is probably not in its original containing directory any more,
|
|
|
|
// deletions of that directory will not fail with "directory not empty" as they
|
|
|
|
// otherwise normally would either.
|
|
|
|
fsRenameFile(uploadsMetaPath, tmpPath)
|
|
|
|
|
|
|
|
// Proceed to deleting the directory.
|
|
|
|
if err := fsDeleteFile(multipartBucketPath, uploadPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally delete the renamed file.
|
|
|
|
return fsDeleteFile(pathutil.Dir(tmpPath), tmpPath)
|
|
|
|
}
|
|
|
|
return fsDeleteFile(multipartBucketPath, uploadsMetaPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the uploadID, called either by CompleteMultipart of AbortMultipart. If the resuling uploads
|
|
|
|
// slice is empty then we remove/purge the file.
|
|
|
|
func (fs fsObjects) removeUploadID(bucket, object, uploadID string, rwlk *lock.LockedFile) error {
|
|
|
|
uploadIDs := uploadsV1{}
|
|
|
|
_, err := uploadIDs.ReadFrom(rwlk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes upload id from the uploads list.
|
|
|
|
uploadIDs.RemoveUploadID(uploadID)
|
|
|
|
|
|
|
|
// Check this is the last entry.
|
|
|
|
if uploadIDs.IsEmpty() {
|
|
|
|
// No more uploads left, so we delete `uploads.json` file.
|
|
|
|
return fs.deleteUploadsJSON(bucket, object, uploadID)
|
|
|
|
} // else not empty
|
|
|
|
|
|
|
|
// Write update `uploads.json`.
|
|
|
|
_, err = uploadIDs.WriteTo(rwlk)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a new uploadID if no previous `uploads.json` is
|
|
|
|
// found we initialize a new one.
|
|
|
|
func (fs fsObjects) addUploadID(bucket, object, uploadID string, initiated time.Time, rwlk *lock.LockedFile) error {
|
|
|
|
uploadIDs := uploadsV1{}
|
|
|
|
|
|
|
|
_, err := uploadIDs.ReadFrom(rwlk)
|
|
|
|
// For all unexpected errors, we return.
|
|
|
|
if err != nil && errorCause(err) != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we couldn't read anything, we assume a default
|
|
|
|
// (empty) upload info.
|
|
|
|
if errorCause(err) == io.EOF {
|
|
|
|
uploadIDs = newUploadsV1("fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds new upload id to the list.
|
|
|
|
uploadIDs.AddUploadID(uploadID, initiated)
|
|
|
|
|
|
|
|
// Write update `uploads.json`.
|
|
|
|
_, err = uploadIDs.WriteTo(rwlk)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// listMultipartUploadIDs - list all the upload ids from a marker up to 'count'.
|
|
|
|
func (fs fsObjects) listMultipartUploadIDs(bucketName, objectName, uploadIDMarker string, count int) ([]uploadMetadata, bool, error) {
|
|
|
|
var uploads []uploadMetadata
|
|
|
|
|
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, pathJoin(bucketName, objectName))
|
|
|
|
objectMPartPathLock.RLock()
|
|
|
|
defer objectMPartPathLock.RUnlock()
|
|
|
|
|
|
|
|
uploadsPath := pathJoin(bucketName, objectName, uploadsJSONFile)
|
|
|
|
rlk, err := fs.rwPool.Open(pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadsPath))
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
return nil, true, nil
|
|
|
|
}
|
|
|
|
return nil, false, traceError(err)
|
|
|
|
}
|
|
|
|
defer fs.rwPool.Close(pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadsPath))
|
|
|
|
|
|
|
|
// Read `uploads.json`.
|
|
|
|
uploadIDs := uploadsV1{}
|
2017-01-25 21:29:06 +01:00
|
|
|
if _, err = uploadIDs.ReadFrom(rlk.LockedFile); err != nil {
|
2017-01-17 02:05:00 +01:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
for ; index < len(uploadIDs.Uploads); index++ {
|
|
|
|
if uploadIDs.Uploads[index].UploadID == uploadIDMarker {
|
|
|
|
// Skip the uploadID as it would already be listed in previous listing.
|
|
|
|
index++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for index < len(uploadIDs.Uploads) {
|
|
|
|
uploads = append(uploads, uploadMetadata{
|
|
|
|
Object: objectName,
|
|
|
|
UploadID: uploadIDs.Uploads[index].UploadID,
|
|
|
|
Initiated: uploadIDs.Uploads[index].Initiated,
|
|
|
|
})
|
|
|
|
count--
|
|
|
|
index++
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end := (index == len(uploadIDs.Uploads))
|
|
|
|
return uploads, end, nil
|
|
|
|
}
|
|
|
|
|
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-11-22 22:15:06 +01:00
|
|
|
multipartPrefixPath := pathJoin(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-11-22 22:15:06 +01:00
|
|
|
multipartMarkerPath = pathJoin(bucket, keyMarker)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
var uploads []uploadMetadata
|
|
|
|
var err error
|
|
|
|
var eof bool
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
if uploadIDMarker != "" {
|
2017-01-17 02:05:00 +01:00
|
|
|
uploads, _, err = fs.listMultipartUploadIDs(bucket, keyMarker, uploadIDMarker, maxUploads)
|
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
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-07-07 18:06:35 +02:00
|
|
|
var walkResultCh chan treeWalkResult
|
|
|
|
var endWalkCh chan struct{}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// true only for xl.ListObjectsHeal(), set to false.
|
|
|
|
heal := false
|
|
|
|
|
|
|
|
// Proceed to list only if we have more uploads to be listed.
|
2016-05-26 23:43:17 +02:00
|
|
|
if maxUploads > 0 {
|
2017-01-17 02:05:00 +01:00
|
|
|
listPrms := listParams{minioMetaMultipartBucket, recursive, multipartMarkerPath, multipartPrefixPath, heal}
|
|
|
|
|
|
|
|
// Pop out any previously waiting marker.
|
|
|
|
walkResultCh, endWalkCh = fs.listPool.Release(listPrms)
|
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
|
2017-01-17 02:05:00 +01:00
|
|
|
listDir := fs.listDirFactory(isLeaf)
|
|
|
|
walkResultCh = startTreeWalk(minioMetaMultipartBucket, multipartPrefixPath,
|
|
|
|
multipartMarkerPath, recursive, listDir, isLeaf, endWalkCh)
|
2016-05-26 23:43:17 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// List until maxUploads requested.
|
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
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
|
|
|
// File not found or Disk not found is a valid case.
|
2016-11-24 05:05:04 +01: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
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-11-22 22:15:06 +01:00
|
|
|
entry := strings.TrimPrefix(walkResult.entry, retainSlash(bucket))
|
2016-05-26 23:43:17 +02:00
|
|
|
if strings.HasSuffix(walkResult.entry, slashSeparator) {
|
|
|
|
uploads = append(uploads, uploadMetadata{
|
|
|
|
Object: entry,
|
|
|
|
})
|
|
|
|
maxUploads--
|
|
|
|
if maxUploads == 0 {
|
|
|
|
if walkResult.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
var tmpUploads []uploadMetadata
|
|
|
|
var end bool
|
|
|
|
uploadIDMarker = ""
|
2016-08-31 20:39:08 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
tmpUploads, end, err = fs.listMultipartUploadIDs(bucket, entry, uploadIDMarker, maxUploads)
|
2016-05-26 23:43:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
uploads = append(uploads, tmpUploads...)
|
|
|
|
maxUploads -= len(tmpUploads)
|
|
|
|
if walkResult.end && end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-26 23:43:17 +02:00
|
|
|
// 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 = ""
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// Success.
|
2016-05-04 01:10:24 +02:00
|
|
|
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-12-02 08:15:17 +01:00
|
|
|
if err := checkListMultipartArgs(bucket, prefix, keyMarker, uploadIDMarker, delimiter, fs); err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return ListMultipartsInfo{}, toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
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-11-12 01:36:07 +01:00
|
|
|
// Save additional metadata.
|
|
|
|
fsMeta.Meta = meta
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2016-11-23 01:52:37 +01:00
|
|
|
uploadID = mustGetUUID()
|
2016-06-02 21:18:56 +02:00
|
|
|
initiated := time.Now().UTC()
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-11-23 00:29:39 +01:00
|
|
|
// Add upload ID to uploads.json
|
2017-01-17 02:05:00 +01:00
|
|
|
uploadsPath := pathJoin(bucket, object, uploadsJSONFile)
|
|
|
|
rwlk, err := fs.rwPool.Create(pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadsPath))
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(traceError(err), bucket, object)
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
metaFile, err := fs.rwPool.Create(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
defer metaFile.Close()
|
|
|
|
|
|
|
|
// Add a new upload id.
|
|
|
|
if err = fs.addUploadID(bucket, object, uploadID, initiated, rwlk); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all the set metadata.
|
|
|
|
if _, err = fsMeta.WriteTo(metaFile); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
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) {
|
2016-12-02 08:15:17 +01:00
|
|
|
if err := checkNewMultipartArgs(bucket, object, fs); err != nil {
|
|
|
|
return "", err
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, pathJoin(bucket, object))
|
|
|
|
objectMPartPathLock.Lock()
|
|
|
|
defer objectMPartPathLock.Unlock()
|
|
|
|
|
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
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// 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
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
return fsMeta.Parts[nextPartIndex], true
|
|
|
|
}
|
|
|
|
|
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-12-02 08:15:17 +01:00
|
|
|
if err := checkPutObjectPartArgs(bucket, object, fs); err != nil {
|
|
|
|
return "", err
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return "", toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, pathJoin(bucket, object))
|
|
|
|
objectMPartPathLock.Lock()
|
|
|
|
defer objectMPartPathLock.Unlock()
|
|
|
|
|
|
|
|
// Disallow any parallel abort or complete multipart operations.
|
|
|
|
uploadsPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object, uploadsJSONFile)
|
|
|
|
if _, err := fs.rwPool.Open(uploadsPath); err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
return "", traceError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return "", toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
defer fs.rwPool.Close(uploadsPath)
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
2016-06-03 00:19:13 +02:00
|
|
|
|
|
|
|
// Just check if the uploadID exists to avoid copy if it doesn't.
|
2017-01-17 02:05:00 +01:00
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
rwlk, err := fs.rwPool.Write(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
return "", traceError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return "", toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
fsMeta := fsMetaV1{}
|
2017-01-25 21:29:06 +01:00
|
|
|
_, err = fsMeta.ReadFrom(rwlk)
|
2017-01-17 02:05:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaMultipartBucket, fsMetaPath)
|
2016-06-03 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
partSuffix := fmt.Sprintf("object%d", partID)
|
2016-11-23 01:52:37 +01:00
|
|
|
tmpPartPath := uploadID + "." + mustGetUUID() + "." + 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...)
|
2017-01-17 02:05:00 +01:00
|
|
|
|
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))
|
2016-10-29 21:44:44 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
fsPartPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, tmpPartPath)
|
|
|
|
bytesWritten, cErr := fsCreateFile(fsPartPath, teeReader, buf, size)
|
2016-07-19 04:06:48 +02:00
|
|
|
if cErr != nil {
|
2017-01-17 02:05:00 +01:00
|
|
|
fsRemoveFile(fsPartPath)
|
2016-11-20 23:25:43 +01:00
|
|
|
return "", toObjectErr(cErr, minioMetaTmpBucket, tmpPartPath)
|
2016-07-19 04:06:48 +02:00
|
|
|
}
|
2016-12-03 20:53:12 +01:00
|
|
|
|
2016-07-19 04:06:48 +02:00
|
|
|
// Should return IncompleteBody{} error when reader has fewer
|
|
|
|
// bytes than specified in request header.
|
|
|
|
if bytesWritten < size {
|
2017-01-17 02:05:00 +01:00
|
|
|
fsRemoveFile(fsPartPath)
|
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.
|
2017-01-17 02:05:00 +01:00
|
|
|
defer fsRemoveFile(fsPartPath)
|
2016-10-20 07:52:03 +02:00
|
|
|
|
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{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
partPath := pathJoin(bucket, object, uploadID, partSuffix)
|
2016-11-23 21:50:09 +01:00
|
|
|
// Lock the part so that another part upload with same part-number gets blocked
|
|
|
|
// while the part is getting appended in the background.
|
2016-12-11 01:15:12 +01:00
|
|
|
partLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, partPath)
|
2016-11-23 21:50:09 +01:00
|
|
|
partLock.Lock()
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
fsNSPartPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, partPath)
|
|
|
|
if err = fsRenameFile(fsPartPath, fsNSPartPath); err != nil {
|
2016-11-23 21:50:09 +01:00
|
|
|
partLock.Unlock()
|
2017-01-17 02:05:00 +01:00
|
|
|
return "", toObjectErr(err, minioMetaMultipartBucket, partPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// Save the object part info in `fs.json`.
|
|
|
|
fsMeta.AddObjectPart(partID, partSuffix, newMD5Hex, size)
|
|
|
|
if _, err = fsMeta.WriteTo(rwlk); err != nil {
|
2016-11-23 21:50:09 +01:00
|
|
|
partLock.Unlock()
|
2016-11-22 22:15:06 +01:00
|
|
|
return "", toObjectErr(err, minioMetaMultipartBucket, uploadIDPath)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-11-21 08:42:53 +01:00
|
|
|
|
2016-12-10 01:10:18 +01:00
|
|
|
// Append the part in background.
|
2017-01-17 02:05:00 +01:00
|
|
|
errCh := fs.append(bucket, object, uploadID, fsMeta)
|
2016-11-23 21:50:09 +01:00
|
|
|
go func() {
|
2017-01-17 02:05:00 +01:00
|
|
|
// Also receive the error so that the appendParts go-routine
|
|
|
|
// does not block on send. But the error received is ignored
|
|
|
|
// as fs.PutObjectPart() would have already returned success
|
|
|
|
// to the client.
|
2016-11-23 21:50:09 +01:00
|
|
|
<-errCh
|
|
|
|
partLock.Unlock()
|
|
|
|
}()
|
2016-11-21 08:42:53 +01:00
|
|
|
|
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
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
metaFile, err := fs.rwPool.Open(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
// On windows oddly this is returned.
|
|
|
|
return ListPartsInfo{}, traceError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return ListPartsInfo{}, toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
defer fs.rwPool.Close(fsMetaPath)
|
|
|
|
|
|
|
|
fsMeta := fsMetaV1{}
|
2017-01-25 21:29:06 +01:00
|
|
|
_, err = fsMeta.ReadFrom(metaFile.LockedFile)
|
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
|
|
|
}
|
2017-01-17 02:05:00 +01: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:]
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
count := maxParts
|
2016-05-25 06:24:20 +02:00
|
|
|
for _, part := range parts {
|
2017-01-17 02:05:00 +01:00
|
|
|
var fi os.FileInfo
|
|
|
|
partNamePath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, part.Name)
|
|
|
|
fi, err = fsStatFile(partNamePath)
|
2016-05-04 01:10:24 +02:00
|
|
|
if err != nil {
|
2017-01-27 00:40:10 +01:00
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaMultipartBucket, 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,
|
2017-01-17 02:05:00 +01: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
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
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
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// Success.
|
2016-05-04 01:10:24 +02:00
|
|
|
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-12-02 08:15:17 +01:00
|
|
|
if err := checkListPartsArgs(bucket, object, fs); err != nil {
|
|
|
|
return ListPartsInfo{}, err
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
2016-08-31 20:39:08 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return ListPartsInfo{}, toObjectErr(err, bucket)
|
2016-05-04 01:10:24 +02:00
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, pathJoin(bucket, object))
|
|
|
|
objectMPartPathLock.RLock()
|
|
|
|
defer objectMPartPathLock.RUnlock()
|
|
|
|
|
|
|
|
listPartsInfo, err := fs.listObjectParts(bucket, object, uploadID, partNumberMarker, maxParts)
|
|
|
|
if err != nil {
|
|
|
|
return ListPartsInfo{}, toObjectErr(err, bucket, object)
|
2016-10-29 21:44:44 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
// Success.
|
|
|
|
return listPartsInfo, nil
|
2016-10-29 21:44:44 +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.
|
2017-01-17 04:23:43 +01:00
|
|
|
func (fs fsObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, error) {
|
2016-12-02 08:15:17 +01:00
|
|
|
if err := checkCompleteMultipartArgs(bucket, object, fs); err != nil {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, err
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
2016-11-10 16:41:02 +01:00
|
|
|
s3MD5, err := getCompleteMultipartMD5(parts)
|
2016-05-21 05:48:47 +02:00
|
|
|
if err != nil {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, err
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
|
|
|
|
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, pathJoin(bucket, object))
|
|
|
|
objectMPartPathLock.Lock()
|
|
|
|
defer objectMPartPathLock.Unlock()
|
|
|
|
|
|
|
|
fsMetaPathMultipart := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
rlk, err := fs.rwPool.Open(fsMetaPathMultipart)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), bucket, object)
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow any parallel abort or complete multipart operations.
|
|
|
|
rwlk, err := fs.rwPool.Write(pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object, uploadsJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), bucket, object)
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
fsMeta := fsMetaV1{}
|
2016-08-31 22:42:57 +02:00
|
|
|
// Read saved fs metadata for ongoing multipart.
|
2017-01-25 21:29:06 +01:00
|
|
|
_, err = fsMeta.ReadFrom(rlk.LockedFile)
|
2016-08-31 22:42:57 +02:00
|
|
|
if err != nil {
|
2017-01-17 02:05:00 +01:00
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, minioMetaMultipartBucket, fsMetaPathMultipart)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Wait for any competing PutObject() operation on bucket/object, since same namespace
|
|
|
|
// would be acquired for `fs.json`.
|
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaBucket, bucketMetaPrefix, bucket, object, fsMetaJSONFile)
|
|
|
|
metaFile, err := fs.rwPool.Create(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), bucket, object)
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
|
|
|
defer metaFile.Close()
|
|
|
|
|
2017-01-17 04:23:43 +01:00
|
|
|
fsNSObjPath := pathJoin(fs.fsPath, bucket, object)
|
|
|
|
|
2016-11-30 08:26:36 +01:00
|
|
|
// This lock is held during rename of the appended tmp file to the actual
|
|
|
|
// location so that any competing GetObject/PutObject/DeleteObject do not race.
|
2016-11-23 12:04:04 +01:00
|
|
|
appendFallback := true // In case background-append did not append the required parts.
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-11-21 08:42:53 +01:00
|
|
|
if isPartsSame(fsMeta.Parts, parts) {
|
2017-01-17 02:05:00 +01:00
|
|
|
err = fs.complete(bucket, object, uploadID, fsMeta)
|
2016-11-21 08:42:53 +01:00
|
|
|
if err == nil {
|
|
|
|
appendFallback = false
|
2017-01-17 02:05:00 +01:00
|
|
|
fsTmpObjPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, uploadID)
|
|
|
|
if err = fsRenameFile(fsTmpObjPath, fsNSObjPath); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, minioMetaTmpBucket, uploadID)
|
2016-11-21 08:42:53 +01:00
|
|
|
}
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
2016-11-21 08:42:53 +01:00
|
|
|
}
|
2016-11-01 08:56:03 +01:00
|
|
|
|
2016-11-21 08:42:53 +01:00
|
|
|
if appendFallback {
|
2016-11-23 12:04:04 +01:00
|
|
|
// background append could not do append all the required parts, hence we do it here.
|
|
|
|
tempObj := uploadID + "-" + "part.1"
|
2016-08-31 22:42:57 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
fsTmpObjPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, tempObj)
|
|
|
|
// Delete the temporary object in the case of a
|
|
|
|
// failure. If PutObject succeeds, then there would be
|
|
|
|
// nothing to delete.
|
|
|
|
defer fsRemoveFile(fsTmpObjPath)
|
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// Allocate staging buffer.
|
|
|
|
var buf = make([]byte, readSizeV1)
|
2016-10-29 21:44:44 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Validate all parts and then commit to disk.
|
2016-08-31 22:42:57 +02:00
|
|
|
for i, part := range parts {
|
|
|
|
partIdx := fsMeta.ObjectPartIndex(part.PartNumber)
|
|
|
|
if partIdx == -1 {
|
2017-01-17 02:05:00 +01:00
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, traceError(InvalidPart{})
|
2016-06-28 23:51:49 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
if fsMeta.Parts[partIdx].ETag != part.ETag {
|
2017-01-17 02:05:00 +01:00
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, traceError(BadDigest{})
|
2016-06-25 12:03:27 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01: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) {
|
2017-01-17 02:05:00 +01:00
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, 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
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
2016-08-31 22:42:57 +02:00
|
|
|
// Construct part suffix.
|
|
|
|
partSuffix := fmt.Sprintf("object%d", part.PartNumber)
|
2017-01-17 02:05:00 +01:00
|
|
|
multipartPartFile := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, partSuffix)
|
|
|
|
|
|
|
|
var reader io.ReadCloser
|
2016-08-31 22:42:57 +02:00
|
|
|
offset := int64(0)
|
2017-01-17 02:05:00 +01:00
|
|
|
reader, _, err = fsOpenFile(multipartPartFile, offset)
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
if err == errFileNotFound {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, traceError(InvalidPart{})
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), minioMetaMultipartBucket, partSuffix)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// No need to hold a lock, this is a unique file and will be only written
|
|
|
|
// to one one process per uploadID per minio process.
|
|
|
|
var wfile *os.File
|
|
|
|
wfile, err = os.OpenFile(preparePath(fsTmpObjPath), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
reader.Close()
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), bucket, object)
|
2016-08-31 22:42:57 +02:00
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
|
|
|
|
_, err = io.CopyBuffer(wfile, reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
wfile.Close()
|
|
|
|
reader.Close()
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(traceError(err), bucket, object)
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wfile.Close()
|
|
|
|
reader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fsRenameFile(fsTmpObjPath, fsNSObjPath); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, minioMetaTmpBucket, uploadID)
|
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
|
|
|
|
|
2016-11-12 01:36:07 +01:00
|
|
|
// Save additional metadata.
|
|
|
|
if len(fsMeta.Meta) == 0 {
|
|
|
|
fsMeta.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
fsMeta.Meta["md5Sum"] = s3MD5
|
2016-08-16 00:02:19 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Write all the set metadata.
|
|
|
|
if _, err = fsMeta.WriteTo(metaFile); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-07-22 02:31:14 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Close lock held on bucket/object/uploadid/fs.json,
|
|
|
|
// this needs to be done for windows so that we can happily
|
|
|
|
// delete the bucket/object/uploadid
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
|
2016-05-21 05:48:47 +02:00
|
|
|
// Cleanup all the parts if everything else has been safely committed.
|
2017-01-17 02:05:00 +01:00
|
|
|
multipartObjectDir := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object)
|
|
|
|
multipartUploadIDDir := pathJoin(multipartObjectDir, uploadID)
|
|
|
|
if err = fsRemoveUploadIDPath(multipartObjectDir, multipartUploadIDDir); err != nil {
|
2017-01-27 00:40:10 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Remove entry from `uploads.json`.
|
|
|
|
if err = fs.removeUploadID(bucket, object, uploadID, rwlk); err != nil {
|
2017-01-17 04:23:43 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, minioMetaMultipartBucket, pathutil.Join(bucket, object))
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := fsStatFile(fsNSObjPath)
|
|
|
|
if err != nil {
|
2017-01-27 00:40:10 +01:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-06-03 00:54:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 04:23:43 +01:00
|
|
|
// Return object info.
|
|
|
|
return fsMeta.ToObjectInfo(bucket, object, fi), nil
|
2016-05-21 05:48:47 +02:00
|
|
|
}
|
|
|
|
|
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-12-02 08:15:17 +01:00
|
|
|
if err := checkAbortMultipartArgs(bucket, object, fs); err != nil {
|
|
|
|
return err
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
|
|
|
|
|
|
|
// Hold the lock so that two parallel complete-multipart-uploads
|
|
|
|
// do not leave a stale uploads.json behind.
|
|
|
|
objectMPartPathLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket,
|
|
|
|
pathJoin(bucket, object))
|
|
|
|
objectMPartPathLock.Lock()
|
|
|
|
defer objectMPartPathLock.Unlock()
|
|
|
|
|
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
if _, err := fs.rwPool.Open(fsMetaPath); err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
return traceError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadsPath := pathJoin(bucket, object, uploadsJSONFile)
|
|
|
|
rwlk, err := fs.rwPool.Write(pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadsPath))
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPath)
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
|
|
|
return traceError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return toObjectErr(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
// Signal appendParts routine to stop waiting for new parts to arrive.
|
|
|
|
fs.abort(uploadID)
|
|
|
|
|
|
|
|
// Close lock held on bucket/object/uploadid/fs.json,
|
|
|
|
// this needs to be done for windows so that we can happily
|
|
|
|
// delete the bucket/object/uploadid
|
|
|
|
fs.rwPool.Close(fsMetaPath)
|
|
|
|
|
|
|
|
// Cleanup all uploaded parts and abort the upload.
|
|
|
|
multipartObjectDir := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object)
|
|
|
|
multipartUploadIDDir := pathJoin(multipartObjectDir, uploadID)
|
|
|
|
if err = fsRemoveUploadIDPath(multipartObjectDir, multipartUploadIDDir); err != nil {
|
2017-01-27 00:40:10 +01:00
|
|
|
return toObjectErr(err, bucket, object)
|
2017-01-17 02:05:00 +01:00
|
|
|
}
|
2016-06-02 21:18:56 +02:00
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Remove entry from `uploads.json`.
|
|
|
|
if err = fs.removeUploadID(bucket, object, uploadID, rwlk); err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
2016-06-02 21:18:56 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
return nil
|
2016-05-03 01:57:31 +02:00
|
|
|
}
|