From df062d2c788fd76546d59c67452d8d0fe569c533 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 5 Jan 2024 18:28:33 +0530 Subject: fix(ext/node): add fs.cp, fs.cpSync, promises.cp (#21745) Fixes https://github.com/denoland/deno/issues/20803 Fixes https://github.com/denoland/deno/issues/21723 Performance: copying a 48GiB rust `target` folder (recursive) | Platform | `deno` | `node v21.5` | Improvement | | -------- | ------- | ------- | ------- | | macOS (APFS) | 3.1secs | 127.99 secs | **42x** | | Windows | 18.3secs | 67.2secs | **3.8x** | Copying files with varying sizes: ![image](https://github.com/denoland/deno/assets/34997667/58932652-6f7a-47f5-8504-896dc9ab4ddc) --- ext/node/ops/fs.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'ext/node/ops') diff --git a/ext/node/ops/fs.rs b/ext/node/ops/fs.rs index 8e4805f6c..c5ae2371e 100644 --- a/ext/node/ops/fs.rs +++ b/ext/node/ops/fs.rs @@ -1,6 +1,9 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use std::cell::RefCell; +use std::path::Path; use std::path::PathBuf; +use std::rc::Rc; use deno_core::error::AnyError; use deno_core::op2; @@ -24,3 +27,54 @@ where let fs = state.borrow::(); Ok(fs.lstat_sync(&path).is_ok()) } + +#[op2(fast)] +pub fn op_node_cp_sync

( + state: &mut OpState, + #[string] path: &str, + #[string] new_path: &str, +) -> Result<(), AnyError> +where + P: NodePermissions + 'static, +{ + let path = Path::new(path); + let new_path = Path::new(new_path); + + state + .borrow_mut::

() + .check_read_with_api_name(path, Some("node:fs.cpSync"))?; + state + .borrow_mut::

() + .check_write_with_api_name(new_path, Some("node:fs.cpSync"))?; + + let fs = state.borrow::(); + fs.cp_sync(path, new_path)?; + Ok(()) +} + +#[op2(async)] +pub async fn op_node_cp

( + state: Rc>, + #[string] path: String, + #[string] new_path: String, +) -> Result<(), AnyError> +where + P: NodePermissions + 'static, +{ + let path = PathBuf::from(path); + let new_path = PathBuf::from(new_path); + + let fs = { + let mut state = state.borrow_mut(); + state + .borrow_mut::

() + .check_read_with_api_name(&path, Some("node:fs.cpSync"))?; + state + .borrow_mut::

() + .check_write_with_api_name(&new_path, Some("node:fs.cpSync"))?; + state.borrow::().clone() + }; + + fs.cp_async(path, new_path).await?; + Ok(()) +} -- cgit v1.2.3