aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath
diff options
context:
space:
mode:
authorqmuntal <quimmuntal@gmail.com>2024-02-28 16:06:04 +0100
committerQuim Muntal <quimmuntal@gmail.com>2024-03-04 18:59:50 +0000
commit7986e26a39e9df870886a9933107372f4e16ea4c (patch)
treedcda4d454d0487d63d6a4902042aaf71751fd9ba /src/path/filepath
parent90796f44d5d10163196c6116ae667eddba8b06c0 (diff)
downloadgo-7986e26a39e9df870886a9933107372f4e16ea4c.tar.xz
os: don't treat mount points as symbolic links
This CL changes the behavior of os.Lstat to stop setting the os.ModeSymlink type mode bit for mount points on Windows. As a result, filepath.EvalSymlinks no longer evaluates mount points, which was the cause of many inconsistencies and bugs. Additionally, os.Lstat starts setting the os.ModeIrregular type mode bit for all reparse tags on Windows, except for those that are explicitly supported by the os package, which, since this CL, doesn't include mount points. This helps to identify files that need special handling outside of the os package. This behavior is controlled by the `winsymlink` GODEBUG setting. For Go 1.23, it defaults to `winsymlink=1`. Previous versions default to `winsymlink=0`. Fixes #39786 Fixes #40176 Fixes #61893 Updates #63703 Updates #40180 Updates #63429 Cq-Include-Trybots: luci.golang.try:gotip-windows-amd64-longtest,gotip-windows-arm64 Change-Id: I2e7372ab8862f5062667d30db6958d972bce5407 Reviewed-on: https://go-review.googlesource.com/c/go/+/565136 Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/path/filepath')
-rw-r--r--src/path/filepath/path_test.go13
-rw-r--r--src/path/filepath/path_windows_test.go117
2 files changed, 127 insertions, 3 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index 1b2a66bc6d..8a66538f6a 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -1980,3 +1980,16 @@ func TestEscaping(t *testing.T) {
}
}
}
+
+func TestEvalSymlinksTooManyLinks(t *testing.T) {
+ testenv.MustHaveSymlink(t)
+ dir := filepath.Join(t.TempDir(), "dir")
+ err := os.Symlink(dir, dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = filepath.EvalSymlinks(dir)
+ if err == nil {
+ t.Fatal("expected error, got nil")
+ }
+}
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index 42aeb4f619..524b0d0f92 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -7,6 +7,7 @@ package filepath_test
import (
"flag"
"fmt"
+ "internal/godebug"
"internal/testenv"
"io/fs"
"os"
@@ -486,6 +487,109 @@ func TestWalkDirectorySymlink(t *testing.T) {
testWalkMklink(t, "D")
}
+func createMountPartition(t *testing.T, vhd string, args string) []byte {
+ testenv.MustHaveExecPath(t, "powershell")
+ t.Cleanup(func() {
+ cmd := testenv.Command(t, "powershell", "-Command", fmt.Sprintf("Dismount-VHD %q", vhd))
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ if t.Skipped() {
+ // Probably failed to dismount because we never mounted it in
+ // the first place. Log the error, but ignore it.
+ t.Logf("%v: %v (skipped)\n%s", cmd, err, out)
+ } else {
+ // Something went wrong, and we don't want to leave dangling VHDs.
+ // Better to fail the test than to just log the error and continue.
+ t.Errorf("%v: %v\n%s", cmd, err, out)
+ }
+ }
+ })
+
+ script := filepath.Join(t.TempDir(), "test.ps1")
+ cmd := strings.Join([]string{
+ "$ErrorActionPreference = \"Stop\"",
+ fmt.Sprintf("$vhd = New-VHD -Path %q -SizeBytes 3MB -Fixed", vhd),
+ "$vhd | Mount-VHD",
+ fmt.Sprintf("$vhd = Get-VHD %q", vhd),
+ "$vhd | Get-Disk | Initialize-Disk -PartitionStyle GPT",
+ "$part = $vhd | Get-Disk | New-Partition -UseMaximumSize -AssignDriveLetter:$false",
+ "$vol = $part | Format-Volume -FileSystem NTFS",
+ args,
+ }, "\n")
+
+ err := os.WriteFile(script, []byte(cmd), 0666)
+ if err != nil {
+ t.Fatal(err)
+ }
+ output, err := testenv.Command(t, "powershell", "-File", script).CombinedOutput()
+ if err != nil {
+ // This can happen if Hyper-V is not installed or enabled.
+ t.Skip("skipping test because failed to create VHD: ", err, string(output))
+ }
+ return output
+}
+
+var winsymlink = godebug.New("winsymlink")
+
+func TestEvalSymlinksJunctionToVolumeID(t *testing.T) {
+ // Test that EvalSymlinks resolves a directory junction which
+ // is mapped to volumeID (instead of drive letter). See go.dev/issue/39786.
+ if winsymlink.Value() == "0" {
+ t.Skip("skipping test because winsymlink is not enabled")
+ }
+ t.Parallel()
+
+ output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output()
+ if !strings.Contains(string(output), " /J ") {
+ t.Skip("skipping test because mklink command does not support junctions")
+ }
+
+ tmpdir := tempDirCanonical(t)
+ vhd := filepath.Join(tmpdir, "Test.vhdx")
+ output = createMountPartition(t, vhd, "Write-Host $vol.Path -NoNewline")
+ vol := string(output)
+
+ dirlink := filepath.Join(tmpdir, "dirlink")
+ output, err := testenv.Command(t, "cmd", "/c", "mklink", "/J", dirlink, vol).CombinedOutput()
+ if err != nil {
+ t.Fatalf("failed to run mklink %v %v: %v %q", dirlink, vol, err, output)
+ }
+ got, err := filepath.EvalSymlinks(dirlink)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got != dirlink {
+ t.Errorf(`EvalSymlinks(%q): got %q, want %q`, dirlink, got, dirlink)
+ }
+}
+
+func TestEvalSymlinksMountPointRecursion(t *testing.T) {
+ // Test that EvalSymlinks doesn't follow recursive mount points.
+ // See go.dev/issue/40176.
+ if winsymlink.Value() == "0" {
+ t.Skip("skipping test because winsymlink is not enabled")
+ }
+ t.Parallel()
+
+ tmpdir := tempDirCanonical(t)
+ dirlink := filepath.Join(tmpdir, "dirlink")
+ err := os.Mkdir(dirlink, 0755)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ vhd := filepath.Join(tmpdir, "Test.vhdx")
+ createMountPartition(t, vhd, fmt.Sprintf("$part | Add-PartitionAccessPath -AccessPath %q\n", dirlink))
+
+ got, err := filepath.EvalSymlinks(dirlink)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got != dirlink {
+ t.Errorf(`EvalSymlinks(%q): got %q, want %q`, dirlink, got, dirlink)
+ }
+}
+
func TestNTNamespaceSymlink(t *testing.T) {
output, _ := exec.Command("cmd", "/c", "mklink", "/?").Output()
if !strings.Contains(string(output), " /J ") {
@@ -511,7 +615,13 @@ func TestNTNamespaceSymlink(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if want := vol + `\`; got != want {
+ var want string
+ if winsymlink.Value() == "0" {
+ want = vol + `\`
+ } else {
+ want = dirlink
+ }
+ if got != want {
t.Errorf(`EvalSymlinks(%q): got %q, want %q`, dirlink, got, want)
}
@@ -524,7 +634,7 @@ func TestNTNamespaceSymlink(t *testing.T) {
t.Fatal(err)
}
- target += file[len(filepath.VolumeName(file)):]
+ target = filepath.Join(target, file[len(filepath.VolumeName(file)):])
filelink := filepath.Join(tmpdir, "filelink")
output, err = exec.Command("cmd", "/c", "mklink", filelink, target).CombinedOutput()
@@ -536,7 +646,8 @@ func TestNTNamespaceSymlink(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if want := file; got != want {
+ want = file
+ if got != want {
t.Errorf(`EvalSymlinks(%q): got %q, want %q`, filelink, got, want)
}
}