2020-10-30 20:01:56 +01:00
|
|
|
package speedwalk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
2021-05-21 00:12:30 +02:00
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
|
|
|
|
2020-10-30 20:01:56 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
|
|
|
|
"github.com/karrick/godirwalk"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AllFiles recursively lists every file in every subdirectory of a given directory
|
2020-11-02 20:48:56 +01:00
|
|
|
// If basename is true, return the basename of each file. Otherwise return the full path starting at startDir.
|
2020-10-30 20:01:56 +01:00
|
|
|
func AllFiles(startDir string, basename bool) ([]string, error) {
|
|
|
|
items, err := ioutil.ReadDir(startDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathChan := make(chan string)
|
|
|
|
paths := make([]string, 0, 1000)
|
2020-11-02 20:48:56 +01:00
|
|
|
pathWG := &sync.WaitGroup{}
|
|
|
|
pathWG.Add(1)
|
2021-05-21 00:12:30 +02:00
|
|
|
metrics.RoutinesQueue.WithLabelValues("speedwalk", "worker").Inc()
|
2020-10-30 20:01:56 +01:00
|
|
|
go func() {
|
2020-11-02 20:48:56 +01:00
|
|
|
defer pathWG.Done()
|
2020-10-30 20:01:56 +01:00
|
|
|
for {
|
|
|
|
path, ok := <-pathChan
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
maxThreads := runtime.NumCPU() - 1
|
|
|
|
goroutineLimiter := make(chan struct{}, maxThreads)
|
|
|
|
for i := 0; i < maxThreads; i++ {
|
|
|
|
goroutineLimiter <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:48:56 +01:00
|
|
|
walkerWG := &sync.WaitGroup{}
|
2020-10-30 20:01:56 +01:00
|
|
|
for _, item := range items {
|
|
|
|
if !item.IsDir() {
|
|
|
|
if basename {
|
|
|
|
pathChan <- item.Name()
|
|
|
|
} else {
|
|
|
|
pathChan <- filepath.Join(startDir, item.Name())
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
<-goroutineLimiter
|
2020-11-02 20:48:56 +01:00
|
|
|
walkerWG.Add(1)
|
2020-10-30 20:01:56 +01:00
|
|
|
|
|
|
|
go func(dir string) {
|
|
|
|
defer func() {
|
2020-11-02 20:48:56 +01:00
|
|
|
walkerWG.Done()
|
2020-10-30 20:01:56 +01:00
|
|
|
goroutineLimiter <- struct{}{}
|
|
|
|
}()
|
|
|
|
err = godirwalk.Walk(filepath.Join(startDir, dir), &godirwalk.Options{
|
|
|
|
Unsorted: true, // faster this way
|
|
|
|
Callback: func(osPathname string, de *godirwalk.Dirent) error {
|
|
|
|
if de.IsRegular() {
|
|
|
|
if basename {
|
|
|
|
pathChan <- de.Name()
|
|
|
|
} else {
|
2020-11-02 20:48:56 +01:00
|
|
|
pathChan <- osPathname
|
2020-10-30 20:01:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf(errors.FullTrace(err))
|
|
|
|
}
|
|
|
|
}(item.Name())
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:48:56 +01:00
|
|
|
walkerWG.Wait()
|
2020-10-30 20:01:56 +01:00
|
|
|
|
|
|
|
close(pathChan)
|
2020-11-02 20:48:56 +01:00
|
|
|
pathWG.Wait()
|
2020-10-30 20:01:56 +01:00
|
|
|
return paths, nil
|
|
|
|
}
|