-
Notifications
You must be signed in to change notification settings - Fork 162
/
fingerprint.go
121 lines (96 loc) · 3.19 KB
/
fingerprint.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
package resource
import (
"os"
gopath "path"
"sort"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
)
type FingerprinterImpl struct {
sha1calc bicrypto.SHA1Calculator
fs boshsys.FileSystem
}
func NewFingerprinterImpl(sha1calc bicrypto.SHA1Calculator, fs boshsys.FileSystem) FingerprinterImpl {
return FingerprinterImpl{sha1calc: sha1calc, fs: fs}
}
func (f FingerprinterImpl) Calculate(files []File, additionalChunks []string) (string, error) {
chunks := []string{"v2"}
// Ensure consistent ordering of files
sortedFiles := make([]File, len(files))
copy(sortedFiles, files)
sort.Sort(FileRelativePathSorting(sortedFiles))
for _, file := range sortedFiles {
chunk, err := f.fingerprintPath(file)
if err != nil {
return "", bosherr.WrapErrorf(err, "Fingerprinting file '%s'", file.Path)
}
chunks = append(chunks, chunk)
}
if len(additionalChunks) > 0 {
// Ensure consistent ordering of additional chunks
sortedAdditionalChunks := make([]string, len(additionalChunks))
copy(sortedAdditionalChunks, additionalChunks)
sort.Sort(AdditionalChunkSorting(sortedAdditionalChunks))
chunks = append(chunks, strings.Join(sortedAdditionalChunks, ","))
}
return f.sha1calc.CalculateString(strings.Join(chunks, "")), nil
}
// fingerprintPath currently works with:
// - pkg: [rel_path, digest, is_hook ? '' : file_mode]
// - changes: sorting
// - job: [File.basename(abs_path), digest, file_mode]
// - changes: rel_path, sorting
// - lic: [File.basename(abs_path), digest]
// - changes: sorting
func (f FingerprinterImpl) fingerprintPath(file File) (string, error) {
var result string
if file.UseBasename {
result += gopath.Base(file.Path)
} else {
result += file.RelativePath
}
fileInfo, err := f.fs.Lstat(file.Path)
if err != nil {
return "", err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
symlinkTarget, err := f.fs.Readlink(file.Path)
if err != nil {
return "", err
}
sha1 := f.sha1calc.CalculateString(symlinkTarget)
result += sha1
} else if !fileInfo.IsDir() {
sha1, err := f.sha1calc.Calculate(file.Path)
if err != nil {
return "", err
}
result += sha1
}
if !file.ExcludeMode {
// Git doesn't really track file permissions, it just looks at executable
// bit and uses 0755 if it's set or 0644 if not. We have to mimic that
// behavior in the fingerprint calculation to avoid the situation where
// seemingly clean working copy would trigger new fingerprints for
// artifacts with changed permissions. Also we don't want current
// fingerprints to change, hence the exact values below.
var modeStr string
if fileInfo.IsDir() {
modeStr = "40755"
} else if fileInfo.Mode()&os.ModeSymlink != 0 {
modeStr = "symlink"
} else if fileInfo.Mode()&0111 != 0 {
modeStr = "100755"
} else {
modeStr = "100644"
}
result += modeStr
}
return result, nil
}
type AdditionalChunkSorting []string
func (s AdditionalChunkSorting) Len() int { return len(s) }
func (s AdditionalChunkSorting) Less(i, j int) bool { return s[i] < s[j] }
func (s AdditionalChunkSorting) Swap(i, j int) { s[i], s[j] = s[j], s[i] }