*.go: ignoreSuffixes to not walk the basedir

and maybe to let the user specify paths too
This commit is contained in:
Vincent Batts 2023-04-12 10:59:00 -04:00
parent 2835d1a77d
commit ef98140425
2 changed files with 32 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"
)
@ -23,13 +24,21 @@ type HashInfo struct {
// HashFileGetter walks the provided `path` with `workers` number of threads.
// The channel of HashInfo are for each regular file encountered.
func HashFileGetter(path string, hash crypto.Hash, workers int, done <-chan struct{}) <-chan HashInfo {
func HashFileGetter(path string, hash crypto.Hash, ignoreSuffixes []string, workers int, done <-chan struct{}) <-chan HashInfo {
out := make(chan HashInfo, workers)
go func() {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, suff := range ignoreSuffixes {
if os.Getenv("DEBUG") != "" {
fmt.Printf("[DEBUG] path: %q ; suff: %q\n", filepath.Clean(path), filepath.Clean(suff))
}
if strings.HasSuffix(filepath.Clean(path), filepath.Clean(suff)) {
return filepath.SkipDir
}
}
if !info.Mode().IsRegular() {
return nil
}

24
main.go
View File

@ -58,11 +58,31 @@ func main() {
}
var (
hash = cryptomap.DetermineHash(*flCipher)
ignoreSuffixes = []string{}
hash = cryptomap.DetermineHash(*flCipher)
//infos = []*file.HashInfo{}
//results := make(chan file.HashInfo, 2)
)
bpath, err := filepath.Abs(*flVarBase)
if err != nil {
log.Fatal(err)
}
for _, arg := range flag.Args() {
apath, err := filepath.Abs(arg)
if err != nil {
log.Fatal(err)
}
rel, err := filepath.Rel(apath, bpath)
if err != nil {
log.Fatal(err)
}
ignoreSuffixes = append(ignoreSuffixes, rel)
}
if os.Getenv("DEBUG") != "" {
fmt.Printf("[DEBUG] ignoreSuffixes: %#v\n", ignoreSuffixes)
}
for _, arg := range flag.Args() {
if !*flNoop {
if m, err := file.SameDevPaths(*flVarBase, arg); err != nil {
@ -73,7 +93,7 @@ func main() {
}
}
done := make(chan struct{})
infos := file.HashFileGetter(arg, hash, *flWorkers, done)
infos := file.HashFileGetter(arg, hash, ignoreSuffixes, *flWorkers, done)
for fi := range infos {
if fi.Err != nil {
log.Println(fi.Err)