littlefs2 impl stuff

This commit is contained in:
2025-09-17 01:36:53 +02:00
parent 8b938e7687
commit 4c54edbcea
4 changed files with 124 additions and 58 deletions

View File

@@ -6,6 +6,7 @@ use anyhow::{anyhow, bail, Context};
use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::hal::LittleFS2StorageAdapter::LittleFs2Filesystem;
use alloc::string::ToString;
use alloc::sync::Arc;
use alloc::{format, string::String, vec::Vec};
@@ -32,6 +33,8 @@ use esp_wifi::wifi::{
AccessPointConfiguration, AccessPointInfo, Configuration, Interfaces, ScanConfig,
ScanTypeConfig, WifiController, WifiDevice, WifiEvent, WifiState,
};
use littlefs2::fs::Filesystem;
use littlefs2_core::{FileType, PathBuf};
use log::{info, warn};
#[link_section = ".rtc.data"]
@@ -54,8 +57,6 @@ pub struct FileList {
total: usize,
used: usize,
files: Vec<FileInfo>,
file_system_corrupt: Option<String>,
iter_error: Option<String>,
}
pub struct FileSystemSizeInfo {
@@ -70,6 +71,7 @@ pub struct MqttClient<'a> {
base_topic: heapless::String<64>,
}
pub struct Esp<'a> {
pub fs: Arc<Mutex<CriticalSectionRawMutex, Filesystem<'static, LittleFs2Filesystem>>>,
pub rng: Rng,
//first starter (ap or sta will take these)
pub interfaces: Option<Interfaces<'static>>,
@@ -432,60 +434,32 @@ impl Esp<'_> {
// })
}
pub(crate) async fn list_files(&self) -> FileList {
return FileList {
pub(crate) async fn list_files(&self) -> anyhow::Result<FileList> {
let path = PathBuf::new();
let mut result = FileList {
total: 0,
used: 0,
file_system_corrupt: None,
files: Vec::new(),
iter_error: None,
};
//
// let storage = CString::new(Self::SPIFFS_PARTITION_NAME).unwrap();
//
// let mut file_system_corrupt = None;
//
// let mut iter_error = None;
// let mut result = Vec::new();
//
// let filepath = Path::new(Self::BASE_PATH);
// let read_dir = fs::read_dir(filepath);
// match read_dir {
// OkStd(read_dir) => {
// for item in read_dir {
// match item {
// OkStd(file) => {
// let f = FileInfo {
// filename: file.file_name().into_string().unwrap(),
// size: file.metadata().map(|it| it.len()).unwrap_or_default()
// as usize,
// };
// result.push(f);
// }
// Err(err) => {
// iter_error = Some(format!("{err:?}"));
// break;
// }
// }
// }
// }
// Err(err) => {
// file_system_corrupt = Some(format!("{err:?}"));
// }
// }
// let mut total: usize = 0;
// let mut used: usize = 0;
// unsafe {
// esp_spiffs_info(storage.as_ptr(), &mut total, &mut used);
// }
//
// FileList {
// total,
// used,
// file_system_corrupt,
// files: result,
// iter_error,
// }
match self.fs.lock().await.read_dir_and_then(&path, |dir| {
for entry in dir {
let e = entry?;
result.files.push(FileInfo {
filename: e.path().to_string(),
size: e.metadata().len(),
});
}
Result::Ok(())
}) {
Ok(_) => {}
Err(err) => {
bail!(format!("{err:?}"))
}
}
Ok(result)
}
pub(crate) async fn delete_file(&self, _filename: &str) -> anyhow::Result<()> {
bail!("todo");