Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP:Report found files & folders while scanning #39

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions core/file_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,28 @@ func (f *File) UpdateSize() {

type ReadDir func(dirname string) ([]os.FileInfo, error)

func GetSubTree(path string, parent *File, readDir ReadDir, ignoredFolders map[string]struct{}) *File {
func GetSubTree(path string, parent *File, readDir ReadDir, ignoredFolders map[string]struct{}, progress chan<- int) *File {
var mutex sync.Mutex
var wg sync.WaitGroup
c := make(chan bool, maxConcurrentScans)
root := getSubTreeConcurrently(path, parent, readDir, ignoredFolders, c, &mutex, &wg)
root := getSubTreeConcurrently(path, parent, readDir, ignoredFolders, progress, c, &mutex, &wg)
wg.Wait()
close(progress)
root.UpdateSize()
return root
}

func getSubTreeConcurrently(path string, parent *File, readDir ReadDir, ignoredFolders map[string]struct{}, c chan bool, mutex *sync.Mutex, wg *sync.WaitGroup) *File {
func getSubTreeConcurrently(path string, parent *File, readDir ReadDir, ignoredFolders map[string]struct{}, progress chan<- int, c chan bool, mutex *sync.Mutex, wg *sync.WaitGroup) *File {
result := &File{}
entries, err := readDir(path)
if err != nil {
log.Println(err)
return result
}
dirName, name := filepath.Split(path)
result.Files = make([]*File, 0, len(entries))
lenEntries := len(entries)
result.Files = make([]*File, 0, lenEntries)
progress <- lenEntries
for _, entry := range entries {
if entry.IsDir() {
if _, ignored := ignoredFolders[entry.Name()]; ignored {
Expand All @@ -68,7 +71,7 @@ func getSubTreeConcurrently(path string, parent *File, readDir ReadDir, ignoredF
wg.Add(1)
go func() {
c <- true
subFolder := getSubTreeConcurrently(subFolderPath, result, readDir, ignoredFolders, c, mutex, wg)
subFolder := getSubTreeConcurrently(subFolderPath, result, readDir, ignoredFolders, progress, c, mutex, wg)
mutex.Lock()
result.Files = append(result.Files, subFolder)
mutex.Unlock()
Expand Down
13 changes: 11 additions & 2 deletions core/file_walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func TestGetSubTreeOnSimpleDir(t *testing.T) {
}},
}}
ignoredFolders := map[string]struct{}{"g": struct{}{}}
result := GetSubTree("b", nil, createReadDir(testStructure), ignoredFolders)
progress := make(chan int, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 0 here is default (channel not buffered), we might omit that

go dummyProgressConsumer(progress)
result := GetSubTree("b", nil, createReadDir(testStructure), ignoredFolders, progress)
buildExpected := func() *File {
b := &File{"b", nil, 180, true, []*File{}}
c := &File{"c", b, 100, false, []*File{}}
Expand All @@ -105,8 +107,15 @@ func TestGetSubTreeHandlesError(t *testing.T) {
failing := func(path string) ([]os.FileInfo, error) {
return []os.FileInfo{}, errors.New("Not found")
}
result := GetSubTree("xyz", nil, failing, map[string]struct{}{})
progress := make(chan int, 0)
go dummyProgressConsumer(progress)
result := GetSubTree("xyz", nil, failing, map[string]struct{}{}, progress)
if !reflect.DeepEqual(*result, File{}) {
t.Error("GetSubTree didn't return emtpy file on ReadDir failure")
}
}

func dummyProgressConsumer(progress <-chan int) {
for range progress {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are actually not testing that the functionality you implemented works

}
}
22 changes: 21 additions & 1 deletion godu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"sync"
"time"

"github.com/gdamore/tcell"
"github.com/viktomas/godu/core"
Expand All @@ -22,7 +23,9 @@ func main() {
root = args[0]
}
log.Printf("godu will walk through `%s` that might take up to few minutes\n", root)
tree := core.GetSubTree(root, nil, ioutil.ReadDir, getIgnoredFolders())
progress := make(chan int, 0)
go reportProgress(progress)
tree := core.GetSubTree(root, nil, ioutil.ReadDir, getIgnoredFolders(), progress)
err := core.PrepareTree(tree, *limit*core.MEGABYTE)
if err != nil {
log.Println(err.Error())
Expand All @@ -43,6 +46,23 @@ func main() {
printMarkedFiles(lastState)
}

func reportProgress(progress <-chan int) {
objs := 0
ticker := time.NewTicker(time.Second * 2)
go func() {
for i := range progress {
objs += i
}
ticker.Stop()
log.Println("Scanning done")
}()
go func() {
for range ticker.C {
log.Printf("Scanning.. Already found %d objects\n", objs)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objects? Would "files" describe better what we found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider 'folders' to also be 'files'?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. We are now using folder/tree/file, often interchangeably. I think we should come up with naming convention going forward. I was thinking about calling everything file, but that might be more confusing. Maybe we can say that folder is a special case of file and stop using tree completely? What's your opinion?

}
}()
}

func printMarkedFiles(lastState *core.State) {
markedFiles := interactive.QuoteMarkedFiles(lastState.MarkedFiles)
for _, quotedFile := range markedFiles {
Expand Down