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

FileSystem: missing a function to construct an absolute path from a relative one #381

Open
cloudshiftchris opened this issue Sep 4, 2024 · 2 comments

Comments

@cloudshiftchris
Copy link

While SytemFileSystem.resolve is documented as throwing FileNotFoundException this behaviour seems unnecessary for the stated goal of resolving an absolute path.

Behaviour is inconsistent with java.io.File.absolutePath (and canonicalPath), neither of which fail on a non-existing file.

There doesn't appear to be any other mechanism to get an absolute path from a Path.

Proposing that SytemFileSystem.resolve does not throw FileNotFoundException, instead leaving it to consumers of the Path to handle existence in the context of using that path.

@fzhinkin fzhinkin added the fs label Sep 4, 2024
@fzhinkin
Copy link
Collaborator

fzhinkin commented Sep 4, 2024

@cloudshiftchris thanks for opening the issue!

There are three distinct operations on a file path:

  • canonicalization / normalization: remove all unnecessary (or trivially resolvable) .. and ., remove trailing path separator, etc. (that's what j.i.File::canonicalPath and j.n.f.Path::normalize do)
  • convert a relative path to an absolute path (that's what j.i.File::absolutePath and j.n.f.Path::toAbsolutePath do)
  • and finally, there's a real path resolution (j.n.f.Path::toRealPath, k.i.f.FileSystem::resolve) that does both previous operations and also resolves symbolic links along the way.

First two operations are abstract and work with a path that might point to a nonexistent file.
But the last one works only with path that actually points somewhere.

So there's nothing wrong with FNFE being thrown from resolve. The problem is missing normalization function (#223) and toAbstractPath analogue.

@cloudshiftchris
Copy link
Author

Gotcha. makes sense, thank you for clarifying.

As a workaround using this:

internal val Path.absolutePath: Path
    get() = File(this.toString()).canonicalFile.asPath()

internal fun File.asPath(): Path = Path(this.toString())

@fzhinkin fzhinkin changed the title SystemFileSystem.resolve throws FileNotFoundException FileSystem: missing a function to construct an absolute path from a relative one Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants