Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
feature: filesystem #37
feature: filesystem #37
Changes from 19 commits
ef4a614
abfd6dc
e1e37b5
dab0d8a
2ca5f82
6f46b39
ef3c8ae
1ab7934
abab55e
2543bd1
e3783b9
5d6cff1
f32fcc5
b01ddc0
70a60f3
f854179
207e4d4
10d4d74
6cb0fcc
502fd82
ee16dbc
66aa317
2a2b392
7f7c03f
51f77df
d728ac1
6eaa44f
dd51cc4
ec770ac
f17ab68
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to use
Formatter::write_str
here, because Rust's formatting macros pull in 10+ KiB of formatting code even for a simple case like this one.I didn't know about
escape_ascii
. That's very useful to know!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to need to ponder on this one.
write!
is somehow converting theEscapeAscii
into a printable byte sequence without needingalloc
. My naïve solution needsalloc
, which I think isn't ideal. I'll head back to the drawing boardThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Figured it out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API is not yet part of stable Rust (rust-lang/rust#59359), so we may not want to offer it here yet (or else be prepared to potentially alter it if/when
std::io::Seek::stream_len
is stabilised).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SDK exposes a
stream_size
function which serves a similar role.So far I consider all APIs still subject to change. With the amount of churn in the Flipper Zero SDK, I haven't been willing to call anything stable yet. So if we need to alter it, then that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll defer to @dcoles for a final decision on whether to nix it - I don't have a dog in this race.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine to keep it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having an equivalent to
std::io::Error::WriteZero
to return here, would require thatError::to_sys
returnOption<sys::FS_Error>
, likestd::io::Error::raw_os_error
does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change and pushed it to this branch. It's a little cumbersome but.. so are a lot of the OS-adjacent API's in rust :)
Any thoughts on this, @dcoles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend using
core::ffi::c_char
just to make this a bit more clear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation is out of date, since
File
is not buffered.There's a few other mentions of "stream" that should also be corrected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to update those comments but keep them unspecified since this /could/ be the API we use for actual stream objects later on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
storage_file_alloc
holds onto the storage record, so we need to keep a validUnsafeRecord
on theFile
object, so we don't close the record prematurely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shame on me for not reading the
UnsafeRecord
docs 🙁There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
sys::storage_file_read
guaranteed to read as many bytes as it can? Mostfread
-style methods give no such guarantees, and indeed the only guarantee thatstd::io::Read::read(buf)
gives is that ifOk(n)
is returned, then0 <= n <= buf.len()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't count on it. Most other places in the Flipper Zero firmware seem to loop on
storage_file_read
until they receive a0
, then check whether an error actually occurred usingstorage_file_ferror
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point. I think it makes more sense to check for an error unconditionally, rather than trying to infer what happened based on the number of bytes read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally I would have a suspicion that the way we are supposed to use
storage_file_get_error
is to check it after every operation involving a file handle (i.e. if it returnssys::FS_Error_FSE_OK
, that means the previous operation succeeded):And indeed, looking at how
storage_file_get_error
is used inflipperzero-firmware
, you see that it is almost always of the form:However, looking at how
storage_file_read
is used inflipperzero-firmware
, it seems at a glance that boolean error conditions are produced via the same check as above (that as many bytes were read as were asked for). So IDK, we might want to ask the Flipper devs what their intentions are forstorage_file_get_error
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to action on that; I can make a forum post
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://forum.flipperzero.one/t/detecting-errors-in-the-storage-api/13441
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
offset_type = false
here will causeSeekFrom::End
to be treated the same way asSeekFrom::Current
(becauseoffset_type
is reallyfrom_start
).Instead, for
SeekFrom::End
we will need to measure the length of the file, and then usefrom_start = true
andoffset = file_length - n
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm realizing that this is all subtly incorrect if we're going off of the Rust semantics. In particular, the user should be able to specify a negative number for
End
orCurrent
, but the current code doesn't allow that.My problem is that the
offset
parameter instorage_file_seek
is au32
whereas the rest of the API treats the file offset as au64
, so it isn't super clear how to calculateoffset = file_length - n
such that it can be passed tostorage_file_seek
.I'll see if there are any examples upstream
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find many, but there's at least one example of necking
storage_file_size
down to auint32_t
inlib/toolbox/crc32_calc.c
.The LittleFS 'file max' is a 32-bit int and
lfs_file_size
returns aint32_t
, so I'm tempted to say that we can assumestorage_file_size
and friends return a value in[0, u32::MAX]
. Does that sound right? Or is it bad to assume that the firmware is always going to be on LittleFSThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same kind of comment here as for
Read::read
: isstorage_file_write
guaranteed to write as many bytes as it can, or onlyn <= buf.len()
?