Windows符号链接和目录之间的区别是什么?

huangapple go评论99阅读模式
英文:

Difference between windows symbolic links and directories

问题

我在使用Go时遇到了一个问题,无法区分Windows符号链接和目录。
我已经搜索过了,但只找到了这个链接:
https://github.com/golang/go/issues/3498#issuecomment-142810957

不幸的是,这个问题已经关闭,并且没有人在处理。

所以我的问题是,有没有任何解决方法?我尝试使用symlinks列出路径,但返回的结果与空目录相同。

在Python中,我可以这样做:

def test(dir):
    try:
        os.chdir(dir)
    except Exception as e:
        if "[Error 2]" in str(e):
            return False
        else:
            return True

有没有任何bash命令可以从Go中调用来检测它?

我已经没有更多的想法了 Windows符号链接和目录之间的区别是什么?

英文:

I run into a problem with Go when trying to tell difference between windows symbolic links and directories.
I've googled and all I could find was this:
https://github.com/golang/go/issues/3498#issuecomment-142810957

Which is unfortunately closed and not being worked on.

So my question is, is there any workaround? I tried to listdir the path with the symlinks but it is returning the same that it would return on an empty directory.

With python I was able to do something like this:

def test(dir):
    try:
        os.chdir(dir)
    except Exception as e:
        if "[Error 2]" in str(e):
            return False
        else:
            return True

Is there any bash command I could use to call from go to detect it?

I'm running out of ideas Windows符号链接和目录之间的区别是什么?

答案1

得分: 3

我只看到一个测试(我刚刚在Windows上使用go 1.5.1进行了测试),在os/os_test.go中:

fromstat, err = Lstat(from)
if err != nil {
	t.Fatalf("lstat %q failed: %v", from, err)
}
if fromstat.Mode()&ModeSymlink == 0 {
	t.Fatalf("symlink %q, %q did not create symlink", to, from)
}

它使用了os/#Lstat

> Lstat返回描述指定文件的FileInfo
如果文件是符号链接,则返回的FileInfo描述符号链接。Lstat不会尝试跟踪链接。
如果出现错误,它将是*PathError类型的。

您还可以获取同一文件夹的os.Stat(),然后调用os.Samefile()(如此测试中所示):

if !SameFile(tostat, fromstat) {
	t.Errorf("symlink %q, %q did not create symlink", to, from)
}
英文:

The only test I see (and I just tested it with go 1.5.1 on Windows) is in os/os_test.go:

fromstat, err = Lstat(from)
if err != nil {
	t.Fatalf("lstat %q failed: %v", from, err)
}
if fromstat.Mode()&ModeSymlink == 0 {
	t.Fatalf("symlink %q, %q did not create symlink", to, from)
}

It uses os/#Lstat:

> Lstat returns a FileInfo describing the named file.
If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link.
If there is an error, it will be of type *PathError.

You can also get os.Stat() of the same folder, and then call os.Samefile() (as in this test):

if !SameFile(tostat, fromstat) {
	t.Errorf("symlink %q, %q did not create symlink", to, from)
}

huangapple
  • 本文由 发表于 2015年9月24日 13:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/32753907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定