From 286135400fc5fcf7be2402647cfa387b3e386571 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 14 Jul 2020 14:59:26 +0200 Subject: [PATCH] Query installation status and orphan files --- tools/yabridgectl/src/files.rs | 53 +++++++++++++++++++++++++++++++++- tools/yabridgectl/src/main.rs | 1 + 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/tools/yabridgectl/src/files.rs b/tools/yabridgectl/src/files.rs index efc965e6..f81d1045 100644 --- a/tools/yabridgectl/src/files.rs +++ b/tools/yabridgectl/src/files.rs @@ -19,6 +19,7 @@ use aho_corasick::AhoCorasick; use lazy_static::lazy_static; use rayon::prelude::*; +use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::process::Command; use walkdir::WalkDir; @@ -35,12 +36,62 @@ pub struct SearchResults { pub so_files: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum FoundFile { Symlink(PathBuf), Regular(PathBuf), } +impl SearchResults { + /// For every found VST2 plugin, find the associated copy or symlink of `libyabridge.so`. The + /// returned hashmap will contain a `None` value for plugins that have not yet been set up. + /// + /// These two functions could be combined into a single function, but speed isn't really an + /// issue here and it's a bit more organized this way. + pub fn installation_status(&self) -> HashMap<&Path, Option<&FoundFile>> { + let so_files: HashMap<&Path, &FoundFile> = self + .so_files + .iter() + .map(|file| (file.path(), file)) + .collect(); + + self.vst2_files + .iter() + .map( + |path| match so_files.get(path.with_extension("so").as_path()) { + Some(&file) => (path.as_path(), Some(file)), + None => (path.as_path(), None), + }, + ) + .collect() + } + + /// Find all `.so` files in the search results that do not belong to a VST2 plugin `.dll` file. + pub fn orphans(&self) -> Vec<&FoundFile> { + // We need to store these in a map so we can easily entries with corresponding `.dll` files + let mut orphans: HashMap<&Path, &FoundFile> = self + .so_files + .iter() + .map(|file| (file.path(), file)) + .collect(); + for vst2_path in &self.vst2_files { + orphans.remove(vst2_path.with_extension("so").as_path()); + } + + orphans.into_iter().map(|(_, file)| file).collect() + } +} + +impl FoundFile { + /// Return the path of a found `.so` file. + pub fn path(&self) -> &Path { + match &self { + FoundFile::Symlink(path) => path, + FoundFile::Regular(path) => path, + } + } +} + /// Search for Windows VST2 plugins and .so files under a directory. This will return an error if /// the directory does not exist, or if `winedump` could not be found. pub fn index(directory: &Path) -> Result { diff --git a/tools/yabridgectl/src/main.rs b/tools/yabridgectl/src/main.rs index 90faed50..b3dc57d4 100644 --- a/tools/yabridgectl/src/main.rs +++ b/tools/yabridgectl/src/main.rs @@ -15,6 +15,7 @@ // along with this program. If not, see . mod config; +mod files; use config::Config;