File: fs_windows.go

package info (click to toggle)
golang-github-microsoft-go-winio 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 764 kB
  • sloc: makefile: 3
file content (32 lines) | stat: -rw-r--r-- 899 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package fs

import (
	"errors"
	"path/filepath"

	"golang.org/x/sys/windows"

	"github.com/Microsoft/go-winio/internal/stringbuffer"
)

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://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationw
func GetFileSystemType(path string) (fsType string, err error) {
	drive := filepath.VolumeName(path)
	if len(drive) != 2 {
		return "", ErrInvalidPath
	}

	buf := stringbuffer.NewWString()
	defer buf.Free()

	drive += `\`
	err = windows.GetVolumeInformation(windows.StringToUTF16Ptr(drive), nil, 0, nil, nil, nil, buf.Pointer(), buf.Cap())
	return buf.String(), err
}