2016-05-02 19:57:31 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2016-05-05 15:51:56 -04:00
|
|
|
"sort"
|
2016-05-03 19:10:24 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-05-02 19:57:31 -04:00
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-02 19:57:31 -04:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
2016-05-07 05:08:03 -04:00
|
|
|
const (
|
|
|
|
incompleteFile = "00000.incomplete"
|
|
|
|
uploadsJSONFile = "uploads.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createUploadsJSON - create uploads.json placeholder file.
|
|
|
|
func createUploadsJSON(storage StorageAPI, bucket, object, uploadID string) error {
|
|
|
|
// Place holder uploads.json
|
|
|
|
uploadsPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)
|
|
|
|
tmpUploadsPath := path.Join(tmpMetaPrefix, bucket, object, uploadID, uploadsJSONFile)
|
|
|
|
w, err := storage.CreateFile(minioMetaBucket, uploadsPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
if clErr := safeCloseAndRemove(w); clErr != nil {
|
|
|
|
return clErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = storage.StatFile(minioMetaBucket, uploadsPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
err = storage.RenameFile(minioMetaBucket, tmpUploadsPath, minioMetaBucket, uploadsPath)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if derr := storage.DeleteFile(minioMetaBucket, tmpUploadsPath); derr != nil {
|
|
|
|
return derr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:57:31 -04:00
|
|
|
/// Common multipart object layer functions.
|
|
|
|
|
|
|
|
// newMultipartUploadCommon - initialize a new multipart, is a common
|
|
|
|
// function for both object layers.
|
|
|
|
func newMultipartUploadCommon(storage StorageAPI, bucket string, object string) (uploadID string, err error) {
|
|
|
|
// Verify if bucket name is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !isBucketExist(storage, bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:57:31 -04:00
|
|
|
// Verify if object name is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loops through until successfully generates a new unique upload id.
|
|
|
|
for {
|
|
|
|
uuid, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
uploadID := uuid.String()
|
2016-05-07 05:08:03 -04:00
|
|
|
// Create placeholder file 'uploads.json'
|
|
|
|
err = createUploadsJSON(storage, bucket, object, uploadID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, incompleteFile)
|
|
|
|
tempUploadIDPath := path.Join(tmpMetaPrefix, bucket, object, uploadID, incompleteFile)
|
2016-05-02 19:57:31 -04:00
|
|
|
if _, err = storage.StatFile(minioMetaBucket, uploadIDPath); err != nil {
|
|
|
|
if err != errFileNotFound {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
|
|
|
}
|
|
|
|
// uploadIDPath doesn't exist, so create empty file to reserve the name
|
|
|
|
var w io.WriteCloser
|
2016-05-03 19:10:24 -04:00
|
|
|
if w, err = storage.CreateFile(minioMetaBucket, tempUploadIDPath); err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
|
|
|
// Close the writer.
|
|
|
|
if err = w.Close(); err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(w); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
|
|
|
err = storage.RenameFile(minioMetaBucket, tempUploadIDPath, minioMetaBucket, uploadIDPath)
|
|
|
|
if err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if derr := storage.DeleteFile(minioMetaBucket, tempUploadIDPath); derr != nil {
|
|
|
|
return "", toObjectErr(derr, minioMetaBucket, tempUploadIDPath)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", toObjectErr(err, minioMetaBucket, uploadIDPath)
|
|
|
|
}
|
|
|
|
return uploadID, nil
|
|
|
|
}
|
|
|
|
// uploadIDPath already exists.
|
|
|
|
// loop again to try with different uuid generated.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// putObjectPartCommon - put object part.
|
|
|
|
func putObjectPartCommon(storage StorageAPI, bucket string, object string, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
2016-05-08 04:58:05 -04:00
|
|
|
if !isBucketExist(storage, bucket) {
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
if !isUploadIDExists(storage, bucket, object, uploadID) {
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
|
|
|
|
2016-05-07 05:08:03 -04:00
|
|
|
partSuffix := fmt.Sprintf("%s.%.5d", uploadID, partID)
|
2016-05-03 19:10:24 -04:00
|
|
|
partSuffixPath := path.Join(tmpMetaPrefix, bucket, object, partSuffix)
|
2016-05-02 19:57:31 -04:00
|
|
|
fileWriter, err := storage.CreateFile(minioMetaBucket, partSuffixPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
|
|
|
// Instantiate a new multi writer.
|
|
|
|
multiWriter := io.MultiWriter(md5Writer, fileWriter)
|
|
|
|
|
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
|
|
|
if size > 0 {
|
|
|
|
if _, err = io.CopyN(multiWriter, data, size); err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
// Reader shouldn't have more data what mentioned in size argument.
|
|
|
|
// reading one more byte from the reader to validate it.
|
|
|
|
// expected to fail, success validates existence of more data in the reader.
|
|
|
|
if _, err = io.CopyN(ioutil.Discard, data, 1); err == nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", UnExpectedDataSize{Size: int(size)}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err = io.Copy(multiWriter, data); err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", BadDigest{md5Hex, newMD5Hex}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = fileWriter.Close()
|
|
|
|
if err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", toObjectErr(clErr, bucket, object)
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
|
2016-05-07 05:08:03 -04:00
|
|
|
partSuffixMD5 := fmt.Sprintf("%.5d.%s", partID, newMD5Hex)
|
|
|
|
partSuffixMD5Path := path.Join(mpartMetaPrefix, bucket, object, uploadID, partSuffixMD5)
|
2016-05-03 19:10:24 -04:00
|
|
|
err = storage.RenameFile(minioMetaBucket, partSuffixPath, minioMetaBucket, partSuffixMD5Path)
|
|
|
|
if err != nil {
|
2016-05-05 23:28:22 -04:00
|
|
|
if derr := storage.DeleteFile(minioMetaBucket, partSuffixPath); derr != nil {
|
|
|
|
return "", toObjectErr(derr, minioMetaBucket, partSuffixPath)
|
|
|
|
}
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, partSuffixMD5Path)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
// Cleanup all temp entries inside tmpMetaPrefix directory, upon server initialization.
|
|
|
|
func cleanupAllTmpEntries(storage StorageAPI) error {
|
2016-05-05 15:51:56 -04:00
|
|
|
return cleanupUploadedParts(storage, tmpMetaPrefix, "", "", "")
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper to which removes all the uploaded parts after a successful
|
|
|
|
// complete multipart upload.
|
2016-05-05 15:51:56 -04:00
|
|
|
func cleanupUploadedParts(storage StorageAPI, prefix, bucket, object, uploadID string) error {
|
2016-05-07 05:08:03 -04:00
|
|
|
multipartDir := path.Join(prefix, bucket, object, uploadID)
|
2016-05-05 15:51:56 -04:00
|
|
|
entries, err := storage.ListDir(minioMetaBucket, multipartDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
2016-05-07 05:08:03 -04:00
|
|
|
if err = storage.DeleteFile(minioMetaBucket, path.Join(multipartDir, entry)); err != nil {
|
|
|
|
return err
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:57:31 -04:00
|
|
|
// abortMultipartUploadCommon - aborts a multipart upload, common
|
|
|
|
// function used by both object layers.
|
|
|
|
func abortMultipartUploadCommon(storage StorageAPI, bucket, object, uploadID string) error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !isBucketExist(storage, bucket) {
|
|
|
|
return BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
if !isUploadIDExists(storage, bucket, object, uploadID) {
|
2016-05-02 19:57:31 -04:00
|
|
|
return InvalidUploadID{UploadID: uploadID}
|
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
return cleanupUploadedParts(storage, mpartMetaPrefix, bucket, object, uploadID)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-02 19:57:31 -04:00
|
|
|
|
2016-05-07 05:08:03 -04:00
|
|
|
// isIncompleteMultipart - is object incomplete multipart.
|
|
|
|
func isIncompleteMultipart(storage StorageAPI, objectPath string) (bool, error) {
|
|
|
|
_, err := storage.StatFile(minioMetaBucket, path.Join(objectPath, uploadsJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -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-05-05 15:51:56 -04:00
|
|
|
func listLeafEntries(storage StorageAPI, prefixPath string) (entries []string, err error) {
|
2016-05-07 05:08:03 -04:00
|
|
|
var ok bool
|
|
|
|
if ok, err = isIncompleteMultipart(storage, prefixPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
entries, err = storage.ListDir(minioMetaBucket, prefixPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
var newEntries []string
|
2016-05-05 15:51:56 -04:00
|
|
|
for _, entry := range entries {
|
|
|
|
if strings.HasSuffix(entry, slashSeparator) {
|
2016-05-07 05:08:03 -04:00
|
|
|
newEntries = append(newEntries, entry)
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
return newEntries, nil
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// listMetaBucketMultipartFiles - list all files at a given prefix inside minioMetaBucket.
|
2016-05-05 15:51:56 -04:00
|
|
|
func listMetaBucketMultipartFiles(layer ObjectLayer, prefixPath string, markerPath string, recursive bool, maxKeys int) (fileInfos []FileInfo, eof bool, err error) {
|
|
|
|
var storage StorageAPI
|
|
|
|
switch l := layer.(type) {
|
|
|
|
case fsObjects:
|
|
|
|
storage = l.storage
|
|
|
|
case xlObjects:
|
|
|
|
storage = l.storage
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := lookupTreeWalk(layer, listParams{minioMetaBucket, recursive, markerPath, prefixPath})
|
|
|
|
if walker == nil {
|
|
|
|
walker = startTreeWalk(layer, minioMetaBucket, prefixPath, markerPath, recursive)
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -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 {
|
2016-05-05 15:51:56 -04:00
|
|
|
walkResult, ok := <-walker.ch
|
|
|
|
if !ok {
|
|
|
|
// Closed channel.
|
|
|
|
eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// For any walk error return right away.
|
|
|
|
if walkResult.err != nil {
|
2016-05-03 19:10:24 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
2016-05-05 15:51:56 -04:00
|
|
|
"bucket": minioMetaBucket,
|
|
|
|
"prefix": prefixPath,
|
|
|
|
"marker": markerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
}).Debugf("Walk resulted in an error %s", walkResult.err)
|
|
|
|
// File not found is a valid case.
|
|
|
|
if walkResult.err == errFileNotFound {
|
|
|
|
return nil, true, nil
|
|
|
|
}
|
|
|
|
return nil, false, toObjectErr(walkResult.err, minioMetaBucket, prefixPath)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
fi := walkResult.fileInfo
|
|
|
|
var entries []string
|
|
|
|
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, err = listLeafEntries(storage, fi.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": fi.Name,
|
|
|
|
}).Errorf("%s", err)
|
|
|
|
return nil, false, err
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
if len(entries) > 0 {
|
|
|
|
// We reach here for non-recursive case and a leaf entry.
|
|
|
|
sort.Strings(entries)
|
|
|
|
for _, entry := range entries {
|
|
|
|
var fileInfo FileInfo
|
2016-05-07 05:08:03 -04:00
|
|
|
incompleteUploadFile := path.Join(fi.Name, entry, incompleteFile)
|
|
|
|
fileInfo, err = storage.StatFile(minioMetaBucket, incompleteUploadFile)
|
2016-05-05 15:51:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
fileInfo.Name = path.Join(fi.Name, entry)
|
2016-05-05 15:51:56 -04:00
|
|
|
fileInfos = append(fileInfos, fileInfo)
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
} else {
|
|
|
|
// We reach here for a non-recursive case non-leaf entry
|
2016-05-07 05:08:03 -04:00
|
|
|
// OR recursive case with fi.Name.
|
2016-05-05 15:51:56 -04:00
|
|
|
if !fi.Mode.IsDir() { // Do not skip non-recursive case directory entries.
|
2016-05-07 05:08:03 -04:00
|
|
|
// Validate if 'fi.Name' is incomplete multipart.
|
|
|
|
if !strings.HasSuffix(fi.Name, incompleteFile) {
|
2016-05-05 15:51:56 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
fi.Name = path.Dir(fi.Name)
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
|
|
|
fileInfos = append(fileInfos, fi)
|
2016-05-07 05:08:03 -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
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return entries here.
|
2016-05-05 15:51:56 -04:00
|
|
|
return fileInfos, eof, nil
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// listMultipartUploadsCommon - lists all multipart uploads, common
|
|
|
|
// function for both object layers.
|
2016-05-05 15:51:56 -04:00
|
|
|
func listMultipartUploadsCommon(layer ObjectLayer, bucket, prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, error) {
|
2016-05-08 04:58:05 -04:00
|
|
|
var storage StorageAPI
|
|
|
|
switch l := layer.(type) {
|
|
|
|
case xlObjects:
|
|
|
|
storage = l.storage
|
|
|
|
case fsObjects:
|
|
|
|
storage = l.storage
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:10:24 -04:00
|
|
|
result := ListMultipartsInfo{}
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListMultipartsInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
if !isBucketExist(storage, bucket) {
|
|
|
|
return ListMultipartsInfo{}, BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
2016-05-03 19:10:24 -04:00
|
|
|
if !IsValidObjectPrefix(prefix) {
|
|
|
|
return ListMultipartsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: prefix}
|
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
|
|
|
return ListMultipartsInfo{}, UnsupportedDelimiter{
|
|
|
|
Delimiter: delimiter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if keyMarker != "" && !strings.HasPrefix(keyMarker, prefix) {
|
|
|
|
return ListMultipartsInfo{}, InvalidMarkerPrefixCombination{
|
|
|
|
Marker: keyMarker,
|
|
|
|
Prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if uploadIDMarker != "" {
|
|
|
|
if strings.HasSuffix(keyMarker, slashSeparator) {
|
|
|
|
return result, InvalidUploadIDKeyCombination{
|
|
|
|
UploadIDMarker: uploadIDMarker,
|
|
|
|
KeyMarker: keyMarker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id, err := uuid.Parse(uploadIDMarker)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
if id.IsZero() {
|
|
|
|
return result, MalformedUploadID{
|
|
|
|
UploadID: uploadIDMarker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recursive := true
|
|
|
|
if delimiter == slashSeparator {
|
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
result.IsTruncated = true
|
|
|
|
result.MaxUploads = maxUploads
|
|
|
|
|
|
|
|
// Not using path.Join() as it strips off the trailing '/'.
|
|
|
|
// Also bucket should always be followed by '/' even if prefix is empty.
|
|
|
|
multipartPrefixPath := pathJoin(mpartMetaPrefix, pathJoin(bucket, prefix))
|
|
|
|
multipartMarkerPath := ""
|
|
|
|
if keyMarker != "" {
|
|
|
|
keyMarkerPath := pathJoin(pathJoin(bucket, keyMarker), uploadIDMarker)
|
|
|
|
multipartMarkerPath = pathJoin(mpartMetaPrefix, keyMarkerPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all the multipart files at prefixPath, starting with marker keyMarkerPath.
|
2016-05-05 15:51:56 -04:00
|
|
|
fileInfos, eof, err := listMetaBucketMultipartFiles(layer, multipartPrefixPath, multipartMarkerPath, recursive, maxUploads)
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"prefixPath": multipartPrefixPath,
|
|
|
|
"markerPath": multipartMarkerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"maxUploads": maxUploads,
|
|
|
|
}).Errorf("listMetaBucketMultipartFiles failed with %s", err)
|
|
|
|
return ListMultipartsInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all the received files fill in the multiparts result.
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
var objectName string
|
|
|
|
var uploadID string
|
|
|
|
if fi.Mode.IsDir() {
|
|
|
|
// All directory entries are common prefixes.
|
|
|
|
uploadID = "" // Upload ids are empty for CommonPrefixes.
|
|
|
|
objectName = strings.TrimPrefix(fi.Name, retainSlash(path.Join(mpartMetaPrefix, bucket)))
|
|
|
|
result.CommonPrefixes = append(result.CommonPrefixes, objectName)
|
|
|
|
} else {
|
|
|
|
uploadID = path.Base(fi.Name)
|
|
|
|
objectName = strings.TrimPrefix(path.Dir(fi.Name), retainSlash(path.Join(mpartMetaPrefix, bucket)))
|
|
|
|
result.Uploads = append(result.Uploads, uploadMetadata{
|
|
|
|
Object: objectName,
|
|
|
|
UploadID: uploadID,
|
|
|
|
Initiated: fi.ModTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
result.NextKeyMarker = objectName
|
|
|
|
result.NextUploadIDMarker = uploadID
|
|
|
|
}
|
|
|
|
result.IsTruncated = !eof
|
|
|
|
if !result.IsTruncated {
|
|
|
|
result.NextKeyMarker = ""
|
|
|
|
result.NextUploadIDMarker = ""
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 05:21:12 -04:00
|
|
|
// ListObjectParts - list object parts, common function across both object layers.
|
2016-05-03 19:10:24 -04:00
|
|
|
func listObjectPartsCommon(storage StorageAPI, bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify whether the bucket exists.
|
|
|
|
if !isBucketExist(storage, bucket) {
|
|
|
|
return ListPartsInfo{}, BucketNotFound{Bucket: bucket}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-05-08 04:58:05 -04:00
|
|
|
return ListPartsInfo{}, ObjectNameInvalid{Bucket: bucket, Object: object}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
if !isUploadIDExists(storage, bucket, object, uploadID) {
|
|
|
|
return ListPartsInfo{}, InvalidUploadID{UploadID: uploadID}
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result := ListPartsInfo{}
|
2016-05-07 05:08:03 -04:00
|
|
|
entries, err := storage.ListDir(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadID))
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-05 15:51:56 -04:00
|
|
|
return result, err
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
sort.Strings(entries)
|
|
|
|
var newEntries []string
|
|
|
|
for _, entry := range entries {
|
2016-05-07 05:08:03 -04:00
|
|
|
newEntries = append(newEntries, path.Base(entry))
|
2016-05-05 15:51:56 -04:00
|
|
|
}
|
2016-05-07 05:08:03 -04:00
|
|
|
idx := sort.SearchStrings(newEntries, fmt.Sprintf("%.5d.", partNumberMarker+1))
|
2016-05-05 15:51:56 -04:00
|
|
|
newEntries = newEntries[idx:]
|
|
|
|
count := maxParts
|
|
|
|
for _, entry := range newEntries {
|
2016-05-07 05:08:03 -04:00
|
|
|
fi, err := storage.StatFile(minioMetaBucket, path.Join(mpartMetaPrefix, bucket, object, uploadID, entry))
|
2016-05-08 05:21:12 -04:00
|
|
|
splitEntry := strings.SplitN(entry, ".", 2)
|
|
|
|
partStr := splitEntry[0]
|
|
|
|
etagStr := splitEntry[1]
|
|
|
|
partNum, err := strconv.Atoi(partStr)
|
2016-05-03 19:10:24 -04:00
|
|
|
if err != nil {
|
2016-05-05 15:51:56 -04:00
|
|
|
return ListPartsInfo{}, err
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result.Parts = append(result.Parts, partInfo{
|
|
|
|
PartNumber: partNum,
|
2016-05-05 15:51:56 -04:00
|
|
|
LastModified: fi.ModTime,
|
2016-05-08 05:21:12 -04:00
|
|
|
ETag: etagStr,
|
2016-05-05 15:51:56 -04:00
|
|
|
Size: fi.Size,
|
2016-05-03 19:10:24 -04:00
|
|
|
})
|
2016-05-05 15:51:56 -04:00
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 05:21:12 -04:00
|
|
|
// If listed entries are more than maxParts, we set IsTruncated as true.
|
2016-05-05 15:51:56 -04:00
|
|
|
if len(newEntries) > len(result.Parts) {
|
|
|
|
result.IsTruncated = true
|
2016-05-08 05:21:12 -04:00
|
|
|
// Make sure to fill next part number marker if IsTruncated is
|
|
|
|
// true for subsequent listing.
|
|
|
|
nextPartNumberMarker := result.Parts[len(result.Parts)-1].PartNumber
|
|
|
|
result.NextPartNumberMarker = nextPartNumberMarker
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
|
|
|
result.Bucket = bucket
|
|
|
|
result.Object = object
|
|
|
|
result.UploadID = uploadID
|
|
|
|
result.MaxParts = maxParts
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
2016-05-08 04:58:05 -04:00
|
|
|
func isUploadIDExists(storage StorageAPI, bucket, object, uploadID string) bool {
|
2016-05-07 05:08:03 -04:00
|
|
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, incompleteFile)
|
2016-05-03 19:10:24 -04:00
|
|
|
st, err := storage.StatFile(minioMetaBucket, uploadIDPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == errFileNotFound {
|
2016-05-08 04:58:05 -04:00
|
|
|
return false
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
log.Errorf("StatFile failed wtih %s", err)
|
|
|
|
return false
|
2016-05-03 19:10:24 -04:00
|
|
|
}
|
2016-05-08 04:58:05 -04:00
|
|
|
log.Debugf("FileInfo: %v", st)
|
|
|
|
return st.Mode.IsRegular()
|
2016-05-02 19:57:31 -04:00
|
|
|
}
|