From 39d95575599d7e295815c32968d561fc71efeafa Mon Sep 17 00:00:00 2001 From: guidable0 <68713456+guidable0@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:17:16 +0200 Subject: [PATCH] Fix overwrite option for copying directories (#133) --- src/lune/builtins/fs/copy.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lune/builtins/fs/copy.rs b/src/lune/builtins/fs/copy.rs index 677d0b2..10c77fc 100644 --- a/src/lune/builtins/fs/copy.rs +++ b/src/lune/builtins/fs/copy.rs @@ -138,7 +138,16 @@ pub async fn copy( let contents = get_contents_at(source.to_path_buf(), options).await?; if options.overwrite { - fs::remove_dir_all(target).await?; + let (is_dir, is_file) = match fs::metadata(&target).await { + Ok(meta) => (meta.is_dir(), meta.is_file()), + Err(e) if e.kind() == ErrorKind::NotFound => (false, false), + Err(e) => return Err(e.into()), + }; + if is_dir { + fs::remove_dir_all(target).await?; + } else if is_file { + fs::remove_file(target).await?; + } } // FUTURE: Write dirs / files concurrently