2016-04-30 02:52:17 +02:00
|
|
|
/*
|
2018-03-15 21:03:41 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
2016-04-30 02:52:17 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-04-30 02:52:17 +02:00
|
|
|
|
2016-05-25 10:33:39 +02:00
|
|
|
import (
|
2018-03-15 21:27:16 +01:00
|
|
|
"context"
|
2018-01-30 03:43:13 +01:00
|
|
|
"path"
|
2016-05-25 10:33:39 +02:00
|
|
|
"sync"
|
2016-11-23 03:18:22 +01:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2018-04-06 00:04:40 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2016-05-25 10:33:39 +02:00
|
|
|
)
|
2016-04-30 02:52:17 +02:00
|
|
|
|
2016-05-30 00:38:14 +02:00
|
|
|
const (
|
|
|
|
// Block size used for all internal operations version 1.
|
2016-11-23 03:18:22 +01:00
|
|
|
blockSizeV1 = 10 * humanize.MiByte
|
2016-06-25 12:03:27 +02:00
|
|
|
|
|
|
|
// Staging buffer read size for all internal operations version 1.
|
2016-11-23 03:18:22 +01:00
|
|
|
readSizeV1 = 1 * humanize.MiByte
|
2016-07-22 02:31:14 +02:00
|
|
|
|
|
|
|
// Buckets meta prefix.
|
|
|
|
bucketMetaPrefix = "buckets"
|
2017-04-11 04:51:23 +02:00
|
|
|
|
2017-05-14 21:05:51 +02:00
|
|
|
// ETag (hex encoded md5sum) of empty string.
|
|
|
|
emptyETag = "d41d8cd98f00b204e9800998ecf8427e"
|
2016-05-30 00:38:14 +02:00
|
|
|
)
|
|
|
|
|
2016-10-10 08:03:10 +02:00
|
|
|
// Global object layer mutex, used for safely updating object layer.
|
2017-03-03 10:07:45 +01:00
|
|
|
var globalObjLayerMutex *sync.RWMutex
|
2016-10-10 08:03:10 +02:00
|
|
|
|
|
|
|
// Global object layer, only accessed by newObjectLayerFn().
|
|
|
|
var globalObjectAPI ObjectLayer
|
|
|
|
|
2018-03-28 23:14:06 +02:00
|
|
|
//Global cacheObjects, only accessed by newCacheObjectsFn().
|
|
|
|
var globalCacheObjectAPI CacheObjectLayer
|
|
|
|
|
2016-10-10 08:03:10 +02:00
|
|
|
func init() {
|
|
|
|
// Initialize this once per server initialization.
|
2017-03-03 10:07:45 +01:00
|
|
|
globalObjLayerMutex = &sync.RWMutex{}
|
2016-10-10 08:03:10 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 01:33:01 +01:00
|
|
|
// Checks if the object is a directory, this logic uses
|
|
|
|
// if size == 0 and object ends with slashSeparator then
|
|
|
|
// returns true.
|
|
|
|
func isObjectDir(object string, size int64) bool {
|
2017-02-16 23:52:14 +01:00
|
|
|
return hasSuffix(object, slashSeparator) && size == 0
|
2017-01-21 01:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converts just bucket, object metadata into ObjectInfo datatype.
|
|
|
|
func dirObjectInfo(bucket, object string, size int64, metadata map[string]string) ObjectInfo {
|
|
|
|
// This is a special case with size as '0' and object ends with
|
|
|
|
// a slash separator, we treat it like a valid operation and
|
|
|
|
// return success.
|
2017-05-14 21:05:51 +02:00
|
|
|
etag := metadata["etag"]
|
|
|
|
delete(metadata, "etag")
|
|
|
|
if etag == "" {
|
|
|
|
etag = emptyETag
|
2017-04-11 04:51:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 01:33:01 +01:00
|
|
|
return ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
2017-03-18 19:28:41 +01:00
|
|
|
ModTime: UTCNow(),
|
2017-01-21 01:33:01 +01:00
|
|
|
ContentType: "application/octet-stream",
|
|
|
|
IsDir: true,
|
|
|
|
Size: size,
|
2017-05-14 21:05:51 +02:00
|
|
|
ETag: etag,
|
2017-01-21 01:33:01 +01:00
|
|
|
UserDefined: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 00:04:40 +02:00
|
|
|
func deleteBucketMetadata(ctx context.Context, bucket string, objAPI ObjectLayer) {
|
2018-02-13 03:34:30 +01:00
|
|
|
// Delete bucket access policy, if present - ignore any errors.
|
2018-04-25 00:53:30 +02:00
|
|
|
removePolicyConfig(ctx, objAPI, bucket)
|
2018-02-13 03:34:30 +01:00
|
|
|
|
|
|
|
// Delete notification config, if present - ignore any errors.
|
2018-04-06 00:04:40 +02:00
|
|
|
removeNotificationConfig(ctx, objAPI, bucket)
|
2018-02-13 03:34:30 +01:00
|
|
|
|
|
|
|
// Delete listener config, if present - ignore any errors.
|
2018-04-06 00:04:40 +02:00
|
|
|
removeListenerConfig(ctx, objAPI, bucket)
|
2018-02-13 03:34:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 10:49:46 +02:00
|
|
|
// Depending on the disk type network or local, initialize storage API.
|
2017-04-12 00:44:27 +02:00
|
|
|
func newStorageAPI(endpoint Endpoint) (storage StorageAPI, err error) {
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
return newPosix(endpoint.Path)
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
2017-04-12 00:44:27 +02:00
|
|
|
|
2018-10-05 02:44:06 +02:00
|
|
|
return newStorageRESTClient(endpoint), nil
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 19:15:34 +02:00
|
|
|
// Cleanup a directory recursively.
|
2018-04-06 00:04:40 +02:00
|
|
|
func cleanupDir(ctx context.Context, storage StorageAPI, volume, dirPath string) error {
|
2016-05-08 19:15:34 +02:00
|
|
|
var delFunc func(string) error
|
|
|
|
// Function to delete entries recursively.
|
|
|
|
delFunc = func(entryPath string) error {
|
2017-02-16 23:52:14 +01:00
|
|
|
if !hasSuffix(entryPath, slashSeparator) {
|
2016-07-12 10:01:47 +02:00
|
|
|
// Delete the file entry.
|
2018-04-06 00:04:40 +02:00
|
|
|
err := storage.DeleteFile(volume, entryPath)
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2016-05-08 19:15:34 +02:00
|
|
|
}
|
2016-07-12 10:01:47 +02:00
|
|
|
|
2016-05-08 19:15:34 +02:00
|
|
|
// If it's a directory, list and call delFunc() for each entry.
|
2018-05-09 04:08:21 +02:00
|
|
|
entries, err := storage.ListDir(volume, entryPath, -1)
|
2016-07-12 10:01:47 +02:00
|
|
|
// If entryPath prefix never existed, safe to ignore.
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return nil
|
|
|
|
} else if err != nil { // For any other errors fail.
|
2018-04-06 00:04:40 +02:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2016-07-12 10:01:47 +02:00
|
|
|
} // else on success..
|
2016-06-14 10:39:40 +02:00
|
|
|
|
2018-01-30 03:43:13 +01:00
|
|
|
// Entry path is empty, just delete it.
|
|
|
|
if len(entries) == 0 {
|
2018-04-06 00:04:40 +02:00
|
|
|
err = storage.DeleteFile(volume, path.Clean(entryPath))
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2018-01-30 03:43:13 +01:00
|
|
|
}
|
|
|
|
|
2016-07-12 10:01:47 +02:00
|
|
|
// Recurse and delete all other entries.
|
|
|
|
for _, entry := range entries {
|
|
|
|
if err = delFunc(pathJoin(entryPath, entry)); err != nil {
|
2016-05-08 19:15:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-03 01:34:15 +02:00
|
|
|
err := delFunc(retainSlash(pathJoin(dirPath)))
|
|
|
|
return err
|
2016-05-08 19:15:34 +02:00
|
|
|
}
|
2018-03-15 21:03:41 +01:00
|
|
|
|
|
|
|
// Removes notification.xml for a given bucket, only used during DeleteBucket.
|
2018-04-06 00:04:40 +02:00
|
|
|
func removeNotificationConfig(ctx context.Context, objAPI ObjectLayer, bucket string) error {
|
2018-03-15 21:03:41 +01:00
|
|
|
// Verify bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
ncPath := path.Join(bucketConfigPrefix, bucket, bucketNotificationConfig)
|
2018-04-06 00:04:40 +02:00
|
|
|
return objAPI.DeleteObject(ctx, minioMetaBucket, ncPath)
|
2018-03-15 21:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove listener configuration from storage layer. Used when a bucket is deleted.
|
2018-04-06 00:04:40 +02:00
|
|
|
func removeListenerConfig(ctx context.Context, objAPI ObjectLayer, bucket string) error {
|
2018-03-15 21:03:41 +01:00
|
|
|
// make the path
|
|
|
|
lcPath := path.Join(bucketConfigPrefix, bucket, bucketListenerConfig)
|
2018-04-06 00:04:40 +02:00
|
|
|
return objAPI.DeleteObject(ctx, minioMetaBucket, lcPath)
|
2018-03-15 21:03:41 +01:00
|
|
|
}
|