Skip to content

Added ReadDirRecursiveFiltered and some minor optimizations #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 11, 2022
Merged
41 changes: 0 additions & 41 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,47 +306,6 @@ func (p *Path) IsDirCheck() (bool, error) {
return false, err
}

// ReadDir returns a PathList containing the content of the directory
// pointed by the current Path
func (p *Path) ReadDir() (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
return nil, err
}
paths := PathList{}
for _, info := range infos {
path := p.Clone().Join(info.Name())
paths.Add(path)
}
return paths, nil
}

// ReadDirRecursive returns a PathList containing the content of the directory
// and its subdirectories pointed by the current Path
func (p *Path) ReadDirRecursive() (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
return nil, err
}
paths := PathList{}
for _, info := range infos {
path := p.Clone().Join(info.Name())
paths.Add(path)

if isDir, err := path.IsDirCheck(); err != nil {
return nil, err
} else if isDir {
subPaths, err := path.ReadDirRecursive()
if err != nil {
return nil, err
}
paths.AddAll(subPaths)
}

}
return paths, nil
}

// CopyTo copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
Expand Down
39 changes: 0 additions & 39 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
package paths

import (
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -255,44 +254,6 @@ func TestParents(t *testing.T) {
pathEqualsTo(t, ".", parents2[4])
}

func TestReadDirRecursive(t *testing.T) {
testPath := New("_testdata")

list, err := testPath.ReadDirRecursive()
require.NoError(t, err)
require.Len(t, list, 16)

pathEqualsTo(t, "_testdata/anotherFile", list[0])
pathEqualsTo(t, "_testdata/file", list[1])
pathEqualsTo(t, "_testdata/folder", list[2])
pathEqualsTo(t, "_testdata/folder/.hidden", list[3])
pathEqualsTo(t, "_testdata/folder/file2", list[4])
pathEqualsTo(t, "_testdata/folder/file3", list[5])
pathEqualsTo(t, "_testdata/folder/subfolder", list[6])
pathEqualsTo(t, "_testdata/folder/subfolder/file4", list[7])
pathEqualsTo(t, "_testdata/symlinktofolder", list[8])
pathEqualsTo(t, "_testdata/symlinktofolder/.hidden", list[9])
pathEqualsTo(t, "_testdata/symlinktofolder/file2", list[10])
pathEqualsTo(t, "_testdata/symlinktofolder/file3", list[11])
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder", list[12])
pathEqualsTo(t, "_testdata/symlinktofolder/subfolder/file4", list[13])
pathEqualsTo(t, "_testdata/test.txt", list[14])
pathEqualsTo(t, "_testdata/test.txt.gz", list[15])

// Test symlink loop
tmp, err := MkTempDir("", "")
require.NoError(t, err)
defer tmp.RemoveAll()

folder := tmp.Join("folder")
err = os.Symlink(tmp.String(), folder.String())
require.NoError(t, err)

l, err := tmp.ReadDirRecursive()
require.Error(t, err)
require.Nil(t, l)
}

func TestFilterDirs(t *testing.T) {
testPath := New("_testdata")

Expand Down
228 changes: 228 additions & 0 deletions readdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* This file is part of PathsHelper library.
*
* Copyright 2018-2022 Arduino AG (http://www.arduino.cc/)
*
* PathsHelper library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/

package paths

import (
"io/ioutil"
"strings"
)

// ReadDirFilter is a filter for Path.ReadDir and Path.ReadDirRecursive methods.
// The filter should return true to accept a file or false to reject it.
type ReadDirFilter func(file *Path) bool

// ReadDir returns a PathList containing the content of the directory
// pointed by the current Path. The resulting list is filtered by the given filters chained.
func (p *Path) ReadDir(filters ...ReadDirFilter) (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
return nil, err
}

accept := func(p *Path) bool {
for _, filter := range filters {
if !filter(p) {
return false
}
}
return true
}

paths := PathList{}
for _, info := range infos {
path := p.Join(info.Name())
if !accept(path) {
continue
}
paths.Add(path)
}
return paths, nil
}

// ReadDirRecursive returns a PathList containing the content of the directory
// and its subdirectories pointed by the current Path
func (p *Path) ReadDirRecursive() (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
return nil, err
}
paths := PathList{}
for _, info := range infos {
path := p.Join(info.Name())
paths.Add(path)

if isDir, err := path.IsDirCheck(); err != nil {
return nil, err
} else if isDir {
subPaths, err := path.ReadDirRecursive()
if err != nil {
return nil, err
}
paths.AddAll(subPaths)
}

}
return paths, nil
}

// ReadDirRecursiveFiltered returns a PathList containing the content of the directory
// and its subdirectories pointed by the current Path, filtered by the given skipFilter
// and filters:
// - `recursionFilter` is a filter that is checked to determine if the subdirectory must
// by visited recursively (if the filter rejects the entry, the entry is not visited
// but can still be added to the result)
// - `filters` are the filters that are checked to determine if the entry should be
// added to the resulting PathList
func (p *Path) ReadDirRecursiveFiltered(recursionFilter ReadDirFilter, filters ...ReadDirFilter) (PathList, error) {
infos, err := ioutil.ReadDir(p.path)
if err != nil {
return nil, err
}

accept := func(p *Path) bool {
for _, filter := range filters {
if !filter(p) {
return false
}
}
return true
}

paths := PathList{}
for _, info := range infos {
path := p.Join(info.Name())

if accept(path) {
paths.Add(path)
}

if recursionFilter == nil || recursionFilter(path) {
if isDir, err := path.IsDirCheck(); err != nil {
return nil, err
} else if isDir {
subPaths, err := path.ReadDirRecursiveFiltered(recursionFilter, filters...)
if err != nil {
return nil, err
}
paths.AddAll(subPaths)
}
}
}
return paths, nil
}

// FilterDirectories is a ReadDirFilter that accepts only directories
func FilterDirectories() ReadDirFilter {
return func(path *Path) bool {
return path.IsDir()
}
}

// FilterOutDirectories is a ReadDirFilter that rejects all directories
func FilterOutDirectories() ReadDirFilter {
return func(path *Path) bool {
return !path.IsDir()
}
}

// FilterSuffixes creates a ReadDirFilter that accepts only the given
// filename suffixes
func FilterSuffixes(allowedSuffixes ...string) ReadDirFilter {
return func(file *Path) bool {
for _, suffix := range allowedSuffixes {
if strings.HasSuffix(file.String(), suffix) {
return true
}
}
return false
}
}

// FilterOutSuffixes creates a ReadDirFilter that rejects all the given
// filename suffixes
func FilterOutSuffixes(rejectedSuffixes ...string) ReadDirFilter {
return func(file *Path) bool {
for _, suffix := range rejectedSuffixes {
if strings.HasSuffix(file.String(), suffix) {
return false
}
}
return true
}
}

// FilterPrefixes creates a ReadDirFilter that accepts only the given
// filename prefixes
func FilterPrefixes(allowedPrefixes ...string) ReadDirFilter {
return func(file *Path) bool {
name := file.Base()
for _, prefix := range allowedPrefixes {
if strings.HasPrefix(name, prefix) {
return true
}
}
return false
}
}

// FilterOutPrefixes creates a ReadDirFilter that rejects all the given
// filename prefixes
func FilterOutPrefixes(rejectedPrefixes ...string) ReadDirFilter {
return func(file *Path) bool {
name := file.Base()
for _, prefix := range rejectedPrefixes {
if strings.HasPrefix(name, prefix) {
return false
}
}
return true
}
}

// OrFilter creates a ReadDirFilter that accepts all items that are accepted by x or by y
func OrFilter(x, y ReadDirFilter) ReadDirFilter {
return func(path *Path) bool {
return x(path) || y(path)
}
}

// AndFilter creates a ReadDirFilter that accepts all items that are accepted by both x and y
func AndFilter(x, y ReadDirFilter) ReadDirFilter {
return func(path *Path) bool {
return x(path) && y(path)
}
}

// NotFilter creates a ReadDifFilter that accepts all items rejected by x and viceversa
func NotFilter(x ReadDirFilter) ReadDirFilter {
return func(path *Path) bool {
return !x(path)
}
}
Loading