-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
297 lines (244 loc) · 6.48 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package filetree
import (
"fmt"
"os"
"path"
"sort"
"strings"
"sync"
"github.com/becheran/wildmatch-go"
"github.com/scylladb/go-set/strset"
"github.com/nextlinux/stereoscope/internal/log"
"github.com/nextlinux/stereoscope/pkg/file"
)
type Index interface {
IndexReader
IndexWriter
}
type IndexReader interface {
Exists(f file.Reference) bool
Get(f file.Reference) (IndexEntry, error)
GetByMIMEType(mTypes ...string) ([]IndexEntry, error)
GetByFileType(fTypes ...file.Type) ([]IndexEntry, error)
GetByExtension(extensions ...string) ([]IndexEntry, error)
GetByBasename(basenames ...string) ([]IndexEntry, error)
GetByBasenameGlob(globs ...string) ([]IndexEntry, error)
Basenames() []string
}
type IndexWriter interface {
Add(f file.Reference, m file.Metadata)
}
// Index represents all file metadata and source tracing for all files contained within the image layer
// blobs (i.e. everything except for the image index/manifest/metadata files).
type index struct {
*sync.RWMutex
index map[file.ID]IndexEntry
byFileType map[file.Type]file.IDSet
byMIMEType map[string]file.IDSet
byExtension map[string]file.IDSet
byBasename map[string]file.IDSet
basenames *strset.Set
}
// NewIndex returns an empty Index.
func NewIndex() Index {
return &index{
RWMutex: &sync.RWMutex{},
index: make(map[file.ID]IndexEntry),
byFileType: make(map[file.Type]file.IDSet),
byMIMEType: make(map[string]file.IDSet),
byExtension: make(map[string]file.IDSet),
byBasename: make(map[string]file.IDSet),
basenames: strset.New(),
}
}
// IndexEntry represents all stored metadata for a single file reference.
type IndexEntry struct {
file.Reference
file.Metadata
}
// Add creates a new IndexEntry for the given file reference and metadata, cataloged by the ID of the
// file reference (overwriting any existing entries without warning).
func (c *index) Add(f file.Reference, m file.Metadata) {
c.Lock()
defer c.Unlock()
id := f.ID()
if _, ok := c.index[id]; ok {
log.WithFields("id", id, "path", f.RealPath).Debug("overwriting existing file index entry")
}
if m.MIMEType != "" {
if _, ok := c.byMIMEType[m.MIMEType]; !ok {
c.byMIMEType[m.MIMEType] = file.NewIDSet()
}
// an empty MIME type means that we didn't have the contents of the file to determine the MIME type. If we have
// the contents and the MIME type could not be determined then the default value is application/octet-stream.
c.byMIMEType[m.MIMEType].Add(id)
}
basename := path.Base(string(f.RealPath))
if _, ok := c.byBasename[basename]; !ok {
c.byBasename[basename] = file.NewIDSet()
}
c.byBasename[basename].Add(id)
c.basenames.Add(basename)
for _, ext := range fileExtensions(string(f.RealPath)) {
if _, ok := c.byExtension[ext]; !ok {
c.byExtension[ext] = file.NewIDSet()
}
c.byExtension[ext].Add(id)
}
if _, ok := c.byFileType[m.Type]; !ok {
c.byFileType[m.Type] = file.NewIDSet()
}
c.byFileType[m.Type].Add(id)
c.index[id] = IndexEntry{
Reference: f,
Metadata: m,
}
}
// Exists indicates if the given file reference exists in the index.
func (c *index) Exists(f file.Reference) bool {
c.RLock()
defer c.RUnlock()
_, ok := c.index[f.ID()]
return ok
}
// Get fetches a IndexEntry for the given file reference, or returns an error if the file reference has not
// been added to the index.
func (c *index) Get(f file.Reference) (IndexEntry, error) {
c.RLock()
defer c.RUnlock()
value, ok := c.index[f.ID()]
if !ok {
return IndexEntry{}, os.ErrNotExist
}
return value, nil
}
func (c *index) Basenames() []string {
c.RLock()
defer c.RUnlock()
bns := c.basenames.List()
sort.Strings(bns)
return bns
}
func (c *index) GetByFileType(fTypes ...file.Type) ([]IndexEntry, error) {
c.RLock()
defer c.RUnlock()
var entries []IndexEntry
for _, fType := range fTypes {
fileIDs, ok := c.byFileType[fType]
if !ok {
continue
}
for _, id := range fileIDs.Sorted() {
entry, ok := c.index[id]
if !ok {
return nil, os.ErrNotExist
}
entries = append(entries, entry)
}
}
return entries, nil
}
func (c *index) GetByMIMEType(mTypes ...string) ([]IndexEntry, error) {
c.RLock()
defer c.RUnlock()
var entries []IndexEntry
for _, mType := range mTypes {
fileIDs, ok := c.byMIMEType[mType]
if !ok {
continue
}
for _, id := range fileIDs.Sorted() {
entry, ok := c.index[id]
if !ok {
return nil, os.ErrNotExist
}
entries = append(entries, entry)
}
}
return entries, nil
}
func (c *index) GetByExtension(extensions ...string) ([]IndexEntry, error) {
c.RLock()
defer c.RUnlock()
var entries []IndexEntry
for _, extension := range extensions {
fileIDs, ok := c.byExtension[extension]
if !ok {
continue
}
for _, id := range fileIDs.Sorted() {
entry, ok := c.index[id]
if !ok {
return nil, os.ErrNotExist
}
entries = append(entries, entry)
}
}
return entries, nil
}
func (c *index) GetByBasename(basenames ...string) ([]IndexEntry, error) {
c.RLock()
defer c.RUnlock()
var entries []IndexEntry
for _, basename := range basenames {
if strings.Contains(basename, "/") {
return nil, fmt.Errorf("found directory separator in a basename")
}
fileIDs, ok := c.byBasename[basename]
if !ok {
continue
}
for _, id := range fileIDs.Sorted() {
entry, ok := c.index[id]
if !ok {
return nil, os.ErrNotExist
}
entries = append(entries, entry)
}
}
return entries, nil
}
func (c *index) GetByBasenameGlob(globs ...string) ([]IndexEntry, error) {
c.RLock()
defer c.RUnlock()
var entries []IndexEntry
for _, glob := range globs {
if strings.Contains(glob, "**") {
return nil, fmt.Errorf("basename glob patterns with '**' are not supported")
}
if strings.Contains(glob, "/") {
return nil, fmt.Errorf("found directory separator in a basename")
}
patternObj := wildmatch.NewWildMatch(glob)
for _, b := range c.Basenames() {
if patternObj.IsMatch(b) {
bns, err := c.GetByBasename(b)
if err != nil {
return nil, fmt.Errorf("unable to fetch file references by basename (%q): %w", b, err)
}
entries = append(entries, bns...)
}
}
}
return entries, nil
}
func fileExtensions(p string) []string {
var exts []string
p = strings.TrimSpace(p)
// ignore oddities
if strings.HasSuffix(p, ".") {
return exts
}
// ignore directories
if strings.HasSuffix(p, "/") {
return exts
}
// ignore . which indicate a hidden file
p = strings.TrimLeft(path.Base(p), ".")
for i := len(p) - 1; i >= 0; i-- {
if p[i] == '.' {
exts = append(exts, p[i:])
}
}
return exts
}