Skip to content

Modify FileUtil.toPath to never return null - #9820

Open
jtulach wants to merge 10 commits into
apache:masterfrom
jtulach:jtulach/FileObjectAndPath
Open

jtulach wants to merge 10 commits into
apache:masterfrom
jtulach:jtulach/FileObjectAndPath

Conversation

@jtulach

@jtulach jtulach commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor
  • in Made FileUtil to copy POSIX permissions #4654 @lkishalmi added FileUtil.toPath
    • however it works only for LocalFileSystem
  • this PR enhances it to work for any FileObject
    • e.g. change the method to never return null
  • create a shared test suite - done in 1e50301
    • that's the TCK
  • use it in LocalFileSystemTest and make it pass - done in 1e50301
    • the TCK runs on regular OS NIO Path implementation
    • that guarantees compatibility of the TCK with JDK implementation of NIO Path
  • expand the test suite to cover more Path <-> FileObject interactions
    • this is kinda never ending task, but I consider it done now
    • the TCK covers really huge set of functionality
    • inspiration for some of the tests was taken from JimFs project that solves similar goal
    • the JimFs project is Apache Licensed and that makes inspiration taking safe
  • use the same test suite for masterfs - in 9b03fd0 - they already pass
  • use the same test suite for other filesystems - done in 283aee0
    • this guarantees compatibility of the default implementation for virtual file objects with the TCK
  • make it all pass - fallback implementation provided in bc3c106
  • document the (behavioral) API change - done in 5319023
  • verify FileTypeDetector works in NetBeans applications and its classloaders - fixed in 2cfbe32

I was using VSCode NetBeans extension and VSCode Copilot (Luna model) when coding this change request.

