-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.go
55 lines (43 loc) · 866 Bytes
/
count.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/rubiojr/hashup/internal/log"
)
type FileCount struct {
Chan chan int64
Errors []error
}
func FileCounter(ctx context.Context, root string) *FileCount {
count := &FileCount{
Chan: make(chan int64),
}
go func() {
defer close(count.Chan)
counter := int64(0)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("error walking path %s: %v", path, err)
return nil
}
if ctx.Err() != nil {
return ctx.Err()
}
counter++
if info.IsDir() {
return nil
}
if !info.Mode().IsRegular() {
return nil
}
return nil
})
if err != nil {
count.Errors = append(count.Errors, fmt.Errorf("failed walking directory: %w", err))
}
count.Chan <- counter
}()
return count
}