1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// This file is part of PathsHelper library.
//
// Copyright 2018-2025 Arduino AG (http://www.arduino.cc/)
//
// This program 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 3 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, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package paths
import (
"errors"
"os"
"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 := os.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) {
return p.ReadDirRecursiveFiltered(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) {
var search func(*Path) (PathList, error)
explored := map[string]bool{}
search = func(currPath *Path) (PathList, error) {
canonical := currPath.Canonical().path
if explored[canonical] {
return nil, errors.New("directories symlink loop detected")
}
explored[canonical] = true
defer delete(explored, canonical)
infos, err := os.ReadDir(currPath.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 := currPath.Join(info.Name())
if accept(path) {
paths.Add(path)
}
if recursionFilter == nil || recursionFilter(path) {
if path.IsDir() {
subPaths, err := search(path)
if err != nil {
return nil, err
}
paths.AddAll(subPaths)
}
}
}
return paths, nil
}
return search(p)
}
// 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()
}
}
// FilterNames is a ReadDirFilter that accepts only the given filenames
func FilterNames(allowedNames ...string) ReadDirFilter {
return func(file *Path) bool {
for _, name := range allowedNames {
if file.Base() == name {
return true
}
}
return false
}
}
// FilterOutNames is a ReadDirFilter that rejects the given filenames
func FilterOutNames(rejectedNames ...string) ReadDirFilter {
return func(file *Path) bool {
for _, name := range rejectedNames {
if file.Base() == name {
return false
}
}
return true
}
}
// 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 any (at least one) of the given filters
func OrFilter(filters ...ReadDirFilter) ReadDirFilter {
return func(path *Path) bool {
for _, f := range filters {
if f(path) {
return true
}
}
return false
}
}
// AndFilter creates a ReadDirFilter that accepts all items that are accepted
// by all the given filters
func AndFilter(filters ...ReadDirFilter) ReadDirFilter {
return func(path *Path) bool {
for _, f := range filters {
if !f(path) {
return false
}
}
return true
}
}
// 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)
}
}
|