2015-10-16 20:26:01 +02: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 (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2015-10-17 09:00:50 +02:00
|
|
|
// 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 {
|
2015-10-17 09:13:46 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-17 09:00:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := WalkUnsorted(path, allFiles)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
// 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 {
|
|
|
|
info, err := os.Lstat(root)
|
|
|
|
if err != nil {
|
|
|
|
return walkFn(root, nil, err)
|
|
|
|
}
|
|
|
|
return walk(root, info, walkFn)
|
|
|
|
}
|
|
|
|
|
2015-10-17 08:11:41 +02:00
|
|
|
// WalkUnsorted walks the file tree rooted at root, calling walkFn for each file or
|
|
|
|
// directory in the tree, including root.
|
|
|
|
func WalkUnsorted(root string, walkFn WalkFunc) error {
|
|
|
|
info, err := os.Lstat(root)
|
|
|
|
if err != nil {
|
|
|
|
return walkFn(root, nil, err)
|
|
|
|
}
|
|
|
|
return walk(root, info, walkFn)
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
// readDirNames reads the directory named by dirname and returns
|
|
|
|
// a sorted list of directory entries.
|
|
|
|
func readDirNames(dirname string) ([]string, error) {
|
2015-10-17 08:11:41 +02:00
|
|
|
names, err := readDirUnsortedNames(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readDirUnsortedNames(dirname string) ([]string, error) {
|
2015-10-16 20:26:01 +02:00
|
|
|
f, err := os.Open(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
names, err := f.Readdirnames(-1)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkFunc is the type of the function called for each file or directory
|
|
|
|
// visited by Walk. The path argument contains the argument to Walk as a
|
|
|
|
// prefix; that is, if Walk is called with "dir", which is a directory
|
|
|
|
// containing the file "a", the walk function will be called with argument
|
|
|
|
// "dir/a". The info argument is the os.FileInfo for the named path.
|
|
|
|
type WalkFunc func(path string, info os.FileInfo, err error) error
|
|
|
|
|
|
|
|
// ErrSkipDir is used as a return value from WalkFuncs to indicate that
|
|
|
|
// the directory named in the call is to be skipped. It is not returned
|
|
|
|
// as an error by any function.
|
|
|
|
var ErrSkipDir = errors.New("skip this directory")
|
|
|
|
|
|
|
|
// ErrSkipFile is used as a return value from WalkFuncs to indicate that
|
|
|
|
// the file named in the call is to be skipped. It is not returned
|
|
|
|
// as an error by any function.
|
|
|
|
var ErrSkipFile = errors.New("skip this file")
|
|
|
|
|
2015-10-17 09:00:50 +02:00
|
|
|
// 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")
|
|
|
|
|
2015-10-17 08:11:41 +02:00
|
|
|
func walkUnsorted(path string, info os.FileInfo, walkFn WalkFunc) error {
|
|
|
|
err := walkFn(path, info, nil)
|
|
|
|
if err != nil {
|
|
|
|
if info.Mode().IsDir() && err == ErrSkipDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if info.Mode().IsRegular() && err == ErrSkipFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
names, err := readDirUnsortedNames(path)
|
|
|
|
if err != nil {
|
|
|
|
return walkFn(path, info, err)
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
filename := filepath.Join(path, name)
|
|
|
|
fileInfo, err := os.Lstat(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err := walkFn(filename, fileInfo, err); err != nil && err != ErrSkipDir && err != ErrSkipFile {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = walk(filename, fileInfo, walkFn)
|
|
|
|
if err != nil {
|
|
|
|
if err == ErrSkipDir || err == ErrSkipFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
// walk recursively descends path, calling w.
|
|
|
|
func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
|
|
|
|
err := walkFn(path, info, nil)
|
|
|
|
if err != nil {
|
|
|
|
if info.Mode().IsDir() && err == ErrSkipDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if info.Mode().IsRegular() && err == ErrSkipFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
names, err := readDirNames(path)
|
|
|
|
if err != nil {
|
|
|
|
return walkFn(path, info, err)
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
filename := filepath.Join(path, name)
|
|
|
|
fileInfo, err := os.Lstat(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err := walkFn(filename, fileInfo, err); err != nil && err != ErrSkipDir && err != ErrSkipFile {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = walk(filename, fileInfo, walkFn)
|
|
|
|
if err != nil {
|
|
|
|
if err == ErrSkipDir || err == ErrSkipFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|