From 5faed9e2e75adf17037c901ba1eb9cba2f9f5728 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 2 Sep 2022 14:25:57 +0200 Subject: [PATCH] Remove files before reflinking in xtask bundle Otherwise the reflink will fail, and apparently the fallback copy also doesn't always work correctly on macOS. This fixes #26. --- nih_plug_xtask/src/lib.rs | 9 +++++---- nih_plug_xtask/src/util.rs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 nih_plug_xtask/src/util.rs diff --git a/nih_plug_xtask/src/lib.rs b/nih_plug_xtask/src/lib.rs index 605718d2..e93ba8e5 100644 --- a/nih_plug_xtask/src/lib.rs +++ b/nih_plug_xtask/src/lib.rs @@ -9,6 +9,7 @@ use std::process::Command; use std::os::unix::fs::PermissionsExt; mod symbols; +mod util; /// Re-export for the main function. pub use anyhow::Result; @@ -255,7 +256,7 @@ fn bundle_binary( fs::create_dir_all(standalone_binary_path.parent().unwrap()) .context("Could not create standalone bundle directory")?; - reflink::reflink_or_copy(&bin_path, &standalone_binary_path) + util::reflink(&bin_path, &standalone_binary_path) .context("Could not copy binary to standalone bundle")?; // FIXME: The reflink crate seems to sometime strip away the executable bit, so we need to help @@ -325,7 +326,7 @@ fn bundle_plugin( fs::create_dir_all(clap_lib_path.parent().unwrap()) .context("Could not create CLAP bundle directory")?; - reflink::reflink_or_copy(&lib_path, &clap_lib_path) + util::reflink(&lib_path, &clap_lib_path) .context("Could not copy library to CLAP bundle")?; // In contrast to VST3, CLAP only uses bundles on macOS, so we'll just take the first @@ -352,7 +353,7 @@ fn bundle_plugin( fs::create_dir_all(vst2_lib_path.parent().unwrap()) .context("Could not create VST2 bundle directory")?; - reflink::reflink_or_copy(&lib_path, &vst2_lib_path) + util::reflink(&lib_path, &vst2_lib_path) .context("Could not copy library to VST2 bundle")?; // VST2 only uses bundles on macOS, so we'll just take the first component of the library @@ -379,7 +380,7 @@ fn bundle_plugin( fs::create_dir_all(vst3_lib_path.parent().unwrap()) .context("Could not create VST3 bundle directory")?; - reflink::reflink_or_copy(&lib_path, &vst3_lib_path) + util::reflink(&lib_path, &vst3_lib_path) .context("Could not copy library to VST3 bundle")?; let vst3_bundle_home = vst3_lib_path diff --git a/nih_plug_xtask/src/util.rs b/nih_plug_xtask/src/util.rs new file mode 100644 index 00000000..80972cb4 --- /dev/null +++ b/nih_plug_xtask/src/util.rs @@ -0,0 +1,15 @@ +use anyhow::{Context, Result}; +use std::fs; +use std::path::Path; + +/// Acts the same as [`reflink::reflink_or_copy()`], but it removes existing files first. This works +/// around a limitation of macOS that the reflink crate also applies to other platforms to stay +/// consistent. See the [`reflink`] crate documentation or #26 for more information. +pub fn reflink, Q: AsRef>(from: P, to: Q) -> Result> { + let to = to.as_ref(); + if to.exists() { + fs::remove_file(to).context("Could not remove file before reflinking")?; + } + + reflink::reflink_or_copy(from, to).context("Could not reflink or copy file") +}