Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4316

SDK • FAT12 driver in C++-23

$
0
0
Trying to update my C++ skills, here's a dynamic FAT12 implementation:

https://eric.buddington.net/code/pico/F ... -05-05.hpp

Code is
- GPL
- C++-23
- limited to FAT12 with 64k clusters
- limited to 1-sector (64 kB) files.
- nothrow - won't pull in exception-handling code
- nearly heap-free (except for std::function, I think)

My goal is to present any user-supplied class as a FAT file. It just needs read() and optional write() methods.
It's also easy to create subdirectories; they're also FAT::Files, so you can stick them in the same array with regular files.

Footprint:
- compiled code size of FAT::* is about 4k
- File struct is 64 bytes
- Filesystem struct is 80 bytes

Implementation Details:

- Each FAT::File wraps your read/write functions in lambda and stores them in std::functions.
- Every FAT::File has a std::span<File> of directory contents, which can conveniently refer to a user's std::array<File>.
- Directories are just Files without a user-supplied read() function, and on attempted read() they will construct and return the directory.
- The file allocation tables show a disk full of 1-sector files. This incorrectly shows unused sectors as allocated, but is code-efficient.
- A user's read() function can returns a short read. The buffer will be padded with 0 and returned as complete.
- Note that reading updated data is tricky because your OS will cache reads and not check for changed data.
- May not deal well with requests that aren't full aligned blocks (can that happen in a USB block storage device?)

Comments and critiques are welcome.

Share and enjoy!

Statistics: Posted by Eric Buddington — Sun May 05, 2024 4:46 pm



Viewing all articles
Browse latest Browse all 4316

Trending Articles