Skip to content

Commit 76e3d72

Browse files
committed
feat(dumper): walk dir in async mode
1 parent ab23dac commit 76e3d72

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

cmd/dump.go

+51
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"reflect"
1616
"strings"
17+
"sync"
1718
"text/tabwriter"
1819
"time"
1920
"unicode"
@@ -23,6 +24,50 @@ import (
2324
"github.com/saferwall/pe/log"
2425
)
2526

27+
var (
28+
wg sync.WaitGroup
29+
jobs chan string = make(chan string)
30+
)
31+
32+
func loopFilesWorker(cfg config) error {
33+
for path := range jobs {
34+
files, err := os.ReadDir(path)
35+
if err != nil {
36+
wg.Done()
37+
return err
38+
}
39+
40+
for _, file := range files {
41+
if !file.IsDir() {
42+
fullpath := filepath.Join(path, file.Name())
43+
parse(fullpath, cfg)
44+
}
45+
}
46+
wg.Done()
47+
}
48+
return nil
49+
}
50+
51+
func LoopDirsFiles(path string) error {
52+
files, err := os.ReadDir(path)
53+
if err != nil {
54+
return err
55+
}
56+
//Add this path as a job to the workers
57+
//You must call it in a go routine, since if every worker is busy, then you have to wait for the channel to be free.
58+
go func() {
59+
wg.Add(1)
60+
jobs <- path
61+
}()
62+
for _, file := range files {
63+
if file.IsDir() {
64+
//Recursively go further in the tree
65+
LoopDirsFiles(filepath.Join(path, file.Name()))
66+
}
67+
}
68+
return nil
69+
}
70+
2671
func prettyPrint(buff []byte) string {
2772
var prettyJSON bytes.Buffer
2873
error := json.Indent(&prettyJSON, buff, "", "\t")
@@ -500,6 +545,12 @@ func parsePE(filename string, cfg config) {
500545

501546
fmt.Printf("\nRESOURCES\n**********\n")
502547
printRsrcDir(pe.Resources)
548+
549+
r, err := pe.ParseVersionResources()
550+
if err == nil {
551+
fmt.Print(r)
552+
}
553+
fmt.Print()
503554
}
504555

505556
if cfg.wantException && pe.FileInfo.HasException {

cmd/main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"flag"
99
"fmt"
1010
"os"
11+
"runtime"
1112
)
1213

1314
type config struct {
@@ -88,7 +89,14 @@ func main() {
8889
wantCLR: *dumpCLR,
8990
}
9091

91-
parse(os.Args[2], cfg)
92+
//Start as many workers you want, now 10 workers
93+
numWorkers := runtime.GOMAXPROCS(runtime.NumCPU() - 1)
94+
for w := 1; w <= numWorkers; w++ {
95+
go loopFilesWorker(cfg)
96+
}
97+
//Start the recursion
98+
LoopDirsFiles(os.Args[2])
99+
wg.Wait()
92100

93101
case "version":
94102
verCmd.Parse(os.Args[2:])
@@ -109,5 +117,6 @@ func showHelp() {
109117
Brought to you by Saferwall (c) 2018 MIT
110118
`)
111119
fmt.Println("\nAvailable sub-commands 'dump' or 'version' subcommands")
120+
112121
os.Exit(1)
113122
}

0 commit comments

Comments
 (0)