-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (123 loc) · 2.7 KB
/
main.go
File metadata and controls
142 lines (123 loc) · 2.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// imosum is a sample application using imohash. It will calculate and report
// file hashes in a format similar to md5sum, etc.
package main
import (
"bufio"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/kalafut/imohash"
)
func processFile(path string) {
hash, err := imohash.SumFile(path)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%016x %s\n", hash, path)
}
func processWalkFunc(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !dirEntry.IsDir() {
processFile(path)
}
return nil
}
func checkFile(path string) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
malformedLines := 0
failedHashes := 0
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
malformedLines += 1
continue
}
readableResult := "FAILED"
hash, err := imohash.SumFile(parts[1])
if err != nil {
failedHashes += 1
} else {
hashStr := fmt.Sprintf("%016x", hash)
if hashStr == parts[0] {
readableResult = "OK"
} else {
failedHashes += 1
}
}
fmt.Printf("%s: %s\n", parts[1], readableResult)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
if failedHashes > 0 {
checksumStr := "checksum"
if failedHashes > 1 {
checksumStr += "s"
}
fmt.Fprintf(os.Stderr, "imosum: WARNING: %d computed %s did NOT match\n", failedHashes, checksumStr)
}
if malformedLines > 0 {
lineStr := "line is"
if malformedLines > 1 {
lineStr = "lines are"
}
fmt.Fprintf(os.Stderr, "imosum: WARNING: %d %s improperly formatted\n", malformedLines, lineStr)
}
}
func checkWalkFunc(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !dirEntry.IsDir() {
checkFile(path)
}
return nil
}
func walkDir(path string, fn fs.WalkDirFunc) {
if err := filepath.WalkDir(path, fn); err != nil {
log.Fatal(err)
}
}
func main() {
chkFlag := flag.Bool("c", false, "read hashes from the FILEs and check them")
flag.Parse()
paths := flag.Args()
if len(paths) == 0 {
fmt.Println("imosum - Prints the imohash from the file system")
fmt.Println("USAGE: imosum [-c] path1 path2...")
fmt.Println("If directories are provided, all files within them will be processed.")
fmt.Println("Pass -c to read hashes from the FILEs and check them.")
os.Exit(0)
}
for _, path := range paths {
fileInfo, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if fileInfo.IsDir() {
if *chkFlag {
walkDir(path, checkWalkFunc)
} else {
walkDir(path, processWalkFunc)
}
} else {
if *chkFlag {
checkFile(path)
} else {
processFile(path)
}
}
}
}