2016-05-02 19:57:31 -04:00
|
|
|
/*
|
2017-03-18 14:28:41 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-05-02 19:57:31 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-02 19:57:31 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-01-16 20:05:00 -05:00
|
|
|
"os"
|
|
|
|
pathutil "path"
|
2016-05-03 19:10:24 -04:00
|
|
|
"strings"
|
2016-05-20 23:48:47 -04:00
|
|
|
"time"
|
2016-12-19 22:32:55 -05:00
|
|
|
|
2017-01-26 15:51:12 -05:00
|
|
|
"github.com/minio/minio/pkg/lock"
|
2016-05-02 19:57:31 -04:00
|
|
|
)
|
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
const (
|
2017-08-10 17:11:57 -04:00
|
|
|
// Expiry duration after which the multipart uploads are deemed stale.
|
|
|
|
fsMultipartExpiry = time.Hour * 24 * 14 // 2 weeks.
|
|
|
|
// Cleanup interval when the stale multipart cleanup is initiated.
|
|
|
|
fsMultipartCleanupInterval = time.Hour * 24 // 24 hrs.
|
2017-08-04 13:45:57 -04:00
|
|
|
)
|
|
|
|
|
2017-01-26 15:51:12 -05: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 {
|
2017-09-14 19:09:12 -04:00
|
|
|
if errorCause(err) == errFileNotFound {
|
2017-01-26 15:51:12 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
errorIf(err, "Unable to access uploads.json "+uploadsIDPath)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-04-14 15:06:24 -04:00
|
|
|
// Delete uploads.json file wrapper
|
2017-01-26 15:51:12 -05:00
|
|
|
func (fs fsObjects) deleteUploadsJSON(bucket, object, uploadID string) error {
|
|
|
|
multipartBucketPath := pathJoin(fs.fsPath, minioMetaMultipartBucket)
|
|
|
|
uploadPath := pathJoin(multipartBucketPath, bucket, object)
|
|
|
|
uploadsMetaPath := pathJoin(uploadPath, uploadsJSONFile)
|
|
|
|
|
2017-04-14 15:06:24 -04:00
|
|
|
tmpDir := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID)
|
2017-01-26 15:51:12 -05:00
|
|
|
|
2017-04-14 15:06:24 -04:00
|
|
|
return fsRemoveMeta(multipartBucketPath, uploadsMetaPath, tmpDir)
|
2017-01-26 15:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the uploadID, called either by CompleteMultipart of AbortMultipart. If the resuling uploads
|
|
|
|
// slice is empty then we remove/purge the file.
|
2017-09-28 11:09:28 -04:00
|
|
|
func (fs fsObjects) removeUploadID(bucket, object, uploadID string, rwlk *lock.LockedFile) (bool, error) {
|
2017-01-26 15:51:12 -05:00
|
|
|
uploadIDs := uploadsV1{}
|
|
|
|
_, err := uploadIDs.ReadFrom(rwlk)
|
|
|
|
if err != nil {
|
2017-09-28 11:09:28 -04:00
|
|
|
return false, err
|
2017-01-26 15:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-09-28 11:09:28 -04:00
|
|
|
return true, fs.deleteUploadsJSON(bucket, object, uploadID)
|
2017-01-26 15:51:12 -05:00
|
|
|
} // else not empty
|
|
|
|
|
|
|
|
// Write update `uploads.json`.
|
|
|
|
_, err = uploadIDs.WriteTo(rwlk)
|
2017-09-28 11:09:28 -04:00
|
|
|
return false, err
|
2017-01-26 15:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-16 20:05:00 -05: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))
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := objectMPartPathLock.GetRLock(globalListingTimeout); err != nil {
|
|
|
|
return nil, false, traceError(err)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
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 15:29:06 -05:00
|
|
|
if _, err = uploadIDs.ReadFrom(rlk.LockedFile); err != nil {
|
2017-01-16 20:05:00 -05: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-30 19:51:59 -04:00
|
|
|
// listMultipartUploads - lists all multipart uploads.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (fs fsObjects) listMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (lmi ListMultipartsInfo, e error) {
|
2016-05-03 19:10:24 -04:00
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
recursive := true
|
|
|
|
if delimiter == slashSeparator {
|
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
result.IsTruncated = true
|
|
|
|
result.MaxUploads = maxUploads
|
2016-05-26 17:43:17 -04:00
|
|
|
result.KeyMarker = keyMarker
|
|
|
|
result.Prefix = prefix
|
|
|
|
result.Delimiter = delimiter
|
2016-05-03 19:10:24 -04:00
|
|
|
|
|
|
|
// Not using path.Join() as it strips off the trailing '/'.
|
2016-11-22 16:15:06 -05:00
|
|
|
multipartPrefixPath := pathJoin(bucket, prefix)
|
2016-05-17 14:44:32 -04:00
|
|
|
if prefix == "" {
|
|
|
|
// Should have a trailing "/" if prefix is ""
|
|
|
|
// For ex. multipartPrefixPath should be "multipart/bucket/" if prefix is ""
|
|
|
|
multipartPrefixPath += slashSeparator
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
multipartMarkerPath := ""
|
|
|
|
if keyMarker != "" {
|
2016-11-22 16:15:06 -05:00
|
|
|
multipartMarkerPath = pathJoin(bucket, keyMarker)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
var uploads []uploadMetadata
|
|
|
|
var err error
|
|
|
|
var eof bool
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
if uploadIDMarker != "" {
|
2017-01-16 20:05:00 -05:00
|
|
|
uploads, _, err = fs.listMultipartUploadIDs(bucket, keyMarker, uploadIDMarker, maxUploads)
|
2016-05-26 17:43:17 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, err
|
2016-05-26 17:43:17 -04:00
|
|
|
}
|
|
|
|
maxUploads = maxUploads - len(uploads)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-07-07 12:06:35 -04:00
|
|
|
var walkResultCh chan treeWalkResult
|
|
|
|
var endWalkCh chan struct{}
|
2017-01-16 20:05:00 -05: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 17:43:17 -04:00
|
|
|
if maxUploads > 0 {
|
2017-01-16 20:05:00 -05:00
|
|
|
listPrms := listParams{minioMetaMultipartBucket, recursive, multipartMarkerPath, multipartPrefixPath, heal}
|
|
|
|
|
|
|
|
// Pop out any previously waiting marker.
|
|
|
|
walkResultCh, endWalkCh = fs.listPool.Release(listPrms)
|
2016-06-24 19:41:57 -04:00
|
|
|
if walkResultCh == nil {
|
|
|
|
endWalkCh = make(chan struct{})
|
2016-07-17 18:16:52 -04:00
|
|
|
isLeaf := fs.isMultipartUpload
|
2017-01-16 20:05:00 -05:00
|
|
|
listDir := fs.listDirFactory(isLeaf)
|
|
|
|
walkResultCh = startTreeWalk(minioMetaMultipartBucket, multipartPrefixPath,
|
|
|
|
multipartMarkerPath, recursive, listDir, isLeaf, endWalkCh)
|
2016-05-26 17:43:17 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// List until maxUploads requested.
|
2016-05-26 17:43:17 -04:00
|
|
|
for maxUploads > 0 {
|
2016-06-24 19:41:57 -04:00
|
|
|
walkResult, ok := <-walkResultCh
|
2016-05-26 17:43:17 -04:00
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04: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-23 23:05:04 -05:00
|
|
|
if isErrIgnored(walkResult.err, fsTreeWalkIgnoredErrs...) {
|
2016-05-26 17:43:17 -04:00
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, walkResult.err
|
2016-05-26 17:43:17 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-11-22 16:15:06 -05:00
|
|
|
entry := strings.TrimPrefix(walkResult.entry, retainSlash(bucket))
|
2017-02-16 17:52:14 -05:00
|
|
|
if hasSuffix(walkResult.entry, slashSeparator) {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploads = append(uploads, uploadMetadata{
|
|
|
|
Object: entry,
|
|
|
|
})
|
|
|
|
maxUploads--
|
|
|
|
if maxUploads == 0 {
|
|
|
|
if walkResult.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
var tmpUploads []uploadMetadata
|
|
|
|
var end bool
|
|
|
|
uploadIDMarker = ""
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
tmpUploads, end, err = fs.listMultipartUploadIDs(bucket, entry, uploadIDMarker, maxUploads)
|
2016-05-26 17:43:17 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, err
|
2016-05-26 17:43:17 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
uploads = append(uploads, tmpUploads...)
|
|
|
|
maxUploads -= len(tmpUploads)
|
|
|
|
if walkResult.end && end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
// Loop through all the received uploads fill in the multiparts result.
|
|
|
|
for _, upload := range uploads {
|
2016-05-03 19:10:24 -04:00
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
2017-02-16 17:52:14 -05:00
|
|
|
if hasSuffix(upload.Object, slashSeparator) {
|
2016-05-03 19:10:24 -04:00
|
|
|
// All directory entries are common prefixes.
|
|
|
|
uploadID = "" // Upload ids are empty for CommonPrefixes.
|
2016-05-26 17:43:17 -04:00
|
|
|
objectName = upload.Object
|
2016-05-03 19:10:24 -04:00
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objectName)
|
|
|
|
} else {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadID = upload.UploadID
|
|
|
|
objectName = upload.Object
|
|
|
|
result.Uploads = append(result.Uploads, upload)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result.NextKeyMarker = objectName
|
|
|
|
result.NextUploadIDMarker = uploadID
|
|
|
|
}
|
2016-07-07 12:06:35 -04: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 14:36:33 -04:00
|
|
|
fs.listPool.Set(listParams{bucket, recursive, result.NextKeyMarker, prefix, heal}, walkResultCh, endWalkCh)
|
2016-07-07 12:06:35 -04:00
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// Success.
|
2016-05-03 19:10:24 -04:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
// ListMultipartUploads - returns empty response always. The onus is
|
|
|
|
// on applications to remember uploadId of the multipart uploads that
|
|
|
|
// are in progress.
|
|
|
|
func (fs fsObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (lmi ListMultipartsInfo, e error) {
|
|
|
|
if err := checkListMultipartArgs(bucket, prefix, keyMarker, uploadIDMarker, delimiter, fs); err != nil {
|
|
|
|
return lmi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return lmi, toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
return ListMultipartsInfo{
|
|
|
|
Prefix: prefix,
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
Delimiter: delimiter,
|
|
|
|
MaxUploads: maxUploads,
|
|
|
|
IsTruncated: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// listMultipartUploadsHelper - lists all the pending multipart uploads on a
|
2016-06-02 15:18:56 -04:00
|
|
|
// 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.
|
|
|
|
//
|
2017-08-04 13:45:57 -04:00
|
|
|
// This function remains here to aid in listing uploads that require cleanup.
|
|
|
|
func (fs fsObjects) listMultipartUploadsHelper(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (lmi ListMultipartsInfo, e error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkListMultipartArgs(bucket, prefix, keyMarker, uploadIDMarker, delimiter, fs); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, err
|
2016-06-02 15:18:56 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, toObjectErr(err, bucket)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
return fs.listMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:18:56 -04: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-06 23:31:50 -04:00
|
|
|
// incoming object at '.minio.sys/multipart/bucket/object/uploads.json' on
|
2016-06-02 15:18:56 -04: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 16:26:08 -04:00
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
// Save additional metadata.
|
|
|
|
fsMeta.Meta = meta
|
2016-06-02 15:18:56 -04:00
|
|
|
|
2016-11-22 19:52:37 -05:00
|
|
|
uploadID = mustGetUUID()
|
2017-03-18 14:28:41 -04:00
|
|
|
initiated := UTCNow()
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
// Add upload ID to uploads.json
|
2017-01-16 20:05:00 -05: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 15:18:56 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05: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 15:18:56 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-06-02 15:18:56 -04:00
|
|
|
// Return success.
|
|
|
|
return uploadID, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:18:56 -04: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 02:15:17 -05:00
|
|
|
if err := checkNewMultipartArgs(bucket, object, fs); err != nil {
|
|
|
|
return "", err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05: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))
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := objectMPartPathLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
defer objectMPartPathLock.Unlock()
|
|
|
|
|
2016-06-02 15:18:56 -04:00
|
|
|
return fs.newMultipartUpload(bucket, object, meta)
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-08-31 16:42:57 -04: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-16 20:05:00 -05:00
|
|
|
|
2016-08-31 16:42:57 -04: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-16 20:05:00 -05:00
|
|
|
|
2016-08-31 16:42:57 -04:00
|
|
|
return fsMeta.Parts[nextPartIndex], true
|
|
|
|
}
|
|
|
|
|
2017-01-31 12:38:34 -05:00
|
|
|
// CopyObjectPart - similar to PutObjectPart but reads data from an existing
|
|
|
|
// object. Internally incoming data is written to '.minio.sys/tmp' location
|
|
|
|
// and safely renamed to '.minio.sys/multipart' for reach parts.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (fs fsObjects) CopyObjectPart(srcBucket, srcObject, dstBucket, dstObject, uploadID string, partID int, startOffset int64, length int64) (pi PartInfo, e error) {
|
2017-01-31 12:38:34 -05:00
|
|
|
if err := checkNewMultipartArgs(srcBucket, srcObject, fs); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, err
|
2017-01-31 12:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize pipe.
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if gerr := fs.GetObject(srcBucket, srcObject, startOffset, length, pipeWriter); gerr != nil {
|
|
|
|
errorIf(gerr, "Unable to read %s/%s.", srcBucket, srcObject)
|
|
|
|
pipeWriter.CloseWithError(gerr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pipeWriter.Close() // Close writer explicitly signalling we wrote all data.
|
|
|
|
}()
|
|
|
|
|
2017-09-19 15:40:27 -04:00
|
|
|
partInfo, err := fs.PutObjectPart(dstBucket, dstObject, uploadID, partID, NewHashReader(pipeReader, length, "", ""))
|
2017-01-31 12:38:34 -05:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, dstBucket, dstObject)
|
2017-01-31 12:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly close the reader.
|
|
|
|
pipeReader.Close()
|
|
|
|
|
|
|
|
return partInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 18:19:13 -04:00
|
|
|
// PutObjectPart - reads incoming data until EOF for the part file on
|
2016-06-02 15:18:56 -04:00
|
|
|
// an ongoing multipart transaction. Internally incoming data is
|
2016-09-06 23:31:50 -04:00
|
|
|
// written to '.minio.sys/tmp' location and safely renamed to
|
|
|
|
// '.minio.sys/multipart' for reach parts.
|
2017-09-19 15:40:27 -04:00
|
|
|
func (fs fsObjects) PutObjectPart(bucket, object, uploadID string, partID int, data *HashReader) (pi PartInfo, e error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkPutObjectPartArgs(bucket, object, fs); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, err
|
2016-06-02 18:19:13 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, bucket)
|
2017-01-16 20:05:00 -05: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))
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := objectMPartPathLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return pi, err
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
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 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
defer fs.rwPool.Close(uploadsPath)
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
2016-06-02 18:19:13 -04:00
|
|
|
|
|
|
|
// Just check if the uploadID exists to avoid copy if it doesn't.
|
2017-01-16 20:05:00 -05:00
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
rwlk, err := fs.rwPool.Write(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
fsMeta := fsMetaV1{}
|
2017-01-25 15:29:06 -05:00
|
|
|
_, err = fsMeta.ReadFrom(rwlk)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, minioMetaMultipartBucket, fsMetaPath)
|
2016-06-02 18:19:13 -04:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
partSuffix := fmt.Sprintf("object%d", partID)
|
2016-11-22 19:52:37 -05:00
|
|
|
tmpPartPath := uploadID + "." + mustGetUUID() + "." + partSuffix
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-07-05 23:59:54 -04:00
|
|
|
bufSize := int64(readSizeV1)
|
2017-09-19 15:40:27 -04:00
|
|
|
if size := data.Size(); size > 0 && bufSize > size {
|
2016-07-05 23:59:54 -04:00
|
|
|
bufSize = size
|
|
|
|
}
|
2017-09-19 15:40:27 -04:00
|
|
|
buf := make([]byte, bufSize)
|
2016-10-29 15:44:44 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
fsPartPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, tmpPartPath)
|
2017-09-19 15:40:27 -04:00
|
|
|
bytesWritten, cErr := fsCreateFile(fsPartPath, data, buf, data.Size())
|
2016-07-18 22:06:48 -04:00
|
|
|
if cErr != nil {
|
2017-01-16 20:05:00 -05:00
|
|
|
fsRemoveFile(fsPartPath)
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(cErr, minioMetaTmpBucket, tmpPartPath)
|
2016-07-18 22:06:48 -04:00
|
|
|
}
|
2016-12-03 14:53:12 -05:00
|
|
|
|
2016-07-18 22:06:48 -04:00
|
|
|
// Should return IncompleteBody{} error when reader has fewer
|
|
|
|
// bytes than specified in request header.
|
2017-09-19 15:40:27 -04:00
|
|
|
if bytesWritten < data.Size() {
|
2017-01-16 20:05:00 -05:00
|
|
|
fsRemoveFile(fsPartPath)
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, traceError(IncompleteBody{})
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-10-20 01:52:03 -04:00
|
|
|
// Delete temporary part in case of failure. If
|
|
|
|
// PutObjectPart succeeds then there would be nothing to
|
|
|
|
// delete.
|
2017-01-16 20:05:00 -05:00
|
|
|
defer fsRemoveFile(fsPartPath)
|
2016-10-20 01:52:03 -04:00
|
|
|
|
2017-09-19 15:40:27 -04:00
|
|
|
if err = data.Verify(); err != nil {
|
|
|
|
return pi, toObjectErr(err, minioMetaTmpBucket, tmpPartPath)
|
2016-10-02 18:51:49 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
partPath := pathJoin(bucket, object, uploadID, partSuffix)
|
2016-11-23 15:50:09 -05: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-10 19:15:12 -05:00
|
|
|
partLock := globalNSMutex.NewNSLock(minioMetaMultipartBucket, partPath)
|
2017-08-31 14:29:22 -04:00
|
|
|
if err = partLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return pi, err
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
fsNSPartPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, partPath)
|
|
|
|
if err = fsRenameFile(fsPartPath, fsNSPartPath); err != nil {
|
2016-11-23 15:50:09 -05:00
|
|
|
partLock.Unlock()
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, minioMetaMultipartBucket, partPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
// Save the object part info in `fs.json`.
|
2017-09-19 15:40:27 -04:00
|
|
|
md5Hex := hex.EncodeToString(data.MD5())
|
|
|
|
fsMeta.AddObjectPart(partID, partSuffix, md5Hex, data.Size())
|
2017-01-16 20:05:00 -05:00
|
|
|
if _, err = fsMeta.WriteTo(rwlk); err != nil {
|
2016-11-23 15:50:09 -05:00
|
|
|
partLock.Unlock()
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, minioMetaMultipartBucket, uploadIDPath)
|
2017-01-31 12:38:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
partNamePath := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, partSuffix)
|
|
|
|
fi, err := fsStatFile(partNamePath)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, toObjectErr(err, minioMetaMultipartBucket, partSuffix)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-11-21 02:42:53 -05:00
|
|
|
|
2016-12-09 19:10:18 -05:00
|
|
|
// Append the part in background.
|
2017-01-16 20:05:00 -05:00
|
|
|
errCh := fs.append(bucket, object, uploadID, fsMeta)
|
2016-11-23 15:50:09 -05:00
|
|
|
go func() {
|
2017-01-16 20:05:00 -05: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 15:50:09 -05:00
|
|
|
<-errCh
|
|
|
|
partLock.Unlock()
|
|
|
|
}()
|
2016-11-21 02:42:53 -05:00
|
|
|
|
2017-01-31 12:38:34 -05:00
|
|
|
return PartInfo{
|
|
|
|
PartNumber: partID,
|
|
|
|
LastModified: fi.ModTime(),
|
2017-09-19 15:40:27 -04:00
|
|
|
ETag: md5Hex,
|
2017-01-31 12:38:34 -05:00
|
|
|
Size: fi.Size(),
|
|
|
|
}, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
// ListObjectParts - returns empty response always. Applications are
|
|
|
|
// expected to remember the uploaded part numbers and corresponding
|
|
|
|
// etags.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (fs fsObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (lpi ListPartsInfo, e error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkListPartsArgs(bucket, object, fs); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lpi, err
|
2016-06-02 15:18:56 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
// Check if bucket exists
|
2017-01-16 20:05:00 -05:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lpi, toObjectErr(err, bucket)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
// Check if uploadID exists. N B This check may race with a
|
|
|
|
// concurrent abortMultipartUpload on the same uploadID. It is
|
|
|
|
// OK to be eventually consistent w.r.t listing of objects,
|
|
|
|
// uploads and parts.
|
|
|
|
uploadIDPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object, uploadID)
|
|
|
|
_, err := fsStatDir(uploadIDPath)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2017-08-04 13:45:57 -04:00
|
|
|
return lpi, traceError(InvalidUploadID{UploadID: uploadID})
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2017-08-04 13:45:57 -04:00
|
|
|
// Return empty list parts response
|
|
|
|
return ListPartsInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
UploadID: uploadID,
|
|
|
|
PartNumberMarker: 0,
|
|
|
|
MaxParts: maxParts,
|
|
|
|
}, nil
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:18:56 -04: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-06-21 22:53:09 -04:00
|
|
|
func (fs fsObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (oi ObjectInfo, e error) {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkCompleteMultipartArgs(bucket, object, fs); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-02 15:18:56 -04:00
|
|
|
|
2017-05-09 20:46:46 -04:00
|
|
|
// Check if an object is present as one of the parent dir.
|
|
|
|
if fs.parentDirIsObject(bucket, pathutil.Dir(object)) {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(errFileAccessDenied), bucket, object)
|
2017-05-09 20:46:46 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
2016-11-10 10:41:02 -05:00
|
|
|
s3MD5, err := getCompleteMultipartMD5(parts)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
2017-09-28 11:09:28 -04:00
|
|
|
var removeObjectDir bool
|
2017-01-16 20:05:00 -05: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))
|
2017-08-31 14:29:22 -04:00
|
|
|
if err = objectMPartPathLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return oi, err
|
|
|
|
}
|
2017-09-28 11:09:28 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if removeObjectDir {
|
|
|
|
basePath := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket)
|
|
|
|
derr := fsDeleteFile(basePath, pathJoin(basePath, object))
|
|
|
|
errorIf(derr, "unable to remove %s in %s", pathJoin(basePath, object), basePath)
|
|
|
|
}
|
|
|
|
objectMPartPathLock.Unlock()
|
|
|
|
}()
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
fsMetaPathMultipart := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, fsMetaJSONFile)
|
|
|
|
rlk, err := fs.rwPool.Open(fsMetaPathMultipart)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound || err == errFileAccessDenied {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05: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-06-21 22:53:09 -04:00
|
|
|
return oi, traceError(InvalidUploadID{UploadID: uploadID})
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
defer rwlk.Close()
|
|
|
|
|
|
|
|
fsMeta := fsMetaV1{}
|
2016-08-31 16:42:57 -04:00
|
|
|
// Read saved fs metadata for ongoing multipart.
|
2017-01-25 15:29:06 -05:00
|
|
|
_, err = fsMeta.ReadFrom(rlk.LockedFile)
|
2016-08-31 16:42:57 -04:00
|
|
|
if err != nil {
|
2017-01-16 20:05:00 -05:00
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, minioMetaMultipartBucket, fsMetaPathMultipart)
|
2016-08-31 16:42:57 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2017-07-07 11:41:29 -04:00
|
|
|
// Validate all parts and then commit to disk.
|
|
|
|
for i, part := range parts {
|
|
|
|
partIdx := fsMeta.ObjectPartIndex(part.PartNumber)
|
|
|
|
if partIdx == -1 {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
return oi, traceError(InvalidPart{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if fsMeta.Parts[partIdx].ETag != part.ETag {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
return oi, traceError(InvalidPart{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// All parts except the last part has to be atleast 5MB.
|
|
|
|
if (i < len(parts)-1) && !isMinAllowedPartSize(fsMeta.Parts[partIdx].Size) {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
return oi, traceError(PartTooSmall{
|
|
|
|
PartNumber: part.PartNumber,
|
|
|
|
PartSize: fsMeta.Parts[partIdx].Size,
|
|
|
|
PartETag: part.ETag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Wait for any competing PutObject() operation on bucket/object, since same namespace
|
|
|
|
// would be acquired for `fs.json`.
|
2017-06-12 20:40:28 -04:00
|
|
|
fsMetaPath := pathJoin(fs.fsPath, minioMetaBucket, bucketMetaPrefix, bucket, object, fsMetaJSONFile)
|
2017-01-16 20:05:00 -05:00
|
|
|
metaFile, err := fs.rwPool.Create(fsMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
defer metaFile.Close()
|
|
|
|
|
2017-01-16 22:23:43 -05:00
|
|
|
fsNSObjPath := pathJoin(fs.fsPath, bucket, object)
|
|
|
|
|
2016-11-30 02:26:36 -05: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 06:04:04 -05:00
|
|
|
appendFallback := true // In case background-append did not append the required parts.
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2016-11-21 02:42:53 -05:00
|
|
|
if isPartsSame(fsMeta.Parts, parts) {
|
2017-01-16 20:05:00 -05:00
|
|
|
err = fs.complete(bucket, object, uploadID, fsMeta)
|
2016-11-21 02:42:53 -05:00
|
|
|
if err == nil {
|
|
|
|
appendFallback = false
|
2017-01-16 20:05:00 -05:00
|
|
|
fsTmpObjPath := pathJoin(fs.fsPath, minioMetaTmpBucket, fs.fsUUID, uploadID)
|
|
|
|
if err = fsRenameFile(fsTmpObjPath, fsNSObjPath); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, minioMetaTmpBucket, uploadID)
|
2016-11-21 02:42:53 -05:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-11-21 02:42:53 -05:00
|
|
|
}
|
2016-11-01 03:56:03 -04:00
|
|
|
|
2016-11-21 02:42:53 -05:00
|
|
|
if appendFallback {
|
2016-11-23 06:04:04 -05:00
|
|
|
// background append could not do append all the required parts, hence we do it here.
|
|
|
|
tempObj := uploadID + "-" + "part.1"
|
2016-08-31 16:42:57 -04:00
|
|
|
|
2017-01-16 20:05:00 -05: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 16:42:57 -04:00
|
|
|
// Allocate staging buffer.
|
|
|
|
var buf = make([]byte, readSizeV1)
|
2016-10-29 15:44:44 -04:00
|
|
|
|
2017-07-07 11:41:29 -04:00
|
|
|
for _, part := range parts {
|
2016-08-31 16:42:57 -04:00
|
|
|
// Construct part suffix.
|
|
|
|
partSuffix := fmt.Sprintf("object%d", part.PartNumber)
|
2017-01-16 20:05:00 -05:00
|
|
|
multipartPartFile := pathJoin(fs.fsPath, minioMetaMultipartBucket, uploadIDPath, partSuffix)
|
|
|
|
|
|
|
|
var reader io.ReadCloser
|
2017-02-24 12:20:40 -05:00
|
|
|
var offset int64
|
2017-01-16 20:05:00 -05:00
|
|
|
reader, _, err = fsOpenFile(multipartPartFile, offset)
|
|
|
|
if err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
|
|
|
if err == errFileNotFound {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, traceError(InvalidPart{})
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), minioMetaMultipartBucket, partSuffix)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05: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
|
2017-08-12 22:25:43 -04:00
|
|
|
wfile, err = os.OpenFile((fsTmpObjPath), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
|
|
|
reader.Close()
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), bucket, object)
|
2016-08-31 16:42:57 -04:00
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
_, err = io.CopyBuffer(wfile, reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
wfile.Close()
|
|
|
|
reader.Close()
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(traceError(err), bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wfile.Close()
|
|
|
|
reader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fsRenameFile(fsTmpObjPath, fsNSObjPath); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, minioMetaTmpBucket, uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:31:14 -04:00
|
|
|
// No need to save part info, since we have concatenated all parts.
|
|
|
|
fsMeta.Parts = nil
|
|
|
|
|
2016-11-11 19:36:07 -05:00
|
|
|
// Save additional metadata.
|
|
|
|
if len(fsMeta.Meta) == 0 {
|
|
|
|
fsMeta.Meta = make(map[string]string)
|
|
|
|
}
|
2017-05-14 15:05:51 -04:00
|
|
|
fsMeta.Meta["etag"] = s3MD5
|
2016-08-15 18:02:19 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Write all the set metadata.
|
|
|
|
if _, err = fsMeta.WriteTo(metaFile); err != nil {
|
|
|
|
fs.rwPool.Close(fsMetaPathMultipart)
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, bucket, object)
|
2016-07-21 20:31:14 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05: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-20 23:48:47 -04:00
|
|
|
// Cleanup all the parts if everything else has been safely committed.
|
2017-01-16 20:05:00 -05:00
|
|
|
multipartObjectDir := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, object)
|
|
|
|
multipartUploadIDDir := pathJoin(multipartObjectDir, uploadID)
|
|
|
|
if err = fsRemoveUploadIDPath(multipartObjectDir, multipartUploadIDDir); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Remove entry from `uploads.json`.
|
2017-09-28 11:09:28 -04:00
|
|
|
removeObjectDir, err = fs.removeUploadID(bucket, object, uploadID, rwlk)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, minioMetaMultipartBucket, pathutil.Join(bucket, object))
|
2017-01-16 22:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := fsStatFile(fsNSObjPath)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, bucket, object)
|
2016-06-02 18:54:00 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 22:23:43 -05:00
|
|
|
// Return object info.
|
|
|
|
return fsMeta.ToObjectInfo(bucket, object, fi), nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:18:56 -04: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-20 23:48:47 -04:00
|
|
|
func (fs fsObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
2016-12-02 02:15:17 -05:00
|
|
|
if err := checkAbortMultipartArgs(bucket, object, fs); err != nil {
|
|
|
|
return err
|
2016-06-02 15:18:56 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
if _, err := fs.statBucketDir(bucket); err != nil {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadIDPath := pathJoin(bucket, object, uploadID)
|
2017-09-28 11:09:28 -04:00
|
|
|
var removeObjectDir bool
|
2017-01-16 20:05:00 -05: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))
|
2017-08-31 14:29:22 -04:00
|
|
|
if err := objectMPartPathLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-28 11:09:28 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if removeObjectDir {
|
|
|
|
basePath := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket)
|
|
|
|
derr := fsDeleteFile(basePath, pathJoin(basePath, object))
|
|
|
|
errorIf(derr, "unable to remove %s in %s", pathJoin(basePath, object), basePath)
|
|
|
|
}
|
|
|
|
objectMPartPathLock.Unlock()
|
|
|
|
}()
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
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-26 18:40:10 -05:00
|
|
|
return toObjectErr(err, bucket, object)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2016-06-02 15:18:56 -04:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Remove entry from `uploads.json`.
|
2017-09-28 11:09:28 -04:00
|
|
|
removeObjectDir, err = fs.removeUploadID(bucket, object, uploadID, rwlk)
|
|
|
|
if err != nil {
|
2017-01-16 20:05:00 -05:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-06-02 15:18:56 -04:00
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
return nil
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
2017-08-04 13:45:57 -04:00
|
|
|
|
2017-08-10 17:11:57 -04:00
|
|
|
// Removes multipart uploads if any older than `expiry` duration in a given bucket.
|
|
|
|
func (fs fsObjects) cleanupStaleMultipartUpload(bucket string, expiry time.Duration) (err error) {
|
2017-08-04 13:45:57 -04:00
|
|
|
var lmi ListMultipartsInfo
|
2017-08-10 17:11:57 -04:00
|
|
|
var st os.FileInfo
|
2017-08-04 13:45:57 -04:00
|
|
|
for {
|
|
|
|
// List multipart uploads in a bucket 1000 at a time
|
2017-08-04 17:14:23 -04:00
|
|
|
prefix := ""
|
2017-09-14 19:00:57 -04:00
|
|
|
lmi, err = fs.listMultipartUploadsHelper(bucket, prefix, lmi.KeyMarker, lmi.UploadIDMarker, "", 1000)
|
2017-08-04 13:45:57 -04:00
|
|
|
if err != nil {
|
2017-08-10 17:11:57 -04:00
|
|
|
errorIf(err, "Unable to list uploads")
|
2017-08-04 13:45:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:11:57 -04:00
|
|
|
// Remove uploads (and its parts) older than expiry duration.
|
2017-08-04 13:45:57 -04:00
|
|
|
for _, upload := range lmi.Uploads {
|
|
|
|
uploadIDPath := pathJoin(fs.fsPath, minioMetaMultipartBucket, bucket, upload.Object, upload.UploadID)
|
2017-08-10 17:11:57 -04:00
|
|
|
if st, err = fsStatDir(uploadIDPath); err != nil {
|
|
|
|
errorIf(err, "Failed to lookup uploads directory path %s", uploadIDPath)
|
|
|
|
continue
|
2017-08-04 13:45:57 -04:00
|
|
|
}
|
2017-08-10 17:11:57 -04:00
|
|
|
if time.Since(st.ModTime()) > expiry {
|
2017-08-04 13:45:57 -04:00
|
|
|
fs.AbortMultipartUpload(bucket, upload.Object, upload.UploadID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:11:57 -04:00
|
|
|
// No more incomplete uploads remain, break and return.
|
2017-08-04 13:45:57 -04:00
|
|
|
if !lmi.IsTruncated {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:11:57 -04:00
|
|
|
// Removes multipart uploads if any older than `expiry` duration
|
|
|
|
// on all buckets for every `cleanupInterval`, this function is
|
|
|
|
// blocking and should be run in a go-routine.
|
|
|
|
func (fs fsObjects) cleanupStaleMultipartUploads(cleanupInterval, expiry time.Duration, doneCh chan struct{}) {
|
2017-09-14 22:17:26 -04:00
|
|
|
ticker := time.NewTicker(cleanupInterval)
|
2017-08-04 13:45:57 -04:00
|
|
|
for {
|
2017-08-10 17:11:57 -04:00
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
// Stop the timer.
|
2017-09-14 22:17:26 -04:00
|
|
|
ticker.Stop()
|
2017-08-04 13:45:57 -04:00
|
|
|
return
|
2017-09-14 22:17:26 -04:00
|
|
|
case <-ticker.C:
|
2017-08-10 17:11:57 -04:00
|
|
|
bucketInfos, err := fs.ListBuckets()
|
|
|
|
if err != nil {
|
|
|
|
errorIf(err, "Unable to list buckets")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, bucketInfo := range bucketInfos {
|
|
|
|
fs.cleanupStaleMultipartUpload(bucketInfo.Name, expiry)
|
|
|
|
}
|
2017-08-04 13:45:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|