pub struct Filesystem<S: Storage> { /* private fields */ }Expand description
A mounted LittleFS filesystem.
All methods take &self via interior mutability, so multiple File and
ReadDir handles can coexist. The internal state is heap-allocated and
pinned so that core pointers remain stable across moves.
Use Filesystem::format to initialize storage, then Filesystem::mount
to obtain a Filesystem. Call Filesystem::unmount to cleanly unmount
and recover the storage, or let Drop handle it automatically.
Filesystem is !Send and !Sync (due to interior RefCell). This is
appropriate for single-threaded embedded use. If you need cross-thread
access, wrap it in a Mutex.
Implementations§
Source§impl<S: Storage> Filesystem<S>
impl<S: Storage> Filesystem<S>
Sourcepub fn format(storage: &mut S, config: &Config) -> Result<(), Error>
pub fn format(storage: &mut S, config: &Config) -> Result<(), Error>
Format storage with a fresh LittleFS filesystem.
This erases any existing data. The storage can be mounted afterwards
with Filesystem::mount.
Sourcepub fn mount(storage: S, config: Config) -> Result<Self, (Error, S)>
pub fn mount(storage: S, config: Config) -> Result<Self, (Error, S)>
Mount an existing filesystem. Takes ownership of the storage.
On failure the storage is returned alongside the error so the caller can retry (e.g. format + mount).
Sourcepub fn unmount(self) -> Result<S, Error>
pub fn unmount(self) -> Result<S, Error>
Unmount and return the underlying storage.
Prefer this over dropping when you need to check for errors or reuse the storage.
Sourcepub fn open(&self, path: &str, flags: OpenFlags) -> Result<File<'_, S>, Error>
pub fn open(&self, path: &str, flags: OpenFlags) -> Result<File<'_, S>, Error>
Open a file with the given OpenFlags.
Common combinations: READ, WRITE | CREATE | TRUNC,
WRITE | CREATE | APPEND.
Sourcepub fn read_to_vec(&self, path: &str) -> Result<Vec<u8>, Error>
pub fn read_to_vec(&self, path: &str) -> Result<Vec<u8>, Error>
Read an entire file into a Vec<u8>.
Sourcepub fn write_file(&self, path: &str, data: &[u8]) -> Result<(), Error>
pub fn write_file(&self, path: &str, data: &[u8]) -> Result<(), Error>
Write data to a file, creating or truncating it.
Sourcepub fn mkdir(&self, path: &str) -> Result<(), Error>
pub fn mkdir(&self, path: &str) -> Result<(), Error>
Create a directory. Fails if it already exists.
Sourcepub fn rename(&self, from: &str, to: &str) -> Result<(), Error>
pub fn rename(&self, from: &str, to: &str) -> Result<(), Error>
Rename or move a file or directory.
Sourcepub fn stat(&self, path: &str) -> Result<Metadata, Error>
pub fn stat(&self, path: &str) -> Result<Metadata, Error>
Get metadata for a file or directory.