-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfv.go
177 lines (160 loc) · 3.86 KB
/
sfv.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Package sfv provides a simple way of reading and verifying SFV (Simple File
// Verification) files.
package verifysfv
import (
"bufio"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
)
// Checksum represents a line in a SFV file, containing the filename, full path
// to the file and the CRC32 checksum
type Checksum struct {
Filename string
Path string
CRC32 uint32
}
// SFV contains all the checksums read from a SFV file.
type SFV struct {
Checksums []Checksum
Path string
}
var bufSize uint64 = 4096
func SetBufSize(bs int) {
atomic.SwapUint64(&bufSize, uint64(bs))
}
func GetBufSize() uint64 {
return atomic.LoadUint64(&bufSize)
}
// Verify calculates the CRC32 of the associated file and returns true if the
// checksum is correct along with the calculated checksum
func (c *Checksum) Verify(polynomial uint32) (bool, uint32, error) {
f, err := os.Open(c.Path)
if err != nil {
return false, 0, err
}
defer f.Close()
h := crc32.New(crc32.MakeTable(polynomial))
reader := bufio.NewReader(f)
buf := make([]byte, bufSize)
for {
n, err := reader.Read(buf)
if err != nil && err != io.EOF {
return false, 0, err
}
if n == 0 {
break
}
h.Write(buf[:n])
}
result := h.Sum32()
return result == c.CRC32, result, nil
}
// IsExist returns a boolean indicating if the file associated with the checksum
// exists
func (c *Checksum) IsExist() bool {
_, err := os.Stat(c.Path)
return err == nil
}
// Verify verifies all checksums contained in SFV and returns true if all
// checksums are correct.
func (s *SFV) Verify(polynomial uint32) (bool, error) {
if len(s.Checksums) == 0 {
return false, fmt.Errorf("no checksums found in %s", s.Path)
}
for _, c := range s.Checksums {
ok, _, err := c.Verify(polynomial)
if err != nil {
return false, err
}
if !ok {
return false, nil
}
}
return true, nil
}
// IsExist returns a boolean if all the files in SFV exists
func (s *SFV) IsExist() bool {
for _, c := range s.Checksums {
if !c.IsExist() {
return false
}
}
return true
}
func parseChecksum(dir string, line string) (*Checksum, error) {
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("could not parse checksum: %q", line)
}
filename := strings.TrimSpace(parts[0])
path := path.Join(dir, filename)
crc32, err := strconv.ParseUint(strings.TrimSpace(parts[1]), 16, 32)
if err != nil {
return nil, err
}
// ParseUint will return error if number exceeds 32 bits
return &Checksum{
Path: path,
Filename: filename,
CRC32: uint32(crc32),
}, nil
}
func parseChecksums(dir string, r io.Reader) ([]Checksum, error) {
checksums := []Checksum{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || strings.HasPrefix(line, ";") {
continue
}
checksum, err := parseChecksum(dir, line)
if err != nil {
return nil, err
}
checksums = append(checksums, *checksum)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return checksums, nil
}
// Read reads a SFV file from filepath and creates a new SFV containing
// checksums parsed from the SFV file.
func Read(filepath string) (*SFV, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
dir := path.Dir(filepath)
checksums, err := parseChecksums(dir, f)
if err != nil {
return nil, err
}
return &SFV{
Checksums: checksums,
Path: filepath,
}, nil
}
// Find tries to find a SFV file in the given path. If multiple SFV files exist
// in path, the first one will be returned.
func Find(path string) (*SFV, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".sfv" {
return Read(filepath.Join(path, f.Name()))
}
}
return nil, fmt.Errorf("no sfv found in %s", path)
}