diff --git a/pkg/snclient/check_drivesize_windows.go b/pkg/snclient/check_drivesize_windows.go index f8a39412..f1a6df93 100644 --- a/pkg/snclient/check_drivesize_windows.go +++ b/pkg/snclient/check_drivesize_windows.go @@ -349,15 +349,15 @@ func (l *CheckDrivesize) setDeviceInfo(drive map[string]string) { } } -// adds all logical drives to the requiredDisks -// this function is better suited for usage with-network drives. gopsutil has disk.Partitions, but that does not work with network drives +// gopsutil disk.Partition had an issue with Bitlocker, but a fix was upstreamed +// use it instead of custom wrapper around GetLogicalDriveStringsW func (l *CheckDrivesize) setDrives(requiredDrives map[string]map[string]string) (err error) { - logicalDrives, err := GetLogicalDriveStrings(1024) - if err != nil { - log.Debug("Error when getting logical drive strings: %s", err.Error()) + partitions, err := disk.Partitions(true) + if err != nil && len(partitions) == 0 { + return fmt.Errorf("disk partitions failed: %s", err.Error()) } - - for _, logicalDrive := range logicalDrives { + for _, partition := range partitions { + logicalDrive := strings.TrimSuffix(partition.Device, "\\") + "\\" entry, ok := requiredDrives[logicalDrive] if !ok { entry = make(map[string]string) @@ -366,6 +366,7 @@ func (l *CheckDrivesize) setDrives(requiredDrives map[string]map[string]string) entry["drive_or_id"] = logicalDrive entry["drive_or_name"] = logicalDrive entry["letter"] = fmt.Sprintf("%c", logicalDrive[0]) + entry["fstype"] = partition.Fstype requiredDrives[logicalDrive] = entry } @@ -574,12 +575,13 @@ func (l *CheckDrivesize) setCustomPath(path string, requiredDisks map[string]map // adds all network shares to requiredDisks func (l *CheckDrivesize) setShares(requiredDisks map[string]map[string]string) { - logicalDrives, err := GetLogicalDriveStrings(1024) + partitions, err := disk.Partitions(true) if err != nil { - log.Debug("Error when getting logical drive strings: %s", err.Error()) + log.Debug("Error when discovering partitions: %s", err.Error()) } - for _, logicalDrive := range logicalDrives { + for _, partition := range partitions { + logicalDrive := strings.TrimSuffix(partition.Device, "\\") + "\\" driveType, err := GetDriveType(logicalDrive) if err != nil { log.Debug("Error when getting the drive type for logical drive %s network drives: %s", logicalDrive, err.Error()) diff --git a/pkg/snclient/check_drivesize_windows_win32api.go b/pkg/snclient/check_drivesize_windows_win32api.go index 8eddf167..202d9d21 100644 --- a/pkg/snclient/check_drivesize_windows_win32api.go +++ b/pkg/snclient/check_drivesize_windows_win32api.go @@ -70,6 +70,7 @@ var ( wNetGetConnectionW = winnetwkDll.NewProc("WNetGetConnectionW") ) +// this function is not being used anymore, as gopsutil disk.Partitions() is fixed func GetLogicalDriveStrings(nBufferLength uint32) (logicalDrives []string, err error) { // We are using getLogicalDriveStringsW , which uses Long Width Chars @@ -97,6 +98,12 @@ func GetLogicalDriveStrings(nBufferLength uint32) (logicalDrives []string, err e // There are multiple drives in the returned string, separated by a NULL character for index := 0; uintptr(index) < returnValue; { decodedDriveStr := windows.UTF16ToString(lpBuffer[index:]) + if decodedDriveStr == "" { + // At the end there is are two consecutive null terminators + // One from the last drive C-string + // One that denotes the end of strings + break + } logicalDrives = append(logicalDrives, decodedDriveStr) index += len(decodedDriveStr) + 1 }