2016-04-11 04:29:18 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-04-25 13:39:28 -04:00
|
|
|
"io/ioutil"
|
2016-04-11 04:29:18 -04:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-04-26 20:57:16 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-04-11 04:29:18 -04:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
2016-04-26 20:57:16 -04:00
|
|
|
// listLeafEntries - lists all entries if a given prefixPath is a leaf
|
|
|
|
// directory, returns error if any - returns empty list if prefixPath
|
|
|
|
// is not a leaf directory.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) listLeafEntries(prefixPath string) (entries []FileInfo, e error) {
|
2016-04-21 21:05:26 -04:00
|
|
|
var markerPath string
|
2016-04-11 04:29:18 -04:00
|
|
|
for {
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, eof, err := xl.storage.ListFiles(minioMetaVolume, prefixPath, markerPath, false, 1000)
|
|
|
|
if err != nil {
|
2016-04-26 20:57:16 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": markerPath,
|
2016-04-29 17:24:10 -04:00
|
|
|
}).Errorf("%s", err)
|
|
|
|
return nil, err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-27 03:15:40 -04:00
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
// Set marker for next batch of ListFiles.
|
|
|
|
markerPath = fileInfo.Name
|
|
|
|
if fileInfo.Mode.IsDir() {
|
|
|
|
// If a directory is found, doesn't return anything.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fileName := path.Base(fileInfo.Name)
|
|
|
|
if !strings.Contains(fileName, ".") {
|
|
|
|
// Skip the entry if it is of the pattern bucket/object/uploadID.partNum.md5sum
|
|
|
|
// and retain entries of the pattern bucket/object/uploadID
|
|
|
|
entries = append(entries, fileInfo)
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 04:29:18 -04:00
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// listMetaVolumeFiles - list all files at a given prefix inside minioMetaVolume.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) listMetaVolumeFiles(prefixPath string, markerPath string, recursive bool, maxKeys int) (allFileInfos []FileInfo, eof bool, err error) {
|
2016-04-26 20:57:16 -04:00
|
|
|
// newMaxKeys tracks the size of entries which are going to be
|
|
|
|
// returned back.
|
|
|
|
var newMaxKeys int
|
|
|
|
|
|
|
|
// Following loop gathers and filters out special files inside
|
|
|
|
// minio meta volume.
|
|
|
|
for {
|
|
|
|
var fileInfos []FileInfo
|
|
|
|
// List files up to maxKeys-newMaxKeys, since we are skipping entries for special files.
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, eof, err = xl.storage.ListFiles(minioMetaVolume, prefixPath, markerPath, recursive, maxKeys-newMaxKeys)
|
|
|
|
if err != nil {
|
2016-04-26 20:57:16 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": markerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"maxKeys": maxKeys,
|
2016-04-29 17:24:10 -04:00
|
|
|
}).Errorf("%s", err)
|
|
|
|
return nil, true, err
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
|
|
|
// Loop through and validate individual file.
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
var entries []FileInfo
|
2016-04-27 03:15:40 -04:00
|
|
|
if fi.Mode.IsDir() {
|
|
|
|
// List all the entries if fi.Name is a leaf directory, if
|
|
|
|
// fi.Name is not a leaf directory then the resulting
|
|
|
|
// entries are empty.
|
2016-04-29 17:24:10 -04:00
|
|
|
entries, err = xl.listLeafEntries(fi.Name)
|
|
|
|
if err != nil {
|
2016-04-27 03:15:40 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": fi.Name,
|
2016-04-29 17:24:10 -04:00
|
|
|
}).Errorf("%s", err)
|
|
|
|
return nil, false, err
|
2016-04-27 03:15:40 -04:00
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
|
|
|
// Set markerPath for next batch of listing.
|
|
|
|
markerPath = fi.Name
|
|
|
|
if len(entries) > 0 {
|
2016-04-27 03:15:40 -04:00
|
|
|
// We reach here for non-recursive case and a leaf entry.
|
2016-04-26 20:57:16 -04:00
|
|
|
for _, entry := range entries {
|
|
|
|
allFileInfos = append(allFileInfos, entry)
|
2016-04-27 03:15:40 -04:00
|
|
|
newMaxKeys++
|
|
|
|
// If we have reached the maxKeys, it means we have listed
|
|
|
|
// everything that was requested. Return right here.
|
|
|
|
if newMaxKeys == maxKeys {
|
|
|
|
// Return values:
|
|
|
|
// allFileInfos : "maxKeys" number of entries.
|
2016-04-29 17:24:10 -04:00
|
|
|
// eof : eof returned by xl.storage.ListFiles()
|
2016-04-27 03:15:40 -04:00
|
|
|
// error : nil
|
|
|
|
return
|
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
} else {
|
|
|
|
// We reach here for a non-recursive case non-leaf entry
|
|
|
|
// OR recursive case with fi.Name matching pattern bucket/object/uploadID[.partNum.md5sum]
|
|
|
|
if !fi.Mode.IsDir() { // Do not skip non-recursive case directory entries.
|
|
|
|
// Skip files matching pattern bucket/object/uploadID.partNum.md5sum
|
|
|
|
// and retain files matching pattern bucket/object/uploadID
|
|
|
|
specialFile := path.Base(fi.Name)
|
|
|
|
if strings.Contains(specialFile, ".") {
|
|
|
|
// Contains partnumber and md5sum info, skip this.
|
|
|
|
continue
|
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 03:15:40 -04:00
|
|
|
allFileInfos = append(allFileInfos, fi)
|
2016-04-26 20:57:16 -04:00
|
|
|
newMaxKeys++
|
|
|
|
// If we have reached the maxKeys, it means we have listed
|
|
|
|
// everything that was requested. Return right here.
|
|
|
|
if newMaxKeys == maxKeys {
|
2016-04-27 03:15:40 -04:00
|
|
|
// Return values:
|
|
|
|
// allFileInfos : "maxKeys" number of entries.
|
2016-04-29 17:24:10 -04:00
|
|
|
// eof : eof returned by xl.storage.ListFiles()
|
2016-04-27 03:15:40 -04:00
|
|
|
// error : nil
|
|
|
|
return
|
2016-04-26 20:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we have reached eof then we break out.
|
|
|
|
if eof {
|
|
|
|
break
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
|
|
|
|
// Return entries here.
|
|
|
|
return allFileInfos, eof, nil
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMultipartUploads - list multipart uploads.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-04-11 04:29:18 -04:00
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListMultipartsInfo{}, (BucketNameInvalid{Bucket: bucket})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectPrefix(prefix) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListMultipartsInfo{}, (ObjectNameInvalid{Bucket: bucket, Object: prefix})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
2016-04-21 21:05:26 -04:00
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListMultipartsInfo{}, (UnsupportedDelimiter{
|
2016-04-11 04:29:18 -04:00
|
|
|
Delimiter: delimiter,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, prefix) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListMultipartsInfo{}, (InvalidMarkerPrefixCombination{
|
2016-04-11 04:29:18 -04:00
|
|
|
Marker: keyMarker,
|
|
|
|
Prefix: prefix,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if uploadIDMarker != "" {
|
2016-04-21 21:05:26 -04:00
|
|
|
if strings.HasSuffix(keyMarker, slashSeparator) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return result, (InvalidUploadIDKeyCombination{
|
2016-04-11 04:29:18 -04:00
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
})
|
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
id, err := uuid.Parse(uploadIDMarker)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if id.IsZero() {
|
2016-04-29 17:24:10 -04:00
|
|
|
return result, (MalformedUploadID{
|
2016-04-11 04:29:18 -04:00
|
|
|
UploadID: uploadIDMarker,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recursive := true
|
2016-04-21 21:05:26 -04:00
|
|
|
if delimiter == slashSeparator {
|
2016-04-11 04:29:18 -04:00
|
|
|
recursive = false
|
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
|
2016-04-13 04:00:30 -04:00
|
|
|
result.IsTruncated = true
|
2016-04-16 14:43:03 -04:00
|
|
|
result.MaxUploads = maxUploads
|
2016-04-26 20:57:16 -04:00
|
|
|
|
2016-04-21 21:05:26 -04:00
|
|
|
// Not using path.Join() as it strips off the trailing '/'.
|
2016-04-16 14:43:03 -04:00
|
|
|
// Also bucket should always be followed by '/' even if prefix is empty.
|
|
|
|
prefixPath := pathJoin(bucket, prefix)
|
2016-04-26 20:57:16 -04:00
|
|
|
keyMarkerPath := ""
|
|
|
|
if keyMarker != "" {
|
2016-04-27 16:19:48 -04:00
|
|
|
keyMarkerPath = pathJoin(pathJoin(bucket, keyMarker), uploadIDMarker)
|
2016-04-13 04:00:30 -04:00
|
|
|
}
|
2016-04-27 16:19:48 -04:00
|
|
|
// List all the multipart files at prefixPath, starting with marker keyMarkerPath.
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, eof, err := xl.listMetaVolumeFiles(prefixPath, keyMarkerPath, recursive, maxUploads)
|
|
|
|
if err != nil {
|
2016-04-26 20:57:16 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": keyMarkerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"maxUploads": maxUploads,
|
2016-04-29 17:24:10 -04:00
|
|
|
}).Errorf("listMetaVolumeFiles failed with %s", err)
|
|
|
|
return ListMultipartsInfo{}, err
|
2016-04-13 04:00:30 -04:00
|
|
|
}
|
2016-04-27 16:19:48 -04:00
|
|
|
|
|
|
|
// Loop through all the received files fill in the multiparts result.
|
2016-04-13 04:00:30 -04:00
|
|
|
for _, fi := range fileInfos {
|
2016-04-26 20:57:16 -04:00
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
|
|
|
if fi.Mode.IsDir() {
|
2016-04-27 16:19:48 -04:00
|
|
|
// All directory entries are common prefixes.
|
|
|
|
uploadID = "" // Upload ids are empty for CommonPrefixes.
|
2016-04-26 20:57:16 -04:00
|
|
|
objectName = strings.TrimPrefix(fi.Name, retainSlash(bucket))
|
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objectName)
|
|
|
|
} else {
|
|
|
|
uploadID = path.Base(fi.Name)
|
|
|
|
objectName = strings.TrimPrefix(path.Dir(fi.Name), retainSlash(bucket))
|
|
|
|
result.Uploads = append(result.Uploads, uploadMetadata{
|
|
|
|
Object: objectName,
|
|
|
|
UploadID: uploadID,
|
|
|
|
Initiated: fi.ModTime,
|
|
|
|
})
|
2016-04-13 04:00:30 -04:00
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
result.NextKeyMarker = objectName
|
|
|
|
result.NextUploadIDMarker = uploadID
|
2016-04-13 04:00:30 -04:00
|
|
|
}
|
2016-04-26 20:57:16 -04:00
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
2016-04-16 14:43:03 -04:00
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) NewMultipartUpload(bucket, object string) (string, error) {
|
2016-04-25 13:39:28 -04:00
|
|
|
// Verify if bucket name is valid.
|
2016-04-11 04:29:18 -04:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (BucketNameInvalid{Bucket: bucket})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-25 13:39:28 -04:00
|
|
|
// Verify if object name is valid.
|
2016-04-11 04:29:18 -04:00
|
|
|
if !IsValidObjectName(object) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-25 13:39:28 -04:00
|
|
|
// Verify whether the bucket exists.
|
2016-04-29 20:52:17 -04:00
|
|
|
if isExist, err := isBucketExist(xl.storage, bucket); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", err
|
|
|
|
} else if !isExist {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
2016-04-25 13:39:28 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
if _, err := xl.storage.StatVol(minioMetaVolume); err != nil {
|
|
|
|
if err == errVolumeNotFound {
|
|
|
|
err = xl.storage.MakeVol(minioMetaVolume)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
2016-04-29 17:24:10 -04:00
|
|
|
uuid, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
uploadID := uuid.String()
|
2016-04-16 15:48:41 -04:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
2016-04-29 17:24:10 -04:00
|
|
|
if _, err = xl.storage.StatFile(minioMetaVolume, uploadIDPath); err != nil {
|
|
|
|
if err != errFileNotFound {
|
|
|
|
return "", (toObjectErr(err, minioMetaVolume, uploadIDPath))
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
// uploadIDPath doesn't exist, so create empty file to reserve the name
|
2016-04-11 04:29:18 -04:00
|
|
|
var w io.WriteCloser
|
2016-04-29 17:24:10 -04:00
|
|
|
if w, err = xl.storage.CreateFile(minioMetaVolume, uploadIDPath); err == nil {
|
2016-04-20 00:58:11 -04:00
|
|
|
// Close the writer.
|
2016-04-29 17:24:10 -04:00
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", toObjectErr(err, minioMetaVolume, uploadIDPath)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
return uploadID, nil
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
// uploadIDPath already exists.
|
2016-04-11 04:29:18 -04:00
|
|
|
// loop again to try with different uuid generated.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 13:39:28 -04:00
|
|
|
// PutObjectPart - writes the multipart upload chunks.
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
2016-04-11 04:29:18 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-25 13:39:28 -04:00
|
|
|
// Verify whether the bucket exists.
|
2016-04-29 20:52:17 -04:00
|
|
|
if isExist, err := isBucketExist(xl.storage, bucket); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", err
|
|
|
|
} else if !isExist {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
2016-04-25 13:39:28 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 20:52:17 -04:00
|
|
|
if status, err := isUploadIDExists(xl.storage, bucket, object, uploadID); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
} else if !status {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
partSuffix := fmt.Sprintf("%s.%d.%s", uploadID, partID, md5Hex)
|
2016-04-29 17:24:10 -04:00
|
|
|
fileWriter, err := xl.storage.CreateFile(minioMetaVolume, path.Join(bucket, object, partSuffix))
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-04-29 17:24:10 -04:00
|
|
|
if _, err = io.CopyN(multiWriter, data, size); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (toObjectErr(err))
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-25 13:39:28 -04:00
|
|
|
// 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.
|
2016-04-29 17:24:10 -04:00
|
|
|
if _, err = io.CopyN(ioutil.Discard, data, 1); err == nil {
|
2016-04-25 13:39:28 -04:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (UnExpectedDataSize{Size: int(size)})
|
2016-04-25 13:39:28 -04:00
|
|
|
}
|
2016-04-11 04:29:18 -04:00
|
|
|
} else {
|
2016-04-29 17:24:10 -04:00
|
|
|
if _, err = io.Copy(multiWriter, data); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (toObjectErr(err))
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-04-16 15:48:41 -04:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (BadDigest{md5Hex, newMD5Hex})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
err = fileWriter.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
2016-04-11 04:29:18 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListPartsInfo{}, (BucketNameInvalid{Bucket: bucket})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListPartsInfo{}, (ObjectNameInvalid{Bucket: bucket, Object: object})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-29 20:52:17 -04:00
|
|
|
if status, err := isUploadIDExists(xl.storage, bucket, object, uploadID); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListPartsInfo{}, err
|
2016-04-11 04:29:18 -04:00
|
|
|
} else if !status {
|
2016-04-29 17:24:10 -04:00
|
|
|
return ListPartsInfo{}, (InvalidUploadID{UploadID: uploadID})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
result := ListPartsInfo{}
|
2016-04-21 21:05:26 -04:00
|
|
|
var markerPath string
|
2016-04-11 04:29:18 -04:00
|
|
|
nextPartNumberMarker := 0
|
2016-04-16 15:48:41 -04:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
// Figure out the marker for the next subsequent calls, if the
|
|
|
|
// partNumberMarker is already set.
|
2016-04-11 04:29:18 -04:00
|
|
|
if partNumberMarker > 0 {
|
2016-04-20 20:23:23 -04:00
|
|
|
partNumberMarkerPath := uploadIDPath + "." + strconv.Itoa(partNumberMarker) + "."
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, _, err := xl.storage.ListFiles(minioMetaVolume, partNumberMarkerPath, "", false, 1)
|
|
|
|
if err != nil {
|
|
|
|
return result, toObjectErr(err, minioMetaVolume, partNumberMarkerPath)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if len(fileInfos) == 0 {
|
2016-04-29 17:24:10 -04:00
|
|
|
return result, (InvalidPart{})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-21 21:05:26 -04:00
|
|
|
markerPath = fileInfos[0].Name
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-21 21:05:26 -04:00
|
|
|
uploadIDPrefix := uploadIDPath + "."
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, eof, err := xl.storage.ListFiles(minioMetaVolume, uploadIDPrefix, markerPath, false, maxParts)
|
|
|
|
if err != nil {
|
|
|
|
return result, InvalidPart{}
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
for _, fileInfo := range fileInfos {
|
2016-04-13 14:32:47 -04:00
|
|
|
fileName := path.Base(fileInfo.Name)
|
2016-04-11 04:29:18 -04:00
|
|
|
splitResult := strings.Split(fileName, ".")
|
2016-04-29 17:24:10 -04:00
|
|
|
partNum, err := strconv.Atoi(splitResult[1])
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
md5sum := splitResult[2]
|
|
|
|
result.Parts = append(result.Parts, partInfo{
|
|
|
|
PartNumber: partNum,
|
|
|
|
LastModified: fileInfo.ModTime,
|
|
|
|
ETag: md5sum,
|
|
|
|
Size: fileInfo.Size,
|
|
|
|
})
|
|
|
|
nextPartNumberMarker = partNum
|
|
|
|
}
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
result.PartNumberMarker = partNumberMarker
|
|
|
|
result.NextPartNumberMarker = nextPartNumberMarker
|
|
|
|
result.MaxParts = maxParts
|
|
|
|
result.IsTruncated = !eof
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
func (xl xlObjects) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, error) {
|
2016-04-11 04:29:18 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (BucketNameInvalid{Bucket: bucket})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (ObjectNameInvalid{
|
2016-04-16 15:48:41 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-29 20:52:17 -04:00
|
|
|
if status, err := isUploadIDExists(xl.storage, bucket, object, uploadID); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
} else if !status {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", (InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
var md5Sums []string
|
2016-04-11 04:29:18 -04:00
|
|
|
for _, part := range parts {
|
2016-03-28 00:52:38 -04:00
|
|
|
// Construct part suffix.
|
2016-04-11 04:29:18 -04:00
|
|
|
partSuffix := fmt.Sprintf("%s.%d.%s", uploadID, part.PartNumber, part.ETag)
|
2016-04-29 20:52:17 -04:00
|
|
|
err := xl.storage.RenameFile(minioMetaVolume, path.Join(bucket, object, partSuffix), bucket, path.Join(object, fmt.Sprint(part.PartNumber)))
|
|
|
|
// We need a way to roll back if of the renames failed.
|
2016-04-29 17:24:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
md5Sums = append(md5Sums, part.ETag)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-29 17:24:10 -04:00
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
// Save the s3 md5.
|
|
|
|
s3MD5, err := makeS3MD5(md5Sums...)
|
|
|
|
if err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return "", err
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Return md5sum.
|
2016-04-16 15:48:41 -04:00
|
|
|
return s3MD5, nil
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
// AbortMultipartUpload - abort multipart upload.
|
|
|
|
func (xl xlObjects) AbortMultipartUpload(bucket, object, uploadID string) error {
|
2016-04-11 04:29:18 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return (BucketNameInvalid{Bucket: bucket})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-29 17:24:10 -04:00
|
|
|
return (ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-04-29 20:52:17 -04:00
|
|
|
if status, err := isUploadIDExists(xl.storage, bucket, object, uploadID); err != nil {
|
2016-04-29 17:24:10 -04:00
|
|
|
return err
|
|
|
|
} else if !status {
|
|
|
|
return (InvalidUploadID{UploadID: uploadID})
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-29 17:24:10 -04:00
|
|
|
markerPath := ""
|
2016-04-11 04:29:18 -04:00
|
|
|
for {
|
2016-04-16 15:48:41 -04:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
2016-04-29 17:24:10 -04:00
|
|
|
fileInfos, eof, err := xl.storage.ListFiles(minioMetaVolume, uploadIDPath, markerPath, false, 1000)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return (InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
return toObjectErr(err)
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
for _, fileInfo := range fileInfos {
|
2016-04-29 17:24:10 -04:00
|
|
|
xl.storage.DeleteFile(minioMetaVolume, fileInfo.Name)
|
|
|
|
markerPath = fileInfo.Name
|
2016-04-11 04:29:18 -04:00
|
|
|
}
|
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|