feat: Make the rename effect (#1669)

* feat: Make the rename effect

* chore: Change to dst and src

* chore: update the remove file to use dst src
This commit is contained in:
J M
2022-07-20 13:42:54 -06:00
committed by GitHub
parent 0f027fefb8
commit 5fa743755d
4 changed files with 204 additions and 28 deletions

View File

@@ -259,6 +259,7 @@ impl JsExecutionEnvironment {
fns::read_file::decl(),
fns::metadata::decl(),
fns::write_file::decl(),
fns::rename::decl(),
fns::remove_file::decl(),
fns::create_dir::decl(),
fns::remove_dir::decl(),
@@ -529,6 +530,56 @@ mod fns {
Ok(())
}
#[op]
async fn rename(
state: Rc<RefCell<OpState>>,
src_volume: VolumeId,
src_path: PathBuf,
dst_volume: VolumeId,
dst_path: PathBuf,
) -> Result<(), AnyError> {
let state = state.borrow();
let ctx: &JsContext = state.borrow();
let volume_path = ctx
.volumes
.path_for(&ctx.datadir, &ctx.package_id, &ctx.version, &src_volume)
.ok_or_else(|| anyhow!("There is no {} in volumes", src_volume))?;
let volume_path_out = ctx
.volumes
.path_for(&ctx.datadir, &ctx.package_id, &ctx.version, &dst_volume)
.ok_or_else(|| anyhow!("There is no {} in volumes", dst_volume))?;
if ctx.volumes.readonly(&dst_volume) {
bail!("Volume {} is readonly", dst_volume);
}
let old_file = volume_path.join(src_path);
let parent_old_file = old_file
.parent()
.ok_or_else(|| anyhow!("Expecting that file is not root"))?;
// With the volume check
if !is_subset(&volume_path, &parent_old_file).await? {
bail!(
"Path '{}' has broken away from parent '{}'",
old_file.to_string_lossy(),
volume_path.to_string_lossy(),
);
}
let new_file = volume_path_out.join(dst_path);
let parent_new_file = new_file
.parent()
.ok_or_else(|| anyhow!("Expecting that file is not root"))?;
// With the volume check
if !is_subset(&volume_path_out, &parent_new_file).await? {
bail!(
"Path '{}' has broken away from parent '{}'",
new_file.to_string_lossy(),
volume_path_out.to_string_lossy(),
);
}
tokio::fs::rename(old_file, new_file).await?;
Ok(())
}
#[op]
async fn remove_file(
state: Rc<RefCell<OpState>>,
volume_id: VolumeId,