From c325de68b8560ca9ddb89add68a7faa4d0a23c47 Mon Sep 17 00:00:00 2001 From: morde Date: Tue, 23 Jul 2024 12:17:22 +0300 Subject: [PATCH] change: now the same for linux --- src/handlers/install_handler.rs | 2 +- src/helpers/filesystem.rs | 27 +++++++++++++++++++++++++-- src/helpers/unarchive.rs | 20 +++++++------------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/handlers/install_handler.rs b/src/handlers/install_handler.rs index 1e480c5..09d4bc9 100644 --- a/src/handlers/install_handler.rs +++ b/src/handlers/install_handler.rs @@ -234,7 +234,7 @@ async fn handle_rollback(config: &Config) -> Result<()> { .collect(); info!("Creating rollback: nightly-{id}"); - filesystem::copy_dir("nightly", format!("nightly-{id}")).await?; + filesystem::copy_dir_async("nightly", format!("nightly-{id}")).await?; json_struct.tag_name += &format!("-{id}"); diff --git a/src/helpers/filesystem.rs b/src/helpers/filesystem.rs index 6abe45e..f63fa32 100644 --- a/src/helpers/filesystem.rs +++ b/src/helpers/filesystem.rs @@ -89,7 +89,7 @@ pub async fn remove_dir(directory: &str) -> Result<()> { /// copy_dir(from, to).await; /// ``` #[async_recursion(?Send)] -pub async fn copy_dir( +pub async fn copy_dir_async( from: impl AsRef + 'static, to: impl AsRef + 'static, ) -> Result<()> { @@ -105,7 +105,7 @@ pub async fn copy_dir( if path.is_dir() { let new_dest = destination.join(path.file_name().unwrap()); - copy_dir(path, new_dest).await?; + copy_dir_async(path, new_dest).await?; } else { let new_dest = destination.join(path.file_name().unwrap()); fs::copy(path, new_dest).await?; @@ -114,3 +114,26 @@ pub async fn copy_dir( Ok(()) } + +pub fn copy_dir(from: impl AsRef, to: impl AsRef) -> Result<()> { + let original_path = from.as_ref().to_owned(); + let destination = to.as_ref().to_owned(); + + std::fs::create_dir(&destination)?; + + let mut entries = std::fs::read_dir(original_path)?; + + while let Some(entry) = entries.next() { + let path = entry?.path(); + + if path.is_dir() { + let new_dest = destination.join(path.file_name().unwrap()); + copy_dir(path, new_dest)?; + } else { + let new_dest = destination.join(path.file_name().unwrap()); + std::fs::copy(path, new_dest)?; + } + } + + Ok(()) +} diff --git a/src/helpers/unarchive.rs b/src/helpers/unarchive.rs index a642327..1eec3e4 100644 --- a/src/helpers/unarchive.rs +++ b/src/helpers/unarchive.rs @@ -106,9 +106,10 @@ pub async fn start(file: LocalVersion) -> Result<()> { /// ``` #[cfg(target_os = "linux")] fn expand(downloaded_file: LocalVersion) -> Result<()> { + use crate::helpers::filesystem::copy_dir; + use super::sync; - use std::env::set_current_dir; - use std::fs::{remove_file, rename}; + use std::fs::remove_dir_all; use std::os::unix::fs::PermissionsExt; use std::process::Command; @@ -126,18 +127,11 @@ fn expand(downloaded_file: LocalVersion) -> Result<()> { sync::handle_subprocess(Command::new(file).arg("--appimage-extract"))?; - rename("squashfs-root", &downloaded_file.file_name)?; - - set_current_dir(downloaded_file.file_name)?; - - for x in ["AppRun", "nvim.desktop", "nvim.png", ".DirIcon"] { - remove_file(x)?; - } - - rename("usr", "nvim-linux64")?; + let src_root = "squashfs-root"; + let dest = downloaded_file.file_name; - let parent_dir = std::env::current_dir()?.parent().unwrap().to_path_buf(); - std::env::set_current_dir(parent_dir)?; + copy_dir(Path::new(src_root).join("usr"), Path::new(&dest))?; + remove_dir_all(src_root)?; Ok(()) }