mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-09-05 16:47:31 -04:00
Merge pull request #188 from TBBle/correct-GetVolumeInformation-return-handling
Correct GetVolumeInformation return type handling
This commit is contained in:
+3
-19
@@ -3,8 +3,6 @@ package fs
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
@@ -16,32 +14,18 @@ var (
|
|||||||
|
|
||||||
// GetFileSystemType obtains the type of a file system through GetVolumeInformation.
|
// GetFileSystemType obtains the type of a file system through GetVolumeInformation.
|
||||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
|
||||||
func GetFileSystemType(path string) (fsType string, hr error) {
|
func GetFileSystemType(path string) (fsType string, err error) {
|
||||||
drive := filepath.VolumeName(path)
|
drive := filepath.VolumeName(path)
|
||||||
if len(drive) != 2 {
|
if len(drive) != 2 {
|
||||||
return "", ErrInvalidPath
|
return "", ErrInvalidPath
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
|
||||||
procGetVolumeInformation = modkernel32.NewProc("GetVolumeInformationW")
|
|
||||||
buf = make([]uint16, 255)
|
buf = make([]uint16, 255)
|
||||||
size = windows.MAX_PATH + 1
|
size = uint32(windows.MAX_PATH + 1)
|
||||||
)
|
)
|
||||||
drive += `\`
|
drive += `\`
|
||||||
n := uintptr(unsafe.Pointer(nil))
|
err = windows.GetVolumeInformation(windows.StringToUTF16Ptr(drive), nil, 0, nil, nil, nil, &buf[0], size)
|
||||||
r0, _, _ := syscall.Syscall9(procGetVolumeInformation.Addr(), 8, uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(drive))), n, n, n, n, n, uintptr(unsafe.Pointer(&buf[0])), uintptr(size), 0)
|
|
||||||
if int32(r0) < 0 {
|
|
||||||
hr = syscall.Errno(win32FromHresult(r0))
|
|
||||||
}
|
|
||||||
fsType = windows.UTF16ToString(buf)
|
fsType = windows.UTF16ToString(buf)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// win32FromHresult is a helper function to get the win32 error code from an HRESULT.
|
|
||||||
func win32FromHresult(hr uintptr) uintptr {
|
|
||||||
if hr&0x1fff0000 == 0x00070000 {
|
|
||||||
return hr & 0xffff
|
|
||||||
}
|
|
||||||
return hr
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetFSTypeOfKnownDrive(t *testing.T) {
|
||||||
|
fsType, err := GetFileSystemType("C:\\")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fsType == "" {
|
||||||
|
t.Fatal("No filesystem type name returned")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetFSTypeOfInvalidPath(t *testing.T) {
|
||||||
|
_, err := GetFileSystemType("7:\\")
|
||||||
|
if err != ErrInvalidPath {
|
||||||
|
t.Fatalf("Expected `ErrInvalidPath`, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetFSTypeOfValidButAbsentDrive(t *testing.T) {
|
||||||
|
drive := ""
|
||||||
|
for _, letter := range "abcdefghijklmnopqrstuvwxyz" {
|
||||||
|
possibleDrive := string(letter) + ":\\"
|
||||||
|
if _, err := os.Stat(possibleDrive); os.IsNotExist(err) {
|
||||||
|
drive = possibleDrive
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if drive == "" {
|
||||||
|
t.Skip("Every possible drive exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := GetFileSystemType(drive)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("GetFileSystemType %s unexpectedly succeeded", drive)
|
||||||
|
}
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("GetFileSystemType %s failed with %v, expected 'ErrNotExist' or similar", drive, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user