Skip to content
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
22 changes: 12 additions & 10 deletions pkg/snclient/check_drivesize_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions pkg/snclient/check_drivesize_windows_win32api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down