-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashcode.go
206 lines (173 loc) · 4.04 KB
/
hashcode.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"crypto/md5"
"fmt"
"os"
"path/filepath"
"sort"
"time"
)
var hash = md5.New()
type dirCache struct {
cache map[string]struct{}
}
func NewDirCache() *dirCache {
return &dirCache{
cache: make(map[string]struct{}),
}
}
func (dc *dirCache) parse(path string) []string {
dirs := []string{}
for d := path; d != "/" && d != "."; d = filepath.Dir(d) {
if _, ok := dc.cache[d]; ok {
break
}
dc.cache[d] = struct{}{}
dirs = append(dirs, d)
}
return dirs
}
type dirFileInfo struct {
name string
}
func (dfi dirFileInfo) Name() string {
return dfi.name
}
func (dfi dirFileInfo) Size() int64 {
return 0
}
func (dfi dirFileInfo) Mode() os.FileMode {
return 0755
}
func (dfi dirFileInfo) ModTime() time.Time {
return time.Now()
}
func (dfi dirFileInfo) IsDir() bool {
return true
}
func (dfi dirFileInfo) Sys() interface{} {
return nil
}
func NewDirFileInfo(name string) *dirFileInfo {
return &dirFileInfo{
name: name,
}
}
// fillFileInfo fills in the FileInfo field of each content file entry and returns the new file slice.
// For directories, it recursively walk the tree to collect each directory or file.
func fillFileInfo(contentFiles []*FileEntry) ([]*FileEntry, error) {
newDirs := []*FileEntry{}
newFiles := []*FileEntry{}
newSymlinks := []*FileEntry{}
pdirs := NewDirCache()
for _, f := range contentFiles {
fi, err := os.Lstat(f.Path)
if err != nil {
return nil, err
}
if fi.IsDir() {
err := filepath.Walk(f.Path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
relpath, err := filepath.Rel(f.Path, path)
if err != nil {
if *verbose {
fmt.Printf("Failed to get relative path for %s against %s.", path, f.Path)
}
return err
}
de := &FileEntry{
Path: path,
DebPath: filepath.Join(f.DebPath, relpath),
FileInfo: info,
}
if _, ok := pdirs.cache[de.DebPath]; !ok {
newDirs = append(newDirs, de)
}
return nil
}
relpath, err := filepath.Rel(f.Path, path)
if err != nil {
if *verbose {
fmt.Printf("Failed to get relative path for %s against %s.", path, f.Path)
}
return err
}
fe := &FileEntry{
Path: path,
DebPath: filepath.Join(f.DebPath, relpath),
FileInfo: info,
}
if err = fillMd5Sum(fe); err != nil {
return err
}
if info.Mode()&os.ModeSymlink != 0 {
newSymlinks = append(newSymlinks, fe)
} else {
newFiles = append(newFiles, fe)
}
return nil
})
if err != nil {
return nil, err
}
} else if fi.Mode()&os.ModeSymlink != 0 {
f.FileInfo = fi
newSymlinks = append(newSymlinks, f)
} else {
f.FileInfo = fi
if err = fillMd5Sum(f); err != nil {
return nil, err
}
newFiles = append(newFiles, f)
// Make sure the file's directory exists.
for _, d := range pdirs.parse(filepath.Dir(f.DebPath)) {
de := &FileEntry{
Path: "",
DebPath: d,
FileInfo: NewDirFileInfo(d),
}
newDirs = append(newDirs, de)
}
}
}
sort.Slice(newDirs, func(i, j int) bool {
return newDirs[i].DebPath < newDirs[j].DebPath
})
return append(append(newDirs, newFiles...), newSymlinks...), nil
}
// fillMd5Sum fills the md5sum of the given content file entry.
func fillMd5Sum(cf *FileEntry) error {
var md5sum string
var err error
if cf.FileInfo.Mode()&os.ModeSymlink != os.ModeSymlink {
if md5sum, err = calculateMd5sum(cf.Path, cf.FileInfo.Size()); err != nil {
return err
}
cf.Md5sum = md5sum
}
return nil
}
// calcMd5Sum calculates the md5sum of the given file.
func calculateMd5sum(path string, size int64) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
hash.Reset()
buffer := make([]byte, 16384)
var num int
var n int64
for n = 0; n < size; {
num, err = f.Read(buffer)
if err != nil {
if *verbose {
fmt.Printf("Failed to read from file %s.", path, path)
}
return "", err
}
n += int64(num)
hash.Write(buffer[:num])
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}