| 398 | } |
| 399 | |
| 400 | pub fn read_dir<P: AsRef<Path>>( |
| 401 | path: P |
| 402 | ) -> Result<ReadDir, SyscallError> { |
| 403 | let path: &Path = path.as_ref(); |
| 404 | |
| 405 | let f = File::open(path)?; |
| 406 | let query = f.query()?; |
| 407 | |
| 408 | let mut entries = match query.0["files"].as_array() { |
| 409 | Some(vec) => { |
| 410 | // Transform into a Vec of DirEntry objects |
| 411 | vec.iter().map(|obj| DirEntry{ |
| 412 | name: String::from(obj["name"].as_str().unwrap_or("_bad_")), |
| 413 | meta: Metadata { |
| 414 | is_dir: false |
| 415 | } |
| 416 | }).collect() |
| 417 | } |
| 418 | _ => Vec::new() |
| 419 | }; |
| 420 | |
| 421 | if let Some(vec) = query.0["subdirs"].as_array() { |
| 422 | // Some directories |
| 423 | for obj in vec { |
| 424 | entries.push(DirEntry{ |
| 425 | name: String::from(obj["name"].as_str().unwrap_or("_bad_")), |
| 426 | meta: Metadata { |
| 427 | is_dir: true |
| 428 | } |
| 429 | }); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | Ok(ReadDir{ |
| 434 | entries |
| 435 | }) |
| 436 | } |
| 437 | |
| 438 | /// Delete a file |
| 439 | pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<(), SyscallError> { |