Skip to content

Commit

Permalink
Merge pull request golang#569 from darkowlzz/improve-IsNonEmptyDir-Re…
Browse files Browse the repository at this point in the history
…addirnames

Improve IsNonEmptyDir - use File.Readdirnames
  • Loading branch information
sdboyer authored May 12, 2017
2 parents 76e8dbd + a637888 commit 1355884
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@ func IsNonEmptyDir(name string) (bool, error) {
return isDir, err
}

files, err := ioutil.ReadDir(name)
// Get file descriptor
f, err := os.Open(name)
if err != nil {
return false, err
}
return len(files) != 0, nil
defer f.Close()

// Query only 1 child. EOF if no children.
_, err = f.Readdirnames(1)
switch err {
case io.EOF:
return false, nil
case nil:
return true, nil
default:
return false, err
}
}

func writeFile(path string, in toml.Marshaler) error {
Expand Down

0 comments on commit 1355884

Please sign in to comment.