github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/fileutils/path_helpers_win.go (about) 1 //go:build windows 2 // +build windows 3 4 package fileutils 5 6 import ( 7 "syscall" 8 9 "github.com/ActiveState/cli/internal/errs" 10 ) 11 12 // GetShortPathName returns the Windows short path (ie., DOS 8.3 notation) 13 func GetShortPathName(path string) (string, error) { 14 p, err := syscall.UTF16FromString(path) 15 if err != nil { 16 return "", errs.Wrap(err, "failed to convert path to UTF16") 17 } 18 b := p // GetShortPathName says we can reuse buffer 19 n, err := syscall.GetShortPathName(&p[0], &b[0], uint32(len(b))) 20 if err != nil { 21 return "", err 22 } 23 if n > uint32(len(b)) { 24 b = make([]uint16, n) 25 _, err = syscall.GetShortPathName(&p[0], &b[0], uint32(len(b))) 26 if err != nil { 27 return "", err 28 } 29 } 30 return syscall.UTF16ToString(b), nil 31 } 32 33 // GetLongPathName name returns the Windows long path (ie., DOS 8.3 notation is expanded) 34 func GetLongPathName(path string) (string, error) { 35 p, err := syscall.UTF16FromString(path) 36 if err != nil { 37 return "", errs.Wrap(err, "failed to convert path to UTF16") 38 } 39 b := p // GetLongPathName says we can reuse buffer 40 n, err := syscall.GetLongPathName(&p[0], &b[0], uint32(len(b))) 41 if err != nil { 42 return "", err 43 } 44 if n > uint32(len(b)) { 45 b = make([]uint16, n) 46 n, err = syscall.GetLongPathName(&p[0], &b[0], uint32(len(b))) 47 if err != nil { 48 return "", err 49 } 50 } 51 b = b[:n] 52 return syscall.UTF16ToString(b), nil 53 }