Add option in readDir to enable symlink following of dirs (#12668)

This commit is contained in:
Anis Elleuch
2021-07-10 00:20:51 +01:00
committed by GitHub
parent da0fd5f056
commit 9be040dd14
5 changed files with 52 additions and 28 deletions
+7 -12
View File
@@ -30,11 +30,6 @@ func access(name string) error {
return err
}
// Return all the entries at the directory dirPath.
func readDir(dirPath string) (entries []string, err error) {
return readDirN(dirPath, -1)
}
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
// the directory itself, if the dirPath doesn't exist this function doesn't return
// an error.
@@ -90,8 +85,8 @@ func readDirFn(dirPath string, filter func(name string, typ os.FileMode) error)
return nil
}
// Return N entries at the directory dirPath. If count is -1, return all entries
func readDirN(dirPath string, count int) (entries []string, err error) {
// Return entries at the directory dirPath.
func readDirWithOpts(dirPath string, opts readDirOpts) (entries []string, err error) {
d, err := Open(dirPath)
if err != nil {
return nil, osErrToFileErr(err)
@@ -99,12 +94,12 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
defer d.Close()
maxEntries := 1000
if count > 0 && count < maxEntries {
if opts.count > 0 && opts.count < maxEntries {
maxEntries = count
}
done := false
remaining := count
remaining := opts.count
for !done {
// Read up to max number of entries.
@@ -115,7 +110,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
}
return nil, osErrToFileErr(err)
}
if count > -1 {
if opts.count > -1 {
if remaining <= len(fis) {
fis = fis[:remaining]
done = true
@@ -136,7 +131,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
}
// Ignore symlinked directories.
if fi.IsDir() {
if !opts.followDirSymlink && fi.IsDir() {
continue
}
}
@@ -147,7 +142,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
} else if fi.Mode().IsRegular() {
entries = append(entries, fi.Name())
}
if count > 0 {
if opts.count > 0 {
remaining--
}
}