Skip to content

Commit 6b21b21

Browse files
committed
Revert "Use clib instead"
This reverts commit 61e1b65.
1 parent 61e1b65 commit 6b21b21

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/api/processor.rs

+7-26
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
use crate::api::processor::Response::{Error, FetchFileChunk, GetInfo, ListFiles, Reboot};
22
use crate::api::websocket::{ConnectionState, SessionEvent, WebSocketSession};
3-
use anyhow::{anyhow, bail};
3+
use anyhow::anyhow;
44
use embedded_svc::ws::FrameType;
55
use esp_idf_svc::hal::gpio::Pull;
66
use esp_idf_svc::ping::Info;
7-
use esp_idf_svc::sys::{ctime, fstat, stat, strerror, S_IFDIR, S_IFMT};
87
use serde::{Deserialize, Serialize};
9-
use std::ffi::CString;
108
use std::fs::{read_dir, FileType};
119
use std::io::{Read, Seek};
1210
use std::mem::MaybeUninit;
13-
use std::os::fd::AsFd;
14-
use std::os::unix::fs::MetadataExt;
1511
use std::path::Path;
1612
use std::time::SystemTime;
1713
use time::serde::timestamp::milliseconds;
@@ -98,10 +94,7 @@ impl Processor {
9894

9995
fn list_files(&self, path: &str) -> anyhow::Result<Response> {
10096
let dir_path = Path::new(&self.root_dir).join(path);
101-
log::info!(
102-
"Listing files at {}",
103-
dir_path.to_str().unwrap_or("<Unknown>")
104-
);
97+
log::info!("Listing files at {}", dir_path.to_str().unwrap_or("<Unknown>"));
10598
// Ideally we should find a way to learn the size of all files, but we need to
10699
// iterate over all files anyway... so.. maybe not? :/
107100
let mut files: Vec<File> = vec![];
@@ -117,25 +110,13 @@ impl Processor {
117110
continue;
118111
}
119112
let path = path.unwrap();
120-
121-
// Notice: there's entry.metadata(), but somehow the implementation of rust's std
122-
// is broken and cannot return correct stat. So we call C lib directly to get
123-
// stat for the file instead.
124-
let path_c_str = CString::new(path.as_bytes());
125-
let mut file_stat = stat::default();
126-
let ret = unsafe { stat(path_c_str?.as_ptr(), &mut file_stat) };
127-
if ret != 0 {
128-
bail!("Failed to get file {} stat with error: {}", path, unsafe {
129-
CString::from_raw(strerror(ret)).to_str()?
130-
});
131-
}
132-
113+
let metadata = entry.metadata()?;
133114
files.push(File {
134115
path,
135-
size: file_stat.st_size as u64,
136-
modified_at: OffsetDateTime::from_unix_timestamp(file_stat.st_mtim.tv_sec)?,
137-
created_at: OffsetDateTime::from_unix_timestamp(file_stat.st_ctim.tv_sec)?,
138-
is_dir: file_stat.st_mode & S_IFMT == S_IFDIR,
116+
size: metadata.len(),
117+
modified_at: metadata.modified()?.into(),
118+
created_at: metadata.created()?.into(),
119+
is_dir: metadata.is_dir(),
139120
})
140121
}
141122
Ok(ListFiles {

0 commit comments

Comments
 (0)