Skip to content

Commit

Permalink
use GetLogicalDriveStrings instead of GetLogicalDrives
Browse files Browse the repository at this point in the history
  • Loading branch information
FRex committed Nov 6, 2020
1 parent 2b7d398 commit f91de5a
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions mounts_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,42 @@ func mountPointAlreadyPresent(mounts []Mount, mountPoint string) bool {
return false
}

func myWcsLen(str []uint16) (ret int32) {
for i, c := range str {
if c == 0 {
ret = int32(i)
return
}
}

ret = 0
return
}

func appendLogicalDrives(mounts []Mount, warnings []string) ([]Mount, []string) {
drivebitmap, err := windows.GetLogicalDrives()
if err != nil {
warnings = append(warnings, fmt.Sprintf("GetLogicalDrives(): %s", err))
return mounts, warnings
var mountPointsBuf []uint16
mountPointsBufLen := uint32(20)
for true {
mountPointsBufLen *= 2
mountPointsBuf = make([]uint16, mountPointsBufLen + 1)
writtenLen, err := windows.GetLogicalDriveStrings(mountPointsBufLen, (*uint16)(unsafe.Pointer(&mountPointsBuf[0])))
if err != nil {
warnings = append(warnings, fmt.Sprintf("GetLogicalDriveStrings(): %s", err))
return mounts, warnings
}

if writtenLen < mountPointsBufLen {
break
}
}

for i := 0; i < 26; i++ {
if (drivebitmap & (1 << i)) == 0 {
continue
for true {
if myWcsLen(mountPointsBuf) == 0 {
break
}

mountPoint := fmt.Sprintf("%s:\\", string(65+i))
mountPoint := windows.UTF16ToString(mountPointsBuf)
mountPointsBuf = mountPointsBuf[myWcsLen(mountPointsBuf) + 1:]
if mountPointAlreadyPresent(mounts, mountPoint) {
continue
}
Expand All @@ -315,7 +338,7 @@ func mounts() (ret []Mount, warnings []string, err error) {
return
}

// Logical devices (from GetLogicalDrives bitflag)
// Logical devices (from GetLogicalDriveStrings list)
ret, warnings = appendLogicalDrives(ret, warnings)

// Network devices
Expand Down

0 comments on commit f91de5a

Please sign in to comment.