Include full path to file in callback

This commit is contained in:
Dane Everitt 2020-04-18 18:59:10 -07:00
parent 3bca54655b
commit 8eaf590f78
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 5 additions and 5 deletions

View File

@ -155,7 +155,7 @@ func (fs *Filesystem) DirectorySize(dir string) (int64, error) {
ctx := context.Background() ctx := context.Background()
var size int64 var size int64
err := w.Walk(dir, ctx, func(f os.FileInfo) bool { err := w.Walk(dir, ctx, func(f os.FileInfo, _ string) bool {
// Only increment the size when we're dealing with a file specifically, otherwise // Only increment the size when we're dealing with a file specifically, otherwise
// just continue digging deeper until there are no more directories to iterate over. // just continue digging deeper until there are no more directories to iterate over.
if !f.IsDir() { if !f.IsDir() {

View File

@ -20,7 +20,7 @@ func (fs *Filesystem) NewWalker() *FileWalker {
// Iterate over all of the files and directories within a given directory. When a file is // Iterate over all of the files and directories within a given directory. When a file is
// found the callback will be called with the file information. If a directory is encountered // found the callback will be called with the file information. If a directory is encountered
// it will be recursively passed back through to this function. // it will be recursively passed back through to this function.
func (fw *FileWalker) Walk(dir string, ctx context.Context, callback func (os.FileInfo) bool) error { func (fw *FileWalker) Walk(dir string, ctx context.Context, callback func (os.FileInfo, string) bool) error {
cleaned, err := fw.SafePath(dir) cleaned, err := fw.SafePath(dir)
if err != nil { if err != nil {
return err return err
@ -38,7 +38,7 @@ func (fw *FileWalker) Walk(dir string, ctx context.Context, callback func (os.Fi
for _, f := range files { for _, f := range files {
if f.IsDir() { if f.IsDir() {
p := filepath.Join(dir, f.Name()) p := filepath.Join(cleaned, f.Name())
// Recursively call this function to continue digging through the directory tree within // Recursively call this function to continue digging through the directory tree within
// a seperate goroutine. If the context is canceled abort this process. // a seperate goroutine. If the context is canceled abort this process.
g.Go(func() error { g.Go(func() error {
@ -49,7 +49,7 @@ func (fw *FileWalker) Walk(dir string, ctx context.Context, callback func (os.Fi
// If the callback returns true, go ahead and keep walking deeper. This allows // If the callback returns true, go ahead and keep walking deeper. This allows
// us to programatically continue deeper into directories, or stop digging // us to programatically continue deeper into directories, or stop digging
// if that pathway knows it needs nothing else. // if that pathway knows it needs nothing else.
if callback(f) { if callback(f, p) {
return fw.Walk(p, ctx, callback) return fw.Walk(p, ctx, callback)
} }
@ -60,7 +60,7 @@ func (fw *FileWalker) Walk(dir string, ctx context.Context, callback func (os.Fi
// If this isn't a directory, go ahead and pass the file information into the // If this isn't a directory, go ahead and pass the file information into the
// callback. We don't care about the response since we won't be stepping into // callback. We don't care about the response since we won't be stepping into
// anything from here. // anything from here.
callback(f) callback(f, filepath.Join(cleaned, f.Name()))
} }
} }