Created
July 6, 2020 18:08
-
-
Save kostix/e3d1d3072f034c46cc84a1991117f692 to your computer and use it in GitHub Desktop.
Get fixed DOS drives in Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"log" | |
"syscall" | |
"unsafe" | |
) | |
var ( | |
kernel32 = syscall.NewLazyDLL("kernel32.dll") | |
getDriveTypeWProc = kernel32.NewProc("GetDriveTypeW") | |
) | |
func getDriveType(rootPathName []uint16) (int, error) { | |
rc, _, _ := getDriveTypeWProc.Call( | |
uintptr(unsafe.Pointer(&rootPathName[0])), | |
) | |
dt := int(rc) | |
if dt == driveUnknown || dt == driveNoRootDir { | |
return -1, driveTypeErrors[dt] | |
} | |
return dt, nil | |
} | |
var ( | |
errUnknownDriveType = errors.New("unknown drive type") | |
errNoRootDir = errors.New("invalid root drive path") | |
driveTypeErrors = [...]error{ | |
0: errUnknownDriveType, | |
1: errNoRootDir, | |
} | |
) | |
const ( | |
driveUnknown = iota | |
driveNoRootDir | |
driveRemovable | |
driveFixed | |
driveRemote | |
driveCDROM | |
driveRamdisk | |
) | |
func getFixedDOSDrives() ([]string, error) { | |
var drive = [4]uint16{ | |
1: ':', | |
2: '\\', | |
} | |
var drives []string | |
for c := 'A'; c <= 'Z'; c++ { | |
drive[0] = uint16(c) | |
dt, err := getDriveType(drive[:]) | |
if err != nil { | |
if err == errNoRootDir { | |
continue | |
} | |
return nil, fmt.Errorf("error getting type of: %s: %s", | |
syscall.UTF16ToString(drive[:]), err) | |
} | |
if dt != driveFixed { | |
continue | |
} | |
drives = append(drives, syscall.UTF16ToString(drive[:])) | |
} | |
return drives, nil | |
} | |
func main() { | |
drives, err := getFixedDOSDrives() | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, drive := range drives { | |
log.Println(drive) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running on by box (under Wine 4.0) I get:
(All drives are mapped using
winecfg
.)