2016-04-08 13:37:38 -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"
|
2016-04-16 15:48:41 -04:00
|
|
|
"errors"
|
2016-04-08 13:37:38 -04:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
|
|
|
"github.com/minio/minio/pkg/probe"
|
|
|
|
"github.com/minio/minio/pkg/safe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type objectAPI struct {
|
|
|
|
storage StorageAPI
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:05:38 -04:00
|
|
|
func newObjectLayer(storage StorageAPI) objectAPI {
|
|
|
|
return objectAPI{storage}
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Bucket operations
|
|
|
|
|
|
|
|
// MakeBucket - make a bucket.
|
|
|
|
func (o objectAPI) MakeBucket(bucket string) *probe.Error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if e := o.storage.MakeVol(bucket); e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return probe.NewError(toObjectErr(e, bucket))
|
|
|
|
}
|
|
|
|
// This happens for the first time, but keep this here since this
|
|
|
|
// is the only place where it can be made expensive optimizing all
|
|
|
|
// other calls.
|
|
|
|
// Create minio meta volume, if it doesn't exist yet.
|
|
|
|
if e := o.storage.MakeVol(minioMetaVolume); e != nil {
|
|
|
|
if e != errVolumeExists {
|
|
|
|
return probe.NewError(toObjectErr(e, minioMetaVolume))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketInfo - get bucket info.
|
|
|
|
func (o objectAPI) GetBucketInfo(bucket string) (BucketInfo, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
vi, e := o.storage.StatVol(bucket)
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return BucketInfo{}, probe.NewError(toObjectErr(e, bucket))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
return BucketInfo{
|
2016-04-16 15:48:41 -04:00
|
|
|
Name: bucket,
|
2016-04-08 13:37:38 -04:00
|
|
|
Created: vi.Created,
|
2016-04-21 23:07:47 -04:00
|
|
|
Total: vi.Total,
|
|
|
|
Free: vi.Free,
|
2016-04-08 13:37:38 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets - list buckets.
|
|
|
|
func (o objectAPI) ListBuckets() ([]BucketInfo, *probe.Error) {
|
|
|
|
var bucketInfos []BucketInfo
|
|
|
|
vols, e := o.storage.ListVols()
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return nil, probe.NewError(toObjectErr(e))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
for _, vol := range vols {
|
2016-04-13 14:32:47 -04:00
|
|
|
// StorageAPI can send volume names which are incompatible
|
|
|
|
// with buckets, handle it and skip them.
|
2016-04-08 13:37:38 -04:00
|
|
|
if !IsValidBucketName(vol.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-13 14:32:47 -04:00
|
|
|
bucketInfos = append(bucketInfos, BucketInfo{
|
|
|
|
Name: vol.Name,
|
|
|
|
Created: vol.Created,
|
2016-04-21 23:07:47 -04:00
|
|
|
Total: vol.Total,
|
|
|
|
Free: vol.Free,
|
2016-04-13 14:32:47 -04:00
|
|
|
})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
return bucketInfos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucket - delete a bucket.
|
|
|
|
func (o objectAPI) DeleteBucket(bucket string) *probe.Error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if e := o.storage.DeleteVol(bucket); e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return probe.NewError(toObjectErr(e))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Object Operations
|
|
|
|
|
|
|
|
// GetObject - get an object.
|
|
|
|
func (o objectAPI) GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return nil, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
// Verify if object is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return nil, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
r, e := o.storage.ReadFile(bucket, object, startOffset)
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return nil, probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectInfo - get object info.
|
|
|
|
func (o objectAPI) GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
// Verify if object is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
fi, e := o.storage.StatFile(bucket, object)
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(object); objectExt != "" {
|
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ObjectInfo{
|
2016-04-16 15:48:41 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
2016-04-08 13:37:38 -04:00
|
|
|
ModTime: fi.ModTime,
|
|
|
|
Size: fi.Size,
|
|
|
|
IsDir: fi.Mode.IsDir(),
|
|
|
|
ContentType: contentType,
|
|
|
|
MD5Sum: "", // Read from metadata.
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
// safeCloseAndRemove - safely closes and removes underlying temporary
|
|
|
|
// file writer if possible.
|
|
|
|
func safeCloseAndRemove(writer io.WriteCloser) error {
|
|
|
|
// If writer is a safe file, Attempt to close and remove.
|
|
|
|
safeWriter, ok := writer.(*safe.File)
|
|
|
|
if ok {
|
|
|
|
return safeWriter.CloseAndRemove()
|
|
|
|
}
|
|
|
|
pipeWriter, ok := writer.(*io.PipeWriter)
|
|
|
|
if ok {
|
|
|
|
return pipeWriter.CloseWithError(errors.New("Close and error out."))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (string, *probe.Error) {
|
2016-04-08 13:37:38 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-04-16 15:48:41 -04:00
|
|
|
return "", probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
2016-04-16 15:48:41 -04:00
|
|
|
return "", probe.NewError(ObjectNameInvalid{
|
2016-04-08 13:37:38 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fileWriter, e := o.storage.CreateFile(bucket, object)
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return "", probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
|
|
|
// Instantiate a new multi writer.
|
|
|
|
multiWriter := io.MultiWriter(md5Writer, fileWriter)
|
|
|
|
|
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
|
|
|
if size > 0 {
|
|
|
|
if _, e = io.CopyN(multiWriter, data, size); e != nil {
|
2016-03-28 00:52:38 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", probe.NewError(clErr)
|
|
|
|
}
|
2016-04-20 20:23:23 -04:00
|
|
|
return "", probe.NewError(toObjectErr(e))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, e = io.Copy(multiWriter, data); e != nil {
|
2016-03-28 00:52:38 -04:00
|
|
|
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
|
|
|
|
return "", probe.NewError(clErr)
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
return "", probe.NewError(e)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
// md5Hex representation.
|
|
|
|
var md5Hex string
|
|
|
|
if len(metadata) != 0 {
|
|
|
|
md5Hex = metadata["md5Sum"]
|
|
|
|
}
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-03-28 00:52:38 -04:00
|
|
|
if e = safeCloseAndRemove(fileWriter); e != nil {
|
|
|
|
return "", probe.NewError(e)
|
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
return "", probe.NewError(BadDigest{md5Hex, newMD5Hex})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e = fileWriter.Close()
|
|
|
|
if e != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return "", probe.NewError(e)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Return md5sum, successfully wrote object.
|
2016-04-16 15:48:41 -04:00
|
|
|
return newMD5Hex, nil
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) DeleteObject(bucket, object 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})
|
|
|
|
}
|
|
|
|
if e := o.storage.DeleteFile(bucket, object); e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return probe.NewError(toObjectErr(e, bucket, object))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o objectAPI) ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ListObjectsInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectPrefix(prefix) {
|
|
|
|
return ListObjectsInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: prefix})
|
|
|
|
}
|
|
|
|
// Verify if delimiter is anything other than '/', which we do not support.
|
2016-04-21 21:05:26 -04:00
|
|
|
if delimiter != "" && delimiter != slashSeparator {
|
2016-04-11 04:29:18 -04:00
|
|
|
return ListObjectsInfo{}, probe.NewError(UnsupportedDelimiter{
|
|
|
|
Delimiter: delimiter,
|
|
|
|
})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
// Verify if marker has prefix.
|
|
|
|
if marker != "" {
|
|
|
|
if !strings.HasPrefix(marker, prefix) {
|
2016-04-11 04:29:18 -04:00
|
|
|
return ListObjectsInfo{}, probe.NewError(InvalidMarkerPrefixCombination{
|
|
|
|
Marker: marker,
|
|
|
|
Prefix: prefix,
|
|
|
|
})
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
recursive := true
|
2016-04-21 21:05:26 -04:00
|
|
|
if delimiter == slashSeparator {
|
2016-04-08 13:37:38 -04:00
|
|
|
recursive = false
|
|
|
|
}
|
|
|
|
fileInfos, eof, e := o.storage.ListFiles(bucket, prefix, marker, recursive, maxKeys)
|
|
|
|
if e != nil {
|
2016-04-20 20:23:23 -04:00
|
|
|
return ListObjectsInfo{}, probe.NewError(toObjectErr(e, bucket))
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
if maxKeys == 0 {
|
|
|
|
return ListObjectsInfo{}, nil
|
|
|
|
}
|
|
|
|
result := ListObjectsInfo{IsTruncated: !eof}
|
|
|
|
for _, fileInfo := range fileInfos {
|
2016-04-16 15:48:41 -04:00
|
|
|
// With delimiter set we fill in NextMarker and Prefixes.
|
2016-04-21 21:05:26 -04:00
|
|
|
if delimiter == slashSeparator {
|
2016-04-16 15:48:41 -04:00
|
|
|
result.NextMarker = fileInfo.Name
|
|
|
|
if fileInfo.Mode.IsDir() {
|
|
|
|
result.Prefixes = append(result.Prefixes, fileInfo.Name)
|
|
|
|
continue
|
|
|
|
}
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
result.Objects = append(result.Objects, ObjectInfo{
|
|
|
|
Name: fileInfo.Name,
|
|
|
|
ModTime: fileInfo.ModTime,
|
|
|
|
Size: fileInfo.Size,
|
2016-03-28 00:52:38 -04:00
|
|
|
IsDir: false,
|
2016-04-08 13:37:38 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|