fs_windows.go 899 B

1234567891011121314151617181920212223242526272829303132
  1. package fs
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "golang.org/x/sys/windows"
  6. "github.com/Microsoft/go-winio/internal/stringbuffer"
  7. )
  8. var (
  9. // ErrInvalidPath is returned when the location of a file path doesn't begin with a driver letter.
  10. ErrInvalidPath = errors.New("the path provided to GetFileSystemType must start with a drive letter")
  11. )
  12. // GetFileSystemType obtains the type of a file system through GetVolumeInformation.
  13. //
  14. // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationw
  15. func GetFileSystemType(path string) (fsType string, err error) {
  16. drive := filepath.VolumeName(path)
  17. if len(drive) != 2 {
  18. return "", ErrInvalidPath
  19. }
  20. buf := stringbuffer.NewWString()
  21. defer buf.Free()
  22. drive += `\`
  23. err = windows.GetVolumeInformation(windows.StringToUTF16Ptr(drive), nil, 0, nil, nil, nil, buf.Pointer(), buf.Cap())
  24. return buf.String(), err
  25. }