131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func ResolveDirectoryVolume(dir string) (DirectoryVolume, error) {
|
|
if invalidDirectoryPath(dir) {
|
|
return DirectoryVolume{}, os.ErrInvalid
|
|
}
|
|
abs, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
p, err := windows.UTF16PtrFromString(`\\?\` + abs)
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
handle, err := windows.CreateFile(p, windows.FILE_READ_ATTRIBUTES,
|
|
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, nil,
|
|
windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
defer windows.CloseHandle(handle)
|
|
var info windows.ByHandleFileInformation
|
|
if err := windows.GetFileInformationByHandle(handle, &info); err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
if info.FileAttributes&windows.FILE_ATTRIBUTE_DIRECTORY == 0 {
|
|
return DirectoryVolume{}, os.ErrInvalid
|
|
}
|
|
|
|
return directoryHandleVolume(handle, p)
|
|
}
|
|
|
|
// ResolveOpenDirectoryVolume resolves the directory backing an already opened root.
|
|
func ResolveOpenDirectoryVolume(dir string, opened *os.File) (DirectoryVolume, error) {
|
|
p, err := windows.UTF16PtrFromString(`\\?\` + dir)
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
return directoryHandleVolume(windows.Handle(opened.Fd()), p)
|
|
}
|
|
|
|
func directoryHandleVolume(handle windows.Handle, p *uint16) (DirectoryVolume, error) {
|
|
final, err := finalDirectoryPath(handle, 0) // VOLUME_NAME_DOS
|
|
if err != nil && !errors.Is(err, windows.ERROR_PATH_NOT_FOUND) {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
if strings.HasPrefix(strings.ToUpper(final), `\\?\UNC\`) {
|
|
return DirectoryVolume{}, os.ErrInvalid
|
|
}
|
|
if final != "" {
|
|
p, err = windows.UTF16PtrFromString(final)
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
}
|
|
mount := make([]uint16, 32768)
|
|
if err := windows.GetVolumePathName(p, &mount[0], uint32(len(mount))); err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
var volume [50]uint16
|
|
if err := windows.GetVolumeNameForVolumeMountPoint(&mount[0], &volume[0], uint32(len(volume))); err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
guid := windows.UTF16ToString(volume[:])
|
|
guidPath, err := finalDirectoryPath(handle, 1) // VOLUME_NAME_GUID
|
|
if err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
devicePath := strings.TrimSuffix(guid, `\`)
|
|
guidPath = strings.TrimSuffix(guidPath, `\`)
|
|
if !strings.HasPrefix(strings.ToLower(guidPath)+`\`, strings.ToLower(guid)) {
|
|
return DirectoryVolume{}, os.ErrPermission
|
|
}
|
|
relative := strings.TrimLeft(guidPath[len(devicePath):], `\`)
|
|
canonical := strings.TrimPrefix(final, `\\?\`)
|
|
mountPath := strings.TrimPrefix(windows.UTF16ToString(mount), `\\?\`)
|
|
fixed := windows.GetDriveType(&mount[0]) == driveFixed
|
|
if canonical == "" {
|
|
// Use one canonical mount path even when the volume has several aliases.
|
|
var n uint32
|
|
if err := windows.GetVolumePathNamesForVolumeName(&volume[0], &mount[0], uint32(len(mount)), &n); err != nil {
|
|
return DirectoryVolume{}, err
|
|
}
|
|
for start := 0; start < int(n) && mount[start] != 0; {
|
|
end := start + slices.Index(mount[start:n], 0)
|
|
alias := windows.UTF16ToString(mount[start:end])
|
|
if strings.ToLower(alias) < strings.ToLower(mountPath) {
|
|
mountPath = alias
|
|
}
|
|
start = end + 1
|
|
}
|
|
canonical = filepath.Join(mountPath, relative)
|
|
}
|
|
letter := ""
|
|
if len(mountPath) == 3 {
|
|
letter = strings.ToUpper(mountPath[:1])
|
|
}
|
|
if fixed {
|
|
internal, known := volumeInternalFixed(devicePath)
|
|
fixed = known && internal
|
|
}
|
|
return DirectoryVolume{
|
|
CanonicalPath: canonical, VolumeGUID: guid, DriveLetter: letter,
|
|
FixedInternal: fixed, IsVolumeRoot: relative == "",
|
|
}, nil
|
|
}
|
|
|
|
func finalDirectoryPath(handle windows.Handle, flags uint32) (string, error) {
|
|
buf := make([]uint16, 32768)
|
|
n, err := windows.GetFinalPathNameByHandle(handle, &buf[0], uint32(len(buf)), flags)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if n >= uint32(len(buf)) {
|
|
return "", windows.ERROR_FILENAME_EXCED_RANGE
|
|
}
|
|
return windows.UTF16ToString(buf[:n]), nil
|
|
}
|