2020-04-18 22:31:34 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-07-17 04:51:31 +00:00
|
|
|
"github.com/gammazero/workerpool"
|
2020-04-18 22:31:34 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-07-17 04:51:31 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
2020-04-18 22:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileWalker struct {
|
|
|
|
*Filesystem
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:51:31 +00:00
|
|
|
type PooledFileWalker struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
pool *workerpool.WorkerPool
|
|
|
|
callback filepath.WalkFunc
|
|
|
|
|
|
|
|
Filesystem *Filesystem
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:31:34 +00:00
|
|
|
// Returns a new walker instance.
|
|
|
|
func (fs *Filesystem) NewWalker() *FileWalker {
|
|
|
|
return &FileWalker{fs}
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:51:31 +00:00
|
|
|
// Creates a new pooled file walker that will concurrently walk over a given directory but limit itself
|
|
|
|
// to a worker pool as to not completely flood out the system or cause a process crash.
|
|
|
|
func newPooledWalker(fs *Filesystem) *PooledFileWalker {
|
|
|
|
return &PooledFileWalker{
|
|
|
|
Filesystem: fs,
|
|
|
|
// Create a worker pool that is the same size as the number of processors available on the
|
|
|
|
// system. Going much higher doesn't provide much of a performance boost, and is only more
|
|
|
|
// likely to lead to resource overloading anyways.
|
|
|
|
pool: workerpool.New(runtime.GOMAXPROCS(0)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process a given path by calling the callback function for all of the files and directories within
|
|
|
|
// the path, and then dropping into any directories that we come across.
|
|
|
|
func (w *PooledFileWalker) process(path string) error {
|
|
|
|
p, err := w.Filesystem.SafePath(path)
|
2020-04-18 22:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:51:31 +00:00
|
|
|
files, err := ioutil.ReadDir(p)
|
2020-04-18 22:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:51:31 +00:00
|
|
|
// Loop over all of the files and directories in the given directory and call the provided
|
|
|
|
// callback function. If we encounter a directory, push that directory onto the worker queue
|
|
|
|
// to be processed.
|
2020-04-18 22:31:34 +00:00
|
|
|
for _, f := range files {
|
2020-07-17 04:51:31 +00:00
|
|
|
sp := filepath.Join(p, f.Name())
|
|
|
|
i, err := os.Stat(sp)
|
|
|
|
|
|
|
|
if err = w.callback(sp, i, err); err != nil {
|
|
|
|
if err == filepath.SkipDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.IsDir() {
|
|
|
|
w.push(sp)
|
2020-04-18 22:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:51:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push a new path into the worker pool.
|
|
|
|
//
|
|
|
|
// @todo probably helps to handle errors.
|
|
|
|
func (w *PooledFileWalker) push(path string) {
|
|
|
|
w.wg.Add(1)
|
|
|
|
w.pool.Submit(func() {
|
2020-07-17 04:53:05 +00:00
|
|
|
defer w.wg.Done()
|
2020-07-17 04:51:31 +00:00
|
|
|
w.process(path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walks the given directory and executes the callback function for all of the files and directories
|
|
|
|
// that are encountered.
|
|
|
|
func (fs *Filesystem) Walk(dir string, callback filepath.WalkFunc) error {
|
|
|
|
w := newPooledWalker(fs)
|
|
|
|
w.callback = callback
|
|
|
|
|
|
|
|
w.push(dir)
|
|
|
|
|
|
|
|
w.wg.Wait()
|
|
|
|
w.pool.StopWait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|