mirror of
https://github.com/minio/minio.git
synced 2025-01-24 13:13:16 -05:00
Merge pull request #949 from krishnasrinivas/rm-empty-dirs
Remove empty directories while removing an oobject
This commit is contained in:
commit
7df81f8707
@ -268,6 +268,37 @@ func (fs Filesystem) CreateObject(bucket, object, expectedMD5Sum string, size in
|
|||||||
return newObject, nil
|
return newObject, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error {
|
||||||
|
if basePath == deletePath {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := os.Stat(deletePath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||||||
|
}
|
||||||
|
return probe.NewError(err)
|
||||||
|
}
|
||||||
|
if fi.IsDir() {
|
||||||
|
empty, err := isDirEmpty(deletePath)
|
||||||
|
if err != nil {
|
||||||
|
return err.Trace()
|
||||||
|
}
|
||||||
|
if !empty {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(deletePath); err != nil {
|
||||||
|
return probe.NewError(err)
|
||||||
|
}
|
||||||
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil {
|
||||||
|
return err.Trace()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteObject - delete and object
|
// DeleteObject - delete and object
|
||||||
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
||||||
fs.lock.Lock()
|
fs.lock.Lock()
|
||||||
@ -278,6 +309,7 @@ func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
|||||||
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bucketPath := filepath.Join(fs.path, bucket)
|
||||||
// check bucket exists
|
// check bucket exists
|
||||||
if _, err := os.Stat(filepath.Join(fs.path, bucket)); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(fs.path, bucket)); os.IsNotExist(err) {
|
||||||
return probe.NewError(BucketNotFound{Bucket: bucket})
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
||||||
@ -296,16 +328,12 @@ func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
|||||||
} else {
|
} else {
|
||||||
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
||||||
}
|
}
|
||||||
|
err := deleteObjectPath(bucketPath, objectPath, bucket, object)
|
||||||
_, err := os.Stat(objectPath)
|
if os.IsNotExist(err.ToGoError()) {
|
||||||
if err != nil {
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
||||||
}
|
|
||||||
return probe.NewError(err)
|
|
||||||
}
|
}
|
||||||
if err := os.Remove(objectPath); err != nil {
|
if err != nil {
|
||||||
return probe.NewError(err)
|
return err.Trace()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,31 @@ package fs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/minio/minio-xl/pkg/probe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Check if a directory is empty
|
||||||
|
func isDirEmpty(dirname string) (bool, *probe.Error) {
|
||||||
|
f, err := os.Open(dirname)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
return false, probe.NewError(err)
|
||||||
|
}
|
||||||
|
names, err := f.Readdirnames(1)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return false, probe.NewError(err)
|
||||||
|
}
|
||||||
|
if len(names) > 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveAllDirs - removes only itself and all subdirectories
|
// RemoveAllDirs - removes only itself and all subdirectories
|
||||||
func RemoveAllDirs(path string) error {
|
func RemoveAllDirs(path string) error {
|
||||||
allFiles := func(fp string, fl os.FileInfo, err error) error {
|
allFiles := func(fp string, fl os.FileInfo, err error) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user