1
1
use crate :: api:: processor:: Response :: { Error , FetchFileChunk , GetInfo , ListFiles , Reboot } ;
2
2
use crate :: api:: websocket:: { ConnectionState , SessionEvent , WebSocketSession } ;
3
- use anyhow:: { anyhow, bail } ;
3
+ use anyhow:: anyhow;
4
4
use embedded_svc:: ws:: FrameType ;
5
5
use esp_idf_svc:: hal:: gpio:: Pull ;
6
6
use esp_idf_svc:: ping:: Info ;
7
- use esp_idf_svc:: sys:: { ctime, fstat, stat, strerror, S_IFDIR , S_IFMT } ;
8
7
use serde:: { Deserialize , Serialize } ;
9
- use std:: ffi:: CString ;
10
8
use std:: fs:: { read_dir, FileType } ;
11
9
use std:: io:: { Read , Seek } ;
12
10
use std:: mem:: MaybeUninit ;
13
- use std:: os:: fd:: AsFd ;
14
- use std:: os:: unix:: fs:: MetadataExt ;
15
11
use std:: path:: Path ;
16
12
use std:: time:: SystemTime ;
17
13
use time:: serde:: timestamp:: milliseconds;
@@ -98,10 +94,7 @@ impl Processor {
98
94
99
95
fn list_files ( & self , path : & str ) -> anyhow:: Result < Response > {
100
96
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>" ) ) ;
105
98
// Ideally we should find a way to learn the size of all files, but we need to
106
99
// iterate over all files anyway... so.. maybe not? :/
107
100
let mut files: Vec < File > = vec ! [ ] ;
@@ -117,25 +110,13 @@ impl Processor {
117
110
continue ;
118
111
}
119
112
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 ( ) ?;
133
114
files. push ( File {
134
115
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 ( ) ,
139
120
} )
140
121
}
141
122
Ok ( ListFiles {
0 commit comments