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

Feature request: Add “readlink” method for IO::Path for reading symlink target #5509

Open
unclechu opened this issue Jan 3, 2024 · 0 comments
Labels

Comments

@unclechu
Copy link

unclechu commented Jan 3, 2024

I would like to have an interface to read symlink’s target. There’s IO::Path::absolute but:

  1. It expands the path to the absolute one which is not necessarily what I want to do in every situation. Sometimes I just want to know what exactly a symlink’s target is. To be able to distinguish a relative from an absolute one.
  2. It follows all the symlinks down to the final real target. If I have a symlink like symlink-1 -> symlink-2 -> target, the target for symlink-1 is symlink-2 but .absolute returns an absolute path to target, not symlink-2.

I think you can just reuse nqp::readlink for the implementation? For now I can either make a readlink subprocess call or use this helper on top of nqp::readlink:

use nqp;
# ...
sub readlink(IO::Path:D \filePath, IO::Path :$CWD --> IO::Path:D) {
  IO::Path.new(nqp::readlink(filePath.relative), :CWD($CWD // filePath.dirname))
}

⚠️ To keep in mind when implementing: Symlink’s target (in case it’s a relative, non-absolute path) is relative to where the symlink file is placed. So when implementing it maybe makes sense to override the CWD to the directory of the original symlink path like I did in the helper above.

@ab5tract ab5tract added the IO label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants