Prompt to remove leftover files when removing dir

This commit is contained in:
Robbert van der Helm
2020-07-15 22:49:34 +02:00
parent ae0f4aa64e
commit 7e3086c354
4 changed files with 259 additions and 29 deletions
+50 -22
View File
@@ -99,28 +99,7 @@ impl FoundFile {
/// the directory does not exist, or if `winedump` could not be found.
pub fn index(directory: &Path) -> Result<SearchResults, std::io::Error> {
// First we'll find all .dll and .so files in the directory
let mut dll_files: Vec<PathBuf> = Vec::new();
let mut so_files: Vec<FoundFile> = Vec::new();
// XXX: We're silently skipping directories and files we don't have permission to read. This
// sounds like the expected behavior, but I"m not entirely sure.
for entry in WalkDir::new(directory)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
.filter(|x| !x.file_type().is_dir())
{
match entry.path().extension().and_then(|os| os.to_str()) {
Some("dll") => dll_files.push(entry.into_path()),
Some("so") => {
if entry.path_is_symlink() {
so_files.push(FoundFile::Symlink(entry.into_path()));
} else {
so_files.push(FoundFile::Regular(entry.into_path()));
}
}
_ => (),
}
}
let (dll_files, so_files) = find_files(directory);
lazy_static! {
static ref VST2_AUTOMATON: AhoCorasick =
@@ -160,3 +139,52 @@ pub fn index(directory: &Path) -> Result<SearchResults, std::io::Error> {
so_files,
})
}
/// THe same as [index()](index), but only report found `.so` files. This avoids unnecesarily
/// filtering the found `.dll` files.
pub fn index_so_files(directory: &Path) -> Vec<FoundFile> {
let (_, so_files) = find_files(directory);
so_files
}
/// Find all `.dll` and `.so` files under a directory. The results are a pair of `(dll_files,
/// so_files)`.
fn find_files(directory: &Path) -> (Vec<PathBuf>, Vec<FoundFile>) {
let mut dll_files: Vec<PathBuf> = Vec::new();
let mut so_files: Vec<FoundFile> = Vec::new();
// XXX: We're silently skipping directories and files we don't have permission to read. This
// sounds like the expected behavior, but I"m not entirely sure.
for (file_idx, entry) in WalkDir::new(directory)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
.filter(|x| !x.file_type().is_dir())
.enumerate()
{
// This is a bit of an odd warning, but I can see it happening that someone adds their
// entire home directory by accident. Removing the home directory would cause yabridgectl to
// scan for leftover `.so` files, which would of course take an enternity. This warning will
// at least tell the user what's happening and that they can safely cancel the scan.
if file_idx == 100_000 {
eprintln!(
"Indexed over 100.000 files, press Ctrl+C to cancel this operation if this was not \
intentional."
)
}
match entry.path().extension().and_then(|os| os.to_str()) {
Some("dll") => dll_files.push(entry.into_path()),
Some("so") => {
if entry.path_is_symlink() {
so_files.push(FoundFile::Symlink(entry.into_path()));
} else {
so_files.push(FoundFile::Regular(entry.into_path()));
}
}
_ => (),
}
}
(dll_files, so_files)
}
+37 -7
View File
@@ -29,7 +29,6 @@ mod files;
// TODO: Naming and descriptions could be made clearer
// TODO: When creating copies, check whether `yabridge-host.exe` is in the PATH for the login shell
// TODO: Check for left over files when removing directory
// TODO: Reward parts of the readme
fn main() {
@@ -142,10 +141,10 @@ fn main() {
/// Add a direcotry to the plugin locations. Duplicates get ignord because we're using ordered sets.
fn add_directory(config: &mut Config, path: PathBuf) {
config.plugin_dirs.insert(path);
if let Err(err) = config.write() {
config.write().unwrap_or_else(|err| {
eprintln!("Error while writing config file: {}", err);
exit(1);
};
});
}
/// Remove a direcotry to the plugin locations. The path is assumed to be part of
@@ -154,10 +153,41 @@ fn remove_directory(config: &mut Config, path: &Path) {
// We've already verified that this path is in `config.plugin_dirs`
// XXS: Would it be a good idea to warn about leftover .so files?
config.plugin_dirs.remove(path);
if let Err(err) = config.write() {
config.write().unwrap_or_else(|err| {
eprintln!("Error while writing config file: {}", err);
exit(1);
};
});
// Ask the user to remove any leftover files to prevent possible future problems and out of date
// copies
let orphan_files = files::index_so_files(path);
if !orphan_files.is_empty() {
println!(
"WARNING: Found {} leftover '.so' files still in this directory:",
orphan_files.len()
);
for file in &orphan_files {
println!("- {}", file.path().display());
}
match promptly::prompt_opt::<String, &str>(
"\nWould you like to remove these files? Entering anything other than YES will leave \
these files intact",
) {
Ok(Some(answer)) if answer == "YES" => {
for file in &orphan_files {
fs::remove_file(file.path()).unwrap_or_else(|err| {
eprintln!("Could not remove '{}': {}", file.path().display(), err);
exit(1);
});
}
println!("\nRemoved {} files.", orphan_files.len());
}
_ => {}
}
}
}
/// List the plugin locations.
@@ -229,10 +259,10 @@ fn set_settings(config: &mut Config, options: &ArgMatches) {
Err(err) => err.exit(),
}
if let Err(err) = config.write() {
config.write().unwrap_or_else(|err| {
eprintln!("Error while writing config file: {}", err);
exit(1);
};
});
}
/// Set up yabridge for all Windows VST2 plugins in the plugin directories. Will also remove orphan