Skip to content

Commit

Permalink
Fix bug where SCABLIR temporary directory wasn't deleted after RPM ex…
Browse files Browse the repository at this point in the history
…traction.

PiperOrigin-RevId: 730920555
  • Loading branch information
Mario Leyva authored and copybara-github committed Feb 25, 2025
1 parent 212517d commit 4568f32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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
}

0 comments on commit 4568f32

Please sign in to comment.