-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (58 loc) · 1.16 KB
/
main.go
File metadata and controls
75 lines (58 loc) · 1.16 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Calculate file entropy.
//
// Usage:
//
// entropy path
//
// The arguments are:
//
// path
// File or folder to calculate (required).
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"go.foxforensics.dev/go-mmap"
"go.foxforensics.dev/entropy/entropy"
)
func main() {
if len(os.Args) == 1 || os.Args[1] == "--help" {
_, _ = fmt.Fprintln(os.Stderr, "usage: entropy path")
os.Exit(2)
}
err := filepath.WalkDir(os.Args[1], func(path string, d fs.DirEntry, err error) error {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return nil
}
if d.IsDir() {
return nil
}
path, err = filepath.Abs(path)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return nil
}
f, err := os.Open(path)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return nil
}
defer func() { _ = f.Close() }()
m, err := mmap.Map(f, mmap.RDONLY, 0)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return nil
}
defer func() { _ = m.Unmap() }()
e := entropy.Calculate(m)
_, _ = fmt.Printf("%0.10f %s\n", e, path)
return nil
})
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}