Yo I'm trying to get the target of a symlink in po...
# kotlin-native
b
Yo I'm trying to get the target of a symlink in posix. I'm trying
realpath
(platform.posix.realpath) but for some reason, a
private
prefix is added on the resolved path. Any idea why that could be? Code is something like that
Copy code
val fullpath = realpath(path.toString(), null)
        try {
          symlinkTarget = Buffer().writeNullTerminated(fullpath).toPath()
        } finally {
          free(fullpath)
        }
And my test fails with
Copy code
kotlin.AssertionError: Expected </base/symlink-target>, actual </private/base/symlink-target>.
@jessewilson if you know
e
what platform? if macos, lots of things (/etc /tmp /var) are actually in /private
b
macos64 indeed
The problem is though that to get the base I do
getenv("TMPDIR")
which points to something like
/var/folders/vc/5dwj5pms6s97y5snf5t4042c0000gn/T/
. Why would
realpath
adds a prefix here?
e
because the real path is really in /private
b
/var/folders/vc/5dwj5pms6s97y5snf5t4042c0000gn/T/
looks like an absolute path though. Is there a way to use realpath, or another tool to stay within the working directory?
e
absolute path != real path
if you
ls -l /var
you'll see
/var -> /private/var
what do you mean by "stay within the working directory"?
b
Maybe my test should use the realpath as well. Right now, my tests are doing something like • create symlink
/var/base/source
which points to
/var/base/target
• assert that the symlink target of
/var/base/source
is
/var/base/target
but it fails because of the
/private
being added.
That's funny,
private
has no slash in front
Copy code
$ ls -ahl /var
lrwxr-xr-x 1 root wheel 11 Jan  1  2020 /var -> private/var/
e
it amounts to the same thing
I'm unclear on why you'd be testing that…
b
Because we're creating a multiplatform filesystem
u
so the issue is, realpath resolves all symlinks in your path. you want to just resolve the symlink that you are mentioning in the call Maybe java.nio.file.Files.readSymbolicLink is what you want
b
platform.posix.readlink
did the job well for me eventually