dtqf81594 2015-09-24 05:21
浏览 306
已采纳

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 :(

  • 写回答

1条回答 默认 最新

  • duanbi3786 2015-09-24 06:01
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路