Skip to main content

esp_idf_svc/fs/
spiffs.rs

1use core::ffi::CStr;
2
3use alloc::ffi::CString;
4
5use crate::sys::*;
6
7extern crate alloc;
8
9/// Represents a Spiffs filesystem.
10pub struct Spiffs {
11    partition: CString,
12}
13
14impl Spiffs {
15    /// Create a new - readonly - FAT filesystem instance for a given raw partition in the internal flash.
16    /// This API is unsafe because currently `esp-idf-svc` does not have a safe way to
17    /// represent a flash partition.
18    ///
19    /// # Arguments
20    /// - Spiffs partition label.
21    ///
22    /// # Safety
23    ///
24    /// While the filesystem object is alive, the partition should not be modified elsewhere
25    pub unsafe fn new(partition_label: &str) -> Result<Self, EspError> {
26        Ok(Self {
27            partition: crate::private::cstr::to_cstring_arg(partition_label)?,
28        })
29    }
30
31    /// Get the partition label.
32    pub fn partition_label(&self) -> &CStr {
33        &self.partition
34    }
35
36    /// Check the filesystem for errors.
37    pub fn check(&mut self) -> Result<(), EspError> {
38        esp!(unsafe { esp_spiffs_check(self.partition.as_ptr()) })
39    }
40
41    /// Format the partition.
42    pub fn format(&mut self) -> Result<(), EspError> {
43        esp!(unsafe { esp_spiffs_format(self.partition.as_ptr()) })
44    }
45
46    /// Garbage collect the filesystem.
47    #[cfg(not(esp_idf_version_major = "4"))]
48    pub fn gc(&mut self, size_to_gc: usize) -> Result<(), EspError> {
49        esp!(unsafe { esp_spiffs_gc(self.partition.as_ptr(), size_to_gc) })
50    }
51}