diff --git a/wnfs-wasm/src/fs/public/directory.rs b/wnfs-wasm/src/fs/public/directory.rs index 1d952646..f55f35b6 100644 --- a/wnfs-wasm/src/fs/public/directory.rs +++ b/wnfs-wasm/src/fs/public/directory.rs @@ -211,6 +211,30 @@ impl PublicDirectory { })) } + /// Copies a specific node to another location. + pub fn cp( + &self, + path_segments_from: &Array, + path_segments_to: &Array, + time: &Date, + store: BlockStore, + ) -> JsResult { + let mut directory = Rc::clone(&self.0); + let store = ForeignBlockStore(store); + let time = DateTime::::from(time); + let path_segments_from = utils::convert_path_segments(path_segments_from)?; + let path_segments_to = utils::convert_path_segments(path_segments_to)?; + + Ok(future_to_promise(async move { + (&mut directory) + .cp(&path_segments_from, &path_segments_to, time, &store) + .await + .map_err(error("Cannot copy content between directories"))?; + + Ok(utils::create_public_op_result(directory, JsValue::NULL)?) + })) + } + /// Creates a new directory at the specified path. /// /// This method acts like `mkdir -p` in Unix because it creates intermediate directories if they do not exist. diff --git a/wnfs-wasm/tests/public.spec.ts b/wnfs-wasm/tests/public.spec.ts index 77215c88..7a01cf5f 100644 --- a/wnfs-wasm/tests/public.spec.ts +++ b/wnfs-wasm/tests/public.spec.ts @@ -225,6 +225,52 @@ test.describe("PublicDirectory", () => { expect(imagesContent[0].name).toEqual("cats"); }); + test("cp can copy content between directories", async ({ page }) => { + const [imagesContent, picturesContent] = await page.evaluate(async () => { + const { + wnfs: { PublicDirectory }, + mock: { MemoryBlockStore, sampleCID }, + } = await window.setup(); + + const time = new Date(); + const store = new MemoryBlockStore(); + const root = new PublicDirectory(time); + + var { rootDir } = await root.write( + ["pictures", "cats", "luna.jpeg"], + sampleCID, + time, + store + ); + + var { rootDir } = await rootDir.write( + ["pictures", "cats", "tabby.png"], + sampleCID, + time, + store + ); + + var { rootDir } = await rootDir.mkdir(["images"], time, store); + + var { rootDir } = await rootDir.cp( + ["pictures", "cats"], + ["images", "cats"], + time, + store + ); + + const imagesContent = await rootDir.ls(["images"], store); + + const picturesContent = await rootDir.ls(["pictures"], store); + + return [imagesContent, picturesContent]; + }); + + expect(imagesContent.length).toEqual(1); + expect(picturesContent.length).toEqual(1); + expect(imagesContent[0].name).toEqual("cats"); + }); + test("A PublicDirectory has the correct metadata", async ({ page }) => { const result = await page.evaluate(async () => { const {