You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
11 KiB
370 lines
11 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package main
|
||
9 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
9 years ago
|
"crypto/md5"
|
||
9 years ago
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
|
||
|
"encoding/hex"
|
||
|
"runtime"
|
||
|
|
||
9 years ago
|
"github.com/minio/minio/pkg/atomic"
|
||
9 years ago
|
"github.com/minio/minio/pkg/disk"
|
||
9 years ago
|
"github.com/minio/minio/pkg/mimedb"
|
||
9 years ago
|
"github.com/minio/minio/pkg/probe"
|
||
9 years ago
|
)
|
||
|
|
||
|
/// Object Operations
|
||
|
|
||
|
// GetObject - GET object
|
||
9 years ago
|
func (fs Filesystem) GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error) {
|
||
9 years ago
|
// Input validation.
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return nil, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||
9 years ago
|
}
|
||
|
if !IsValidObjectName(object) {
|
||
9 years ago
|
return nil, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// normalize buckets.
|
||
9 years ago
|
bucket = getActualBucketname(fs.path, bucket)
|
||
9 years ago
|
objectPath := filepath.Join(fs.path, bucket, object)
|
||
|
|
||
|
file, e := os.Open(objectPath)
|
||
|
if e != nil {
|
||
|
// If the object doesn't exist, the bucket might not exist either. Stat for
|
||
|
// the bucket and give a better error message if that is true.
|
||
9 years ago
|
if os.IsNotExist(e) {
|
||
9 years ago
|
_, e = os.Stat(filepath.Join(fs.path, bucket))
|
||
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return nil, probe.NewError(BucketNotFound{Bucket: bucket})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
// Initiate a cached stat operation on the file handler.
|
||
|
st, e := file.Stat()
|
||
9 years ago
|
if e != nil {
|
||
9 years ago
|
return nil, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
// Object path is a directory prefix, return object not found error.
|
||
|
if st.IsDir() {
|
||
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Seet to a starting offset.
|
||
|
_, e = file.Seek(startOffset, os.SEEK_SET)
|
||
9 years ago
|
if e != nil {
|
||
9 years ago
|
// When the "handle is invalid", the file might be a directory on Windows.
|
||
|
if runtime.GOOS == "windows" && strings.Contains(e.Error(), "handle is invalid") {
|
||
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Return successfully seeked file handler.
|
||
|
return file, nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// GetObjectInfo - get object info.
|
||
|
func (fs Filesystem) GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error) {
|
||
9 years ago
|
// Input validation.
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||
9 years ago
|
}
|
||
|
|
||
|
if !IsValidObjectName(object) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Normalize buckets.
|
||
9 years ago
|
bucket = getActualBucketname(fs.path, bucket)
|
||
9 years ago
|
bucketPath := filepath.Join(fs.path, bucket)
|
||
|
if _, e := os.Stat(bucketPath); e != nil {
|
||
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
||
9 years ago
|
}
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
info, err := getObjectInfo(fs.path, bucket, object)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
if os.IsNotExist(err.ToGoError()) {
|
||
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
|
}
|
||
9 years ago
|
return ObjectInfo{}, err.Trace(bucket, object)
|
||
9 years ago
|
}
|
||
9 years ago
|
if info.IsDir {
|
||
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
|
}
|
||
9 years ago
|
return info, nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// getObjectInfo - get object stat info.
|
||
|
func getObjectInfo(rootPath, bucket, object string) (ObjectInfo, *probe.Error) {
|
||
9 years ago
|
// Do not use filepath.Join() since filepath.Join strips off any
|
||
|
// object names with '/', use them as is in a static manner so
|
||
9 years ago
|
// that we can send a proper 'ObjectNotFound' reply back upon os.Stat().
|
||
9 years ago
|
var objectPath string
|
||
9 years ago
|
// For windows use its special os.PathSeparator == "\\"
|
||
9 years ago
|
if runtime.GOOS == "windows" {
|
||
9 years ago
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
||
9 years ago
|
} else {
|
||
9 years ago
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
||
9 years ago
|
}
|
||
9 years ago
|
stat, e := os.Stat(objectPath)
|
||
|
if e != nil {
|
||
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
contentType := "application/octet-stream"
|
||
9 years ago
|
if runtime.GOOS == "windows" {
|
||
9 years ago
|
object = filepath.ToSlash(object)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if objectExt := filepath.Ext(object); objectExt != "" {
|
||
9 years ago
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
||
|
if ok {
|
||
|
contentType = content.ContentType
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
metadata := ObjectInfo{
|
||
9 years ago
|
Bucket: bucket,
|
||
9 years ago
|
Name: object,
|
||
|
ModifiedTime: stat.ModTime(),
|
||
9 years ago
|
Size: stat.Size(),
|
||
|
ContentType: contentType,
|
||
9 years ago
|
IsDir: stat.Mode().IsDir(),
|
||
9 years ago
|
}
|
||
|
return metadata, nil
|
||
|
}
|
||
|
|
||
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
||
9 years ago
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) bool {
|
||
9 years ago
|
// Verify the md5sum.
|
||
9 years ago
|
if expectedMD5Sum != "" && actualMD5Sum != "" {
|
||
9 years ago
|
// Decode md5sum to bytes from their hexadecimal
|
||
|
// representations.
|
||
9 years ago
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
||
|
if err != nil {
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
||
|
if err != nil {
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
9 years ago
|
// Verify md5sum bytes are equal after successful decoding.
|
||
9 years ago
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
9 years ago
|
return true
|
||
9 years ago
|
}
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// PutObject - create an object.
|
||
|
func (fs Filesystem) PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error) {
|
||
9 years ago
|
di, e := disk.GetInfo(fs.path)
|
||
|
if e != nil {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Remove 5% from total space for cumulative disk space used for
|
||
|
// journalling, inodes etc.
|
||
9 years ago
|
availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100
|
||
9 years ago
|
if int64(availableDiskSpace) <= fs.minFreeDisk {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(RootPathFull{Path: fs.path})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Check bucket name valid.
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
bucket = getActualBucketname(fs.path, bucket)
|
||
9 years ago
|
bucketPath := filepath.Join(fs.path, bucket)
|
||
9 years ago
|
if _, e = os.Stat(bucketPath); e != nil {
|
||
9 years ago
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
||
9 years ago
|
}
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Verify object path legal.
|
||
9 years ago
|
if !IsValidObjectName(object) {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Get object path.
|
||
9 years ago
|
objectPath := filepath.Join(bucketPath, object)
|
||
9 years ago
|
|
||
|
// md5Hex representation.
|
||
|
var md5Hex string
|
||
9 years ago
|
if len(metadata) != 0 {
|
||
|
md5Hex = metadata["md5Sum"]
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Write object.
|
||
9 years ago
|
file, e := atomic.FileCreateWithPrefix(objectPath, md5Hex+"$tmpobject")
|
||
9 years ago
|
if e != nil {
|
||
|
switch e := e.(type) {
|
||
|
case *os.PathError:
|
||
|
if e.Op == "mkdir" {
|
||
|
if strings.Contains(e.Error(), "not a directory") {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(ObjectExistsAsPrefix{Bucket: bucket, Prefix: object})
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
default:
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
defer file.Close()
|
||
9 years ago
|
|
||
9 years ago
|
// Initialize md5 writer.
|
||
|
md5Writer := md5.New()
|
||
9 years ago
|
|
||
9 years ago
|
// Instantiate a new multi writer.
|
||
|
multiWriter := io.MultiWriter(md5Writer, file)
|
||
|
|
||
|
// Instantiate checksum hashers and create a multiwriter.
|
||
9 years ago
|
if size > 0 {
|
||
9 years ago
|
if _, e = io.CopyN(multiWriter, data, size); e != nil {
|
||
9 years ago
|
file.CloseAndPurge()
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
} else {
|
||
9 years ago
|
if _, e = io.Copy(multiWriter, data); e != nil {
|
||
9 years ago
|
file.CloseAndPurge()
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
||
9 years ago
|
if md5Hex != "" {
|
||
9 years ago
|
if newMD5Hex != md5Hex {
|
||
|
return ObjectInfo{}, probe.NewError(BadDigest{md5Hex, newMD5Hex})
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Set stat again to get the latest metadata.
|
||
9 years ago
|
st, e := os.Stat(file.Name())
|
||
9 years ago
|
if e != nil {
|
||
9 years ago
|
return ObjectInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
contentType := "application/octet-stream"
|
||
|
if objectExt := filepath.Ext(objectPath); objectExt != "" {
|
||
9 years ago
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
||
|
if ok {
|
||
|
contentType = content.ContentType
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
newObject := ObjectInfo{
|
||
9 years ago
|
Bucket: bucket,
|
||
9 years ago
|
Name: object,
|
||
|
ModifiedTime: st.ModTime(),
|
||
9 years ago
|
Size: st.Size(),
|
||
9 years ago
|
MD5Sum: newMD5Hex,
|
||
9 years ago
|
ContentType: contentType,
|
||
9 years ago
|
}
|
||
|
return newObject, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// deleteObjectPath - delete object path if its empty.
|
||
9 years ago
|
func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error {
|
||
|
if basePath == deletePath {
|
||
|
return nil
|
||
|
}
|
||
9 years ago
|
// Verify if the path exists.
|
||
9 years ago
|
pathSt, e := os.Stat(deletePath)
|
||
9 years ago
|
if e != nil {
|
||
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
|
}
|
||
9 years ago
|
return probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
if pathSt.IsDir() {
|
||
9 years ago
|
// Verify if directory is empty.
|
||
9 years ago
|
empty, e := isDirEmpty(deletePath)
|
||
9 years ago
|
if e != nil {
|
||
|
return probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
if !empty {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
9 years ago
|
// Attempt to remove path.
|
||
9 years ago
|
if e := os.Remove(deletePath); e != nil {
|
||
|
return probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
// Recursively go down the next path and delete again.
|
||
9 years ago
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil {
|
||
9 years ago
|
return err.Trace(basePath, deletePath, bucket, object)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// DeleteObject - delete object.
|
||
9 years ago
|
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
||
9 years ago
|
// Check bucket name valid
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||
|
}
|
||
|
|
||
9 years ago
|
bucket = getActualBucketname(fs.path, bucket)
|
||
9 years ago
|
bucketPath := filepath.Join(fs.path, bucket)
|
||
9 years ago
|
// Check bucket exists
|
||
9 years ago
|
if _, e := os.Stat(bucketPath); e != nil {
|
||
|
if os.IsNotExist(e) {
|
||
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
||
|
}
|
||
|
return probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Verify object path legal
|
||
9 years ago
|
if !IsValidObjectName(object) {
|
||
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
||
|
}
|
||
|
|
||
9 years ago
|
// Do not use filepath.Join() since filepath.Join strips off any
|
||
|
// object names with '/', use them as is in a static manner so
|
||
|
// that we can send a proper 'ObjectNotFound' reply back upon
|
||
|
// os.Stat().
|
||
9 years ago
|
var objectPath string
|
||
|
if runtime.GOOS == "windows" {
|
||
9 years ago
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
||
9 years ago
|
} else {
|
||
9 years ago
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
||
9 years ago
|
}
|
||
9 years ago
|
// Delete object path if its empty.
|
||
9 years ago
|
err := deleteObjectPath(bucketPath, objectPath, bucket, object)
|
||
|
if err != nil {
|
||
9 years ago
|
if os.IsNotExist(err.ToGoError()) {
|
||
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||
|
}
|
||
9 years ago
|
return err.Trace(bucketPath, objectPath, bucket, object)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|