Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: SameFile should not follow symlinks on windows #21854

Closed
hirochachacha opened this issue Sep 13, 2017 · 10 comments
Closed

os: SameFile should not follow symlinks on windows #21854

hirochachacha opened this issue Sep 13, 2017 · 10 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@hirochachacha
Copy link
Contributor

Please answer these questions before submitting your issue. Thanks!

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

package main

import (
	"fmt"
	"os"
)

func main() {
	f, err := os.Create("target")
	if err != nil {
		panic(err)
	}
	err = f.Close()
	if err != nil {
		panic(err)
	}
	defer os.Remove("target")
	err = os.Symlink("target", "symlink")
	if err != nil {
		panic(err)
	}
	defer os.Remove("symlink")
	st1, err := os.Lstat("symlink")
	if err != nil {
		panic(err)
	}
	st2, err := os.Lstat("target")
	if err != nil {
		panic(err)
	}
	fmt.Println(os.SameFile(st1, st2))
}

What did you expect to see?

false

What did you see instead?

true

Does this issue reproduce with the latest release (go1.9)?

I think so.

System details

go version go1.8 windows/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=".exe"
GOHOSTARCH="amd64"
GOHOSTOS="windows"
GOOS="windows"
GOPATH="C:\Users\Hiroshi\gopath"
GORACE=""
GOROOT="C:\Go"
GOTOOLDIR="C:\Go\pkg\tool\windows_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Hiroshi\AppData\Local\Temp\go-build553453167=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
GOROOT/bin/go version: go version go1.8 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.8 X:framepointer
gdb --version: GNU gdb (GDB) 7.12
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Sep 13, 2017
@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Sep 13, 2017
@ianlancetaylor
Copy link
Member

CC @alexbrainman

@alexbrainman
Copy link
Member

Why do you think your program should return false?

I read os.SameFile documentation - "... SameFile only applies to results returned by this package's Stat. It returns false in other cases.". Does that mean os.SameFile should return false if it is given result returned by os.Lstat(path) (regardless what path is) ?

Alex

@hirochachacha
Copy link
Contributor Author

I think that is a documentation bug.
Here is the unix implementation:

func sameFile(fs1, fs2 *fileStat) bool {
	return fs1.sys.Dev == fs2.sys.Dev && fs1.sys.Ino == fs2.sys.Ino
}

@alexbrainman
Copy link
Member

Here is the unix implementation:

Unix implementation is irrelevant, because we don't have API that does not follow symlinks.

Also meaning of "symlinks" is different on Windows. We had to include everything that is not file or directory on Windows in that category. For example, I have U:\ drive on my computer that maps to some share on another computer. Should os.SameFile(os.Lstat(U:\a), os.Lstat(\\another_computer\share\a)) return false or true? I think it should return true. But true is discovered by following from U: into \another_computer\share. Do you agree with me that os.SameFile(os.Lstat(U:\a), os.Lstat(\\another_computer\share\a)) should return true? If we agree to that, maybe we should agree that os.SameFile should do the same with symlinks.

Alex

@hirochachacha
Copy link
Contributor Author

Yes, os.SameFile(os.Lstat(U:\a), os.Lstat(\\another_computer\share\a)) should be true. I agree.
But following symlinks is necessary? I didn't test it yet. I'll try it.

@hirochachacha
Copy link
Contributor Author

I don't think NTFS is so special. It is based on computer science like other filesystems and share basic concepts.
I think the term 'file' in os.SameFile means a entry of the filesystem.
If two filepaths point the same filesystem entry, os.SameFile should be true, if not should be false.
I don't understand the reason we need to think in more complex ways.

@alexbrainman
Copy link
Member

If two filepaths point the same filesystem entry, os.SameFile should be true, if not should be false.

@hirochachacha I do not know what "filesystem entry" is. But if you know how to fix this, go ahead. I won't be working on this.

Alex

@hirochachacha
Copy link
Contributor Author

Thanks, will do.

@odeke-em
Copy link
Member

odeke-em commented Dec 6, 2017

Gentle ping, how's it going @hirochachacha?

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/134195 mentions this issue: os: use FILE_FLAG_OPEN_REPARSE_POINT in SameFile

@golang golang locked and limited conversation to collaborators Sep 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

5 participants