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

FileHandle from SeekableByteChannel #3299

Merged

Conversation

lhns
Copy link
Contributor

@lhns lhns commented Sep 8, 2023

Hi! I wanted to use https://github.com/robtimus/sftp-fs with the fs2 Files api. It turns out that some file systems don't implement a FileChannel so in this PR I propose a fallback solution to use SeekableByteChannel if necessary.
The FileHandle will not support features like locking in that case. Currently these methods just return F[Unit] but it might be better for them to throw an UnsupportedOperationException. Feedback would be welcome!

@armanbilge
Copy link
Member

Instead of no-opping or erroring those methods, maybe we need to extract a parent interface? I.e. if all you have is a SeekableByteChannel is that really a FileHandle?

@lhns
Copy link
Contributor Author

lhns commented Sep 8, 2023

Yes that would probably be a better solution but requires greater changes. What about a SeekableBytesHandle as a parent trait of FileHandle? My main intent is to just be able to read and write files. I don't really need the other operations that FileHandle has.

@armanbilge
Copy link
Member

I guess that's my other question. Do you actually need a FileHandle (e.g. to pass it to an API that expect one) or do you just want an effectual wrapper around SeekableByteChannel?

@lhns
Copy link
Contributor Author

lhns commented Sep 8, 2023

Well actually I just want to use readAll and writeAll. If WriteCursor and ReadCursor were a trait they could be implemented using SeekableByteChannel. Then I could catch the UnsupportedOperationException in Files.readCursor and Files.writeCursor and use the SeekableByteChannel implementation instead. Since that is not easily changable and ReadCursor and WriteCursor require a FileHandle I could construct the hacky FileHandle just for the cursors but it would still be accessible to the library user.

@armanbilge
Copy link
Member

Thanks for the exposition. Hmm, that's frustrating. It seems that {Read,Write}Cursor don't really need those additional methods on FileHandle but its hard to fix that compatibly. Hmmm 🤔

@lhns
Copy link
Contributor Author

lhns commented Sep 13, 2023

I thought a bit about this but this currently seems to be the only way to get it working. I now made the unsupported methods throw an UnsupportedOperationException so instead of getting one when you open the FileHandle without this PR you now get one when using the unsupported methods which is probably equally unsafe. So its still somewhat of an hack but it gets reading and writing working with unsupported filesystems.

@mpilquist
Copy link
Member

Throwing for the unsupported operations seems fine to me. Couple thoughts:

  • If position is not settable, we can't even implement write correctly. The implementation of write in this PR is only safe for sequential writes.
  • We should consider re-raising the original open error upon failure to open a byte channel.

I think the write issue is enough of a reason to not provide a WriteCursor for non-seekable channels.

@lhns
Copy link
Contributor Author

lhns commented Oct 3, 2023

Throwing for the unsupported operations seems fine to me. Couple thoughts:

  • If position is not settable, we can't even implement write correctly. The implementation of write in this PR is only safe for sequential writes.
  • We should consider re-raising the original open error upon failure to open a byte channel.

I think the write issue is enough of a reason to not provide a WriteCursor for non-seekable channels.

The implementation actually tries to set the position on non-sequential writes and will throw if the SeekableByteChannel doesn't support this doesn't it? This is a mutable operation though unlike in the FileChannel impl.

@mpilquist
Copy link
Member

mpilquist commented Oct 3, 2023 via email

@lhns
Copy link
Contributor Author

lhns commented Oct 4, 2023

I changed the PR to re-raise the original exception that is thrown when creating the FileChannel. Is the mima error a real problem? As far as I can tell the hierarchy is sealed enough that it shouldn't cause any problems or am I missing something?

@armanbilge
Copy link
Member

This one seems okay to filter, unfortunately MiMa doesn't recognized sealed traits I think.

@lhns lhns force-pushed the feature/file-handle-seekable-byte-channel branch from 81ac53b to a727bf4 Compare October 4, 2023 17:00
@lhns
Copy link
Contributor Author

lhns commented Oct 17, 2023

I have added the mima exclusion, a test and updated the branch. The PR should now be ready to review.

@mpilquist mpilquist merged commit 1906b33 into typelevel:main Oct 17, 2023
15 checks passed
@mpilquist
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants