mirror of
https://github.com/minio/minio.git
synced 2025-01-23 12:43:16 -05:00
Merge pull request #915 from harshavardhana/delete-bucket
Implement delete bucket properly with proper error handlings
This commit is contained in:
commit
c24235df8b
16
pkg/fs/fs.go
16
pkg/fs/fs.go
@ -17,7 +17,6 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
@ -94,21 +93,16 @@ func (fs API) DeleteBucket(bucket string) *probe.Error {
|
||||
if _, err := os.Stat(bucketDir); os.IsNotExist(err) {
|
||||
return probe.NewError(BucketNotFound{Bucket: bucket})
|
||||
}
|
||||
var errNotEmpty = errors.New("Directory Not empty")
|
||||
isDirNotEmpty := func(fp string, fl os.FileInfo, err error) error {
|
||||
if fl.Mode().IsRegular() || fl.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||
return errNotEmpty
|
||||
}
|
||||
return ErrSkipDir
|
||||
}
|
||||
err := WalkUnsorted(bucketDir, isDirNotEmpty)
|
||||
if err != nil {
|
||||
if err == errNotEmpty {
|
||||
if err := RemoveAllDirs(bucketDir); err != nil {
|
||||
if err == ErrDirNotEmpty || strings.Contains(err.Error(), "directory not empty") {
|
||||
return probe.NewError(BucketNotEmpty{Bucket: bucket})
|
||||
}
|
||||
return probe.NewError(err)
|
||||
}
|
||||
if err := os.Remove(bucketDir); err != nil {
|
||||
if strings.Contains(err.Error(), "directory not empty") {
|
||||
return probe.NewError(BucketNotEmpty{Bucket: bucket})
|
||||
}
|
||||
return probe.NewError(err)
|
||||
}
|
||||
return nil
|
||||
|
@ -23,6 +23,32 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// RemoveAllDirs - removes only itself and all subdirectories
|
||||
func RemoveAllDirs(path string) error {
|
||||
allFiles := func(fp string, fl os.FileInfo, err error) error {
|
||||
if fp == path {
|
||||
return nil
|
||||
}
|
||||
if fl.Mode().IsRegular() || fl.Mode()&os.ModeSymlink == os.ModeSymlink {
|
||||
return ErrDirNotEmpty
|
||||
}
|
||||
if fl.Mode().IsDir() {
|
||||
if err := os.Remove(fp); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err := WalkUnsorted(path, allFiles)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Walk walks the file tree rooted at root, calling walkFn for each file or
|
||||
// directory in the tree, including root.
|
||||
func Walk(root string, walkFn WalkFunc) error {
|
||||
@ -84,6 +110,10 @@ var ErrSkipDir = errors.New("skip this directory")
|
||||
// as an error by any function.
|
||||
var ErrSkipFile = errors.New("skip this file")
|
||||
|
||||
// ErrDirNotEmpty is used to throw error on directories which have atleast one regular
|
||||
// file or a symlink left
|
||||
var ErrDirNotEmpty = errors.New("directory not empty")
|
||||
|
||||
func walkUnsorted(path string, info os.FileInfo, walkFn WalkFunc) error {
|
||||
err := walkFn(path, info, nil)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user