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 (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"runtime"
|
|
|
|
|
2016-02-10 19:40:09 -05:00
|
|
|
"github.com/minio/minio/pkg/atomic"
|
|
|
|
"github.com/minio/minio/pkg/crypto/sha256"
|
2015-10-17 22:17:33 -04:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-02-04 15:52:25 -05:00
|
|
|
"github.com/minio/minio/pkg/ioutils"
|
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-02-21 20:57:05 -05:00
|
|
|
"github.com/minio/minio/pkg/s3/signature4"
|
2015-10-16 14:26:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Object Operations
|
|
|
|
|
|
|
|
// GetObject - GET object
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) GetObject(w io.Writer, bucket, object string, start, length int64) (int64, *probe.Error) {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Critical region requiring read lock.
|
2016-02-04 23:40:58 -05:00
|
|
|
fs.rwLock.RLock()
|
|
|
|
defer fs.rwLock.RUnlock()
|
2015-10-16 14:26:01 -04:00
|
|
|
|
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 0, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return 0, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// normalize buckets.
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return 0, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return 0, probe.NewError(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2015-10-16 14:26:01 -04:00
|
|
|
filestat, err := os.Stat(objectPath)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
if filestat.IsDir() {
|
|
|
|
return 0, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return 0, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
return 0, probe.NewError(err)
|
|
|
|
}
|
|
|
|
file, err := os.Open(objectPath)
|
|
|
|
if err != nil {
|
|
|
|
return 0, probe.NewError(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = file.Seek(start, os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
return 0, probe.NewError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
if length > 0 {
|
|
|
|
count, err = io.CopyN(w, file, length)
|
|
|
|
if err != nil {
|
|
|
|
return count, probe.NewError(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
count, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
|
|
|
return count, probe.NewError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// GetObjectMetadata - get object metadata.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) GetObjectMetadata(bucket, object string) (ObjectMetadata, *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 ObjectMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Normalize buckets.
|
2016-01-25 20:29:20 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
|
|
|
}
|
|
|
|
|
2015-10-16 14:26:01 -04:00
|
|
|
metadata, err := getMetadata(fs.path, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return ObjectMetadata{}, err.Trace(bucket, object)
|
|
|
|
}
|
|
|
|
if metadata.Mode.IsDir() {
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// getMetadata - get object metadata.
|
2015-10-16 14:26:01 -04:00
|
|
|
func getMetadata(rootPath, bucket, object string) (ObjectMetadata, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// 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().
|
2015-10-16 14:26:01 -04:00
|
|
|
var objectPath string
|
2015-10-22 01:25:13 -04:00
|
|
|
// For windows use its special os.PathSeparator == "\\"
|
2015-10-16 14:26:01 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
} else {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
stat, err := os.Stat(objectPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
return ObjectMetadata{}, probe.NewError(err)
|
|
|
|
}
|
|
|
|
contentType := "application/octet-stream"
|
2015-10-22 01:25:13 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-02-19 19:04:29 -05:00
|
|
|
object = filepath.ToSlash(object)
|
2015-10-22 01:25:13 -04:00
|
|
|
}
|
2016-02-05 18:09:23 -05:00
|
|
|
|
2016-02-01 15:19:54 -05:00
|
|
|
if objectExt := filepath.Ext(object); 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
|
|
|
metadata := ObjectMetadata{
|
2016-02-27 06:04:52 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
LastModified: stat.ModTime(),
|
|
|
|
Size: stat.Size(),
|
|
|
|
ContentType: contentType,
|
|
|
|
Mode: stat.Mode(),
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
2016-02-04 15:52:25 -05:00
|
|
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) bool {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify the md5sum.
|
2016-02-15 20:42:39 -05:00
|
|
|
if expectedMD5Sum != "" && actualMD5Sum != "" {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Decode md5sum to bytes from their hexadecimal
|
|
|
|
// representations.
|
2015-10-16 14:26:01 -04:00
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify md5sum bytes are equal after successful decoding.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-04 15:52:25 -05:00
|
|
|
return true
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// CreateObject - create an object.
|
2016-02-21 20:57:05 -05:00
|
|
|
func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size int64, data io.Reader, sig *signature4.Sign) (ObjectMetadata, *probe.Error) {
|
2016-02-02 18:23:28 -05:00
|
|
|
di, e := disk.GetInfo(fs.path)
|
|
|
|
if e != nil {
|
|
|
|
return ObjectMetadata{}, 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 ObjectMetadata{}, probe.NewError(RootPathFull{Path: fs.path})
|
|
|
|
}
|
|
|
|
|
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 ObjectMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
|
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-02 18:23:28 -05:00
|
|
|
if _, e = os.Stat(bucketPath); e != nil {
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return ObjectMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path 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
|
|
|
// Get object path.
|
2016-01-25 02:03:38 -05:00
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-02-15 20:42:39 -05:00
|
|
|
if expectedMD5Sum != "" {
|
2015-10-17 22:17:33 -04:00
|
|
|
var expectedMD5SumBytes []byte
|
2016-02-15 20:42:39 -05:00
|
|
|
expectedMD5SumBytes, e = base64.StdEncoding.DecodeString(expectedMD5Sum)
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Pro-actively close the connection.
|
2016-02-05 05:15:48 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(InvalidDigest{MD5: expectedMD5Sum})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Write object.
|
2016-02-15 20:42:39 -05:00
|
|
|
file, e := atomic.FileCreateWithPrefix(objectPath, expectedMD5Sum+"$tmpobject")
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
if e.Op == "mkdir" {
|
|
|
|
if strings.Contains(e.Error(), "not a directory") {
|
|
|
|
return ObjectMetadata{}, probe.NewError(ObjectExistsAsPrefix{Bucket: bucket, Prefix: object})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
|
|
|
default:
|
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
|
|
|
md5Hasher := md5.New()
|
|
|
|
sha256Hasher := sha256.New()
|
|
|
|
objectWriter := io.MultiWriter(file, md5Hasher, sha256Hasher)
|
2015-10-16 14:26:01 -04:00
|
|
|
|
|
|
|
if size > 0 {
|
2016-02-06 04:36:43 -05:00
|
|
|
if _, e = io.CopyN(objectWriter, data, size); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-02 18:23:28 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-06 04:36:43 -05:00
|
|
|
if _, e = io.Copy(objectWriter, data); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-02 18:23:28 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -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.
|
2016-02-15 20:42:39 -05:00
|
|
|
if expectedMD5Sum != "" {
|
|
|
|
if !isMD5SumEqual(expectedMD5Sum, md5Sum) {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-05 05:15:48 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(BadDigest{MD5: expectedMD5Sum, Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
sha256Sum := hex.EncodeToString(sha256Hasher.Sum(nil))
|
2016-02-15 20:42:39 -05:00
|
|
|
if sig != nil {
|
|
|
|
ok, err := sig.DoesSignatureMatch(sha256Sum)
|
2016-02-02 18:23:28 -05:00
|
|
|
if err != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-02-02 18:23:28 -05:00
|
|
|
return ObjectMetadata{}, err.Trace()
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
file.CloseAndPurge()
|
2016-02-21 20:57:05 -05:00
|
|
|
return ObjectMetadata{}, probe.NewError(SignDoesNotMatch{})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Set stat again to get the latest metadata.
|
2016-02-02 18:23:28 -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{
|
2016-02-27 06:04:52 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
LastModified: st.ModTime(),
|
|
|
|
Size: st.Size(),
|
|
|
|
ContentType: contentType,
|
|
|
|
MD5: md5Sum,
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return newObject, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// deleteObjectPath - delete object path if its empty.
|
2015-10-29 18:09:29 -04:00
|
|
|
func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error {
|
|
|
|
if basePath == deletePath {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify if the path exists.
|
2016-02-04 23:40:58 -05:00
|
|
|
pathSt, e := os.Stat(deletePath)
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2015-10-29 18:09:29 -04:00
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-02 18:23:28 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
if pathSt.IsDir() {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify if directory is empty.
|
2016-02-04 15:52:25 -05:00
|
|
|
empty, e := ioutils.IsDirEmpty(deletePath)
|
|
|
|
if e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Attempt to remove path.
|
2016-02-02 18:23:28 -05:00
|
|
|
if e := os.Remove(deletePath); e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Recursively go down the next path and delete again.
|
2015-10-29 18:09:29 -04:00
|
|
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil {
|
2016-02-02 18:23:28 -05:00
|
|
|
return err.Trace(basePath, deletePath, bucket, object)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// DeleteObject - delete object.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
2016-02-06 04:36:43 -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-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-29 18:09:29 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-06 04:36:43 -05:00
|
|
|
// Check bucket exists
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -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-06 04:36:43 -05:00
|
|
|
// 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().
|
2015-10-16 14:26:01 -04:00
|
|
|
var objectPath string
|
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
} else {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Delete object path if its empty.
|
2015-10-29 18:09:29 -04:00
|
|
|
err := deleteObjectPath(bucketPath, objectPath, bucket, object)
|
|
|
|
if err != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
if os.IsNotExist(err.ToGoError()) {
|
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-02 18:23:28 -05:00
|
|
|
return err.Trace(bucketPath, objectPath, bucket, object)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|