Skip to content
Open
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
77 changes: 4 additions & 73 deletions extractor/filesystem/embeddedfs/vmdk/vmdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
"strings"
"sync"

"github.com/diskfs/go-diskfs"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/embeddedfs/common"
scalibrfs "github.com/google/osv-scalibr/fs"
"github.com/google/osv-scalibr/inventory"
"github.com/google/osv-scalibr/plugin"
)
Expand Down Expand Up @@ -143,25 +141,12 @@
return inventory.Inventory{}, fmt.Errorf("failed to convert %s to raw image: %w", vmdkPath, err)
}

// Open the raw disk image with go-diskfs
disk, err := diskfs.Open(tmpRawPath, diskfs.WithOpenMode(diskfs.ReadOnly))
if err != nil {
os.Remove(tmpRawPath)
return inventory.Inventory{}, fmt.Errorf("failed to open raw disk image %s: %w", tmpRawPath, err)
}

// Get the partition table
partitions, err := disk.GetPartitionTable()
// Retrieve all partitions and the associated disk handle from the raw disk image.
partitionList, disk, err := common.GetDiskPartitions(tmpRawPath)

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: common.GetDiskPartitions

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: common.GetDiskPartitions

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: common.GetDiskPartitions

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

undefined: common.GetDiskPartitions

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

undefined: common.GetDiskPartitions

Check failure on line 145 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

undefined: common.GetDiskPartitions
if err != nil {
disk.Close()
os.Remove(tmpRawPath)
return inventory.Inventory{}, fmt.Errorf("failed to get partition table: %w", err)
}
partitionList := partitions.GetPartitions()
if len(partitionList) == 0 {
disk.Close()
os.Remove(tmpRawPath)
return inventory.Inventory{}, errors.New("no partitions found in raw disk image")
return inventory.Inventory{}, err
}

// Create a reference counter for the temporary file
Expand All @@ -172,61 +157,7 @@
var embeddedFSs []*inventory.EmbeddedFS
for i, p := range partitionList {
partitionIndex := i + 1 // go-diskfs uses 1-based indexing
getEmbeddedFS := func(ctx context.Context) (scalibrfs.FS, error) {
// Open raw image for filesystem parsers
f, err := os.Open(tmpRawPath)
if err != nil {
return nil, fmt.Errorf("failed to open raw image %s: %w", tmpRawPath, err)
}

// Get partition offset and size (already multiplied by sector size)
offset := p.GetStart()
size := p.GetSize()
section := io.NewSectionReader(f, offset, size)
fsType := common.DetectFilesystem(section, 0)

// Create a temporary directory for extracted files
tempDir, err := os.MkdirTemp("", fmt.Sprintf("scalibr-vmdk-%s-%d-", fsType, partitionIndex))
if err != nil {
f.Close()
return nil, fmt.Errorf("failed to create temporary directory for %s partition %d: %w", fsType, partitionIndex, err)
}

params := common.GenerateFSParams{
File: f,
Disk: disk,
Section: section,
PartitionIndex: partitionIndex,
TempDir: tempDir,
TmpRawPath: tmpRawPath,
RefMu: &refMu,
RefCount: &refCount,
}

var fsys scalibrfs.FS
switch fsType {
case "ext4":
fsys, err = common.GenerateEXTFS(params)
case "FAT32":
fsys, err = common.GenerateFAT32FS(params)
case "exFAT":
fsys, err = common.GenerateEXFATFS(params)
case "NTFS":
fsys, err = common.GenerateNTFSFS(params)
default:
fsys, err = nil, fmt.Errorf("unsupported filesystem type %s for partition %d", fsType, partitionIndex)
}
if err != nil {
// common.GenerateFAT32FS is responsible for opening the file itself + closing on error.
if fsType != "FAT32" {
f.Close()
}
os.RemoveAll(tempDir)
return nil, err
}
return fsys, nil
}

getEmbeddedFS := common.NewPartitionEmbeddedFSGetter("vmdk", partitionIndex, p, disk, tmpRawPath, &refMu, &refCount)

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: common.NewPartitionEmbeddedFSGetter

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: common.NewPartitionEmbeddedFSGetter (typecheck)

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: common.NewPartitionEmbeddedFSGetter (typecheck)

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (windows-latest)

undefined: common.NewPartitionEmbeddedFSGetter

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (macos-latest)

undefined: common.NewPartitionEmbeddedFSGetter

Check failure on line 160 in extractor/filesystem/embeddedfs/vmdk/vmdk.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest)

undefined: common.NewPartitionEmbeddedFSGetter
embeddedFSs = append(embeddedFSs, &inventory.EmbeddedFS{
Path: fmt.Sprintf("%s:%d", vmdkPath, partitionIndex),
GetEmbeddedFS: getEmbeddedFS,
Expand Down
Loading