Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where SCABLIR temporary directory wasn't deleted after RPM extraction. #488

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extractor/filesystem/os/rpm/rpm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func (e Extractor) extractFromInput(ctx context.Context, input *filesystem.ScanI
if input.Root == "" {
// The file got copied to a temporary dir, remove it at the end.
defer func() {
dir := filepath.Base(absPath)
dir := filepath.Dir(absPath)
if err := os.RemoveAll(dir); err != nil {
log.Errorf("os.RemoveAll(%q%): %w", dir, err)
log.Errorf("os.RemoveAll(%q): %w", dir, err)
}
}()
}
Expand Down
31 changes: 31 additions & 0 deletions extractor/filesystem/os/rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"runtime"
"slices"
"sort"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -790,6 +791,10 @@ func TestExtract_VirtualFilesystem(t *testing.T) {
d := t.TempDir()
createOsRelease(t, d, tt.osrelease)

// Need to record scalibr files found in /tmp before the rpm extractor runs, as it may create
// some. This is needed to compare the files found after the extractor runs.
filesInTmpWant := scalibrFilesInTmp(t)

r, err := os.Open(tt.path)
defer func() {
if err = r.Close(); err != nil {
Expand Down Expand Up @@ -823,6 +828,13 @@ func TestExtract_VirtualFilesystem(t *testing.T) {
if len(got) != tt.wantResults {
t.Errorf("Extract(%s): got %d results, want %d\n", tt.path, len(got), tt.wantResults)
}

// Check that no scalibr files remain in /tmp.
filesInTmpGot := scalibrFilesInTmp(t)
less := func(a, b string) bool { return a < b }
if diff := cmp.Diff(filesInTmpWant, filesInTmpGot, cmpopts.SortSlices(less)); diff != "" {
t.Errorf("returned unexpected diff (-want +got):\n%s", diff)
}
})
}
}
Expand Down Expand Up @@ -996,3 +1008,22 @@ func createOsRelease(t *testing.T, root string, content string) {
t.Fatalf("write to %s: %v\n", filepath.Join(root, "etc/os-release"), err)
}
}

// scalibrFilesInTmp returns the list of filenames in /tmp that start with "scalibr-".
func scalibrFilesInTmp(t *testing.T) []string {
t.Helper()

filenames := []string{}
files, err := os.ReadDir(os.TempDir())
if err != nil {
t.Fatalf("os.ReadDir('%q') error: %v", os.TempDir(), err)
}

for _, f := range files {
name := f.Name()
if strings.HasPrefix(name, "scalibr-") {
filenames = append(filenames, f.Name())
}
}
return filenames
}
Loading