fud/
util.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use smol::{fs, stream::StreamExt};
20use std::{
21    collections::HashSet,
22    path::{Path, PathBuf},
23};
24
25pub use darkfi::geode::hash_to_string;
26use darkfi::Result;
27
28pub async fn get_all_files(dir: &Path) -> Result<Vec<(PathBuf, u64)>> {
29    let mut files = Vec::new();
30
31    let mut entries = fs::read_dir(dir).await.unwrap();
32
33    while let Some(entry) = entries.try_next().await.unwrap() {
34        let path = entry.path();
35
36        if path.is_dir() {
37            files.append(&mut Box::pin(get_all_files(&path)).await?);
38        } else {
39            let metadata = fs::metadata(&path).await?;
40            let file_size = metadata.len();
41            files.push((path, file_size));
42        }
43    }
44
45    Ok(files)
46}
47
48/// An enum to represent a set of files, where you can use `All` if you want
49/// all files without having to specify all of them.
50/// We could use an `Option<HashSet<PathBuf>>`, but this is more explicit.
51#[derive(Clone, Debug)]
52pub enum FileSelection {
53    All,
54    Set(HashSet<PathBuf>),
55}
56
57impl FromIterator<PathBuf> for FileSelection {
58    fn from_iter<I: IntoIterator<Item = PathBuf>>(iter: I) -> Self {
59        let paths: HashSet<PathBuf> = iter.into_iter().collect();
60        FileSelection::Set(paths)
61    }
62}