2
votes

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

1 Answers

3
votes

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)
}