#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
use crate::ported::errors;
use crate::ported::request::common::normalizePasswordStorePath;
use crate::ported::request::process::request;
use crate::ported::response;
use std::ffi::OsStr;
use std::fs;
use std::path::PathBuf;
pub fn listFiles(request: &request) {
let mut responseData = response::MakeListResponse();
for store in request.Settings.Stores.values() { let mut store = store.clone();
let normalizedStorePath = match normalizePasswordStorePath(&store.Path) { Ok(p) => p,
Err(e) => { eprintln!(
"The password store '{:?}' is not accessible at its location: {}",
store, e
);
response::SendErrorAndExit( errors::Code::InaccessiblePasswordStore,
Some(response::params_of(&[
(errors::field::MESSAGE, "The password store is not accessible"),
(errors::field::ACTION, "list"),
(errors::field::ERROR, &e),
(errors::field::STORE_ID, &store.ID),
(errors::field::STORE_NAME, &store.Name),
(errors::field::STORE_PATH, &store.Path),
])),
);
}
};
store.Path = normalizedStorePath.to_string_lossy().into_owned();
let mut files: Vec<String> = Vec::new();
if let Err(e) = collect_gpg(&normalizedStorePath, &normalizedStorePath, &mut files) {
eprintln!(
"Unable to list the files in the password store '{:?}' at its location: {}",
store, e
);
response::SendErrorAndExit( errors::Code::UnableToListFilesInPasswordStore,
Some(response::params_of(&[
(errors::field::MESSAGE, "Unable to list the files in the password store"),
(errors::field::ACTION, "list"),
(errors::field::ERROR, &e),
(errors::field::STORE_ID, &store.ID),
(errors::field::STORE_NAME, &store.Name),
(errors::field::STORE_PATH, &store.Path),
])),
);
}
for file in files.iter_mut() { *file = file.replace('\\', "/");
}
files.sort(); responseData.Files.insert(store.ID.clone(), files); }
response::SendOk(responseData); }
fn collect_gpg(root: &std::path::Path, dir: &std::path::Path, out: &mut Vec<String>) -> Result<(), String> {
let rd = fs::read_dir(dir).map_err(|e| format!("{e}"))?;
for entry in rd {
let entry = entry.map_err(|e| format!("{e}"))?;
let path: PathBuf = entry.path();
let ft = entry.file_type().map_err(|e| format!("{e}"))?;
if ft.is_dir() {
if path.file_name() == Some(OsStr::new(".git")) {
continue;
}
collect_gpg(root, &path, out)?;
} else if ft.is_file() && path.extension().and_then(|s| s.to_str()) == Some("gpg") {
let rel = path
.strip_prefix(root)
.map_err(|e| format!("{e}"))?
.to_string_lossy()
.into_owned();
out.push(rel);
}
}
Ok(())
}
#[allow(non_snake_case)]
const _: () = ();