diff options
author | Bryan C. Mills <bcmills@google.com> | 2023-01-24 09:05:36 -0500 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-01-25 16:38:21 +0000 |
commit | 1d3088effdcb03612dd03eb24feee4aa92070a63 (patch) | |
tree | 999263a5f8baacb8de82797a9504115384315849 /src/os | |
parent | 7a5460ed9b53ac99805e6578cc67da8ef431c577 (diff) | |
download | go-git-1d3088effdcb03612dd03eb24feee4aa92070a63.tar.gz |
os: make Lstat for symlinks on Windows consistent with POSIX
This also makes path/filepath.Walk more consistent between
Windows and POSIX platforms.
According to
https://pubs.opengroup.org/onlinepubs/9699919799.2013edition/basedefs/V1_chap04.html#tag_04_12
symlinks in a path that includes a trailing slash must be resolved
before a function acts on that path.
POSIX defines an lstat function, whereas the Win32 API does not, so
Go's os.Lstat should follow the (defined) POSIX semantics instead of
doing something arbitrarily different.
CL 134195 added a test for the correct POSIX behavior when os.Lstat is
called on a symlink. However, the test turned out to be broken on Windows,
and when it was fixed (in CL 143578) it was fixed with different Lstat
behavior on Windows than on all other platforms that support symlinks.
In #50807 we are attempting to provide consistent symlink behavior for
cmd/go. This unnecessary platform difference, if left uncorrected,
will make that fix much more difficult.
CL 460595 reworked the implementation of Stat and Lstat on Windows,
and with the new implementation this fix is straightforward.
For #50807.
Updates #27225.
Change-Id: Ia28821aa4aab6cefa021da2d9b803506cdb2621b
Reviewed-on: https://go-review.googlesource.com/c/go/+/463177
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/os')
-rw-r--r-- | src/os/stat_test.go | 7 | ||||
-rw-r--r-- | src/os/stat_windows.go | 11 |
2 files changed, 11 insertions, 7 deletions
diff --git a/src/os/stat_test.go b/src/os/stat_test.go index 72621f257b..96019699aa 100644 --- a/src/os/stat_test.go +++ b/src/os/stat_test.go @@ -9,7 +9,6 @@ import ( "io/fs" "os" "path/filepath" - "runtime" "testing" ) @@ -279,11 +278,7 @@ func TestSymlinkWithTrailingSlash(t *testing.T) { } dirlinkWithSlash := dirlink + string(os.PathSeparator) - if runtime.GOOS == "windows" { - testSymlinkStats(t, dirlinkWithSlash, true) - } else { - testDirStats(t, dirlinkWithSlash) - } + testDirStats(t, dirlinkWithSlash) fi1, err := os.Stat(dir) if err != nil { diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go index 7ac9f7b860..033c3b9353 100644 --- a/src/os/stat_windows.go +++ b/src/os/stat_windows.go @@ -123,5 +123,14 @@ func statNolog(name string) (FileInfo, error) { // lstatNolog implements Lstat for Windows. func lstatNolog(name string) (FileInfo, error) { - return stat("Lstat", name, false) + followSymlinks := false + if name != "" && IsPathSeparator(name[len(name)-1]) { + // We try to implement POSIX semantics for Lstat path resolution + // (per https://pubs.opengroup.org/onlinepubs/9699919799.2013edition/basedefs/V1_chap04.html#tag_04_12): + // symlinks before the last separator in the path must be resolved. Since + // the last separator in this case follows the last path element, we should + // follow symlinks in the last path element. + followSymlinks = true + } + return stat("Lstat", name, followSymlinks) } |