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.
188 lines
5.1 KiB
188 lines
5.1 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 (
|
||
9 years ago
|
"io/ioutil"
|
||
9 years ago
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
9 years ago
|
"time"
|
||
9 years ago
|
|
||
|
"github.com/minio/minio/pkg/disk"
|
||
9 years ago
|
"github.com/minio/minio/pkg/probe"
|
||
9 years ago
|
)
|
||
|
|
||
|
/// Bucket Operations
|
||
|
|
||
9 years ago
|
// DeleteBucket - delete a bucket.
|
||
9 years ago
|
func (fs Filesystem) DeleteBucket(bucket string) *probe.Error {
|
||
9 years ago
|
// Verify bucket is 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
|
bucketDir := filepath.Join(fs.path, bucket)
|
||
9 years ago
|
if e := os.Remove(bucketDir); e != nil {
|
||
|
// Error if there was no bucket in the first place.
|
||
9 years ago
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
||
|
}
|
||
9 years ago
|
// 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.
|
||
9 years ago
|
if strings.Contains(e.Error(), "directory not empty") {
|
||
9 years ago
|
return probe.NewError(BucketNotEmpty{Bucket: bucket})
|
||
|
}
|
||
9 years ago
|
return probe.NewError(e)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// BucketInfo - name and create date
|
||
|
type BucketInfo struct {
|
||
|
Name string
|
||
|
Created time.Time
|
||
|
}
|
||
|
|
||
9 years ago
|
// ListBuckets - Get service.
|
||
9 years ago
|
func (fs Filesystem) ListBuckets() ([]BucketInfo, *probe.Error) {
|
||
9 years ago
|
files, e := ioutil.ReadDir(fs.path)
|
||
|
if e != nil {
|
||
9 years ago
|
return []BucketInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
var metadataList []BucketInfo
|
||
9 years ago
|
for _, file := range files {
|
||
|
if !file.IsDir() {
|
||
9 years ago
|
// If not directory, ignore all file types.
|
||
9 years ago
|
continue
|
||
|
}
|
||
9 years ago
|
// If directories are found with odd names, skip them.
|
||
9 years ago
|
dirName := strings.ToLower(file.Name())
|
||
9 years ago
|
if !IsValidBucketName(dirName) {
|
||
9 years ago
|
continue
|
||
9 years ago
|
}
|
||
9 years ago
|
metadata := BucketInfo{
|
||
9 years ago
|
Name: dirName,
|
||
9 years ago
|
Created: file.ModTime(),
|
||
|
}
|
||
|
metadataList = append(metadataList, metadata)
|
||
|
}
|
||
9 years ago
|
// Remove duplicated entries.
|
||
9 years ago
|
metadataList = removeDuplicateBuckets(metadataList)
|
||
9 years ago
|
return metadataList, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// removeDuplicateBuckets - remove duplicate buckets.
|
||
9 years ago
|
func removeDuplicateBuckets(buckets []BucketInfo) []BucketInfo {
|
||
9 years ago
|
length := len(buckets) - 1
|
||
|
for i := 0; i < length; i++ {
|
||
|
for j := i + 1; j <= length; j++ {
|
||
|
if buckets[i].Name == buckets[j].Name {
|
||
|
if buckets[i].Created.Sub(buckets[j].Created) > 0 {
|
||
|
buckets[i] = buckets[length]
|
||
|
} else {
|
||
|
buckets[j] = buckets[length]
|
||
|
}
|
||
|
buckets = buckets[0:length]
|
||
|
length--
|
||
|
j--
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return buckets
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// MakeBucket - PUT Bucket
|
||
|
func (fs Filesystem) MakeBucket(bucket string) *probe.Error {
|
||
9 years ago
|
di, err := disk.GetInfo(fs.path)
|
||
9 years ago
|
if err != nil {
|
||
|
return probe.NewError(err)
|
||
|
}
|
||
|
|
||
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 probe.NewError(RootPathFull{Path: fs.path})
|
||
|
}
|
||
|
|
||
9 years ago
|
// Verify if bucket is 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
|
bucketDir := filepath.Join(fs.path, bucket)
|
||
9 years ago
|
if _, e := os.Stat(bucketDir); e == nil {
|
||
|
return probe.NewError(BucketExists{Bucket: bucket})
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Make bucket.
|
||
9 years ago
|
if e := os.Mkdir(bucketDir, 0700); e != nil {
|
||
9 years ago
|
return probe.NewError(err)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// getActualBucketname - will convert incoming bucket names to
|
||
|
// corresponding actual bucketnames on the backend in a platform
|
||
9 years ago
|
// compatible way for all operating systems.
|
||
9 years ago
|
func getActualBucketname(fsPath, bucket string) string {
|
||
|
fd, e := os.Open(fsPath)
|
||
|
if e != nil {
|
||
|
return bucket
|
||
|
}
|
||
|
buckets, e := fd.Readdirnames(-1)
|
||
9 years ago
|
if e != nil {
|
||
9 years ago
|
return bucket
|
||
|
}
|
||
|
for _, b := range buckets {
|
||
9 years ago
|
// Verify if lowercase version of the bucket is equal
|
||
|
// to the incoming bucket, then use the proper name.
|
||
|
if strings.ToLower(b) == bucket {
|
||
|
return b
|
||
9 years ago
|
}
|
||
|
}
|
||
|
return bucket
|
||
|
}
|
||
|
|
||
9 years ago
|
// GetBucketInfo - get bucket metadata.
|
||
|
func (fs Filesystem) GetBucketInfo(bucket string) (BucketInfo, *probe.Error) {
|
||
9 years ago
|
if !IsValidBucketName(bucket) {
|
||
9 years ago
|
return BucketInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||
9 years ago
|
}
|
||
9 years ago
|
bucket = getActualBucketname(fs.path, bucket)
|
||
9 years ago
|
// Get bucket path.
|
||
9 years ago
|
bucketDir := filepath.Join(fs.path, bucket)
|
||
9 years ago
|
fi, e := os.Stat(bucketDir)
|
||
|
if e != nil {
|
||
9 years ago
|
// Check if bucket exists.
|
||
9 years ago
|
if os.IsNotExist(e) {
|
||
9 years ago
|
return BucketInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
||
9 years ago
|
}
|
||
9 years ago
|
return BucketInfo{}, probe.NewError(e)
|
||
9 years ago
|
}
|
||
9 years ago
|
bucketMetadata := BucketInfo{}
|
||
9 years ago
|
bucketMetadata.Name = fi.Name()
|
||
|
bucketMetadata.Created = fi.ModTime()
|
||
|
return bucketMetadata, nil
|
||
9 years ago
|
}
|