Skip to content

Commit

Permalink
fix: ensure globbed FileInfo always has correct size (#482)
Browse files Browse the repository at this point in the history
When globbing a file that has a FileInfo set, we would reuse the pointer
to the original file's FileInfo even if the matched files' sizes are
different, causing deb to error when writing the data file due to
mismatching sizes.

Copy the FileInfo and recalculate its size when globbing. Add a test
case to check this scenario.

Fixes #316.
  • Loading branch information
markspolakovs authored Mar 25, 2022
1 parent 1d2e7ff commit f3f9718
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,18 @@ func ExpandContentGlobs(contents Contents, disableGlobbing bool) (files Contents

func appendGlobbedFiles(all Contents, globbed map[string]string, origFile *Content) (Contents, error) {
for src, dst := range globbed {
// if the file has a FileInfo, we need to copy it but recalculate its size
newFileInfo := origFile.FileInfo
if newFileInfo != nil {
newFileInfoVal := *newFileInfo
newFileInfoVal.Size = 0
newFileInfo = &newFileInfoVal
}
newFile := (&Content{
Destination: ToNixPath(dst),
Source: ToNixPath(src),
Type: origFile.Type,
FileInfo: origFile.FileInfo,
FileInfo: newFileInfo,
Packager: origFile.Packager,
}).WithFileInfoDefaults()
if dst, err := os.Readlink(src); err == nil {
Expand Down
23 changes: 23 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,26 @@ func TestGlobbingWhenFilesHaveBrackets(t *testing.T) {
}
}
}

func TestGlobbingFilesWithDifferentSizesWithFileInfo(t *testing.T) {
result, err := files.ExpandContentGlobs(files.Contents{
{
Source: "./testdata/globtest/different-sizes/**/*",
Destination: ".",
FileInfo: &files.ContentFileInfo{
Mode: 0o777,
},
},
}, false)
if err != nil {
t.Fatalf("expand content globs: %v", err)
}

if len(result) != 2 {
t.Fatalf("unexpected result length: %d, expected 2", len(result))
}

if result[0].FileInfo.Size == result[1].FileInfo.Size {
t.Fatal("test FileInfos have the same size, expected different")
}
}
1 change: 1 addition & 0 deletions files/testdata/globtest/different-sizes/a/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
1 change: 1 addition & 0 deletions files/testdata/globtest/different-sizes/b/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barbaz

0 comments on commit f3f9718

Please sign in to comment.