Wrap filesystem functions for readable errors

Without having to repeat this all over the place.
This commit is contained in:
Robbert van der Helm
2020-07-19 13:46:40 +02:00
parent 9580dbea12
commit 86be5c4814
2 changed files with 37 additions and 22 deletions
+6 -22
View File
@@ -18,13 +18,12 @@
use anyhow::{Context, Result};
use colored::Colorize;
use std::fs;
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};
use crate::config::{Config, InstallationMethod};
use crate::files;
use crate::files::FoundFile;
use crate::utils;
use crate::utils::{verify_path_setup, wrap};
/// Add a direcotry to the plugin locations. Duplicates get ignord because we're using ordered sets.
@@ -60,8 +59,7 @@ pub fn remove_directory(config: &mut Config, path: &Path) -> Result<()> {
) {
Ok(Some(answer)) if answer == "YES" => {
for file in &orphan_files {
fs::remove_file(file.path())
.with_context(|| format!("Could not remove '{}'", file.path().display()))?;
utils::remove_file(file.path())?;
}
println!("\nRemoved {} files", orphan_files.len());
@@ -177,28 +175,15 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> {
// mixing symlinks and regular files
let target_path = plugin.with_extension("so");
if target_path.exists() {
fs::remove_file(&target_path)
.with_context(|| format!("Could not remove '{}'", target_path.display()))?;
utils::remove_file(&target_path)?;
}
match config.method {
InstallationMethod::Copy => {
fs::copy(&libyabridge_path, &target_path).with_context(|| {
format!(
"Error copying '{}' to '{}'",
libyabridge_path.display(),
target_path.display()
)
})?;
utils::copy(&libyabridge_path, &target_path)?;
}
InstallationMethod::Symlink => {
symlink(&libyabridge_path, &target_path).with_context(|| {
format!(
"Error symlinking '{}' to '{}'",
libyabridge_path.display(),
target_path.display()
)
})?;
utils::symlink(&libyabridge_path, &target_path)?;
}
}
@@ -238,8 +223,7 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> {
println!("- {}", path.display());
if options.prune {
fs::remove_file(path)
.with_context(|| format!("Could not remove '{}'", path.display()))?;
utils::remove_file(path)?;
}
}
+31
View File
@@ -16,13 +16,44 @@
//! Small helper utilities.
use anyhow::{Context, Result};
use colored::Colorize;
use std::env;
use std::fs;
use std::os::unix::fs as unix_fs;
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Command, Stdio};
use textwrap::Wrapper;
/// Wrapper around `std::fs::copy` with a human readable error message.
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
fs::copy(&from, &to).with_context(|| {
format!(
"Error copying '{}' to '{}'",
from.as_ref().display(),
to.as_ref().display()
)
})
}
/// Wrapper around `std::fs::remove_file` with a human readable error message.
pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> {
fs::remove_file(&path)
.with_context(|| format!("Could not remove '{}'", path.as_ref().display()))
}
/// Wrapper around `std::os::unix::fs::symlink` with a human readable error message.
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> Result<()> {
unix_fs::symlink(&src, &dst).with_context(|| {
format!(
"Error symlinking '{}' to '{}'",
src.as_ref().display(),
dst.as_ref().display()
)
})
}
/// Verify that `yabridge-host.exe` is accessible in a login shell. Returns unit if it is, or if we
/// the login shell is set to an unknown shell. In the last case we'll just print a warning since we
/// don't know how to invoke the shell as a login shell. This is needed when using copies to ensure