re-use optimized readdir for isDirEmpty() (#10829)

reduces effective memory usage by an order
of magnitude, also increases performance for
small objects
This commit is contained in:
Harshavardhana 2020-11-04 13:05:21 -08:00 committed by GitHub
parent 1a1f00fa15
commit fde3299bf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -219,24 +219,14 @@ func getValidPath(path string, requireDirectIO bool) (string, error) {
// isDirEmpty - returns whether given directory is empty or not.
func isDirEmpty(dirname string) bool {
f, err := os.Open(dirname)
entries, err := readDirN(dirname, 1)
if err != nil {
if !os.IsNotExist(err) {
logger.LogIf(GlobalContext, err)
}
return false
}
defer f.Close()
// List one entry.
if _, err = f.Readdirnames(1); err != io.EOF {
if !os.IsNotExist(err) {
if err != errFileNotFound {
logger.LogIf(GlobalContext, err)
}
return false
}
// Returns true if we have reached EOF, directory is indeed empty.
return true
return len(entries) == 0
}
// Initialize a new storage disk.