-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathverify.go
executable file
·51 lines (45 loc) · 1.05 KB
/
verify.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
package extdrm
import (
"crypto/sha1"
"encoding/hex"
"io"
"os"
"path"
"time"
)
type VerifyResult struct {
Time time.Time `json:"time"`
Broken []string `json:"broken"`
Missing []string `json:"missing"`
TotalBroken int `json:"total_broken"`
TotalMissing int `json:"total_missing"`
TotalFiles int `json:"total_files"`
}
func VerifyFS(root string, metadata *Metadata) (*VerifyResult, error) {
result := &VerifyResult{
Time: time.Now(),
Broken: []string{},
Missing: []string{},
}
hash := sha1.New()
result.TotalFiles = len(metadata.Files)
for _, entry := range metadata.Files {
rd, err := os.Open(path.Join(root, entry.SPath))
if err != nil {
result.Missing = append(result.Missing, entry.SPath)
result.TotalMissing++
continue
}
if _, err := io.Copy(hash, rd); err != nil {
rd.Close()
return nil, err
}
rd.Close()
if entry.SSha1 != hex.EncodeToString(hash.Sum(nil)) {
result.Broken = append(result.Broken, entry.SPath)
result.TotalBroken++
}
hash.Reset()
}
return result, nil
}