| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| use {fidl_fuchsia_io::DirectoryProxy, std::path::PathBuf}; |
| |
| pub async fn dir_contains<'a>( |
| root_proxy: &'a DirectoryProxy, |
| path: &'a str, |
| entry_name: &'a str, |
| ) -> bool { |
| let dir = io_util::open_directory(&root_proxy, &PathBuf::from(path)) |
| .expect("Failed to open directory"); |
| let entries = await!(files_async::readdir(&dir)).expect("readdir failed"); |
| let listing = entries.iter().map(|entry| entry.name.clone()).collect::<Vec<String>>(); |
| listing.contains(&String::from(entry_name)) |
| } |
| |
| pub async fn list_directory<'a>(root_proxy: &'a DirectoryProxy) -> Vec<String> { |
| let entries = await!(files_async::readdir(&root_proxy)).expect("readdir failed"); |
| let mut items = entries.iter().map(|entry| entry.name.clone()).collect::<Vec<String>>(); |
| items.sort(); |
| items |
| } |
| |
| pub async fn list_directory_recursive<'a>(root_proxy: &'a DirectoryProxy) -> Vec<String> { |
| let dir = io_util::clone_directory(&root_proxy).expect("Failed to clone DirectoryProxy"); |
| let entries = await!(files_async::readdir_recursive(dir)).expect("readdir failed"); |
| let mut items = entries.iter().map(|entry| entry.name.clone()).collect::<Vec<String>>(); |
| items.sort(); |
| items |
| } |
| |
| pub async fn read_file<'a>(root_proxy: &'a DirectoryProxy, path: &'a str) -> String { |
| let file_proxy = |
| io_util::open_file(&root_proxy, &PathBuf::from(path)).expect("Failed to open file."); |
| let res = await!(io_util::read_file(&file_proxy)); |
| res.expect("Unable to read file.") |
| } |