/
util.go
132 lines (117 loc) · 2.31 KB
/
util.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
package datastores
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"fmt"
"hash"
"io"
"os"
)
func getChunkSize(size int64) int64 {
if size > 0 && size < 0x8000000 {
return 0x40000
}
if size >= 0x8000000 && size < 0x10000000 {
return 0x80000
}
if size <= 0x10000000 || size > 0x20000000 {
return 0x200000
}
return 0x100000
}
func CalcFileGcid(filename string) (string, error) {
fi, err := os.Stat(filename)
if err != nil {
return "", err
}
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
return calcGcid(fi.Size(), f), nil
}
func calcGcid(size int64, file io.Reader) string {
totalHash := sha1.New()
chunkSize := getChunkSize(size)
buff := make([]byte, 8192)
i := int64(0)
//partHash := sha1.New()
var partHash hash.Hash
loop0:
for {
partHash = sha1.New()
i = 0
for {
n, err := file.Read(buff)
if err != nil {
if err == io.EOF {
break loop0
}
return ""
}
partHash.Write(buff[:n])
i += int64(n)
if i >= chunkSize {
break
}
}
dw := partHash.Sum(nil)
totalHash.Write(dw)
}
if i > 0 {
dw := partHash.Sum(nil)
totalHash.Write(dw)
}
return fmt.Sprintf("%x", totalHash.Sum(nil))
}
const (
BLOCK_BITS = 22 // Indicate that the blocksize is 4M
BLOCK_SIZE = 1 << BLOCK_BITS
)
func BlockCount(fsize int64) int {
return int((fsize + (BLOCK_SIZE - 1)) >> BLOCK_BITS)
}
func CalSha1(b []byte, r io.Reader) ([]byte, error) {
h := sha1.New()
_, err := io.Copy(h, r)
if err != nil {
return nil, err
}
return h.Sum(b), nil
}
func GetEtag(filename string) (etag string, err error) {
f, err := os.Open(filename)
if err != nil {
return
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return
}
fsize := fi.Size()
blockCnt := BlockCount(fsize)
sha1Buf := make([]byte, 0, 21)
if blockCnt <= 1 { // file size <= 4M
sha1Buf = append(sha1Buf, 0x16)
sha1Buf, err = CalSha1(sha1Buf, f)
if err != nil {
return
}
} else { // file size > 4M
sha1Buf = append(sha1Buf, 0x96)
sha1BlockBuf := make([]byte, 0, blockCnt*20)
for i := 0; i < blockCnt; i++ {
body := io.LimitReader(f, BLOCK_SIZE)
sha1BlockBuf, err = CalSha1(sha1BlockBuf, body)
if err != nil {
return
}
}
sha1Buf, _ = CalSha1(sha1Buf, bytes.NewReader(sha1BlockBuf))
}
etag = base64.URLEncoding.EncodeToString(sha1Buf)
return
}