Skip to main content

Filesystem

Struct Filesystem 

Source
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>

Source

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.

Source

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).

Source

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.

Source

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.

Source

pub fn read_to_vec(&self, path: &str) -> Result<Vec<u8>, Error>

Read an entire file into a Vec<u8>.

Source

pub fn write_file(&self, path: &str, data: &[u8]) -> Result<(), Error>

Write data to a file, creating or truncating it.

Source

pub fn mkdir(&self, path: &str) -> Result<(), Error>

Create a directory. Fails if it already exists.

Source

pub fn remove(&self, path: &str) -> Result<(), Error>

Remove a file or empty directory.

Source

pub fn rename(&self, from: &str, to: &str) -> Result<(), Error>

Rename or move a file or directory.

Source

pub fn stat(&self, path: &str) -> Result<Metadata, Error>

Get metadata for a file or directory.

Source

pub fn exists(&self, path: &str) -> bool

Returns true if path exists.

Source

pub fn read_dir(&self, path: &str) -> Result<ReadDir<'_, S>, Error>

Open a directory for iteration. The returned ReadDir is an Iterator that skips . and .. entries.

Source

pub fn list_dir(&self, path: &str) -> Result<Vec<DirEntry>, Error>

Collect all entries in a directory into a Vec.

Source

pub fn fs_size(&self) -> Result<u32, Error>

Return the number of allocated blocks.

Source

pub fn gc(&self) -> Result<(), Error>

Run garbage collection to reclaim unused blocks.

Trait Implementations§

Source§

impl<S: Storage> Drop for Filesystem<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<S> !Freeze for Filesystem<S>

§

impl<S> !RefUnwindSafe for Filesystem<S>

§

impl<S> !Send for Filesystem<S>

§

impl<S> !Sync for Filesystem<S>

§

impl<S> Unpin for Filesystem<S>

§

impl<S> UnsafeUnpin for Filesystem<S>

§

impl<S> UnwindSafe for Filesystem<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.