2015-10-16 14:26:01 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2015 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 fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio-xl/pkg/atomic"
|
|
|
|
"github.com/minio/minio-xl/pkg/crypto/sha256"
|
|
|
|
"github.com/minio/minio-xl/pkg/crypto/sha512"
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-10-17 22:17:33 -04:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-02-05 18:09:23 -05:00
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2015-10-16 14:26:01 -04:00
|
|
|
)
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// isValidUploadID - is upload id.
|
2016-02-05 23:05:56 -05:00
|
|
|
func (fs Filesystem) isValidUploadID(object, uploadID string) (ok bool) {
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RLock()
|
|
|
|
defer fs.rwLock.RUnlock()
|
2016-02-05 23:05:56 -05:00
|
|
|
_, ok = fs.multiparts.ActiveSession[uploadID]
|
2015-10-16 14:26:01 -04:00
|
|
|
if !ok {
|
2016-02-05 23:05:56 -05:00
|
|
|
return
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 23:05:56 -05:00
|
|
|
return
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMultipartUploads - list incomplete multipart sessions for a given BucketMultipartResourcesMetadata
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Input validation.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return BucketMultipartResourcesMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
var uploads []*UploadMetadata
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RLock()
|
|
|
|
defer fs.rwLock.RUnlock()
|
2016-02-05 23:05:56 -05:00
|
|
|
for uploadID, session := range fs.multiparts.ActiveSession {
|
|
|
|
objectName := session.ObjectName
|
|
|
|
if strings.HasPrefix(objectName, resources.Prefix) {
|
2015-10-16 14:26:01 -04:00
|
|
|
if len(uploads) > resources.MaxUploads {
|
|
|
|
sort.Sort(byUploadMetadataKey(uploads))
|
|
|
|
resources.Upload = uploads
|
2016-02-05 23:05:56 -05:00
|
|
|
resources.NextKeyMarker = session.ObjectName
|
|
|
|
resources.NextUploadIDMarker = uploadID
|
2015-10-16 14:26:01 -04:00
|
|
|
resources.IsTruncated = true
|
|
|
|
return resources, nil
|
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
// UploadIDMarker is ignored if KeyMarker is empty.
|
2015-10-16 14:26:01 -04:00
|
|
|
switch {
|
|
|
|
case resources.KeyMarker != "" && resources.UploadIDMarker == "":
|
2016-02-05 23:05:56 -05:00
|
|
|
if objectName > resources.KeyMarker {
|
2015-10-16 14:26:01 -04:00
|
|
|
upload := new(UploadMetadata)
|
2016-02-05 23:05:56 -05:00
|
|
|
upload.Object = objectName
|
|
|
|
upload.UploadID = uploadID
|
2015-10-16 14:26:01 -04:00
|
|
|
upload.Initiated = session.Initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
case resources.KeyMarker != "" && resources.UploadIDMarker != "":
|
|
|
|
if session.UploadID > resources.UploadIDMarker {
|
2016-02-05 23:05:56 -05:00
|
|
|
if objectName >= resources.KeyMarker {
|
2015-10-16 14:26:01 -04:00
|
|
|
upload := new(UploadMetadata)
|
2016-02-05 23:05:56 -05:00
|
|
|
upload.Object = objectName
|
|
|
|
upload.UploadID = uploadID
|
2015-10-16 14:26:01 -04:00
|
|
|
upload.Initiated = session.Initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
upload := new(UploadMetadata)
|
2016-02-05 23:05:56 -05:00
|
|
|
upload.Object = objectName
|
|
|
|
upload.UploadID = uploadID
|
2015-10-16 14:26:01 -04:00
|
|
|
upload.Initiated = session.Initiated
|
|
|
|
uploads = append(uploads, upload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(byUploadMetadataKey(uploads))
|
|
|
|
resources.Upload = uploads
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:15:48 -05:00
|
|
|
// verify if parts sent over the network do really match with what we
|
|
|
|
// have for the session.
|
2016-02-05 19:47:31 -05:00
|
|
|
func doPartsMatch(parts []CompletePart, savedParts []PartMetadata) bool {
|
2016-02-05 05:15:48 -05:00
|
|
|
if parts == nil || savedParts == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Range of incoming parts and compare them with saved parts.
|
|
|
|
for i, part := range parts {
|
|
|
|
if strings.Trim(part.ETag, "\"") != savedParts[i].ETag {
|
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type multiCloser struct {
|
|
|
|
Closers []io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m multiCloser) Close() error {
|
|
|
|
for _, c := range m.Closers {
|
|
|
|
if e := c.Close(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiCloser - returns a Closer that's the logical
|
|
|
|
// concatenation of the provided input closers. They're closed
|
|
|
|
// sequentially. If any of the closers return a non-nil error, Close
|
|
|
|
// will return that error.
|
|
|
|
func MultiCloser(closers ...io.Closer) io.Closer {
|
|
|
|
return multiCloser{closers}
|
|
|
|
}
|
2015-11-22 04:49:10 -05:00
|
|
|
|
2016-02-05 05:15:48 -05:00
|
|
|
// removeParts - remove all parts.
|
2016-02-05 19:47:31 -05:00
|
|
|
func removeParts(partPathPrefix string, parts []PartMetadata) *probe.Error {
|
2016-02-05 05:15:48 -05:00
|
|
|
for _, part := range parts {
|
2016-02-05 19:47:31 -05:00
|
|
|
// We are on purpose ignoring the return values here, since
|
|
|
|
// another thread would have purged these entries.
|
2016-02-05 23:05:56 -05:00
|
|
|
os.Remove(partPathPrefix + part.ETag + fmt.Sprintf("$%d-$multiparts", part.PartNumber))
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-22 04:49:10 -05:00
|
|
|
|
2016-02-05 05:15:48 -05:00
|
|
|
// saveParts - concantenate and save all parts.
|
|
|
|
func saveParts(partPathPrefix string, mw io.Writer, parts []CompletePart) *probe.Error {
|
|
|
|
var partReaders []io.Reader
|
|
|
|
var partClosers []io.Closer
|
|
|
|
for _, part := range parts {
|
2016-02-05 23:05:56 -05:00
|
|
|
md5Sum := strings.TrimPrefix(part.ETag, "\"")
|
|
|
|
md5Sum = strings.TrimSuffix(md5Sum, "\"")
|
|
|
|
partFile, e := os.OpenFile(partPathPrefix+md5Sum+fmt.Sprintf("$%d-$multiparts", part.PartNumber), os.O_RDONLY, 0600)
|
2016-02-05 05:15:48 -05:00
|
|
|
if e != nil {
|
2015-11-22 04:49:10 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
partReaders = append(partReaders, partFile)
|
|
|
|
partClosers = append(partClosers, partFile)
|
|
|
|
}
|
|
|
|
// Concatenate a list of closers and close upon return.
|
|
|
|
closer := MultiCloser(partClosers...)
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
reader := io.MultiReader(partReaders...)
|
|
|
|
readBuffer := make([]byte, 4*1024*1024)
|
|
|
|
if _, e := io.CopyBuffer(mw, reader, readBuffer); e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultipartUpload - initiate a new multipart session
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) NewMultipartUpload(bucket, object string) (string, *probe.Error) {
|
2016-01-25 02:03:38 -05:00
|
|
|
di, e := disk.GetInfo(fs.path)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Remove 5% from total space for cumulative disk space used for
|
|
|
|
// journalling, inodes etc.
|
2016-01-25 02:03:38 -05:00
|
|
|
availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100
|
2015-10-19 13:29:54 -04:00
|
|
|
if int64(availableDiskSpace) <= fs.minFreeDisk {
|
2015-10-17 22:17:33 -04:00
|
|
|
return "", probe.NewError(RootPathFull{Path: fs.path})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Input validation.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", probe.NewError(ObjectNameInvalid{Object: object})
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-01-27 04:52:54 -05:00
|
|
|
if _, e = os.Stat(bucketPath); e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
|
2015-10-16 14:26:01 -04:00
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
|
|
|
objectDir := filepath.Dir(objectPath)
|
2016-01-27 04:52:54 -05:00
|
|
|
if _, e = os.Stat(objectDir); e != nil {
|
2016-01-26 17:57:46 -05:00
|
|
|
if !os.IsNotExist(e) {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
|
|
|
e = os.MkdirAll(objectDir, 0700)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Generate new upload id.
|
2015-10-16 14:26:01 -04:00
|
|
|
id := []byte(strconv.FormatInt(rand.Int63(), 10) + bucket + object + time.Now().String())
|
|
|
|
uploadIDSum := sha512.Sum512(id)
|
|
|
|
uploadID := base64.URLEncoding.EncodeToString(uploadIDSum[:])[:47]
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring write lock.
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Lock()
|
2016-02-04 23:40:58 -05:00
|
|
|
// Initialize multipart session.
|
2016-01-26 17:57:46 -05:00
|
|
|
mpartSession := &MultipartSession{}
|
2015-10-16 14:26:01 -04:00
|
|
|
mpartSession.TotalParts = 0
|
2016-02-05 23:05:56 -05:00
|
|
|
mpartSession.ObjectName = object
|
2015-10-16 14:26:01 -04:00
|
|
|
mpartSession.UploadID = uploadID
|
|
|
|
mpartSession.Initiated = time.Now().UTC()
|
2016-02-05 19:47:31 -05:00
|
|
|
var parts []PartMetadata
|
2015-10-16 14:26:01 -04:00
|
|
|
mpartSession.Parts = parts
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.multiparts.ActiveSession[uploadID] = mpartSession
|
2016-02-05 05:15:48 -05:00
|
|
|
if err := saveMultipartsSession(*fs.multiparts); err != nil {
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2016-02-05 19:47:31 -05:00
|
|
|
return "", err.Trace(objectPath)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2015-10-16 14:26:01 -04:00
|
|
|
return uploadID, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// partNumber is a sortable interface for Part slice.
|
2016-02-05 19:47:31 -05:00
|
|
|
type partNumber []PartMetadata
|
2015-10-16 14:26:01 -04:00
|
|
|
|
|
|
|
func (a partNumber) Len() int { return len(a) }
|
|
|
|
func (a partNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a partNumber) Less(i, j int) bool { return a[i].PartNumber < a[j].PartNumber }
|
|
|
|
|
|
|
|
// CreateObjectPart - create a part in a multipart session
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) CreateObjectPart(bucket, object, uploadID, expectedMD5Sum string, partID int, size int64, data io.Reader, signature *Signature) (string, *probe.Error) {
|
2016-01-25 02:03:38 -05:00
|
|
|
di, err := disk.GetInfo(fs.path)
|
2015-10-17 22:17:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", probe.NewError(err)
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Remove 5% from total space for cumulative disk space used for
|
|
|
|
// journalling, inodes etc.
|
2016-01-25 02:03:38 -05:00
|
|
|
availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100
|
2015-10-19 13:29:54 -04:00
|
|
|
if int64(availableDiskSpace) <= fs.minFreeDisk {
|
2015-10-17 22:17:33 -04:00
|
|
|
return "", probe.NewError(RootPathFull{Path: fs.path})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Part id cannot be negative.
|
2015-10-16 14:26:01 -04:00
|
|
|
if partID <= 0 {
|
|
|
|
return "", probe.NewError(errors.New("invalid part id, cannot be zero or less than zero"))
|
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
|
|
|
|
// Check bucket name valid.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path legal.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify upload is valid for the incoming object.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !fs.isValidUploadID(object, uploadID) {
|
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
2015-10-17 22:17:33 -04:00
|
|
|
var expectedMD5SumBytes []byte
|
|
|
|
expectedMD5SumBytes, err = base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum))
|
2015-10-16 14:26:01 -04:00
|
|
|
if err != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Pro-actively close the connection
|
2016-02-05 05:15:48 -05:00
|
|
|
return "", probe.NewError(InvalidDigest{MD5: expectedMD5Sum})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2015-10-17 22:17:33 -04:00
|
|
|
if _, err = os.Stat(bucketPath); err != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2015-10-16 14:26:01 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return "", probe.NewError(err)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-02-05 05:15:48 -05:00
|
|
|
partPathPrefix := objectPath + uploadID
|
2016-02-05 23:05:56 -05:00
|
|
|
partPath := partPathPrefix + expectedMD5Sum + fmt.Sprintf("$%d-$multiparts", partID)
|
2016-02-04 15:52:25 -05:00
|
|
|
partFile, e := atomic.FileCreateWithPrefix(partPath, "$multiparts")
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
|
|
|
md5Hasher := md5.New()
|
|
|
|
sha256Hasher := sha256.New()
|
|
|
|
partWriter := io.MultiWriter(partFile, md5Hasher, sha256Hasher)
|
|
|
|
if _, e = io.CopyN(partWriter, data, size); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
partFile.CloseAndPurge()
|
2016-02-04 15:52:25 -05:00
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
|
|
|
md5sum := hex.EncodeToString(md5Hasher.Sum(nil))
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if the written object is equal to what is expected, only
|
|
|
|
// if it is requested as such.
|
2015-10-16 14:26:01 -04:00
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
2016-02-04 15:52:25 -05:00
|
|
|
if !isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), md5sum) {
|
2015-10-16 14:26:01 -04:00
|
|
|
partFile.CloseAndPurge()
|
2016-02-05 05:15:48 -05:00
|
|
|
return "", probe.NewError(BadDigest{MD5: expectedMD5Sum, Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if signature != nil {
|
2016-02-05 05:15:48 -05:00
|
|
|
ok, err := signature.DoesSignatureMatch(hex.EncodeToString(sha256Hasher.Sum(nil)))
|
2016-02-04 15:52:25 -05:00
|
|
|
if err != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
partFile.CloseAndPurge()
|
2016-02-04 15:52:25 -05:00
|
|
|
return "", err.Trace()
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
partFile.CloseAndPurge()
|
|
|
|
return "", probe.NewError(SignatureDoesNotMatch{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
partFile.Close()
|
|
|
|
|
2016-02-04 15:52:25 -05:00
|
|
|
fi, e := os.Stat(partPath)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
partMetadata := PartMetadata{}
|
|
|
|
partMetadata.ETag = md5sum
|
|
|
|
partMetadata.PartNumber = partID
|
|
|
|
partMetadata.Size = fi.Size()
|
|
|
|
partMetadata.LastModified = fi.ModTime()
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring read lock.
|
2016-02-05 19:47:31 -05:00
|
|
|
fs.rwLock.RLock()
|
2016-02-05 23:05:56 -05:00
|
|
|
deserializedMultipartSession, ok := fs.multiparts.ActiveSession[uploadID]
|
2016-02-05 19:47:31 -05:00
|
|
|
fs.rwLock.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-05 19:47:31 -05:00
|
|
|
// Append any pre-existing partNumber with new metadata, otherwise
|
|
|
|
// append to the list.
|
|
|
|
if len(deserializedMultipartSession.Parts) < partID {
|
|
|
|
deserializedMultipartSession.Parts = append(deserializedMultipartSession.Parts, partMetadata)
|
|
|
|
} else {
|
|
|
|
deserializedMultipartSession.Parts[partID-1] = partMetadata
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 19:47:31 -05:00
|
|
|
deserializedMultipartSession.TotalParts = len(deserializedMultipartSession.Parts)
|
2016-02-05 05:15:48 -05:00
|
|
|
// Sort by part number before saving.
|
2015-10-16 14:26:01 -04:00
|
|
|
sort.Sort(partNumber(deserializedMultipartSession.Parts))
|
2016-02-05 23:05:56 -05:00
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring write lock.
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Lock()
|
|
|
|
fs.multiparts.ActiveSession[uploadID] = deserializedMultipartSession
|
2016-02-05 19:47:31 -05:00
|
|
|
if err := saveMultipartsSession(*fs.multiparts); err != nil {
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2016-02-05 19:47:31 -05:00
|
|
|
return "", err.Trace(partPathPrefix)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2016-02-05 19:47:31 -05:00
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Return etag.
|
2015-10-16 14:26:01 -04:00
|
|
|
return partMetadata.ETag, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteMultipartUpload - complete a multipart upload and persist the data
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) CompleteMultipartUpload(bucket, object, uploadID string, data io.Reader, signature *Signature) (ObjectMetadata, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket name is valid.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path is legal.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if valid upload for incoming object.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !fs.isValidUploadID(object, uploadID) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-04 15:52:25 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2016-02-04 15:52:25 -05:00
|
|
|
if os.IsNotExist(e) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return ObjectMetadata{}, probe.NewError(InternalError{})
|
|
|
|
}
|
|
|
|
|
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-02-05 23:05:56 -05:00
|
|
|
file, e := atomic.FileCreateWithPrefix(objectPath, "$tmpobject")
|
2016-02-04 15:52:25 -05:00
|
|
|
if e != nil {
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
md5Hasher := md5.New()
|
|
|
|
objectWriter := io.MultiWriter(file, md5Hasher)
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-02-04 15:52:25 -05:00
|
|
|
partBytes, e := ioutil.ReadAll(data)
|
|
|
|
if e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-04 15:52:25 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
if signature != nil {
|
|
|
|
sh := sha256.New()
|
|
|
|
sh.Write(partBytes)
|
2016-02-04 15:52:25 -05:00
|
|
|
ok, err := signature.DoesSignatureMatch(hex.EncodeToString(sh.Sum(nil)))
|
|
|
|
if err != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-04 15:52:25 -05:00
|
|
|
return ObjectMetadata{}, err.Trace()
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
file.CloseAndPurge()
|
|
|
|
return ObjectMetadata{}, probe.NewError(SignatureDoesNotMatch{})
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
completeMultipartUpload := &CompleteMultipartUpload{}
|
|
|
|
if e := xml.Unmarshal(partBytes, completeMultipartUpload); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
|
|
|
return ObjectMetadata{}, probe.NewError(MalformedXML{})
|
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
if !sort.IsSorted(completedParts(completeMultipartUpload.Part)) {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidPartOrder{})
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:15:48 -05:00
|
|
|
// Save parts for verification.
|
|
|
|
parts := completeMultipartUpload.Part
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring read lock.
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RLock()
|
2016-02-05 23:05:56 -05:00
|
|
|
savedParts := fs.multiparts.ActiveSession[uploadID].Parts
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RUnlock()
|
|
|
|
|
|
|
|
if !doPartsMatch(parts, savedParts) {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-05 05:15:48 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidPart{})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Parts successfully validated, save all the parts.
|
2016-02-05 05:15:48 -05:00
|
|
|
partPathPrefix := objectPath + uploadID
|
|
|
|
if err := saveParts(partPathPrefix, objectWriter, parts); err != nil {
|
|
|
|
file.CloseAndPurge()
|
|
|
|
return ObjectMetadata{}, err.Trace(partPathPrefix)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 19:47:31 -05:00
|
|
|
// Successfully saved, remove all parts.
|
|
|
|
removeParts(partPathPrefix, savedParts)
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring write lock.
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Lock()
|
2016-02-05 23:05:56 -05:00
|
|
|
delete(fs.multiparts.ActiveSession, uploadID)
|
2016-02-05 05:15:48 -05:00
|
|
|
if err := saveMultipartsSession(*fs.multiparts); err != nil {
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-05 05:15:48 -05:00
|
|
|
return ObjectMetadata{}, err.Trace(partPathPrefix)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
file.Close()
|
2016-02-06 04:36:43 -05:00
|
|
|
fs.rwLock.Unlock()
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Send stat again to get object metadata.
|
2016-02-04 15:52:25 -05:00
|
|
|
st, e := os.Stat(objectPath)
|
|
|
|
if e != nil {
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" {
|
2016-02-05 18:09:23 -05:00
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
newObject := ObjectMetadata{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
Created: st.ModTime(),
|
|
|
|
Size: st.Size(),
|
2016-02-01 15:19:54 -05:00
|
|
|
ContentType: contentType,
|
2016-02-05 05:15:48 -05:00
|
|
|
MD5: hex.EncodeToString(md5Hasher.Sum(nil)),
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return newObject, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectParts - list parts from incomplete multipart session for a given ObjectResourcesMetadata
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket name is valid.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path legal.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:05:56 -05:00
|
|
|
// Save upload id.
|
|
|
|
uploadID := resources.UploadID
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if upload id is valid for incoming object.
|
2016-02-05 23:05:56 -05:00
|
|
|
if !fs.isValidUploadID(object, uploadID) {
|
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
objectResourcesMetadata := resources
|
|
|
|
objectResourcesMetadata.Bucket = bucket
|
|
|
|
objectResourcesMetadata.Object = object
|
|
|
|
var startPartNumber int
|
|
|
|
switch {
|
|
|
|
case objectResourcesMetadata.PartNumberMarker == 0:
|
|
|
|
startPartNumber = 1
|
|
|
|
default:
|
|
|
|
startPartNumber = objectResourcesMetadata.PartNumberMarker
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring read lock.
|
2016-02-05 19:47:31 -05:00
|
|
|
fs.rwLock.RLock()
|
2016-02-05 23:05:56 -05:00
|
|
|
deserializedMultipartSession, ok := fs.multiparts.ActiveSession[uploadID]
|
2016-02-05 19:47:31 -05:00
|
|
|
fs.rwLock.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return ObjectResourcesMetadata{}, probe.NewError(InvalidUploadID{UploadID: resources.UploadID})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 19:47:31 -05:00
|
|
|
var parts []PartMetadata
|
2015-10-16 14:26:01 -04:00
|
|
|
for i := startPartNumber; i <= deserializedMultipartSession.TotalParts; i++ {
|
|
|
|
if len(parts) > objectResourcesMetadata.MaxParts {
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.IsTruncated = true
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
objectResourcesMetadata.NextPartNumberMarker = i
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
|
|
|
parts = append(parts, deserializedMultipartSession.Parts[i-1])
|
|
|
|
}
|
|
|
|
sort.Sort(partNumber(parts))
|
|
|
|
objectResourcesMetadata.Part = parts
|
|
|
|
return objectResourcesMetadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUpload - abort an incomplete multipart session
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) AbortMultipartUpload(bucket, object, uploadID string) *probe.Error {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket name valid.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path legal.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fs.isValidUploadID(object, uploadID) {
|
|
|
|
return probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-16 14:26:01 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-02-05 05:15:48 -05:00
|
|
|
partPathPrefix := objectPath + uploadID
|
2016-02-06 04:36:43 -05:00
|
|
|
|
|
|
|
// Critical region requiring read lock.
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RLock()
|
2016-02-05 23:05:56 -05:00
|
|
|
savedParts := fs.multiparts.ActiveSession[uploadID].Parts
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RUnlock()
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Remove all parts.
|
2016-02-05 05:15:48 -05:00
|
|
|
if err := removeParts(partPathPrefix, savedParts); err != nil {
|
|
|
|
return err.Trace(partPathPrefix)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring write lock.
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Lock()
|
2016-02-05 23:05:56 -05:00
|
|
|
delete(fs.multiparts.ActiveSession, uploadID)
|
2016-02-05 05:15:48 -05:00
|
|
|
if err := saveMultipartsSession(*fs.multiparts); err != nil {
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2016-02-05 05:15:48 -05:00
|
|
|
return err.Trace(partPathPrefix)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 23:05:56 -05:00
|
|
|
fs.rwLock.Unlock()
|
2015-10-16 14:26:01 -04:00
|
|
|
return nil
|
|
|
|
}
|