Skip to content

Modify FileUtil.toPath to never return null - #9820

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

jtulach wants to merge 34 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
  • I want to enhance it to work for any FileObject
  • e.g. change the method to never return null
  • create a shared test suite - done in 1e50301
  • use it in LocalFileSystemTest and make it pass - done in 1e50301
  • expand the test suite to cover more Path <-> FileObject interactions
    • this is kinda never ending task, so spell you wish of what shall be tested
  • use the same test suite for masterfs - in 9b03fd0 - they already pass
  • use the same test suite for other filesystems - done in 283aee0
  • 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

@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).

return count;
}

private void update(long at, ByteBuffer src, long truncate) throws IOException {

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.

Both directions are quadratic in the file size.

  • Writes: every write calls file.asBytes(), copies the whole content and writes the whole file back through getOutputStream. Writing a 10 MB file in 8 KB chunks rewrites the file about 1,250 times, and each rewrite fires a fileChanged event to every listener, including the new WatchService.
  • Reads: readImpl (line 192) opens a new InputStream for every read and skipNBytes to the position, reading at most 8 KB. For an entry in a JAR filesystem, that means inflating the entry again from the start for each chunk.

The Javadoc already says the virtual channel holds the FileObject lock until close. Keeping the content in memory while the channel is open and writing it once on close (and on force) would match that model. Keeping an open stream for sequential reads would avoid re-skipping.

}


private record State(long position, Thread operationOwner) {

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.

All state transitions synchronize and wait/notifyAll on State.class, which is one monitor shared by every NioFileObjectChannel in the JVM. Unrelated channels on different files therefore contend on the same lock, and every finished operation wakes all waiters of all channels. A monitor per channel (e.g. the channel instance or a private lock object) would keep the same semantics without the global coupling. It would probably also allow a simpler scheme than the snapshot/retry logic in completeRead, which fits the "less moving pieces" direction of the recent commits.

@Override
public void force(boolean metaData) throws IOException {
checkOpen();
throw new UnsupportedOperationException("Forcing virtual files to storage is not supported");

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.

FileChannel.force only declares IOException, and callers that write and then force(true) (a common pattern for safe saves, e.g. in Lucene) don't expect UnsupportedOperationException. Since every write is already handed to the FileObject immediately, force can simply return after checkOpen(). If writes get buffered as suggested above, force would be the place to flush them.

public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws IOException {
checkOpen();
if (fileLock == null) {
throw new NonWritableChannelException();

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.

For a read-only channel this throws NonWritableChannelException even when shared == true. The JDK only requires a writable channel for exclusive locks; a shared lock on a readable channel is allowed. The new Javadoc in FileUtil only describes the writable case, so either return a (no-op) shared lock here or document that read-only virtual channels cannot be locked.

throw new NoSuchFileException(path.toString());
}
}
return new WritableFileObjectByteChannel(fo);

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.

With newFileChannel there are now two independent write implementations with different semantics. The writable newByteChannel path (WritableFileObjectByteChannel):

  • ignores TRUNCATE_EXISTING: the old content is always loaded, so writing a shorter text leaves the old tail in place;
  • ignores APPEND: the position starts at 0;
  • ignores CREATE_NEW: there is no FileAlreadyExistsException;
  • takes no FileObject lock, unlike what the new FileUtil Javadoc describes.

Files.newByteChannel(path, WRITE, ...) and FileChannel.open(path, WRITE, ...) therefore behave differently on the same virtual path. Since FileChannel is a SeekableByteChannel, newByteChannel could simply return the new channel for all option sets, leaving one implementation to maintain and test.

@jtulach
jtulach force-pushed the jtulach/FileObjectAndPath branch from 078b1cd to dc6a31a Compare November 24, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants