This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
/
filehash.go
182 lines (162 loc) · 4.49 KB
/
filehash.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
// Copyright (C) 2018 Tim Waugh
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package retrodep
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// FileHash records the hash of a file in the format preferred by the
// version control system that tracks it.
type FileHash string
// Hasher is the interface that wraps the Hash method.
type Hasher interface {
// Hash returns the file hash for the filename absPath, hashed
// as though it were in the repository as filename
// relativePath.
Hash(relativePath, absPath string) (FileHash, error)
}
type sha256Hasher struct{}
// Hash implements the Hasher interface generically using sha256.
func (h sha256Hasher) Hash(relativePath, absPath string) (FileHash, error) {
f, err := os.Open(absPath)
if err != nil {
return FileHash(""), errors.Wrapf(err, "hashing %s", absPath)
}
defer f.Close()
hash := sha256.New()
_, err = io.Copy(hash, f)
if err != nil {
return FileHash(""), errors.Wrapf(err, "hashing %s", absPath)
}
return FileHash(hex.EncodeToString(hash.Sum(nil))), nil
}
func NewHasher(vcsCmd string) (Hasher, bool) {
switch vcsCmd {
case vcsGit:
return &gitHasher{}, true
case vcsHg:
return &sha256Hasher{}, true
}
return nil, false
}
// FileHashes is a map of paths, relative to the top-level of the
// version control system, to their hashes.
type FileHashes struct {
// h is the Hasher used to create each FileHash
h Hasher
// hashes maps a relative filename to its FileHash
hashes map[string]FileHash
}
// NewFileHashes returns a new FileHashes from a filesystem tree at root,
// whose files belong to the version control system named in vcsCmd. Keys in
// the excludes map are filenames to ignore.
func NewFileHashes(h Hasher, root string, excludes map[string]struct{}) (*FileHashes, error) {
hashes := &FileHashes{
h: h,
hashes: make(map[string]FileHash),
}
root = path.Clean(root)
// Make a local copy of excludes we can safely modify
excl := make(map[string]struct{})
if excludes != nil {
for k, v := range excludes {
excl[k] = v
}
}
walkfn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if _, skip := excl[path]; skip {
// This pathname has been ignored, either by caller
// request or due to .gitattributes
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if info.IsDir() {
// Check for .gitattributes in this directory
// FIXME: gitattributes(5) describes a more complex file
// format than handled here. Can git-check-attr(1) help?
ga, err := os.Open(filepath.Join(path, ".gitattributes"))
if err != nil {
if os.IsNotExist(err) {
err = nil
}
return err
}
defer ga.Close()
scanner := bufio.NewScanner(bufio.NewReader(ga))
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 2 {
continue
}
for _, field := range fields[1:] {
if field == "export-subst" {
// Not expected to have matching hash
fn := filepath.Join(path, fields[0])
excl[fn] = struct{}{}
break
}
}
}
return nil
}
if !info.Mode().IsRegular() {
return nil
}
relativePath, err := filepath.Rel(root, path)
if err != nil {
return err
}
fileHash, err := h.Hash(relativePath, path)
if err != nil {
return err
}
hashes.hashes[relativePath] = fileHash
return nil
}
err := filepath.Walk(root, walkfn)
if err != nil {
return nil, err
}
return hashes, nil
}
// IsSubsetOf returns true if these file hashes are a subset of s.
func (h *FileHashes) IsSubsetOf(s *FileHashes) bool {
for path, fileHash := range h.hashes {
sh, ok := s.hashes[path]
if !ok {
// File not present in tag
log.Debugf("%s: not present", path)
return false
}
if fileHash != sh {
// Hash does not match
log.Debugf("%s: hash mismatch", path)
return false
}
}
return true
}