@jtulach jtulach self-assigned this Nov 21, 2026
@jtulach jtulach added the Platform [ci] enable platform tests (platform/*) label Nov 21, 2026
@jtulach
jtulach marked this pull request as draft November 21, 2026 19:56

@eppleton eppleton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making toPath work for every FileObject sounds useful, and the shared PathTestHid suite is a good base. Some observations from reading the code and checking the JDK and bootstrap sources; I didn't build or run the PR.

Most of the points below share one root: every PathTestHid test derives all paths from a single toPath() call, and on the fallback (non-disk) side the suite currently only runs on JarFileSystem, which is read-only. So equality across separate toPath() calls, writable virtual filesystems (MemoryFileSystem, MultiFileSystem/layers) and watch events are not exercised yet. Your "other filesystems" checklist item would catch several of these.

On the API side: toPath on master is only used from tests, so the new "never null" contract shouldn't break anything in the repo, but platform applications may use null as "not on disk". An apichanges.xml entry and a spec version bump would make that visible (I see it's on your checklist).

@jtulach jtulach changed the title More tests for existing FileUtil.toPath Modify FileUtil.toPath to never return null Nov 21, 2026
@jtulach
jtulach marked this pull request as ready for review November 21, 2026 22:10
@jtulach
jtulach requested a review from eppleton November 22, 2026 21:12
*/
public static Path toPath(FileObject fo) {
Parameters.notNull("fo", fo);
File f = toFile(fo);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check for (Path) fo.getAttribute("java.nio.Path") first, and make trivial change to masterfs to return directly? Could allow for better optimization from file system implementations in future and/or option to switch in nio2 based memory and zip filesystems.

Is conversion via attribute documented?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • toFile(fo) check was put here by @lkishalmi and I am trying to not modify it.
  • it works fine for LocalFileSystem and masterfs to provide the underlaying Path implementations
  • it works less well for MultiFileSystem which can also have underlaying impl
    • there I will have to disable the delegation
    • but that can be done locally inside the module now ...
    • ... without any visible API

Is conversion via attribute documented?

  • probably it is not
  • it should be listed in this table

check for (Path) fo.getAttribute("java.nio.Path")

  • not in this PR (unless necessary), but for the future I'd like to do something bigger:
  • let us add FileObject implements Path!
  • then any FileObject will immediately be usable with any JDK path oriented API

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be listed in this table

Yes, I checked there before commenting. Would have thought there and/or in the Javadoc for the method?

...for the future I'd like to do something bigger:
let us add FileObject implements Path!

Every FileObject having a Path sounds great. Every FileObject being a Path might not be so good an idea. Paths from the same provider are special cased (eg. in Files::copy and the providers expect to be able to cast to implementation (eg. UnixPath). That means that a FileObject from masterfs would bypass the optimal same-provider pathways.

@neilcsmith-net

neilcsmith-net commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Thanks! Good changes in the right direction. We need to look at replacing uses of File to Path in various places - eg. replacing uses of File::getCanonicalPath with Path::toRealPath because of #8611 (EDIT : upstream JDK issue might have been fixed there now)

@eppleton eppleton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the new tests and the fixes to relative paths. getRoot/getParent returning null for relative paths, normalize keeping leading .., and resolve(String) honouring absolute arguments all match the Path contract now. The WatchService close handling also looks right to me: the close marker is passed on so every blocked take() wakes up.

A few notes on the new FileChannel emulation, from reading the code (I haven't run it); details inline. One earlier point is still open: toUri() still calls fileObject.toURI() without a null check (see my reply in the thread on NioPathForFileObject).

@jtulach
jtulach force-pushed the jtulach/FileObjectAndPath branch from 078b1cd to dc6a31a Compare November 24, 2026 15:59
@jtulach

jtulach commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

There are failures in masters tests on four platforms:

[junit] Deleting the watched directory must signal its key expected same:sun.nio.fs.LinuxWatchService$LinuxWatchKey@5d00411a was not:

@jtulach

jtulach commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

toUri() still calls fileObject.toURI() without a null check

@eppleton

Copy link
Copy Markdown
Contributor

Thanks for the rework. I re-read the channel code at 61f31e8:

  • Quadratic reads/writes: resolved by bd0b6a5. The content is read once into memory, and writes are flushed on force/close only. The Javadoc now says so explicitly, including that other FileObject readers won't see the changes until then.
  • force: resolved; it now flushes pending content.
  • Two write paths: resolved by 2abc58e. newByteChannel now delegates to NioFileObjectChannel.open, so TRUNCATE_EXISTING/APPEND/CREATE_NEW and locking behave the same for both.
  • Shared State.class monitor: understood. With no foreign code under the lock and the I/O done outside it, I agree it's not a practical problem.

Two points from earlier are still open:

  1. NioPathForFileObject.toUri() still calls fileObject.toURI() without a null check (I saw your note above).
  2. The question in the NioFileTypeDetector thread: NbFileTypeDetector lives in core/core.jar, which a normal nbexec launch doesn't put on the system classpath that Files.probeContentType uses. It would be good to confirm it works in a launched IDE, not only under NbModuleSuite.

@jtulach

jtulach commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Two points from earlier are still open:

  1. NioPathForFileObject.toUri() still calls fileObject.toURI() without a null check (I saw your note above).

Done in 674cb63

  1. The question in the NioFileTypeDetector thread: NbFileTypeDetector lives in core/core.jar, which a normal

Amazing catch. I wrote an always enabled action, did the check there and yes, the FileTypeDetector delegation wasn't working in the IDE. Fixed in ba61f03

I consider this done, I am hoping for the approval, @eppleton & co. The plan is to squash and integrated ~Dec 1.

@eppleton eppleton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both remaining points are resolved: toUri() now works for paths without a FileObject (674cb63), and moving NbFileTypeDetector to o.n.bootstrap puts it on the system classpath that Files.probeContentType uses (ba61f03). Good that you verified it in the running IDE. Approving.

@jtulach
jtulach force-pushed the jtulach/FileObjectAndPath branch from ba61f03 to 6626bc2 Compare November 27, 2026 20:38
if (file != null && file.isValid() && write && options.contains(StandardOpenOption.CREATE_NEW)) {
throw new FileAlreadyExistsException(path.toString());
}
if (file == null || !file.isValid()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the specification for StandardOpenOptions.CREATE says:

The check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations.

Here the existence check is file == null || !file.isValid() - but eventual createData happens non-atomically. So another thread may eventually create the file in between.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you suggest to do it? We are calling into unknown implementation of the filesystem and the filesystem API doesn't seem to have this kind f atomic guarantees...

this.nioLock = fileLock == null ? null : new NioLock(this, fileLock);
this.readable = readable;
this.append = append;
ByteBuffer content = ByteBuffer.wrap(file.asBytes());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I don't like much sucking entire file content to a byte[] here. Very long files cannot be really accessed through j.n.Files.newByteChannel (even to read a header/preamble !).

But it is documented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • there is not much to do about it given the current filesystem API
  • but I can imagine we could add interface AbstractFileSystem.Channel
    • to allow the filesystem to handle this more effectively (via RandomAccess, or co.)
  • but that's for future improvements, not for this PR

@jtulach

jtulach commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author
  • thanks a lot for your review, Sváťo!
  • I believe I processed all your comments
  • the code is better than it was (at least in some aspects)!
  • I'd like to integrate tomorrow - if we get green even after squashing

@jtulach
jtulach requested a review from sdedic November 30, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Need Squashing Platform [ci] enable platform tests (platform/*)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants