mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-09-05 16:47:31 -04:00
Add fs.ResolvePath to resolve symbolic links (#275)
* Add `fs.ResolvePath` to resolve symbolic links `filepath.EvalSymlinks` does not work well on Windows, and can enter infinite loops in certain situations and error out. Use Win32 API GetFinalPathNameByHandle to handle path resolution. Implementation based off on: https://github.com/containerd/containerd/pull/5411 Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com> * PR: types, documentation Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com> * remove unneded constant groups Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com> * Attempt normalized path first Update logic to try querying for normalized path initially, then use opened path if access is denied. Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com> --------- Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
//go:build windows
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func Test_GetFinalPathNameByHandle(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
// open f via a relative path
|
||||
name := t.Name() + ".txt"
|
||||
fullPath := filepath.Join(d, name)
|
||||
|
||||
w, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("could not get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(d); err != nil {
|
||||
t.Fatalf("could not chdir to %s: %v", d, err)
|
||||
}
|
||||
defer os.Chdir(w) //nolint:errcheck
|
||||
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
t.Fatalf("could not open %s: %v", fullPath, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
path, err := GetFinalPathNameByHandle(windows.Handle(f.Fd()), GetFinalPathDefaultFlag)
|
||||
if err != nil {
|
||||
t.Fatalf("could not get final path for %s: %v", fullPath, err)
|
||||
}
|
||||
if strings.EqualFold(fullPath, path) {
|
||||
t.Fatalf("expected %s, got %s", fullPath, path)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user