Repository navigation
Expand file tree
/
Copy pathembedded.go
More file actions
179 lines (174 loc) · 4.59 KB
/
Copy pathembedded.go
File metadata and controls
179 lines (174 loc) · 4.59 KB
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
package contracttrace
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"go/ast"
"go/types"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"unicode"
"golang.org/x/tools/go/packages"
)
func embedPatterns(text string) ([]string, error) {
result := []string{}
for text = strings.TrimSpace(text); text != ""; text = strings.TrimSpace(text) {
if text[0] == '"' || text[0] == '`' {
quote := text[0]
end := 1
for end < len(text) {
if quote == '"' && text[end] == '\\' {
end += 2
continue
}
if text[end] == quote {
break
}
end++
}
if end >= len(text) {
return nil, fmt.Errorf("unterminated embed pattern")
}
word, err := strconv.Unquote(text[:end+1])
if err != nil {
return nil, err
}
result = append(result, word)
text = text[end+1:]
} else {
end := strings.IndexFunc(text, unicode.IsSpace)
if end < 0 {
end = len(text)
}
result = append(result, text[:end])
text = text[end:]
}
}
return result, nil
}
func patternContains(pattern, file string) bool {
pattern = strings.TrimPrefix(pattern, "all:")
for candidate := file; candidate != "."; candidate = filepath.ToSlash(filepath.Dir(candidate)) {
if globMatch(pattern, candidate) {
return true
}
}
return false
}
func (ix *index) loadEmbedded(pkgs []*packages.Package) error {
ix.embedded = map[*types.Var]string{}
ix.assets = selectedEmbeddedAssetPaths(ix.root, pkgs)
bodies := map[string][]byte{}
h := sha256.New()
for _, file := range ix.assets {
body, err := os.ReadFile(filepath.Join(ix.root, filepath.FromSlash(file)))
if err != nil {
return err
}
bodies[file] = body
fmt.Fprintf(h, "%d:%s:%d:", len(file), file, len(body))
h.Write(body)
}
ix.assetHash = hex.EncodeToString(h.Sum(nil))
for _, p := range pkgs {
for _, file := range p.Syntax {
directory := filepath.Dir(ix.fset.Position(file.Pos()).Filename)
for _, decl := range file.Decls {
group, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range group.Specs {
value, ok := spec.(*ast.ValueSpec)
if !ok || len(value.Names) != 1 {
continue
}
patterns := []string{}
for _, comments := range []*ast.CommentGroup{group.Doc, value.Doc} {
if comments != nil {
for _, comment := range comments.List {
if strings.HasPrefix(comment.Text, "//go:embed ") {
parts, err := embedPatterns(strings.TrimPrefix(comment.Text, "//go:embed "))
if err != nil {
return err
}
patterns = append(patterns, parts...)
}
}
}
}
if len(patterns) == 0 {
continue
}
variable, ok := p.TypesInfo.Defs[value.Names[0]].(*types.Var)
if !ok {
continue
}
candidates := []string{}
for _, file := range p.EmbedFiles {
rel, inside := relative(directory, file)
if inside {
for _, pattern := range patterns {
if patternContains(pattern, rel) {
candidates = append(candidates, file)
break
}
}
}
}
isString := false
switch t := variable.Type().Underlying().(type) {
case *types.Basic:
isString = t.Kind() == types.String
case *types.Slice:
if elem, ok := t.Elem().Underlying().(*types.Basic); ok {
isString = elem.Kind() == types.Byte
}
}
if isString && len(candidates) == 1 {
rel, inside := relative(ix.root, candidates[0])
body, present := bodies[rel]
if !inside || !present {
return fmt.Errorf("embedded asset outside target snapshot: %s", candidates[0])
}
ix.embedded[variable] = string(body)
} else {
ix.boundaries = append(ix.boundaries, Boundary{Kind: "embedded_filesystem", Reason: "embedded FS or ambiguous asset value is inventoried but not flattened into string flow", Evidence: ix.evidence(value.Pos())})
}
}
}
}
}
return nil
}
func selectedEmbeddedAssetPaths(root string, pkgs []*packages.Package) []string {
files := map[string]bool{}
for _, p := range pkgs {
for _, file := range p.EmbedFiles {
if rel, inside := relative(root, file); inside {
files[rel] = true
}
}
}
assets := make([]string, 0, len(files))
for file := range files {
assets = append(assets, file)
}
sort.Strings(assets)
return assets
}
func hashFiles(root string, files []string) (string, error) {
h := sha256.New()
for _, file := range files {
body, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(file)))
if err != nil {
return "", err
}
fmt.Fprintf(h, "%d:%s:%d:", len(file), file, len(body))
h.Write(body)
}
return hex.EncodeToString(h.Sum(nil)), nil
}