-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.go
94 lines (78 loc) · 1.71 KB
/
operations.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
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
package main
import (
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/teran/checksum/utils/concurrent"
)
func completeArgs(word string) {
fmt.Println(strings.Join([]string{
"--concurrency",
"--database",
"--datadir",
"--pattern",
"--progressbar",
"--skipfailed",
"--skipmissed",
"--skipok",
"--version",
}, " "))
}
func flength(filename string) int64 {
stat, err := os.Stat(filename)
if err != nil {
log.Fatal(err)
}
return stat.Size()
}
func generateActualChecksum(filename string) (sha1sum string, sha256sum string, err error) {
fi, err := os.Stat(filename)
if err != nil {
return "", "", err
}
fp, err := os.Open(filename)
if err != nil {
return "", "", err
}
defer fp.Close()
sha1hasher := sha1.New()
sha256hasher := sha256.New()
w, err := concurrent.NewConcurrentMultiWriter(context.TODO(), sha1hasher, sha256hasher)
if err != nil {
return "", "", err
}
n, err := io.Copy(w, fp)
if err != nil {
return "", "", err
}
if n != fi.Size() {
return "", "", io.ErrShortWrite
}
return hex.EncodeToString(sha1hasher.Sum(nil)), hex.EncodeToString(sha256hasher.Sum(nil)), nil
}
func verify(path string, length int64, sha1, sha256 string) bool {
actSHA1, actSHA256, err := generateActualChecksum(path)
if err != nil {
return false
}
return flength(path) == length && actSHA1 == sha1 && actSHA256 == sha256
}
func printVersion() {
fmt.Printf("checksum version: %s\n", appVersion)
fmt.Printf("Built wih Go version: %s\n", runtime.Version())
}
func isApplicable(path string) bool {
_, ok := db.ReadOne(path)
if filePattern.MatchString(filepath.Ext(path)) && !ok {
return true
}
return false
}