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.
151 lines
4.1 KiB
151 lines
4.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 ioutils
|
||
9 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
9 years ago
|
"io"
|
||
9 years ago
|
"os"
|
||
|
"path/filepath"
|
||
|
"sort"
|
||
|
)
|
||
|
|
||
9 years ago
|
// IsDirEmpty Check if a directory is empty
|
||
|
func IsDirEmpty(dirname string) (bool, error) {
|
||
|
names, err := ReadDirNamesN(dirname, 1)
|
||
9 years ago
|
if err != nil && err != io.EOF {
|
||
9 years ago
|
return false, err
|
||
9 years ago
|
}
|
||
|
if len(names) > 0 {
|
||
|
return false, nil
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// FTW walks the file tree rooted at root, calling walkFn for each file or
|
||
9 years ago
|
// directory in the tree, including root.
|
||
9 years ago
|
func FTW(root string, walkFn FTWFunc) error {
|
||
9 years ago
|
info, err := os.Lstat(root)
|
||
|
if err != nil {
|
||
|
return walkFn(root, nil, err)
|
||
|
}
|
||
|
return walk(root, info, walkFn)
|
||
|
}
|
||
|
|
||
9 years ago
|
// getRealName - gets the proper filename for sorting purposes
|
||
|
// Readdir() filters out directory names without separators, add
|
||
|
// them back for proper sorting results.
|
||
|
func getRealName(info os.FileInfo) string {
|
||
|
if info.IsDir() {
|
||
|
// Make sure directory has its end separator.
|
||
|
return info.Name() + string(os.PathSeparator)
|
||
|
}
|
||
|
return info.Name()
|
||
|
}
|
||
|
|
||
9 years ago
|
// readDirNames reads the directory named by dirname and returns
|
||
|
// a sorted list of directory entries.
|
||
|
func readDirNames(dirname string) ([]string, error) {
|
||
9 years ago
|
names, err := readDirUnsortedNames(dirname)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
sort.Strings(names)
|
||
|
return names, nil
|
||
|
}
|
||
|
|
||
|
func readDirUnsortedNames(dirname string) ([]string, error) {
|
||
9 years ago
|
f, err := os.Open(dirname)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
nameInfos, err := f.Readdir(-1)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
if err = f.Close(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
var names []string
|
||
|
for _, nameInfo := range nameInfos {
|
||
|
names = append(names, getRealName(nameInfo))
|
||
|
}
|
||
9 years ago
|
return names, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// FTWFunc is the type of the function called for each file or directory
|
||
9 years ago
|
// 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.
|
||
9 years ago
|
type FTWFunc func(path string, info os.FileInfo, err error) error
|
||
9 years ago
|
|
||
|
// 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")
|
||
|
|
||
9 years ago
|
// 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")
|
||
|
|
||
9 years ago
|
// walk recursively descends path, calling walkFn.
|
||
|
func walk(path string, info os.FileInfo, walkFn FTWFunc) error {
|
||
9 years ago
|
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
|
||
|
}
|