aboutsummaryrefslogtreecommitdiff
path: root/src/os/stat_test.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2023-01-04 17:23:46 -0500
committerGopher Robot <gobot@golang.org>2023-01-23 22:46:55 +0000
commit3e44b7d07a7b3c6233eb1bf4cf3cb00a0b85adec (patch)
tree7d404b995c7cba691854cd313f993e0e38987c50 /src/os/stat_test.go
parent2423370136d4b1915d06bb1aaacbedaa900bc5c7 (diff)
downloadgo-3e44b7d07a7b3c6233eb1bf4cf3cb00a0b85adec.tar.xz
os: treat non-symlink reparse points as irregular files
Prior to this change (as of CL 143578), our stat function attempted to resolve all reparse points as if they were symlinks. This results in an additional call to CreateFile when statting a symlink file: we use CreateFile once to obtain the reparse tag and check whether the file is actually a symlink, and if it is we call CreateFile again without FILE_FLAG_OPEN_REPARSE_POINT to stat the link target. Fortunately, since symlinks are rare on Windows that overhead shouldn't be a big deal in practice. Fixes #42919. Change-Id: If453930c6e98040cd6525ac4aea60a84498c9579 Reviewed-on: https://go-review.googlesource.com/c/go/+/460595 Reviewed-by: Quim Muntal <quimmuntal@gmail.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/os/stat_test.go')
-rw-r--r--src/os/stat_test.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/os/stat_test.go b/src/os/stat_test.go
index c876d434fd..72621f257b 100644
--- a/src/os/stat_test.go
+++ b/src/os/stat_test.go
@@ -182,6 +182,31 @@ func testSymlinkSameFile(t *testing.T, path, link string) {
}
}
+func testSymlinkSameFileOpen(t *testing.T, link string) {
+ f, err := os.Open(link)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ defer f.Close()
+
+ fi, err := f.Stat()
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ fi2, err := os.Stat(link)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ if !os.SameFile(fi, fi2) {
+ t.Errorf("os.Open(%q).Stat() and os.Stat(%q) are not the same file", link, link)
+ }
+}
+
func TestDirAndSymlinkStats(t *testing.T) {
testenv.MustHaveSymlink(t)
t.Parallel()
@@ -199,6 +224,7 @@ func TestDirAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, dirlink, true)
testSymlinkSameFile(t, dir, dirlink)
+ testSymlinkSameFileOpen(t, dirlink)
linklink := filepath.Join(tmpdir, "linklink")
if err := os.Symlink(dirlink, linklink); err != nil {
@@ -206,6 +232,7 @@ func TestDirAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, linklink, true)
testSymlinkSameFile(t, dir, linklink)
+ testSymlinkSameFileOpen(t, linklink)
}
func TestFileAndSymlinkStats(t *testing.T) {
@@ -225,6 +252,7 @@ func TestFileAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, filelink, false)
testSymlinkSameFile(t, file, filelink)
+ testSymlinkSameFileOpen(t, filelink)
linklink := filepath.Join(tmpdir, "linklink")
if err := os.Symlink(filelink, linklink); err != nil {
@@ -232,6 +260,7 @@ func TestFileAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, linklink, false)
testSymlinkSameFile(t, file, linklink)
+ testSymlinkSameFileOpen(t, linklink)
}
// see issue 27225 for details