Skip to content

Commit 622e346

Browse files
committed
feat: filter created entries for report
The entries not mentioned explicitly in the slice definition will not be added to the report (e.g. copyright, parent directories). Additionally, we have removed the Globbed option and simplified it.
1 parent 3d84be3 commit 622e346

8 files changed

Lines changed: 339 additions & 100 deletions

File tree

‎internal/deb/extract.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ type ExtractOptions struct {
2424
Package string
2525
TargetDir string
2626
Extract map[string][]ExtractInfo
27-
Globbed map[string][]string
28-
Create func(options *fsutil.CreateOptions) error
27+
// Create creates a fs entry given the options. If the entry is extracted from
28+
// the tarball extractInfo should not be empty, else nil can be passed.
29+
Create func(extractInfo *ExtractInfo, options *fsutil.CreateOptions) error
2930
}
3031

3132
type ExtractInfo struct {
@@ -46,7 +47,7 @@ func getValidOptions(options *ExtractOptions) (*ExtractOptions, error) {
4647

4748
if options.Create == nil {
4849
validOpts := *options
49-
validOpts.Create = func(o *fsutil.CreateOptions) error {
50+
validOpts.Create = func(_ *ExtractInfo, o *fsutil.CreateOptions) error {
5051
_, err := fsutil.Create(o)
5152
return err
5253
}
@@ -185,9 +186,6 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
185186
if globPath != "" {
186187
extractInfos = options.Extract[globPath]
187188
delete(pendingPaths, globPath)
188-
if options.Globbed != nil {
189-
options.Globbed[globPath] = append(options.Globbed[globPath], sourcePath)
190-
}
191189
} else {
192190
extractInfos, ok = options.Extract[sourcePath]
193191
if ok {
@@ -196,11 +194,12 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
196194
// Base directory for extracted content. Relevant mainly to preserve
197195
// the metadata, since the extracted content itself will also create
198196
// any missing directories unaccounted for in the options.
199-
err := options.Create(&fsutil.CreateOptions{
197+
createOptions := &fsutil.CreateOptions{
200198
Path: filepath.Join(options.TargetDir, sourcePath),
201199
Mode: tarHeader.FileInfo().Mode(),
202200
MakeParents: true,
203-
})
201+
}
202+
err := options.Create(nil, createOptions)
204203
if err != nil {
205204
return err
206205
}
@@ -237,13 +236,14 @@ func extractData(dataReader io.Reader, options *ExtractOptions) error {
237236
if extractInfo.Mode != 0 {
238237
tarHeader.Mode = int64(extractInfo.Mode)
239238
}
240-
err := options.Create(&fsutil.CreateOptions{
239+
createOptions := &fsutil.CreateOptions{
241240
Path: targetPath,
242241
Mode: tarHeader.FileInfo().Mode(),
243242
Data: pathReader,
244243
Link: tarHeader.Linkname,
245244
MakeParents: true,
246-
})
245+
}
246+
err := options.Create(&extractInfo, createOptions)
247247
if err != nil {
248248
return err
249249
}

‎internal/deb/extract_test.go‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type extractTest struct {
1919
pkgdata []byte
2020
options deb.ExtractOptions
2121
hackopt func(o *deb.ExtractOptions)
22-
globbed map[string][]string
2322
result map[string]string
2423
// paths which the extractor did not create explicitly.
2524
notCreated []string
@@ -179,7 +178,7 @@ var extractTests = []extractTest{{
179178
},
180179
notCreated: []string{"/dir/"},
181180
}, {
182-
summary: "Globbing with reporting of globbed paths",
181+
summary: "Globbing multiple paths",
183182
pkgdata: testutil.PackageData["test-package"],
184183
options: deb.ExtractOptions{
185184
Extract: map[string][]deb.ExtractInfo{
@@ -199,10 +198,6 @@ var extractTests = []extractTest{{
199198
"/dir/several/levels/deep/": "dir 0755",
200199
"/dir/several/levels/deep/file": "file 0644 6bc26dff",
201200
},
202-
globbed: map[string][]string{
203-
"/dir/n*/": []string{"/dir/nested/"},
204-
"/dir/s**": []string{"/dir/several/", "/dir/several/levels/", "/dir/several/levels/deep/", "/dir/several/levels/deep/file"},
205-
},
206201
notCreated: []string{"/dir/"},
207202
}, {
208203
summary: "Globbing must have matching source and target",
@@ -336,7 +331,7 @@ func (s *S) TestExtract(c *C) {
336331
options.Package = "test-package"
337332
options.TargetDir = dir
338333
createdPaths := make(map[string]bool)
339-
options.Create = func(o *fsutil.CreateOptions) error {
334+
options.Create = func(_ *deb.ExtractInfo, o *fsutil.CreateOptions) error {
340335
relPath := filepath.Clean("/" + strings.TrimPrefix(o.Path, dir))
341336
if o.Mode&fs.ModeDir != 0 {
342337
relPath = relPath + "/"
@@ -350,10 +345,6 @@ func (s *S) TestExtract(c *C) {
350345
test.hackopt(&options)
351346
}
352347

353-
if test.globbed != nil {
354-
options.Globbed = make(map[string][]string)
355-
}
356-
357348
err := deb.Extract(bytes.NewBuffer(test.pkgdata), &options)
358349
if test.error != "" {
359350
c.Assert(err, ErrorMatches, test.error)
@@ -362,10 +353,6 @@ func (s *S) TestExtract(c *C) {
362353
c.Assert(err, IsNil)
363354
}
364355

365-
if test.globbed != nil {
366-
c.Assert(options.Globbed, DeepEquals, test.globbed)
367-
}
368-
369356
if test.notCreated != nil {
370357
notCreated := []string{}
371358
for path := range test.result {

‎internal/fsutil/create.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Create(options *CreateOptions) (*Entry, error) {
3939
o := &optsCopy
4040

4141
var err error
42+
var hash string
4243
if o.MakeParents {
4344
if err := os.MkdirAll(filepath.Dir(o.Path), 0755); err != nil {
4445
return nil, err
@@ -47,6 +48,7 @@ func Create(options *CreateOptions) (*Entry, error) {
4748
switch o.Mode & fs.ModeType {
4849
case 0:
4950
err = createFile(o)
51+
hash = hex.EncodeToString(rp.h.Sum(nil))
5052
case fs.ModeDir:
5153
err = createDir(o)
5254
case fs.ModeSymlink:
@@ -61,7 +63,7 @@ func Create(options *CreateOptions) (*Entry, error) {
6163
entry := &Entry{
6264
Path: o.Path,
6365
Mode: o.Mode,
64-
Hash: hex.EncodeToString(rp.h.Sum(nil)),
66+
Hash: hash,
6567
Size: rp.size,
6668
Link: o.Link,
6769
}

‎internal/slicer/report.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Report struct {
3232
// provided root path.
3333
func NewReport(root string) *Report {
3434
return &Report{
35-
Root: root,
35+
Root: filepath.Clean(root),
3636
Entries: make(map[string]ReportEntry),
3737
}
3838
}
@@ -42,6 +42,9 @@ func (r *Report) Add(slice *setup.Slice, fsEntry *fsutil.Entry) error {
4242
return fmt.Errorf("cannot add path %q outside of root %q", fsEntry.Path, r.Root)
4343
}
4444
relPath := filepath.Clean("/" + strings.TrimPrefix(fsEntry.Path, r.Root))
45+
if fsEntry.Mode&fs.ModeDir != 0 {
46+
relPath = relPath + "/"
47+
}
4548

4649
if entry, ok := r.Entries[relPath]; ok {
4750
if fsEntry.Mode != entry.Mode {

‎internal/slicer/report_test.go‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var otherSlice = &setup.Slice{
2727
}
2828

2929
var sampleDir = fsutil.Entry{
30-
Path: "/base/exampleDir",
30+
Path: "/base/exampleDir/",
3131
Mode: fs.ModeDir | 0654,
3232
Link: "",
3333
}
@@ -64,8 +64,8 @@ var reportTests = []struct {
6464
summary: "Regular directory",
6565
add: []sliceAndEntry{{entry: sampleDir, slice: oneSlice}},
6666
expected: map[string]slicer.ReportEntry{
67-
"/exampleDir": {
68-
Path: "/exampleDir",
67+
"/exampleDir/": {
68+
Path: "/exampleDir/",
6969
Mode: fs.ModeDir | 0654,
7070
Slices: map[*setup.Slice]bool{oneSlice: true},
7171
Link: "",
@@ -77,8 +77,8 @@ var reportTests = []struct {
7777
{entry: sampleDir, slice: otherSlice},
7878
},
7979
expected: map[string]slicer.ReportEntry{
80-
"/exampleDir": {
81-
Path: "/exampleDir",
80+
"/exampleDir/": {
81+
Path: "/exampleDir/",
8282
Mode: fs.ModeDir | 0654,
8383
Slices: map[*setup.Slice]bool{oneSlice: true, otherSlice: true},
8484
Link: "",
@@ -114,8 +114,8 @@ var reportTests = []struct {
114114
{entry: sampleFile, slice: otherSlice},
115115
},
116116
expected: map[string]slicer.ReportEntry{
117-
"/exampleDir": {
118-
Path: "/exampleDir",
117+
"/exampleDir/": {
118+
Path: "/exampleDir/",
119119
Mode: fs.ModeDir | 0654,
120120
Slices: map[*setup.Slice]bool{oneSlice: true},
121121
Link: "",
@@ -195,6 +195,12 @@ var reportTests = []struct {
195195
}, slice: oneSlice},
196196
},
197197
err: `path "/exampleFile" reported twice with diverging link: "distinct link" != ""`,
198+
}, {
199+
summary: "Error for path outside root",
200+
add: []sliceAndEntry{
201+
{entry: fsutil.Entry{Path: "/file"}, slice: oneSlice},
202+
},
203+
err: `cannot add path "/file" outside of root "/base"`,
198204
}}
199205

200206
func (s *S) TestReportAdd(c *C) {

‎internal/slicer/slicer.go‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8+
"io/fs"
89
"os"
910
"path/filepath"
1011
"strings"
@@ -158,13 +159,28 @@ func Run(options *RunOptions) (*Report, error) {
158159
Package: slice.Package,
159160
Extract: extract[slice.Package],
160161
TargetDir: targetDir,
161-
Globbed: globbedPaths,
162162
// Creates the filesystem entry and adds it to the report.
163-
Create: func(o *fsutil.CreateOptions) error {
163+
Create: func(extractInfo *deb.ExtractInfo, o *fsutil.CreateOptions) error {
164164
entry, err := fsutil.Create(o)
165165
if err != nil {
166166
return err
167167
}
168+
169+
// We only want to keep the entries that were explicitly listed
170+
// in the slice definition.
171+
if !isListedInSlice(extractInfo, slice) {
172+
return nil
173+
}
174+
175+
// Check whether the file was created because it matched a glob.
176+
if strings.ContainsAny(extractInfo.Path, "*?") {
177+
relPath := filepath.Clean("/" + strings.TrimLeft(o.Path, targetDir))
178+
if o.Mode&fs.ModeDir != 0 {
179+
relPath = relPath + "/"
180+
}
181+
globbedPaths[extractInfo.Path] = append(globbedPaths[extractInfo.Path], relPath)
182+
addKnownPath(relPath)
183+
}
168184
return report.Add(slice, entry)
169185
},
170186
})
@@ -175,12 +191,6 @@ func Run(options *RunOptions) (*Report, error) {
175191
}
176192
}
177193

178-
for _, expandedPaths := range globbedPaths {
179-
for _, path := range expandedPaths {
180-
addKnownPath(path)
181-
}
182-
}
183-
184194
// Create new content not coming from packages.
185195
done := make(map[string]bool)
186196
for _, slice := range options.Selection.Slices {
@@ -330,3 +340,15 @@ func contains(l []string, s string) bool {
330340
}
331341
return false
332342
}
343+
344+
func isListedInSlice(extractInfo *deb.ExtractInfo, slice *setup.Slice) bool {
345+
if extractInfo == nil {
346+
return false
347+
}
348+
for path := range slice.Contents {
349+
if extractInfo.Path == path {
350+
return true
351+
}
352+
}
353+
return false
354+
}

0 commit comments

Comments
 (0)