mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-08-28 04:46:50 -04:00
The _docs_ say it returns 0 for failure, non-zero for success.
This implementation is looking for <0 as HRESULT failure, and deriving
an Errno from that.
The new tests fail with GetFileSystemType as it was defined, e.g.,
```
--- FAIL: TestGetFSTypeOfValidButAbsentDrive (0.00s)
fs_windows_test.go:41: GetFileSystemType a:\ unexpectedly succeeded
```
when I definitely do not have an A:\ drive.
Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
32 lines
859 B
Go
32 lines
859 B
Go
package fs
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
var (
|
|
// ErrInvalidPath is returned when the location of a file path doesn't begin with a driver letter.
|
|
ErrInvalidPath = errors.New("the path provided to GetFileSystemType must start with a drive letter")
|
|
)
|
|
|
|
// GetFileSystemType obtains the type of a file system through GetVolumeInformation.
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
|
|
func GetFileSystemType(path string) (fsType string, err error) {
|
|
drive := filepath.VolumeName(path)
|
|
if len(drive) != 2 {
|
|
return "", ErrInvalidPath
|
|
}
|
|
|
|
var (
|
|
buf = make([]uint16, 255)
|
|
size = uint32(windows.MAX_PATH + 1)
|
|
)
|
|
drive += `\`
|
|
err = windows.GetVolumeInformation(windows.StringToUTF16Ptr(drive), nil, 0, nil, nil, nil, &buf[0], size)
|
|
fsType = windows.UTF16ToString(buf)
|
|
return
|
|
}
|