2016-05-02 19:57:31 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2016-05-03 19:10:24 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-05-20 23:48:47 -04:00
|
|
|
"time"
|
2016-05-02 19:57:31 -04:00
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Checks whether bucket exists.
|
|
|
|
func (fs fsObjects) isBucketExist(bucket string) bool {
|
|
|
|
// Check whether bucket exists.
|
|
|
|
_, err := fs.storage.StatVol(bucket)
|
2016-05-07 05:08:03 -04:00
|
|
|
if err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == errVolumeNotFound {
|
|
|
|
return false
|
2016-05-07 05:08:03 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
errorIf(err, "Stat failed on bucket "+bucket+".")
|
|
|
|
return false
|
2016-05-07 05:08:03 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return true
|
2016-05-07 05:08:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// newMultipartUploadCommon - initialize a new multipart, is a common function for both object layers.
|
|
|
|
func (fs fsObjects) newMultipartUploadCommon(bucket string, object string, meta map[string]string) (uploadID string, err error) {
|
2016-05-02 19:57:31 -04:00
|
|
|
// Verify if bucket name is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
// Verify whether the bucket exists.
|
2016-05-20 23:48:47 -04:00
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
// Verify if object name is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
2016-05-19 20:10:08 -04:00
|
|
|
// No metadata is set, allocate a new one.
|
|
|
|
if meta == nil {
|
|
|
|
meta = make(map[string]string)
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// Initialize `fs.json` values.
|
|
|
|
fsMeta := newFSMetaV1()
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-05-18 18:05:23 -04:00
|
|
|
// This lock needs to be held for any changes to the directory contents of ".minio/multipart/object/"
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object))
|
2016-05-02 19:57:31 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
uploadID = getUUID()
|
|
|
|
initiated := time.Now().UTC()
|
|
|
|
// Create 'uploads.json'
|
|
|
|
if err = writeUploadJSON(bucket, object, uploadID, initiated, fs.storage); err != nil {
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
tempUploadIDPath := path.Join(tmpMetaPrefix, bucket, object, uploadID)
|
|
|
|
if err = fs.writeFSMetadata(minioMetaBucket, tempUploadIDPath, fsMeta); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
2016-05-18 16:37:28 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
err = fs.storage.RenameFile(minioMetaBucket, path.Join(tempUploadIDPath, fsMetaJSONFile), minioMetaBucket, path.Join(uploadIDPath, fsMetaJSONFile))
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
if dErr := fs.storage.DeleteFile(minioMetaBucket, path.Join(tempUploadIDPath, fsMetaJSONFile)); dErr != nil {
|
|
|
|
return "", toObjectErr(dErr, minioMetaBucket, tempUploadIDPath)
|
2016-05-18 16:30:58 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
2016-05-18 16:30:58 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
// Return success.
|
|
|
|
return uploadID, nil
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
// Returns if the prefix is a multipart upload.
|
|
|
|
func (fs fsObjects) isMultipartUpload(bucket, prefix string) bool {
|
|
|
|
_, err := fs.storage.StatFile(bucket, pathJoin(prefix, uploadsJSONFile))
|
|
|
|
return err == nil
|
2016-05-07 05:08:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// listUploadsInfo - list all uploads info.
|
|
|
|
func (fs fsObjects) listUploadsInfo(prefixPath string) (uploads []uploadInfo, err error) {
|
|
|
|
splitPrefixes := strings.SplitN(prefixPath, "/", 3)
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadIDs, err := readUploadsJSON(splitPrefixes[1], splitPrefixes[2], fs.storage)
|
2016-05-05 15:51:56 -04:00
|
|
|
if err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == errFileNotFound {
|
|
|
|
return []uploadInfo{}, nil
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return nil, err
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
uploads = uploadIDs.Uploads
|
|
|
|
return uploads, nil
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// listMultipartUploadsCommon - lists all multipart uploads, common function for both object layers.
|
|
|
|
func (fs fsObjects) listMultipartUploadsCommon(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-05-03 19:10:24 -04:00
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListMultipartsInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListMultipartsInfo{}, BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
if !IsValidObjectPrefix(prefix) {
|
|
|
|
return ListMultipartsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: prefix}
|
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
|
|
|
return ListMultipartsInfo{}, UnsupportedDelimiter{
|
|
|
|
Delimiter: delimiter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, prefix) {
|
|
|
|
return ListMultipartsInfo{}, InvalidMarkerPrefixCombination{
|
|
|
|
Marker: keyMarker,
|
|
|
|
Prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
if strings.HasSuffix(keyMarker, slashSeparator) {
|
|
|
|
return result, InvalidUploadIDKeyCombination{
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id, err := uuid.Parse(uploadIDMarker)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
if id.IsZero() {
|
|
|
|
return result, MalformedUploadID{
|
|
|
|
UploadID: uploadIDMarker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-26 17:43:17 -04:00
|
|
|
multipartPrefixPath := pathJoin(mpartMetaPrefix, 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-05-26 17:43:17 -04:00
|
|
|
multipartMarkerPath = pathJoin(mpartMetaPrefix, bucket, keyMarker)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-26 17:43:17 -04:00
|
|
|
var uploads []uploadMetadata
|
|
|
|
var err error
|
|
|
|
var eof bool
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
uploads, _, err = listMultipartUploadIDs(bucket, keyMarker, uploadIDMarker, maxUploads, fs.storage)
|
|
|
|
if err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
maxUploads = maxUploads - len(uploads)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-26 17:43:17 -04:00
|
|
|
if maxUploads > 0 {
|
|
|
|
walker := fs.lookupTreeWalk(listParams{minioMetaBucket, recursive, multipartMarkerPath, multipartPrefixPath})
|
|
|
|
if walker == nil {
|
|
|
|
walker = fs.startTreeWalk(minioMetaBucket, multipartPrefixPath, multipartMarkerPath, recursive, func(bucket, object string) bool {
|
|
|
|
return fs.isMultipartUpload(bucket, object)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for maxUploads > 0 {
|
|
|
|
walkResult, ok := <-walker.ch
|
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
|
|
|
// File not found or Disk not found is a valid case.
|
|
|
|
if walkResult.err == errFileNotFound || walkResult.err == errDiskNotFound {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
entry := strings.TrimPrefix(walkResult.entry, retainSlash(pathJoin(mpartMetaPrefix, bucket)))
|
|
|
|
if strings.HasSuffix(walkResult.entry, slashSeparator) {
|
|
|
|
uploads = append(uploads, uploadMetadata{
|
|
|
|
Object: entry,
|
|
|
|
})
|
|
|
|
maxUploads--
|
|
|
|
if maxUploads == 0 {
|
|
|
|
if walkResult.end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var tmpUploads []uploadMetadata
|
|
|
|
var end bool
|
|
|
|
uploadIDMarker = ""
|
|
|
|
tmpUploads, end, err = listMultipartUploadIDs(bucket, entry, uploadIDMarker, maxUploads, fs.storage)
|
|
|
|
if err != nil {
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
uploads = append(uploads, tmpUploads...)
|
|
|
|
maxUploads -= len(tmpUploads)
|
|
|
|
if walkResult.end && end {
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Loop through all the received uploads fill in the multiparts result.
|
|
|
|
for _, upload := range uploads {
|
2016-05-03 19:10:24 -04:00
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
2016-05-26 17:43:17 -04:00
|
|
|
if strings.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
|
|
|
|
}
|
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// ListMultipartUploads - list multipart uploads.
|
|
|
|
func (fs fsObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
|
|
|
return fs.listMultipartUploadsCommon(bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultipartUpload - initialize a new multipart upload, returns a unique id.
|
|
|
|
func (fs fsObjects) NewMultipartUpload(bucket, object string, meta map[string]string) (string, error) {
|
|
|
|
meta = make(map[string]string) // Reset the meta value, we are not going to save headers for fs.
|
|
|
|
return fs.newMultipartUploadCommon(bucket, object, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// putObjectPartCommon - put object part.
|
|
|
|
func (fs fsObjects) putObjectPartCommon(bucket string, object string, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
// Hold read lock on the uploadID so that no one aborts it.
|
|
|
|
nsMutex.RLock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.RUnlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
|
|
|
|
// Hold write lock on the part so that there is no parallel upload on the part.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID, strconv.Itoa(partID)))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID, strconv.Itoa(partID)))
|
|
|
|
|
|
|
|
partSuffix := fmt.Sprintf("object%d", partID)
|
|
|
|
tmpPartPath := path.Join(tmpMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
fileWriter, err := fs.storage.CreateFile(minioMetaBucket, tmpPartPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
|
|
|
// Instantiate a new multi writer.
|
|
|
|
multiWriter := io.MultiWriter(md5Writer, fileWriter)
|
|
|
|
|
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
|
|
|
if size > 0 {
|
|
|
|
if _, err = io.CopyN(multiWriter, data, size); err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
// Reader shouldn't have more data what mentioned in size argument.
|
|
|
|
// reading one more byte from the reader to validate it.
|
|
|
|
// expected to fail, success validates existence of more data in the reader.
|
|
|
|
if _, err = io.CopyN(ioutil.Discard, data, 1); err == nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
|
|
|
return "", UnExpectedDataSize{Size: int(size)}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var n int64
|
|
|
|
if n, err = io.Copy(multiWriter, data); err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
size = n
|
|
|
|
}
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
|
|
|
return "", BadDigest{md5Hex, newMD5Hex}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = fileWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
fsMeta, err := fs.readFSMetadata(minioMetaBucket, uploadIDPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
|
|
|
}
|
2016-05-25 00:24:20 -04:00
|
|
|
fsMeta.AddObjectPart(partID, partSuffix, newMD5Hex, size)
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
partPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
err = fs.storage.RenameFile(minioMetaBucket, tmpPartPath, minioMetaBucket, partPath)
|
|
|
|
if err != nil {
|
|
|
|
if dErr := fs.storage.DeleteFile(minioMetaBucket, tmpPartPath); dErr != nil {
|
|
|
|
return "", toObjectErr(dErr, minioMetaBucket, tmpPartPath)
|
|
|
|
}
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, partPath)
|
|
|
|
}
|
|
|
|
if err = fs.writeFSMetadata(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadID), fsMeta); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
}
|
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPart - writes the multipart upload chunks.
|
|
|
|
func (fs fsObjects) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
|
|
|
return fs.putObjectPartCommon(bucket, object, uploadID, partID, size, data, md5Hex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs fsObjects) listObjectPartsCommon(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-05-03 19:10:24 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
2016-05-20 23:48:47 -04:00
|
|
|
if !fs.isBucketExist(bucket) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, BucketNotFound{Bucket: bucket}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: object}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, InvalidUploadID{UploadID: uploadID}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-18 18:05:23 -04:00
|
|
|
// Hold lock so that there is no competing abort-multipart-upload or complete-multipart-upload.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
2016-05-03 19:10:24 -04:00
|
|
|
result := ListPartsInfo{}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
fsMeta, err := fs.readFSMetadata(minioMetaBucket, uploadIDPath)
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaBucket, uploadIDPath)
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
// Only parts with higher part numbers will be listed.
|
2016-05-26 06:15:01 -04:00
|
|
|
partIdx := fsMeta.ObjectPartIndex(partNumberMarker)
|
2016-05-25 00:24:20 -04:00
|
|
|
parts := fsMeta.Parts
|
|
|
|
if partIdx != -1 {
|
|
|
|
parts = fsMeta.Parts[partIdx+1:]
|
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
count := maxParts
|
2016-05-25 00:24:20 -04:00
|
|
|
for _, part := range parts {
|
2016-05-20 23:48:47 -04:00
|
|
|
var fi FileInfo
|
|
|
|
partNamePath := path.Join(mpartMetaPrefix, bucket, object, uploadID, part.Name)
|
|
|
|
fi, err = fs.storage.StatFile(minioMetaBucket, partNamePath)
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
return ListPartsInfo{}, toObjectErr(err, minioMetaBucket, partNamePath)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result.Parts = append(result.Parts, partInfo{
|
2016-05-25 00:24:20 -04:00
|
|
|
PartNumber: part.Number,
|
2016-05-20 23:48:47 -04:00
|
|
|
ETag: part.ETag,
|
2016-05-05 15:51:56 -04:00
|
|
|
LastModified: fi.ModTime,
|
|
|
|
Size: fi.Size,
|
2016-05-03 19:10:24 -04:00
|
|
|
})
|
2016-05-05 15:51:56 -04:00
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 05:21:12 -04:00
|
|
|
// If listed entries are more than maxParts, we set IsTruncated as true.
|
2016-05-20 23:48:47 -04:00
|
|
|
if len(parts) > len(result.Parts) {
|
2016-05-05 15:51:56 -04:00
|
|
|
result.IsTruncated = true
|
2016-05-08 05:21:12 -04: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-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
result.MaxParts = maxParts
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
func (fs fsObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
|
|
|
return fs.listObjectPartsCommon(bucket, object, uploadID, partNumberMarker, maxParts)
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (fs fsObjects) isUploadIDExists(bucket, object, uploadID string) bool {
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
|
|
|
_, err := fs.storage.StatFile(minioMetaBucket, path.Join(uploadIDPath, fsMetaJSONFile))
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
2016-05-08 04:58:05 -04:00
|
|
|
return false
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
errorIf(err, "Unable to access upload id"+uploadIDPath)
|
2016-05-08 04:58:05 -04:00
|
|
|
return false
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs fsObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !fs.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate s3 compatible md5sum for complete multipart.
|
|
|
|
s3MD5, err := completeMultipartMD5(parts...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tempObj := path.Join(tmpMetaPrefix, bucket, object, uploadID, "object1")
|
|
|
|
fileWriter, err := fs.storage.CreateFile(minioMetaBucket, tempObj)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all parts, validate them and then commit to disk.
|
|
|
|
for i, part := range parts {
|
|
|
|
// Construct part suffix.
|
|
|
|
partSuffix := fmt.Sprintf("object%d", part.PartNumber)
|
|
|
|
multipartPartFile := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffix)
|
|
|
|
var fi FileInfo
|
|
|
|
fi, err = fs.storage.StatFile(minioMetaBucket, multipartPartFile)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return "", InvalidPart{}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// All parts except the last part has to be atleast 5MB.
|
|
|
|
if (i < len(parts)-1) && !isMinAllowedPartSize(fi.Size) {
|
|
|
|
return "", PartTooSmall{}
|
|
|
|
}
|
|
|
|
var fileReader io.ReadCloser
|
|
|
|
fileReader, err = fs.storage.ReadFile(minioMetaBucket, multipartPartFile, 0)
|
|
|
|
if err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", clErr
|
|
|
|
}
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return "", InvalidPart{}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(fileWriter, fileReader)
|
|
|
|
if err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", clErr
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = fileReader.Close()
|
|
|
|
if err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", clErr
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fileWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", clErr
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file back to original location, if not delete the temporary object.
|
|
|
|
err = fs.storage.RenameFile(minioMetaBucket, tempObj, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
if dErr := fs.storage.DeleteFile(minioMetaBucket, tempObj); dErr != nil {
|
|
|
|
return "", toObjectErr(dErr, minioMetaBucket, tempObj)
|
|
|
|
}
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup all the parts if everything else has been safely committed.
|
|
|
|
if err = cleanupUploadedParts(bucket, object, uploadID, fs.storage); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return md5sum.
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// abortMultipartUploadCommon - aborts a multipart upload, common
|
|
|
|
// function used by both object layers.
|
|
|
|
func (fs fsObjects) abortMultipartUploadCommon(bucket, object, uploadID string) error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !fs.isBucketExist(bucket) {
|
|
|
|
return BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
if !fs.isUploadIDExists(bucket, object, uploadID) {
|
|
|
|
return InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold lock so that there is no competing complete-multipart-upload or put-object-part.
|
|
|
|
nsMutex.Lock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
defer nsMutex.Unlock(minioMetaBucket, pathJoin(mpartMetaPrefix, bucket, object, uploadID))
|
|
|
|
|
|
|
|
// Cleanup all uploaded parts.
|
|
|
|
if err := cleanupUploadedParts(bucket, object, uploadID, fs.storage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if there are other incomplete upload-id's present for
|
|
|
|
// the object, if yes do not attempt to delete 'uploads.json'.
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, fs.storage)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err == nil {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadIDIdx := uploadsJSON.Index(uploadID)
|
2016-05-20 23:48:47 -04:00
|
|
|
if uploadIDIdx != -1 {
|
2016-05-26 17:43:17 -04:00
|
|
|
uploadsJSON.Uploads = append(uploadsJSON.Uploads[:uploadIDIdx], uploadsJSON.Uploads[uploadIDIdx+1:]...)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 17:43:17 -04:00
|
|
|
if len(uploadsJSON.Uploads) > 0 {
|
|
|
|
err = updateUploadsJSON(bucket, object, uploadsJSON, fs.storage)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = fs.storage.DeleteFile(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)); err != nil {
|
|
|
|
return toObjectErr(err, minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUpload - aborts a multipart upload.
|
|
|
|
func (fs fsObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
|
|
|
return fs.abortMultipartUploadCommon(bucket, object, uploadID)
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|