2015-10-17 22:17:33 -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 (
|
2016-01-25 02:03:38 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-10-17 22:17:33 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-01-25 02:03:38 -05:00
|
|
|
"github.com/minio/minio/pkg/ioutils"
|
2015-10-17 22:17:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Bucket Operations
|
|
|
|
|
|
|
|
// DeleteBucket - delete bucket
|
|
|
|
func (fs Filesystem) DeleteBucket(bucket string) *probe.Error {
|
|
|
|
// verify bucket path legal
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-17 22:17:33 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-17 22:17:33 -04:00
|
|
|
bucketDir := filepath.Join(fs.path, bucket)
|
|
|
|
// check bucket exists
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketDir); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2015-11-01 23:56:54 -05:00
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
if e := os.Remove(bucketDir); e != nil {
|
2016-01-25 20:57:19 -05:00
|
|
|
// On windows the string is slightly different, handle it here.
|
|
|
|
if strings.Contains(e.Error(), "directory is not empty") {
|
|
|
|
return probe.NewError(BucketNotEmpty{Bucket: bucket})
|
|
|
|
}
|
|
|
|
// Hopefully for all other operating systems, this is
|
|
|
|
// assumed to be consistent.
|
2016-01-25 02:03:38 -05:00
|
|
|
if strings.Contains(e.Error(), "directory not empty") {
|
2015-10-17 22:17:33 -04:00
|
|
|
return probe.NewError(BucketNotEmpty{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Lock()
|
2015-11-01 23:56:54 -05:00
|
|
|
delete(fs.buckets.Metadata, bucket)
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Unlock()
|
|
|
|
if err := saveBucketsMetadata(*fs.buckets); err != nil {
|
2015-11-01 23:56:54 -05:00
|
|
|
return err.Trace(bucket)
|
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// ListBuckets - Get service.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) ListBuckets() ([]BucketMetadata, *probe.Error) {
|
2016-01-25 02:03:38 -05:00
|
|
|
files, err := ioutils.ReadDirN(fs.path, fs.maxBuckets)
|
|
|
|
if err != nil && err != io.EOF {
|
2015-10-17 22:17:33 -04:00
|
|
|
return []BucketMetadata{}, probe.NewError(err)
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
if err == io.EOF {
|
2016-02-04 23:40:58 -05:00
|
|
|
// This message is printed if there are more than 1000 buckets.
|
|
|
|
fmt.Printf("More buckets found, truncating the bucket list to %d entries only.", fs.maxBuckets)
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
var metadataList []BucketMetadata
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
// if files found ignore them
|
|
|
|
continue
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
dirName := strings.ToLower(file.Name())
|
2015-10-17 22:17:33 -04:00
|
|
|
if file.IsDir() {
|
2016-02-04 23:40:58 -05:00
|
|
|
// If directories found with odd names, skip them.
|
2016-01-25 02:03:38 -05:00
|
|
|
if !IsValidBucketName(dirName) {
|
2015-10-17 22:17:33 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata := BucketMetadata{
|
2016-01-25 02:03:38 -05:00
|
|
|
Name: dirName,
|
2015-10-17 22:17:33 -04:00
|
|
|
Created: file.ModTime(),
|
|
|
|
}
|
|
|
|
metadataList = append(metadataList, metadata)
|
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
// Remove duplicated entries.
|
2016-01-25 02:03:38 -05:00
|
|
|
metadataList = removeDuplicateBuckets(metadataList)
|
2015-10-17 22:17:33 -04:00
|
|
|
return metadataList, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// removeDuplicateBuckets - remove duplicate buckets.
|
|
|
|
func removeDuplicateBuckets(elements []BucketMetadata) (result []BucketMetadata) {
|
|
|
|
// Use map to record duplicates as we find them.
|
|
|
|
duplicates := make(map[string]struct{})
|
|
|
|
for _, element := range elements {
|
|
|
|
if _, ok := duplicates[element.Name]; !ok {
|
|
|
|
duplicates[element.Name] = struct{}{}
|
|
|
|
result = append(result, element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeBucket - PUT Bucket.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) MakeBucket(bucket, acl string) *probe.Error {
|
2016-01-25 02:03:38 -05:00
|
|
|
di, err := disk.GetInfo(fs.path)
|
2015-10-17 22:17:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return probe.NewError(err)
|
|
|
|
}
|
|
|
|
|
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 probe.NewError(RootPathFull{Path: fs.path})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if bucket path legal.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-17 22:17:33 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if bucket acl is legal.
|
2015-11-01 23:56:54 -05:00
|
|
|
if !IsValidBucketACL(acl) {
|
|
|
|
return probe.NewError(InvalidACL{ACL: acl})
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2016-02-04 23:40:58 -05:00
|
|
|
|
|
|
|
// Get bucket path.
|
2015-10-17 22:17:33 -04:00
|
|
|
bucketDir := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketDir); e == nil {
|
|
|
|
return probe.NewError(BucketExists{Bucket: bucket})
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Make bucket.
|
2016-01-25 02:03:38 -05:00
|
|
|
if e := os.Mkdir(bucketDir, 0700); e != nil {
|
2015-10-17 22:17:33 -04:00
|
|
|
return probe.NewError(err)
|
|
|
|
}
|
2015-11-01 23:56:54 -05:00
|
|
|
|
|
|
|
bucketMetadata := &BucketMetadata{}
|
2016-01-25 02:03:38 -05:00
|
|
|
fi, e := os.Stat(bucketDir)
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check if bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2015-11-01 23:56:54 -05:00
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return probe.NewError(e)
|
2015-11-01 23:56:54 -05:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(acl) == "" {
|
|
|
|
acl = "private"
|
|
|
|
}
|
|
|
|
bucketMetadata.Name = fi.Name()
|
|
|
|
bucketMetadata.Created = fi.ModTime()
|
|
|
|
bucketMetadata.ACL = BucketACL(acl)
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Lock()
|
2015-11-01 23:56:54 -05:00
|
|
|
fs.buckets.Metadata[bucket] = bucketMetadata
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Unlock()
|
|
|
|
if err := saveBucketsMetadata(*fs.buckets); err != nil {
|
2015-11-01 23:56:54 -05:00
|
|
|
return err.Trace(bucket)
|
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// denormalizeBucket - will convert incoming bucket names to
|
|
|
|
// corresponding valid bucketnames on the backend in a platform
|
|
|
|
// compatible way for all operating systems.
|
2016-01-25 02:03:38 -05:00
|
|
|
func (fs Filesystem) denormalizeBucket(bucket string) string {
|
2016-02-04 23:40:58 -05:00
|
|
|
buckets, e := ioutils.ReadDirNamesN(fs.path, fs.maxBuckets)
|
|
|
|
if e != nil {
|
2016-01-25 02:03:38 -05:00
|
|
|
return bucket
|
|
|
|
}
|
|
|
|
for _, b := range buckets {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify if lowercase version of the bucket is equal to the
|
|
|
|
// incoming bucket, then use the proper name.
|
2016-01-25 02:03:38 -05:00
|
|
|
if strings.ToLower(b) == bucket {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bucket
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// GetBucketMetadata - get bucket metadata.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) GetBucketMetadata(bucket string) (BucketMetadata, *probe.Error) {
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-17 22:17:33 -04:00
|
|
|
return BucketMetadata{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
|
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2016-02-04 23:40:58 -05:00
|
|
|
// Get bucket path.
|
2015-10-17 22:17:33 -04:00
|
|
|
bucketDir := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
fi, e := os.Stat(bucketDir)
|
|
|
|
if e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check if bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
2015-11-01 23:56:54 -05:00
|
|
|
return BucketMetadata{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return BucketMetadata{}, probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RLock()
|
2015-11-01 23:56:54 -05:00
|
|
|
bucketMetadata, ok := fs.buckets.Metadata[bucket]
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.RUnlock()
|
2015-11-01 23:56:54 -05:00
|
|
|
if !ok {
|
|
|
|
bucketMetadata = &BucketMetadata{}
|
|
|
|
bucketMetadata.Name = fi.Name()
|
|
|
|
bucketMetadata.Created = fi.ModTime()
|
|
|
|
bucketMetadata.ACL = BucketACL("private")
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
2015-11-01 23:56:54 -05:00
|
|
|
return *bucketMetadata, nil
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// SetBucketMetadata - set bucket metadata.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) SetBucketMetadata(bucket string, metadata map[string]string) *probe.Error {
|
2016-02-04 23:40:58 -05:00
|
|
|
fs.rwLock.Lock()
|
|
|
|
defer fs.rwLock.Unlock()
|
|
|
|
// Input validation.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-17 22:17:33 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
// Save the acl.
|
2015-10-17 22:17:33 -04:00
|
|
|
acl := metadata["acl"]
|
|
|
|
if !IsValidBucketACL(acl) {
|
|
|
|
return probe.NewError(InvalidACL{ACL: acl})
|
|
|
|
}
|
2015-11-01 23:56:54 -05:00
|
|
|
if strings.TrimSpace(acl) == "" {
|
|
|
|
acl = "private"
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
bucket = fs.denormalizeBucket(bucket)
|
2015-10-17 22:17:33 -04:00
|
|
|
bucketDir := filepath.Join(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
fi, e := os.Stat(bucketDir)
|
|
|
|
if e != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check if bucket exists.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
2015-11-01 23:56:54 -05:00
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
2015-11-01 23:56:54 -05:00
|
|
|
bucketMetadata, ok := fs.buckets.Metadata[bucket]
|
|
|
|
if !ok {
|
|
|
|
bucketMetadata = &BucketMetadata{}
|
|
|
|
bucketMetadata.Name = fi.Name()
|
|
|
|
bucketMetadata.Created = fi.ModTime()
|
|
|
|
}
|
|
|
|
bucketMetadata.ACL = BucketACL(acl)
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Lock()
|
2015-11-01 23:56:54 -05:00
|
|
|
fs.buckets.Metadata[bucket] = bucketMetadata
|
2016-02-05 05:15:48 -05:00
|
|
|
fs.rwLock.Unlock()
|
|
|
|
if err := saveBucketsMetadata(*fs.buckets); err != nil {
|
2015-11-01 23:56:54 -05:00
|
|
|
return err.Trace(bucket)
|
|
|
|
}
|
2015-10-17 22:17:33 -04:00
|
|
|
return nil
|
|
|
|
}
|