2015-10-16 20:26:01 +02: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"
|
2016-03-13 01:08:15 +01:00
|
|
|
"crypto/md5"
|
2015-10-16 20:26:01 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"encoding/hex"
|
|
|
|
"runtime"
|
|
|
|
|
2016-02-11 01:40:09 +01:00
|
|
|
"github.com/minio/minio/pkg/atomic"
|
2015-10-18 04:17:33 +02:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-02-06 00:09:23 +01:00
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2016-02-11 01:40:09 +01:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-10-16 20:26:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Object Operations
|
|
|
|
|
|
|
|
// GetObject - GET object
|
2015-10-18 04:17:33 +02:00
|
|
|
func (fs Filesystem) GetObject(w io.Writer, bucket, object string, start, length int64) (int64, *probe.Error) {
|
2016-02-06 10:36:43 +01:00
|
|
|
// Critical region requiring read lock.
|
2016-02-05 05:40:58 +01:00
|
|
|
fs.rwLock.RLock()
|
|
|
|
defer fs.rwLock.RUnlock()
|
2015-10-16 20:26:01 +02:00
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Input validation.
|
2015-12-07 22:39:18 +01:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 20:26:01 +02:00
|
|
|
return 0, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return 0, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// normalize buckets.
|
2016-01-25 08:03:38 +01:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2016-03-13 09:58:07 +01:00
|
|
|
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.
|
2016-01-25 08:03:38 +01:00
|
|
|
if os.IsNotExist(e) {
|
2016-03-13 09:58:07 +01:00
|
|
|
_, e = os.Stat(filepath.Join(fs.path, bucket))
|
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return 0, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
2016-01-25 08:03:38 +01:00
|
|
|
}
|
|
|
|
return 0, probe.NewError(e)
|
|
|
|
}
|
2016-03-13 09:58:07 +01:00
|
|
|
defer file.Close()
|
2016-01-25 08:03:38 +01:00
|
|
|
|
2016-03-13 09:58:07 +01:00
|
|
|
_, e = file.Seek(start, os.SEEK_SET)
|
|
|
|
if e != nil {
|
|
|
|
// When the "handle is invalid", the file might be a directory on Windows.
|
|
|
|
if runtime.GOOS == "windows" && strings.Contains(e.Error(), "handle is invalid") {
|
2015-10-16 20:26:01 +02:00
|
|
|
return 0, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-03-13 09:58:07 +01:00
|
|
|
return 0, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
2016-03-13 09:58:07 +01:00
|
|
|
// Copy over the whole file if the length is non-positive.
|
2015-10-16 20:26:01 +02:00
|
|
|
if length > 0 {
|
2016-03-13 09:58:07 +01:00
|
|
|
count, e = io.CopyN(w, file, length)
|
2015-10-16 20:26:01 +02:00
|
|
|
} else {
|
2016-03-13 09:58:07 +01:00
|
|
|
count, e = io.Copy(w, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
// This call will fail if the object is a directory. Stat the file to see if
|
|
|
|
// this is true, if so, return an ObjectNotFound error.
|
|
|
|
stat, e := os.Stat(objectPath)
|
|
|
|
if e == nil && stat.IsDir() {
|
|
|
|
return count, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-13 09:58:07 +01:00
|
|
|
|
|
|
|
return count, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-13 09:58:07 +01:00
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:31:24 +01:00
|
|
|
// GetObjectInfo - get object info.
|
|
|
|
func (fs Filesystem) GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error) {
|
2016-02-05 05:40:58 +01:00
|
|
|
// Input validation.
|
2015-12-07 22:39:18 +01:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidObjectName(object) {
|
2016-03-23 14:50:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:36:43 +01:00
|
|
|
// Normalize buckets.
|
2016-01-26 02:29:20 +01:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
2016-01-26 02:29:20 +01:00
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-01-26 02:29:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:31:24 +01:00
|
|
|
info, err := getObjectInfo(fs.path, bucket, object)
|
2015-10-16 20:26:01 +02:00
|
|
|
if err != nil {
|
2016-03-23 14:50:24 +01:00
|
|
|
if os.IsNotExist(err.ToGoError()) {
|
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, err.Trace(bucket, object)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-23 14:50:24 +01:00
|
|
|
if info.IsDir {
|
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
return info, nil
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 01:31:24 +01:00
|
|
|
// getObjectInfo - get object stat info.
|
|
|
|
func getObjectInfo(rootPath, bucket, object string) (ObjectInfo, *probe.Error) {
|
2016-02-05 05:40:58 +01:00
|
|
|
// Do not use filepath.Join() since filepath.Join strips off any
|
|
|
|
// object names with '/', use them as is in a static manner so
|
2016-03-23 14:50:24 +01:00
|
|
|
// that we can send a proper 'ObjectNotFound' reply back upon os.Stat().
|
2015-10-16 20:26:01 +02:00
|
|
|
var objectPath string
|
2015-10-22 07:25:13 +02:00
|
|
|
// For windows use its special os.PathSeparator == "\\"
|
2015-10-16 20:26:01 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 21:01:19 +02:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 20:26:01 +02:00
|
|
|
} else {
|
2015-10-17 21:01:19 +02:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-23 14:50:24 +01:00
|
|
|
stat, e := os.Stat(objectPath)
|
|
|
|
if e != nil {
|
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
contentType := "application/octet-stream"
|
2015-10-22 07:25:13 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-02-20 01:04:29 +01:00
|
|
|
object = filepath.ToSlash(object)
|
2015-10-22 07:25:13 +02:00
|
|
|
}
|
2016-02-06 00:09:23 +01:00
|
|
|
|
2016-02-01 21:19:54 +01:00
|
|
|
if objectExt := filepath.Ext(object); objectExt != "" {
|
2016-02-06 00:09:23 +01:00
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 21:19:54 +01:00
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
metadata := ObjectInfo{
|
2016-02-27 12:04:52 +01:00
|
|
|
Bucket: bucket,
|
2016-03-12 01:31:24 +01:00
|
|
|
Name: object,
|
|
|
|
ModifiedTime: stat.ModTime(),
|
2016-02-27 12:04:52 +01:00
|
|
|
Size: stat.Size(),
|
|
|
|
ContentType: contentType,
|
2016-03-12 01:31:24 +01:00
|
|
|
IsDir: stat.Mode().IsDir(),
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
2016-02-04 21:52:25 +01:00
|
|
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) bool {
|
2016-02-06 10:36:43 +01:00
|
|
|
// Verify the md5sum.
|
2016-02-16 02:42:39 +01:00
|
|
|
if expectedMD5Sum != "" && actualMD5Sum != "" {
|
2016-02-06 10:36:43 +01:00
|
|
|
// Decode md5sum to bytes from their hexadecimal
|
|
|
|
// representations.
|
2015-10-16 20:26:01 +02:00
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 21:52:25 +01:00
|
|
|
return false
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 21:52:25 +01:00
|
|
|
return false
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
// Verify md5sum bytes are equal after successful decoding.
|
2015-10-16 20:26:01 +02:00
|
|
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
2016-02-04 21:52:25 +01:00
|
|
|
return false
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-02-04 21:52:25 +01:00
|
|
|
return true
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-02-04 21:52:25 +01:00
|
|
|
return false
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// CreateObject - create an object.
|
2016-03-13 01:08:15 +01:00
|
|
|
func (fs Filesystem) CreateObject(bucket string, object string, size int64, data io.Reader, md5Bytes []byte) (ObjectInfo, *probe.Error) {
|
2016-02-03 00:23:28 +01:00
|
|
|
di, e := disk.GetInfo(fs.path)
|
|
|
|
if e != nil {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-18 04:17:33 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Remove 5% from total space for cumulative disk space used for
|
|
|
|
// journalling, inodes etc.
|
2016-01-25 08:03:38 +01:00
|
|
|
availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100
|
2015-10-19 19:29:54 +02:00
|
|
|
if int64(availableDiskSpace) <= fs.minFreeDisk {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(RootPathFull{Path: fs.path})
|
2015-10-18 04:17:33 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Check bucket name valid.
|
2015-12-07 22:39:18 +01:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-01-25 08:03:38 +01:00
|
|
|
|
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-03 00:23:28 +01:00
|
|
|
if _, e = os.Stat(bucketPath); e != nil {
|
2016-01-25 08:03:38 +01:00
|
|
|
if os.IsNotExist(e) {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
2016-01-25 08:03:38 +01:00
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Verify object path legal.
|
2015-10-16 20:26:01 +02:00
|
|
|
if !IsValidObjectName(object) {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Get object path.
|
2016-01-25 08:03:38 +01:00
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-03-13 01:08:15 +01:00
|
|
|
|
|
|
|
// md5Hex representation.
|
|
|
|
var md5Hex string
|
|
|
|
if len(md5Bytes) != 0 {
|
|
|
|
md5Hex = hex.EncodeToString(md5Bytes)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// Write object.
|
2016-03-13 01:08:15 +01:00
|
|
|
file, e := atomic.FileCreateWithPrefix(objectPath, md5Hex+"$tmpobject")
|
2016-02-03 00:23:28 +01:00
|
|
|
if e != nil {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
if e.Op == "mkdir" {
|
|
|
|
if strings.Contains(e.Error(), "not a directory") {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectExistsAsPrefix{Bucket: bucket, Prefix: object})
|
2016-02-03 00:23:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-02-03 00:23:28 +01:00
|
|
|
default:
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-02-03 00:23:28 +01:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-13 01:08:15 +01:00
|
|
|
defer file.Close()
|
2015-10-16 20:26:01 +02:00
|
|
|
|
2016-03-13 01:08:15 +01:00
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
2015-10-16 20:26:01 +02:00
|
|
|
|
2016-03-13 01:08:15 +01:00
|
|
|
// Instantiate a new multi writer.
|
|
|
|
multiWriter := io.MultiWriter(md5Writer, file)
|
|
|
|
|
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
2015-10-16 20:26:01 +02:00
|
|
|
if size > 0 {
|
2016-03-13 01:08:15 +01:00
|
|
|
if _, e = io.CopyN(multiWriter, data, size); e != nil {
|
2015-10-16 20:26:01 +02:00
|
|
|
file.CloseAndPurge()
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-13 01:08:15 +01:00
|
|
|
if _, e = io.Copy(multiWriter, data); e != nil {
|
2015-10-16 20:26:01 +02:00
|
|
|
file.CloseAndPurge()
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 01:08:15 +01:00
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
if len(md5Bytes) != 0 {
|
|
|
|
if newMD5Hex != md5Hex {
|
|
|
|
return ObjectInfo{}, probe.NewError(BadDigest{md5Hex, newMD5Hex})
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:36:43 +01:00
|
|
|
// Set stat again to get the latest metadata.
|
2016-03-13 01:08:15 +01:00
|
|
|
st, e := os.Stat(file.Name())
|
2016-02-03 00:23:28 +01:00
|
|
|
if e != nil {
|
2016-03-12 01:31:24 +01:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-03-13 01:08:15 +01:00
|
|
|
|
2016-02-01 21:19:54 +01:00
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" {
|
2016-02-06 00:09:23 +01:00
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 21:19:54 +01:00
|
|
|
}
|
2016-03-12 01:31:24 +01:00
|
|
|
newObject := ObjectInfo{
|
2016-02-27 12:04:52 +01:00
|
|
|
Bucket: bucket,
|
2016-03-12 01:31:24 +01:00
|
|
|
Name: object,
|
|
|
|
ModifiedTime: st.ModTime(),
|
2016-02-27 12:04:52 +01:00
|
|
|
Size: st.Size(),
|
2016-03-13 01:08:15 +01:00
|
|
|
MD5Sum: newMD5Hex,
|
2016-02-27 12:04:52 +01:00
|
|
|
ContentType: contentType,
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
return newObject, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:40:58 +01:00
|
|
|
// deleteObjectPath - delete object path if its empty.
|
2015-10-29 23:09:29 +01:00
|
|
|
func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error {
|
|
|
|
if basePath == deletePath {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
// Verify if the path exists.
|
2016-02-05 05:40:58 +01:00
|
|
|
pathSt, e := os.Stat(deletePath)
|
2016-02-03 00:23:28 +01:00
|
|
|
if e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2015-10-29 23:09:29 +01:00
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-03 00:23:28 +01:00
|
|
|
return probe.NewError(e)
|
2015-10-29 23:09:29 +01:00
|
|
|
}
|
2016-02-05 05:40:58 +01:00
|
|
|
if pathSt.IsDir() {
|
2016-02-06 10:36:43 +01:00
|
|
|
// Verify if directory is empty.
|
2016-03-12 04:47:44 +01:00
|
|
|
empty, e := isDirEmpty(deletePath)
|
2016-02-04 21:52:25 +01:00
|
|
|
if e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 23:09:29 +01:00
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
// Attempt to remove path.
|
2016-02-03 00:23:28 +01:00
|
|
|
if e := os.Remove(deletePath); e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 23:09:29 +01:00
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
// Recursively go down the next path and delete again.
|
2015-10-29 23:09:29 +01:00
|
|
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil {
|
2016-02-03 00:23:28 +01:00
|
|
|
return err.Trace(basePath, deletePath, bucket, object)
|
2015-10-29 23:09:29 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:36:43 +01:00
|
|
|
// DeleteObject - delete object.
|
2015-10-18 04:17:33 +02:00
|
|
|
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
2016-02-06 10:36:43 +01:00
|
|
|
// Check bucket name valid
|
2015-12-07 22:39:18 +01:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 20:26:01 +02:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-01-25 08:03:38 +01:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-29 23:09:29 +01:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-06 10:36:43 +01:00
|
|
|
// Check bucket exists
|
2016-01-25 08:03:38 +01: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 20:26:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:36:43 +01:00
|
|
|
// Verify object path legal
|
2015-10-16 20:26:01 +02:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:36:43 +01: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 20:26:01 +02:00
|
|
|
var objectPath string
|
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 21:01:19 +02:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 20:26:01 +02:00
|
|
|
} else {
|
2015-10-17 21:01:19 +02:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
2016-02-06 10:36:43 +01:00
|
|
|
// Delete object path if its empty.
|
2015-10-29 23:09:29 +01:00
|
|
|
err := deleteObjectPath(bucketPath, objectPath, bucket, object)
|
|
|
|
if err != nil {
|
2016-02-05 05:40:58 +01:00
|
|
|
if os.IsNotExist(err.ToGoError()) {
|
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-03 00:23:28 +01:00
|
|
|
return err.Trace(bucketPath, objectPath, bucket, object)
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|