Files

170 lines
4.9 KiB
Go

package host
import (
"fmt"
"slices"
"strings"
)
// AppleAPFSPartitionTypeGUID is the canonical GPT partition type GUID for Apple APFS containers.
const AppleAPFSPartitionTypeGUID = "7C3457EF-0000-11AA-AA11-00306543ECAC"
var protectedGPTPartitionTypeGUIDs = map[string]bool{
"C12A7328-F81F-11D2-BA4B-00A0C93EC93B": true, // EFI System Partition
"E3C9E316-0B5C-4DB8-817D-F92DF00215AE": true, // Microsoft Reserved (MSR)
"DE94BBA4-06D1-4D40-A16A-BFD50179D6AC": true, // Windows Recovery Environment
}
// RawDiskCandidate describes a Windows raw volume or offline whole disk
// eligible as an unmounted guest block device.
type RawDiskCandidate struct {
DevicePath string `json:"device_path"`
DiskNumber uint32 `json:"disk_number"`
PartitionNumber uint32 `json:"partition_number,omitempty"`
SizeBytes uint64 `json:"size_bytes"`
PartitionTypeGUID string `json:"partition_type_guid,omitempty"`
IsAPFS bool `json:"is_apfs"`
Offline bool `json:"offline"`
ReadOnly bool `json:"read_only"`
Label string `json:"label,omitempty"`
}
// IsWindowsRawDevicePath reports whether path names a Windows raw disk,
// partition, or volume device path (excluding regular Win32 paths such as \\?\C:\...).
func IsWindowsRawDevicePath(path string) bool {
p := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(path), "/", `\`))
if strings.HasPrefix(p, `\\.\`) {
rest := p[len(`\\.\`):]
return strings.HasPrefix(rest, "physicaldrive") ||
strings.HasPrefix(rest, "harddiskvolume") ||
strings.HasPrefix(rest, "harddisk") ||
strings.HasPrefix(rest, "volume{")
}
if strings.HasPrefix(p, `\\?\`) {
rest := p[len(`\\?\`):]
return strings.HasPrefix(rest, "volume{")
}
return false
}
// RawVolumeObservation captures raw probe fields for a Windows volume or offline disk
// before applying host-safety filters.
type RawVolumeObservation struct {
DevicePath string
DiskNumber uint32
PartitionNumber uint32
SizeBytes uint64
PartitionTypeGUID string
HasMountPoints bool
Filesystem string
Offline bool
ReadOnly bool
Label string
}
type indexedVolumeExtent struct {
start int64
end int64
volDev string
multiExtent bool
hasMountPoints bool
filesystem string
occupied bool
}
func applyPartitionExtentMatches(obs *RawVolumeObservation, partStart int64, partLen uint64, extents []indexedVolumeExtent) {
if obs == nil || len(extents) == 0 || partLen == 0 {
return
}
partEnd := partStart + int64(partLen)
if partEnd <= partStart {
partEnd = 1<<63 - 1
}
for _, ext := range extents {
if ext.end <= partStart || ext.start >= partEnd {
continue
}
if ext.occupied || ext.hasMountPoints {
obs.HasMountPoints = true
}
if ext.filesystem != "" && obs.Filesystem == "" {
obs.Filesystem = ext.filesystem
}
if !ext.multiExtent && ext.start == partStart && ext.volDev != "" {
obs.DevicePath = ext.volDev
}
}
}
func isMountedWindowsFilesystem(fsName string) bool {
switch strings.ToUpper(strings.TrimSpace(fsName)) {
case "NTFS", "FAT", "FAT32", "EXFAT", "REFS", "CDFS", "UDF":
return true
default:
return false
}
}
func filterRawPartitionCandidates(observations []RawVolumeObservation, excludedDisks map[uint32]bool) []RawDiskCandidate {
var out []RawDiskCandidate
for _, obs := range observations {
if obs.DevicePath == "" || obs.SizeBytes == 0 {
continue
}
if excludedDisks[obs.DiskNumber] {
continue
}
if obs.HasMountPoints || isMountedWindowsFilesystem(obs.Filesystem) {
continue
}
guid := strings.ToUpper(strings.TrimSpace(obs.PartitionTypeGUID))
if guid != "" && protectedGPTPartitionTypeGUIDs[guid] {
continue
}
isAPFS := guid == AppleAPFSPartitionTypeGUID
label := strings.TrimSpace(obs.Label)
if label == "" {
if obs.PartitionNumber == 0 {
label = fmt.Sprintf("Disk %d (Offline)", obs.DiskNumber)
} else if isAPFS {
label = fmt.Sprintf("Disk %d Partition %d (APFS)", obs.DiskNumber, obs.PartitionNumber)
} else {
label = fmt.Sprintf("Disk %d Partition %d", obs.DiskNumber, obs.PartitionNumber)
}
}
out = append(out, RawDiskCandidate{
DevicePath: obs.DevicePath,
DiskNumber: obs.DiskNumber,
PartitionNumber: obs.PartitionNumber,
SizeBytes: obs.SizeBytes,
PartitionTypeGUID: guid,
IsAPFS: isAPFS,
Offline: obs.Offline,
ReadOnly: obs.ReadOnly,
Label: label,
})
}
slices.SortFunc(out, func(a, b RawDiskCandidate) int {
if a.IsAPFS != b.IsAPFS {
if a.IsAPFS {
return -1
}
return 1
}
if a.DiskNumber != b.DiskNumber {
if a.DiskNumber < b.DiskNumber {
return -1
}
return 1
}
if a.PartitionNumber < b.PartitionNumber {
return -1
}
if a.PartitionNumber > b.PartitionNumber {
return 1
}
return strings.Compare(a.DevicePath, b.DevicePath)
})
return out
}