2016-04-11 10:29:18 +02:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-04-25 19:39:28 +02:00
|
|
|
"io/ioutil"
|
2016-04-11 10:29:18 +02:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-04-27 02:57:16 +02:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-04-11 10:29:18 +02:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-04-22 03:05:26 +02:00
|
|
|
// Minio meta volume.
|
2016-04-16 20:43:03 +02:00
|
|
|
minioMetaVolume = ".minio"
|
2016-04-11 10:29:18 +02:00
|
|
|
)
|
|
|
|
|
2016-04-25 19:39:28 +02:00
|
|
|
// checks whether bucket exists.
|
|
|
|
func (o objectAPI) isBucketExist(bucketName string) (bool, error) {
|
|
|
|
// Check whether bucket exists.
|
|
|
|
if _, e := o.storage.StatVol(bucketName); e != nil {
|
|
|
|
if e == errVolumeNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, e
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:57:16 +02: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.
|
|
|
|
func (o objectAPI) listLeafEntries(prefixPath string) (entries []FileInfo, e error) {
|
2016-04-22 03:05:26 +02:00
|
|
|
var markerPath string
|
2016-04-11 10:29:18 +02:00
|
|
|
for {
|
2016-04-22 03:05:26 +02:00
|
|
|
fileInfos, eof, e := o.storage.ListFiles(minioMetaVolume, prefixPath, markerPath, false, 1000)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-27 02:57:16 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": markerPath,
|
|
|
|
}).Errorf("%s", e)
|
|
|
|
return nil, e
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-27 09:15:40 +02: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 10:29:18 +02:00
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// listMetaVolumeFiles - list all files at a given prefix inside minioMetaVolume.
|
|
|
|
func (o objectAPI) listMetaVolumeFiles(prefixPath string, markerPath string, recursive bool, maxKeys int) (allFileInfos []FileInfo, eof bool, e error) {
|
|
|
|
// 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.
|
|
|
|
fileInfos, eof, e = o.storage.ListFiles(minioMetaVolume, prefixPath, markerPath, recursive, maxKeys-newMaxKeys)
|
|
|
|
if e != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": markerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"maxKeys": maxKeys,
|
|
|
|
}).Errorf("%s", e)
|
|
|
|
return nil, true, e
|
|
|
|
}
|
|
|
|
// Loop through and validate individual file.
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
var entries []FileInfo
|
2016-04-27 09:15:40 +02: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.
|
|
|
|
entries, e = o.listLeafEntries(fi.Name)
|
|
|
|
if e != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": fi.Name,
|
|
|
|
}).Errorf("%s", e)
|
|
|
|
return nil, false, e
|
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
}
|
|
|
|
// Set markerPath for next batch of listing.
|
|
|
|
markerPath = fi.Name
|
|
|
|
if len(entries) > 0 {
|
2016-04-27 09:15:40 +02:00
|
|
|
|
|
|
|
// We reach here for non-recursive case and a leaf entry.
|
|
|
|
|
2016-04-27 02:57:16 +02:00
|
|
|
for _, entry := range entries {
|
|
|
|
allFileInfos = append(allFileInfos, entry)
|
2016-04-27 09:15:40 +02: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.
|
|
|
|
// eof : eof returned by o.storage.ListFiles()
|
|
|
|
// error : nil
|
|
|
|
return
|
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
}
|
2016-04-27 09:15:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-04-27 02:57:16 +02:00
|
|
|
specialFile := path.Base(fi.Name)
|
|
|
|
if strings.Contains(specialFile, ".") {
|
|
|
|
// Contains partnumber and md5sum info, skip this.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 09:15:40 +02:00
|
|
|
allFileInfos = append(allFileInfos, fi)
|
2016-04-27 02:57:16 +02: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 09:15:40 +02:00
|
|
|
// Return values:
|
|
|
|
// allFileInfos : "maxKeys" number of entries.
|
|
|
|
// eof : eof returned by o.storage.ListFiles()
|
|
|
|
// error : nil
|
|
|
|
return
|
2016-04-27 02:57:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we have reached eof then we break out.
|
|
|
|
if eof {
|
|
|
|
break
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
|
|
|
|
// Return entries here.
|
|
|
|
return allFileInfos, eof, nil
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMultipartUploads - list multipart uploads.
|
|
|
|
func (o objectAPI) ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error) {
|
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListMultipartsInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectPrefix(prefix) {
|
|
|
|
return ListMultipartsInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: prefix})
|
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
2016-04-22 03:05:26 +02:00
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
2016-04-11 10:29:18 +02:00
|
|
|
return ListMultipartsInfo{}, probe.NewError(UnsupportedDelimiter{
|
|
|
|
Delimiter: delimiter,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, prefix) {
|
|
|
|
return ListMultipartsInfo{}, probe.NewError(InvalidMarkerPrefixCombination{
|
|
|
|
Marker: keyMarker,
|
|
|
|
Prefix: prefix,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if uploadIDMarker != "" {
|
2016-04-22 03:05:26 +02:00
|
|
|
if strings.HasSuffix(keyMarker, slashSeparator) {
|
2016-04-11 10:29:18 +02:00
|
|
|
return result, probe.NewError(InvalidUploadIDKeyCombination{
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
id, e := uuid.Parse(uploadIDMarker)
|
|
|
|
if e != nil {
|
|
|
|
return result, probe.NewError(e)
|
|
|
|
}
|
|
|
|
if id.IsZero() {
|
|
|
|
return result, probe.NewError(MalformedUploadID{
|
|
|
|
UploadID: uploadIDMarker,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recursive := true
|
2016-04-22 03:05:26 +02:00
|
|
|
if delimiter == slashSeparator {
|
2016-04-11 10:29:18 +02:00
|
|
|
recursive = false
|
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
|
2016-04-13 10:00:30 +02:00
|
|
|
result.IsTruncated = true
|
2016-04-16 20:43:03 +02:00
|
|
|
result.MaxUploads = maxUploads
|
2016-04-27 02:57:16 +02:00
|
|
|
|
2016-04-22 03:05:26 +02:00
|
|
|
// Not using path.Join() as it strips off the trailing '/'.
|
2016-04-16 20:43:03 +02:00
|
|
|
// Also bucket should always be followed by '/' even if prefix is empty.
|
|
|
|
prefixPath := pathJoin(bucket, prefix)
|
2016-04-27 02:57:16 +02:00
|
|
|
keyMarkerPath := ""
|
|
|
|
if keyMarker != "" {
|
|
|
|
keyMarkerPath = path.Join(bucket, keyMarker, uploadIDMarker)
|
2016-04-13 10:00:30 +02:00
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
// List all the multipart files at prefixPath, starting with
|
|
|
|
// marker keyMarkerPath.
|
|
|
|
fileInfos, eof, e := o.listMetaVolumeFiles(prefixPath, keyMarkerPath, recursive, maxUploads)
|
|
|
|
if e != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": prefixPath,
|
|
|
|
"markerPath": keyMarkerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"maxUploads": maxUploads,
|
|
|
|
}).Errorf("listMetaVolumeFiles failed with %s", e)
|
|
|
|
return ListMultipartsInfo{}, probe.NewError(e)
|
2016-04-13 10:00:30 +02:00
|
|
|
}
|
|
|
|
for _, fi := range fileInfos {
|
2016-04-27 02:57:16 +02:00
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
|
|
|
if fi.Mode.IsDir() {
|
|
|
|
objectName = strings.TrimPrefix(fi.Name, retainSlash(bucket))
|
|
|
|
// For a directory entry
|
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objectName)
|
2016-04-13 10:00:30 +02:00
|
|
|
continue
|
2016-04-27 02:57:16 +02:00
|
|
|
} 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 10:00:30 +02:00
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
result.NextKeyMarker = objectName
|
|
|
|
result.NextUploadIDMarker = uploadID
|
2016-04-13 10:00:30 +02:00
|
|
|
}
|
2016-04-27 02:57:16 +02:00
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
2016-04-16 20:43:03 +02:00
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) NewMultipartUpload(bucket, object string) (string, *probe.Error) {
|
2016-04-25 19:39:28 +02:00
|
|
|
// Verify if bucket name is valid.
|
2016-04-11 10:29:18 +02:00
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-04-25 19:39:28 +02:00
|
|
|
// Verify if object name is valid.
|
2016-04-11 10:29:18 +02:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-04-25 19:39:28 +02:00
|
|
|
// Verify whether the bucket exists.
|
|
|
|
isExist, err := o.isBucketExist(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return "", probe.NewError(err)
|
|
|
|
}
|
|
|
|
if !isExist {
|
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-04-11 10:29:18 +02:00
|
|
|
if _, e := o.storage.StatVol(minioMetaVolume); e != nil {
|
|
|
|
if e == errVolumeNotFound {
|
|
|
|
e = o.storage.MakeVol(minioMetaVolume)
|
|
|
|
if e != nil {
|
2016-04-19 11:42:10 +02:00
|
|
|
if e == errDiskFull {
|
|
|
|
return "", probe.NewError(StorageFull{})
|
|
|
|
}
|
2016-04-11 10:29:18 +02:00
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
uuid, e := uuid.New()
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
uploadID := uuid.String()
|
2016-04-16 21:48:41 +02:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
if _, e = o.storage.StatFile(minioMetaVolume, uploadIDPath); e != nil {
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != errFileNotFound {
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e, minioMetaVolume, uploadIDPath))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
// uploadIDPath doesn't exist, so create empty file to reserve the name
|
2016-04-11 10:29:18 +02:00
|
|
|
var w io.WriteCloser
|
2016-04-16 21:48:41 +02:00
|
|
|
if w, e = o.storage.CreateFile(minioMetaVolume, uploadIDPath); e == nil {
|
2016-04-20 06:58:11 +02:00
|
|
|
// Close the writer.
|
2016-04-11 10:29:18 +02:00
|
|
|
if e = w.Close(); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e, minioMetaVolume, uploadIDPath))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
return uploadID, nil
|
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
// uploadIDPath already exists.
|
2016-04-11 10:29:18 +02:00
|
|
|
// loop again to try with different uuid generated.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 21:48:41 +02:00
|
|
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
|
|
|
func (o objectAPI) isUploadIDExists(bucket, object, uploadID string) (bool, error) {
|
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
st, e := o.storage.StatFile(minioMetaVolume, uploadIDPath)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
// Upload id does not exist.
|
2016-04-11 10:29:18 +02:00
|
|
|
if e == errFileNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, e
|
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
// Upload id exists and is a regular file.
|
2016-04-11 10:29:18 +02:00
|
|
|
return st.Mode.IsRegular(), nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:28 +02:00
|
|
|
// PutObjectPart - writes the multipart upload chunks.
|
2016-04-11 10:29:18 +02:00
|
|
|
func (o objectAPI) PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-04-25 19:39:28 +02:00
|
|
|
// Verify whether the bucket exists.
|
|
|
|
isExist, err := o.isBucketExist(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return "", probe.NewError(err)
|
|
|
|
}
|
|
|
|
if !isExist {
|
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-04-16 21:48:41 +02:00
|
|
|
if status, e := o.isUploadIDExists(bucket, object, uploadID); e != nil {
|
2016-04-11 10:29:18 +02:00
|
|
|
return "", probe.NewError(e)
|
|
|
|
} else if !status {
|
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
|
|
|
|
partSuffix := fmt.Sprintf("%s.%d.%s", uploadID, partID, md5Hex)
|
2016-04-13 20:32:47 +02:00
|
|
|
fileWriter, e := o.storage.CreateFile(minioMetaVolume, path.Join(bucket, object, partSuffix))
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-11 10:29:18 +02: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 {
|
|
|
|
if _, e = io.CopyN(multiWriter, data, size); e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-25 19:39:28 +02: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.
|
|
|
|
if _, e = io.CopyN(ioutil.Discard, data, 1); e == nil {
|
|
|
|
safeCloseAndRemove(fileWriter)
|
|
|
|
return "", probe.NewError(UnExpectedDataSize{Size: int(size)})
|
|
|
|
}
|
2016-04-11 10:29:18 +02:00
|
|
|
} else {
|
|
|
|
if _, e = io.Copy(multiWriter, data); e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-04-16 21:48:41 +02:00
|
|
|
safeCloseAndRemove(fileWriter)
|
2016-04-11 10:29:18 +02:00
|
|
|
return "", probe.NewError(BadDigest{md5Hex, newMD5Hex})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e = fileWriter.Close()
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListPartsInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ListPartsInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
if status, e := o.isUploadIDExists(bucket, object, uploadID); e != nil {
|
2016-04-11 10:29:18 +02:00
|
|
|
return ListPartsInfo{}, probe.NewError(e)
|
|
|
|
} else if !status {
|
|
|
|
return ListPartsInfo{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
result := ListPartsInfo{}
|
2016-04-22 03:05:26 +02:00
|
|
|
var markerPath string
|
2016-04-11 10:29:18 +02:00
|
|
|
nextPartNumberMarker := 0
|
2016-04-16 21:48:41 +02: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 10:29:18 +02:00
|
|
|
if partNumberMarker > 0 {
|
2016-04-21 02:23:23 +02:00
|
|
|
partNumberMarkerPath := uploadIDPath + "." + strconv.Itoa(partNumberMarker) + "."
|
|
|
|
fileInfos, _, e := o.storage.ListFiles(minioMetaVolume, partNumberMarkerPath, "", false, 1)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-21 02:23:23 +02:00
|
|
|
return result, probe.NewError(toObjectErr(e, minioMetaVolume, partNumberMarkerPath))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
if len(fileInfos) == 0 {
|
|
|
|
return result, probe.NewError(InvalidPart{})
|
|
|
|
}
|
2016-04-22 03:05:26 +02:00
|
|
|
markerPath = fileInfos[0].Name
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-22 03:05:26 +02:00
|
|
|
uploadIDPrefix := uploadIDPath + "."
|
|
|
|
fileInfos, eof, e := o.storage.ListFiles(minioMetaVolume, uploadIDPrefix, markerPath, false, maxParts)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
|
|
|
return result, probe.NewError(InvalidPart{})
|
|
|
|
}
|
|
|
|
for _, fileInfo := range fileInfos {
|
2016-04-13 20:32:47 +02:00
|
|
|
fileName := path.Base(fileInfo.Name)
|
2016-04-11 10:29:18 +02:00
|
|
|
splitResult := strings.Split(fileName, ".")
|
|
|
|
partNum, e := strconv.Atoi(splitResult[1])
|
|
|
|
if e != nil {
|
|
|
|
return result, probe.NewError(e)
|
|
|
|
}
|
|
|
|
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-16 21:48:41 +02:00
|
|
|
// Create an s3 compatible MD5sum for complete multipart transaction.
|
|
|
|
func makeS3MD5(md5Strs ...string) (string, *probe.Error) {
|
|
|
|
var finalMD5Bytes []byte
|
|
|
|
for _, md5Str := range md5Strs {
|
|
|
|
md5Bytes, e := hex.DecodeString(md5Str)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
finalMD5Bytes = append(finalMD5Bytes, md5Bytes...)
|
|
|
|
}
|
|
|
|
md5Hasher := md5.New()
|
|
|
|
md5Hasher.Write(finalMD5Bytes)
|
|
|
|
s3MD5 := fmt.Sprintf("%s-%d", hex.EncodeToString(md5Hasher.Sum(nil)), len(md5Strs))
|
|
|
|
return s3MD5, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (string, *probe.Error) {
|
2016-04-11 10:29:18 +02:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(ObjectNameInvalid{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
})
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
if status, e := o.isUploadIDExists(bucket, object, uploadID); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2016-04-11 10:29:18 +02:00
|
|
|
} else if !status {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
|
2016-04-11 10:29:18 +02:00
|
|
|
fileWriter, e := o.storage.CreateFile(bucket, object)
|
|
|
|
if e != nil {
|
2016-04-21 02:23:23 +02:00
|
|
|
return "", probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
|
|
|
|
var md5Sums []string
|
2016-04-11 10:29:18 +02:00
|
|
|
for _, part := range parts {
|
2016-03-28 06:52:38 +02:00
|
|
|
// Construct part suffix.
|
2016-04-11 10:29:18 +02:00
|
|
|
partSuffix := fmt.Sprintf("%s.%d.%s", uploadID, part.PartNumber, part.ETag)
|
|
|
|
var fileReader io.ReadCloser
|
2016-04-13 20:32:47 +02:00
|
|
|
fileReader, e = o.storage.ReadFile(minioMetaVolume, path.Join(bucket, object, partSuffix), 0)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
if e == errFileNotFound {
|
|
|
|
return "", probe.NewError(InvalidPart{})
|
|
|
|
}
|
|
|
|
return "", probe.NewError(e)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
_, e = io.Copy(fileWriter, fileReader)
|
|
|
|
if e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(e)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
e = fileReader.Close()
|
|
|
|
if e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(e)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
md5Sums = append(md5Sums, part.ETag)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
|
2016-04-11 10:29:18 +02:00
|
|
|
e = fileWriter.Close()
|
|
|
|
if e != nil {
|
2016-04-16 21:48:41 +02:00
|
|
|
return "", probe.NewError(e)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
|
|
|
|
// Save the s3 md5.
|
|
|
|
s3MD5, err := makeS3MD5(md5Sums...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err.Trace(md5Sums...)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
|
|
|
|
// Cleanup all the parts.
|
2016-04-11 10:29:18 +02:00
|
|
|
o.removeMultipartUpload(bucket, object, uploadID)
|
2016-04-16 21:48:41 +02:00
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// Return md5sum.
|
2016-04-16 21:48:41 +02:00
|
|
|
return s3MD5, nil
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) removeMultipartUpload(bucket, object, uploadID string) *probe.Error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
|
2016-04-11 10:29:18 +02:00
|
|
|
marker := ""
|
|
|
|
for {
|
2016-04-16 21:48:41 +02:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
fileInfos, eof, e := o.storage.ListFiles(minioMetaVolume, uploadIDPath, marker, false, 1000)
|
2016-04-11 10:29:18 +02:00
|
|
|
if e != nil {
|
2016-04-21 02:23:23 +02:00
|
|
|
|
|
|
|
return probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
o.storage.DeleteFile(minioMetaVolume, fileInfo.Name)
|
|
|
|
marker = fileInfo.Name
|
|
|
|
}
|
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) AbortMultipartUpload(bucket, object, uploadID string) *probe.Error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
if status, e := o.isUploadIDExists(bucket, object, uploadID); e != nil {
|
2016-04-11 10:29:18 +02:00
|
|
|
return probe.NewError(e)
|
|
|
|
} else if !status {
|
|
|
|
return probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
2016-04-16 21:48:41 +02:00
|
|
|
err := o.removeMultipartUpload(bucket, object, uploadID)
|
|
|
|
if err != nil {
|
|
|
|
return err.Trace(bucket, object, uploadID)
|
2016-04-11 10:29:18 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|