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

Calling Path.toUri() on a directory should return a URI with a trailing slash #16

Closed
seanrohead opened this issue Jan 12, 2015 · 2 comments
Labels
fixed type=enhancement Make an existing feature better
Milestone

Comments

@seanrohead
Copy link

The default file system provider returns URI's with trailing slashes if the URI is known to refer to a directory. This only way to know for sure if a URI refers to a directory or a file is if the directory actually exists on disk. For example:

    Path rootPath = Paths.get(URI.create("file:/"));
    System.out.println(rootPath.toUri());
    Path dir1 = rootPath.resolve("dir1");
    System.out.println(dir1.toUri());
    Path dir1Path = Files.createDirectory(dir1);
    System.out.println(dir1.toUri());
    System.out.println(dir1Path.toUri());

If C:\dir1\ does not exist before running this code, the result will be:

file:///C:/
file:///C:/dir1
file:///C:/dir1/
file:///C:/dir1/

Notice that dir1.toUri() does not contain a trailing slash before the directory is created, but it does contain a trailing slash afterward.

I have verified this same behavior on OSX as well.

@cgdecker
Copy link
Member

Hmm... curious. I'm surprised (assuming this is what it does) that the default file system would take the time to stat a file when creating a URI for it. I would expect the URI for a path to be independent of the file's actual status on the file system (as most things on Path are, with the exception of toRealPath() primarily), particularly given that the status can change at any time.

@cgdecker cgdecker added the type=enhancement Make an existing feature better label Jan 12, 2015
cgdecker added a commit that referenced this issue Feb 26, 2015
…ile is a directory.

See this issue: #16

I was initially reluctant to do this because it seems strange to actually access the file system to convert a path to a URI, but this does in fact seem to be the behavior for actual Unix paths. Additionally, when looking into creating a ClassLoader that takes Path objects for the classpath, I noticed that URLClassLoader treats URLs that end with / differently (as directories) from those that don't, which indicates that there are real consequences for not doing this. Note: URLClassLoader is tied to the default file system via File/JarFile by default, so it's not like this change actually enables it to just work with Jimfs files (nor do I have any real reason to think you'd even want to do that, I was just curious).

This involves changing the PathType API to take a boolean parameter indicating whether or not the file is a directory in its method for getting a URI path, which makes this an incompatible change that would probably require a 2.0 release (not that I expect anyone's actually implementing their own PathType).
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=87278575
@cgdecker
Copy link
Member

Fixed in c1a7fcd.

@cgdecker cgdecker added the fixed label Feb 26, 2015
@cgdecker cgdecker added this to the 1.1 milestone Jan 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

2 participants