Skip to content

Conversation

jacobschloss
Copy link

Provide a lfs_file_movehandle and lfs_file_ishandleopen function to allow moving file handles.

While users should move file handles rarely, being able to move them allows for better integration with C++ RAII wrappers, which users would like to be able to move around -- even if only once in order to place them into a std::optional. Other options could involve using heap or a special arena allocator, but this is nice and simple. Providing a move function also indirectly hints that the lfs_file_t can't just be memcpy'd, and documents that it is expensive and if you are doing it a lot you might need to do something different.

I have a small amount of test code for this I could put somewhere that spins through opening and moving a few file handles.

Previous discussion at #765.

@geky-bot
Copy link
Collaborator

Tests passed ✓, Code: 17160 B (+0.3%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
Code Stack Structs Coverage
Default 17160 B (+0.3%) 1448 B (+0.0%) 812 B (+0.0%) Lines 2457/2617 lines (+0.1%)
Readonly 6290 B (+1.0%) 448 B (+0.0%) 812 B (+0.0%) Branches 1287/1622 branches (-0.1%)
Threadsafe 18056 B (+0.5%) 1448 B (+0.0%) 820 B (+0.0%) Benchmarks
Multiversion 17232 B (+0.3%) 1448 B (+0.0%) 816 B (+0.0%) Readed 29369693876 B (+0.0%)
Migrate 18824 B (+0.3%) 1752 B (+0.0%) 816 B (+0.0%) Proged 1482874766 B (+0.0%)
Error-asserts 18004 B (+0.4%) 1440 B (+0.0%) 812 B (+0.0%) Erased 1568888832 B (+0.0%)

@geky geky added enhancement needs minor version new functionality only allowed in minor versions labels Jul 22, 2025

lfs_file_close(&lfs, &file) => 0;

lfs_file_ishandleopen(&lfs, &file) => LFS_ERR_BADF;
Copy link
Member

Choose a reason for hiding this comment

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

I hadn't thought about the return value of lfs_file_ishandleopen, LFS_ERR_BADF is a good choice. It's also the first use of this error code in the codebase.

Humorously, LFS_ERR_BADF is removed in v3-alpha, but we can bring it back for this purpose.

LFS_ERR_BADF also makes sense for a theoretical LFS_UNTRUSTED_USER build, where file operations error if the file is not open. Some users have asked for such a feature, but it's been low priority. (I'm also hesitant because it isn't bulletproof, we can't provide the same guardrails around if a filesystem is mounted, for example.)

Copy link
Author

Choose a reason for hiding this comment

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

Humorously, LFS_ERR_BADF is removed in v3-alpha, but we can bring it back for this purpose.

That is hilarious! I was also debating LFS_ERR_INVAL, but LFS_ERR_BADF just felt right.

@geky
Copy link
Member

geky commented Jul 22, 2025

Thanks for the PR. And including tests!

I have just have some style related nits.

And another request: Are you able to git commit --amend your most recent commit to indicate the added unit tests are file handle related? And limit the commit subject line to 80 chars in the first commit? (additional sentences can be moved to the commit body) These things help users searching through the commit history.

I can clean up these nits/commits on my end, but it messes with the commit author.

Though these are low-priority due to the ongoing feature freeze (#1111).

Add lfs_file_movehandle to allow moving file handles in the rare case it is
needed. Add lfs_file_ishandleopen to allow checking if a file handle is open.
@jacobschloss jacobschloss force-pushed the lfs_file_movehandle branch from 2156c07 to c8d01e1 Compare July 23, 2025 07:50
@jacobschloss
Copy link
Author

I gave the edits a shot

@geky-bot
Copy link
Collaborator

Tests passed ✓, Code: 17160 B, Stack: 1448 B, Structs: 812 B
Code Stack Structs Coverage
Default 17160 B 1448 B 812 B Lines 2457/2617 lines
Readonly 6290 B 448 B 812 B Branches 1287/1622 branches
Threadsafe 18056 B 1448 B 820 B Benchmarks
Multiversion 17232 B 1448 B 816 B Readed 29369693876 B
Migrate 18824 B 1752 B 816 B Proged 1482874766 B
Error-asserts 18004 B 1440 B 812 B Erased 1568888832 B

@geky
Copy link
Member

geky commented Aug 11, 2025

Looks great! Sorry about the late response, but thanks for updating/amending based on feedback.

Will bring this into the next minor release in both littlefs2 and littlefs3, once the feature freeze is lifted.

Edit for style
@geky-bot
Copy link
Collaborator

Tests passed ✓, Code: 17160 B (+0.3%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
Code Stack Structs Coverage
Default 17160 B (+0.3%) 1448 B (+0.0%) 812 B (+0.0%) Lines 2457/2617 lines (+0.1%)
Readonly 6290 B (+1.0%) 448 B (+0.0%) 812 B (+0.0%) Branches 1287/1622 branches (-0.1%)
Threadsafe 18056 B (+0.5%) 1448 B (+0.0%) 820 B (+0.0%) Benchmarks
Multiversion 17232 B (+0.3%) 1448 B (+0.0%) 816 B (+0.0%) Readed 29369693876 B (+0.0%)
Migrate 18824 B (+0.3%) 1752 B (+0.0%) 816 B (+0.0%) Proged 1482874766 B (+0.0%)
Error-asserts 18004 B (+0.4%) 1440 B (+0.0%) 812 B (+0.0%) Erased 1568888832 B (+0.0%)

@geky
Copy link
Member

geky commented Aug 14, 2025

Looks good here! Thanks for the extra changes. One of these days we'll have actual checks to catch style issues.

Will bring this into the next minor release in both littlefs2 and littlefs3, once the feature freeze is lifted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs minor version new functionality only allowed in minor versions next minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants