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.
|
|
|
|
*/
|
|
|
|
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
package main
|
2015-10-16 14:26:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-03-28 12:52:09 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
2015-10-16 14:26:01 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2016-02-10 19:40:09 -05:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2016-03-28 12:52:09 -04:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2015-10-16 14:26:01 -04:00
|
|
|
)
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
const configDir = ".minio"
|
|
|
|
const uploadIDSuffix = ".uploadid"
|
2016-02-19 19:04:29 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func removeFileTree(fileName string, level string) error {
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := os.Remove(fileName); e != nil {
|
|
|
|
return e
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
for fileDir := filepath.Dir(fileName); fileDir > level; fileDir = filepath.Dir(fileDir) {
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := isDirEmpty(fileDir); e != nil {
|
|
|
|
return e
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
|
|
|
break
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := os.Remove(fileDir); e != nil {
|
|
|
|
return e
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
return nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func safeWrite(fileName string, data io.Reader, size int64, md5sum string) error {
|
2016-04-05 14:24:23 -04:00
|
|
|
tempFile, e := ioutil.TempFile(filepath.Dir(fileName), filepath.Base(fileName)+"-")
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
md5Hasher := md5.New()
|
|
|
|
multiWriter := io.MultiWriter(md5Hasher, tempFile)
|
2016-04-05 14:24:23 -04:00
|
|
|
if _, e := io.CopyN(multiWriter, data, size); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
tempFile.Close()
|
|
|
|
os.Remove(tempFile.Name())
|
2016-04-05 14:24:23 -04:00
|
|
|
return e
|
2016-02-14 03:39:13 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
tempFile.Close()
|
|
|
|
|
|
|
|
dataMd5sum := hex.EncodeToString(md5Hasher.Sum(nil))
|
|
|
|
if md5sum != "" && !isMD5SumEqual(md5sum, dataMd5sum) {
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
return BadDigest{ExpectedMD5: md5sum, CalculatedMD5: dataMd5sum}
|
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := os.Rename(tempFile.Name(), fileName); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
os.Remove(tempFile.Name())
|
2016-04-05 14:24:23 -04:00
|
|
|
return e
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFileExist(filename string) (bool, error) {
|
2016-04-05 14:24:23 -04:00
|
|
|
fi, e := os.Lstat(filename)
|
|
|
|
if e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2016-03-28 12:52:09 -04:00
|
|
|
return false, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
return false, e
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
return fi.Mode().IsRegular(), nil
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 23:01:40 -05: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
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) newUploadID(bucket, object string) (string, error) {
|
|
|
|
metaObjectDir := filepath.Join(fs.path, configDir, bucket, object)
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// create metaObjectDir if not exist
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := isDirExist(metaObjectDir); e != nil {
|
|
|
|
return "", e
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := os.MkdirAll(metaObjectDir, 0755); e != nil {
|
|
|
|
return "", e
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
for {
|
2016-04-05 14:24:23 -04:00
|
|
|
uuid, e := uuid.New()
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
2015-11-22 04:49:10 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
uploadID := uuid.String()
|
|
|
|
uploadIDFile := filepath.Join(metaObjectDir, uploadID+uploadIDSuffix)
|
2016-04-05 14:24:23 -04:00
|
|
|
if _, e := os.Lstat(uploadIDFile); e != nil {
|
|
|
|
if !os.IsNotExist(e) {
|
|
|
|
return "", e
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
// uploadIDFile doesn't exist, so create empty file to reserve the name
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := ioutil.WriteFile(uploadIDFile, []byte{}, 0644); e != nil {
|
|
|
|
return "", e
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
return uploadID, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
// uploadIDFile already exists.
|
|
|
|
// loop again to try with different uuid generated.
|
2016-02-05 05:15:48 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) isUploadIDExist(bucket, object, uploadID string) (bool, error) {
|
|
|
|
return isFileExist(filepath.Join(fs.path, configDir, bucket, object, uploadID+uploadIDSuffix))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) cleanupUploadID(bucket, object, uploadID string) error {
|
|
|
|
metaObjectDir := filepath.Join(fs.path, configDir, bucket, object)
|
|
|
|
uploadIDPrefix := uploadID + "."
|
2015-10-17 22:17:33 -04:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
names, e := filteredReaddirnames(metaObjectDir,
|
2016-03-28 12:52:09 -04:00
|
|
|
func(name string) bool {
|
|
|
|
return strings.HasPrefix(name, uploadIDPrefix)
|
|
|
|
},
|
|
|
|
)
|
2015-10-17 22:17:33 -04:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e != nil {
|
|
|
|
return e
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
for _, name := range names {
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := os.Remove(filepath.Join(metaObjectDir, name)); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return InternalError{Err: err}
|
2016-04-05 14:24:23 -04:00
|
|
|
return e
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := isDirEmpty(metaObjectDir); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
// TODO: add log than returning error
|
|
|
|
//return InternalError{Err: err}
|
2016-04-05 14:24:23 -04:00
|
|
|
return e
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if status {
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := removeFileTree(metaObjectDir, filepath.Join(fs.path, configDir, bucket)); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
// TODO: add log than returning error
|
|
|
|
//return InternalError{Err: err}
|
2016-04-05 14:24:23 -04:00
|
|
|
return e
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) checkBucketArg(bucket string) (string, error) {
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
2016-02-14 03:39:13 -05:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
bucket = getActualBucketname(fs.path, bucket)
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := isDirExist(filepath.Join(fs.path, bucket)); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return "", InternalError{Err: err}
|
2016-04-05 14:24:23 -04:00
|
|
|
return "", e
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return bucket, nil
|
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) checkDiskFree() error {
|
2016-04-05 14:24:23 -04:00
|
|
|
di, e := disk.GetInfo(fs.path)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04: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 {
|
2016-03-28 12:52:09 -04:00
|
|
|
return RootPathFull{Path: fs.path}
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs Filesystem) checkMultipartArgs(bucket, object string) (string, error) {
|
2016-04-05 14:24:23 -04:00
|
|
|
bucket, e := fs.checkBucketArg(bucket)
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidObjectName(object) {
|
2016-03-28 12:52:09 -04:00
|
|
|
return "", ObjectNameInvalid{Object: object}
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return bucket, nil
|
|
|
|
}
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// NewMultipartUpload - initiate a new multipart session
|
|
|
|
func (fs Filesystem) NewMultipartUpload(bucket, object string) (string, *probe.Error) {
|
2016-04-05 14:24:23 -04:00
|
|
|
if bucketDirName, e := fs.checkMultipartArgs(bucket, object); e == nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
2016-04-05 14:24:23 -04:00
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := fs.checkDiskFree(); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2016-03-12 19:08:15 -05:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
uploadID, e := fs.newUploadID(bucket, object)
|
|
|
|
if e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-12 19:08:15 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return uploadID, nil
|
|
|
|
}
|
2016-03-12 19:08:15 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// PutObjectPart - create a part in a multipart session
|
|
|
|
func (fs Filesystem) PutObjectPart(bucket, object, uploadID string, partNumber int, size int64, data io.Reader, md5Hex string) (string, *probe.Error) {
|
2016-04-05 14:24:23 -04:00
|
|
|
if bucketDirName, e := fs.checkMultipartArgs(bucket, object); e == nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
2016-04-05 14:24:23 -04:00
|
|
|
return "", probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := fs.isUploadIDExist(bucket, object, uploadID); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return "", probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return "", probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
|
|
|
return "", probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// Part id cannot be negative.
|
|
|
|
if partNumber <= 0 {
|
|
|
|
return "", probe.NewError(errors.New("invalid part id, cannot be zero or less than zero"))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if partNumber > 10000 {
|
|
|
|
return "", probe.NewError(errors.New("invalid part id, should be not more than 10000"))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := fs.checkDiskFree(); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
2016-02-14 03:39:13 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
partFile := filepath.Join(fs.path, configDir, bucket, object, uploadID+"."+strconv.Itoa(partNumber)+"."+md5Hex)
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := safeWrite(partFile, data, size, md5Hex); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return md5Hex, nil
|
|
|
|
}
|
2016-02-14 03:39:13 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// AbortMultipartUpload - abort an incomplete multipart session
|
|
|
|
func (fs Filesystem) AbortMultipartUpload(bucket, object, uploadID string) *probe.Error {
|
2016-04-05 14:24:23 -04:00
|
|
|
if bucketDirName, e := fs.checkMultipartArgs(bucket, object); e == nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
2016-04-05 14:24:23 -04:00
|
|
|
return probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
2016-02-05 23:05:56 -05:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := fs.isUploadIDExist(bucket, object, uploadID); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
|
|
|
return probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
2016-03-07 00:32:49 -05:00
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := fs.cleanupUploadID(bucket, object, uploadID); e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
return nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteMultipartUpload - complete a multipart upload and persist the data
|
2016-03-28 12:52:09 -04:00
|
|
|
func (fs Filesystem) CompleteMultipartUpload(bucket, object, uploadID string, parts []completePart) (ObjectInfo, *probe.Error) {
|
2016-04-05 14:24:23 -04:00
|
|
|
if bucketDirName, e := fs.checkMultipartArgs(bucket, object); e == nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if status, e := fs.isUploadIDExist(bucket, object, uploadID); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
} else if !status {
|
|
|
|
return ObjectInfo{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
if e := fs.checkDiskFree(); e != nil {
|
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
metaObjectDir := filepath.Join(fs.path, configDir, bucket, object)
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
var md5Sums []string
|
2016-03-28 12:52:09 -04:00
|
|
|
for _, part := range parts {
|
|
|
|
partNumber := part.PartNumber
|
|
|
|
md5sum := strings.Trim(part.ETag, "\"")
|
|
|
|
partFile := filepath.Join(metaObjectDir, uploadID+"."+strconv.Itoa(partNumber)+"."+md5sum)
|
|
|
|
if status, err := isFileExist(partFile); err != nil {
|
|
|
|
return ObjectInfo{}, probe.NewError(err)
|
|
|
|
} else if !status {
|
|
|
|
return ObjectInfo{}, probe.NewError(InvalidPart{})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-04-05 14:24:23 -04:00
|
|
|
md5Sums = append(md5Sums, md5sum)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// Save the s3 md5.
|
2016-04-05 14:24:23 -04:00
|
|
|
s3MD5, err := makeS3MD5(md5Sums...)
|
|
|
|
if err != nil {
|
|
|
|
return ObjectInfo{}, err.Trace(md5Sums...)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:24:23 -04:00
|
|
|
tempFile, e := ioutil.TempFile(metaObjectDir, uploadID+".complete.")
|
|
|
|
if e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
//return ObjectInfo{}, probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
for _, part := range parts {
|
|
|
|
partNumber := part.PartNumber
|
|
|
|
md5sum := strings.Trim(part.ETag, "\"")
|
|
|
|
partFile := filepath.Join(metaObjectDir, uploadID+"."+strconv.Itoa(partNumber)+"."+md5sum)
|
|
|
|
var f *os.File
|
2016-04-05 14:24:23 -04:00
|
|
|
f, e = os.Open(partFile)
|
|
|
|
if e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
tempFile.Close()
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
//return ObjectInfo{}, probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
|
|
|
} else if _, e = io.Copy(tempFile, f); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
tempFile.Close()
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
//return ObjectInfo{}, probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-03-28 12:52:09 -04:00
|
|
|
}
|
|
|
|
f.Close()
|
2016-03-01 23:01:40 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
tempFile.Close()
|
|
|
|
// fi is used later
|
2016-04-05 14:24:23 -04:00
|
|
|
fi, e := os.Stat(tempFile.Name())
|
|
|
|
if e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
os.Remove(tempFile.Name())
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-03-01 23:01:40 -05:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-04-05 14:24:23 -04:00
|
|
|
if e = os.MkdirAll(filepath.Dir(objectPath), 0755); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
//return ObjectInfo{}, probe.NewError(InternalError{Err: err})
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-04-05 14:24:23 -04:00
|
|
|
if e = os.Rename(tempFile.Name(), objectPath); e != nil {
|
2016-03-28 12:52:09 -04:00
|
|
|
os.Remove(tempFile.Name())
|
2016-04-05 14:24:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-03-01 23:01:40 -05:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
fs.cleanupUploadID(bucket, object, uploadID) // TODO: handle and log the error
|
2016-03-01 23:01:40 -05:00
|
|
|
|
2016-02-01 15:19:54 -05:00
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" {
|
2016-03-28 12:52:09 -04:00
|
|
|
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
2016-02-05 18:09:23 -05:00
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
2016-03-11 19:31:24 -05:00
|
|
|
newObject := ObjectInfo{
|
2016-02-27 06:04:52 -05:00
|
|
|
Bucket: bucket,
|
2016-03-11 19:31:24 -05:00
|
|
|
Name: object,
|
2016-03-28 12:52:09 -04:00
|
|
|
ModifiedTime: fi.ModTime(),
|
|
|
|
Size: fi.Size(),
|
2016-02-27 06:04:52 -05:00
|
|
|
ContentType: contentType,
|
2016-03-11 19:31:24 -05:00
|
|
|
MD5Sum: s3MD5,
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
2015-10-16 14:26:01 -04:00
|
|
|
return newObject, nil
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// ListMultipartUploads - list incomplete multipart sessions for a given BucketMultipartResourcesMetadata
|
|
|
|
func (fs Filesystem) ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error) {
|
|
|
|
result := ListMultipartsInfo{}
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if bucketDirName, err := fs.checkBucketArg(bucket); err == nil {
|
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
|
|
|
return result, probe.NewError(err)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if !IsValidObjectPrefix(objectPrefix) {
|
|
|
|
return result, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: objectPrefix})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
prefixPath := filepath.FromSlash(objectPrefix)
|
|
|
|
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
|
|
|
if delimiter != "" && delimiter != "/" {
|
|
|
|
return result, probe.NewError(fmt.Errorf("delimiter '%s' is not supported", delimiter))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// Unescape keyMarker string
|
|
|
|
if tmpKeyMarker, err := url.QueryUnescape(keyMarker); err == nil {
|
|
|
|
keyMarker = tmpKeyMarker
|
|
|
|
} else {
|
|
|
|
return result, probe.NewError(err)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, objectPrefix) {
|
|
|
|
return result, probe.NewError(fmt.Errorf("Invalid combination of marker '%s' and prefix '%s'", keyMarker, objectPrefix))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
markerPath := filepath.FromSlash(keyMarker)
|
|
|
|
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
if strings.HasSuffix(markerPath, string(os.PathSeparator)) {
|
|
|
|
return result, probe.NewError(fmt.Errorf("Invalid combination of uploadID marker '%s' and marker '%s'", uploadIDMarker, keyMarker))
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.Parse(uploadIDMarker)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, probe.NewError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id.IsZero() {
|
|
|
|
return result, probe.NewError(fmt.Errorf("Invalid upload ID marker %s", uploadIDMarker))
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// Return empty response if maxUploads is zero
|
|
|
|
if maxUploads == 0 {
|
|
|
|
return result, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// set listObjectsLimit to maxUploads for out-of-range limit
|
|
|
|
if maxUploads < 0 || maxUploads > listObjectsLimit {
|
|
|
|
maxUploads = listObjectsLimit
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
recursive := true
|
|
|
|
skipDir := true
|
|
|
|
if delimiter == "/" {
|
|
|
|
skipDir = false
|
|
|
|
recursive = false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
bucketDir := filepath.Join(fs.path, bucket)
|
|
|
|
// If listMultipartObjectChannel is available for given parameters, then use it, else create new one
|
|
|
|
objectInfoCh := fs.popListMultipartObjectCh(listMultipartObjectParams{bucket, delimiter, markerPath, prefixPath, uploadIDMarker})
|
|
|
|
if objectInfoCh == nil {
|
|
|
|
ch := scanMultipartDir(bucketDir, objectPrefix, keyMarker, uploadIDMarker, recursive)
|
|
|
|
objectInfoCh = &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
nextKeyMarker := ""
|
|
|
|
nextUploadIDMarker := ""
|
|
|
|
for i := 0; i < maxUploads; {
|
|
|
|
objInfo, ok := objectInfoCh.Read()
|
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.Err != nil {
|
|
|
|
return ListMultipartsInfo{}, probe.NewError(objInfo.Err)
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
if strings.Contains(objInfo.Name, "$multiparts") || strings.Contains(objInfo.Name, "$tmpobject") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.IsDir && skipDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.IsDir {
|
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objInfo.Name)
|
|
|
|
} else {
|
|
|
|
result.Uploads = append(result.Uploads, uploadMetadata{Object: objInfo.Name, UploadID: objInfo.UploadID, Initiated: objInfo.ModifiedTime})
|
|
|
|
}
|
|
|
|
nextKeyMarker = objInfo.Name
|
|
|
|
nextUploadIDMarker = objInfo.UploadID
|
|
|
|
i++
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if !objectInfoCh.IsClosed() {
|
|
|
|
result.IsTruncated = true
|
|
|
|
result.NextKeyMarker = nextKeyMarker
|
|
|
|
result.NextUploadIDMarker = nextUploadIDMarker
|
|
|
|
fs.pushListMultipartObjectCh(listMultipartObjectParams{bucket, delimiter, nextKeyMarker, objectPrefix, nextUploadIDMarker}, *objectInfoCh)
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
return result, nil
|
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
// ListObjectParts - list parts from incomplete multipart session for a given ObjectResourcesMetadata
|
|
|
|
func (fs Filesystem) ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error) {
|
|
|
|
if bucketDirName, err := fs.checkMultipartArgs(bucket, object); err == nil {
|
|
|
|
bucket = bucketDirName
|
|
|
|
} else {
|
|
|
|
return ListPartsInfo{}, probe.NewError(err)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
if status, err := fs.isUploadIDExist(bucket, object, uploadID); err != nil {
|
|
|
|
//return probe.NewError(InternalError{Err: err})
|
|
|
|
return ListPartsInfo{}, probe.NewError(err)
|
|
|
|
} else if !status {
|
|
|
|
return ListPartsInfo{}, probe.NewError(InvalidUploadID{UploadID: uploadID})
|
|
|
|
}
|
2016-03-07 00:32:49 -05:00
|
|
|
|
2016-03-28 12:52:09 -04:00
|
|
|
metaObjectDir := filepath.Join(fs.path, configDir, bucket, object)
|
|
|
|
entries, err := filteredReaddir(metaObjectDir,
|
|
|
|
func(entry DirEntry) bool {
|
|
|
|
if tokens := strings.Split(entry.Name, "."); len(tokens) == 3 {
|
|
|
|
if tokens[0] == uploadID {
|
|
|
|
if partNumber, err := strconv.Atoi(tokens[1]); err == nil {
|
|
|
|
if partNumber >= 1 && partNumber <= 10000 && partNumber > partNumberMarker {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ListPartsInfo{}, probe.NewError(err)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-28 12:52:09 -04:00
|
|
|
|
|
|
|
isTruncated := false
|
|
|
|
if maxParts <= 0 || maxParts > 1000 {
|
|
|
|
maxParts = 1000
|
|
|
|
}
|
|
|
|
nextPartNumberMarker := 0
|
|
|
|
|
|
|
|
parts := []partInfo{}
|
|
|
|
for i := range entries {
|
|
|
|
if i == maxParts {
|
|
|
|
isTruncated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := strings.Split(entries[i].Name, ".")
|
|
|
|
partNumber, _ := strconv.Atoi(tokens[1])
|
|
|
|
md5sum := tokens[2]
|
|
|
|
parts = append(parts, partInfo{
|
|
|
|
PartNumber: partNumber,
|
|
|
|
LastModified: entries[i].ModTime,
|
|
|
|
ETag: md5sum,
|
|
|
|
Size: entries[i].Size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTruncated {
|
|
|
|
nextPartNumberMarker = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListPartsInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
UploadID: uploadID,
|
|
|
|
PartNumberMarker: partNumberMarker,
|
|
|
|
NextPartNumberMarker: nextPartNumberMarker,
|
|
|
|
MaxParts: maxParts,
|
|
|
|
IsTruncated: isTruncated,
|
|
|
|
Parts: parts,
|
|
|
|
}, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|