/
hashfactories.go
141 lines (114 loc) · 2.89 KB
/
hashfactories.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
package factories
import (
"log"
"os"
"path/filepath"
)
const defaultReadDepth int = 10
const defaultHashResultsDepth int = 6
//ReadResponse response from a read operation
type ReadResponse struct {
data []byte
err error
}
//DataReader reads sequentially and asynchronously from a source.
type DataReader interface {
Source() string
Read() <-chan ReadResponse
Size() int64
}
//MD5HashResult TODO
type MD5HashResult struct {
Source string
MD5 []byte
Err error
Size int64
}
//NewBlobHashFactory todo
func NewBlobHashFactory(pattern string, container string, accountName string, accountKey string, setBlobMD5 bool) <-chan MD5HashResult {
az, err := newAzUtil(accountName, accountKey, container, "")
if err != nil {
log.Fatal(err)
}
factory := func() <-chan AsyncMD5 {
blobMD5 := make(chan AsyncMD5, 1000)
go func() {
defer close(blobMD5)
for blobItem := range az.iterateBlobList(pattern, 1000) {
if blobItem.err != nil {
log.Fatal(blobItem.err)
}
blobReader := newBlobReader(blobItem.blob.Name, *blobItem.blob.Properties.ContentLength, *az)
blobMD5 <- *newAsyncMD5(blobReader)
}
}()
return blobMD5
}
if setBlobMD5 {
return md5Hash(factory(), func(md5 []byte, source string) error {
return az.setMD5(source, md5)
})
}
return md5Hash(factory(), nil)
}
//NewFileHashFactory TODO
func NewFileHashFactory(pattern string) <-chan MD5HashResult {
files, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}
factory := func() <-chan AsyncMD5 {
fileMD5 := make(chan AsyncMD5, 1000)
go func() {
defer close(fileMD5)
for _, file := range files {
fileStat, err := os.Stat(file)
if err != nil {
log.Fatal(err)
}
if fileStat.IsDir() {
err := filepath.Walk(file, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() {
fullFileName := filepath.Join(path, f.Name())
fileReader := newFileReader(fullFileName, f.Size())
fileMD5 <- *newAsyncMD5(fileReader)
}
return nil
})
if err != nil {
log.Fatal(err)
}
continue
}
fileReader := newFileReader(file, fileStat.Size())
fileMD5 <- *newAsyncMD5(fileReader)
}
}()
return fileMD5
}
return md5Hash(factory(), nil)
}
func md5Hash(hashfactory <-chan AsyncMD5, resultFunc func(md5 []byte, sourcename string) error) <-chan MD5HashResult {
results := make(chan MD5HashResult, defaultHashResultsDepth)
go func() {
defer close(results)
for hashItem := range hashfactory {
md5, err := hashItem.Hash()
if err != nil {
results <- MD5HashResult{Err: err}
return
}
if resultFunc != nil {
if err = resultFunc(md5, hashItem.Source()); err != nil {
results <- MD5HashResult{Err: err}
return
}
}
results <- MD5HashResult{MD5: md5, Source: hashItem.Source(), Size: hashItem.Size()}
}
}()
return results
}