feat: Add in the read dir. (#2141)

* feat: Add in the read dir.

Have a test that proves that this is working.

* chore: Let the read dir work while in a read only mode

* revert: To old sync
This commit is contained in:
J H
2023-02-17 12:32:18 -07:00
committed by Aiden McClelland
parent 4d3df867da
commit 2736fa5202
4 changed files with 138 additions and 1 deletions

View File

@@ -114,6 +114,10 @@ const readJsonFile = async (
const createDir = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("create_dir", volumeId, path);
const readDir = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("read_dir", volumeId, path);
const removeDir = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("remove_dir", volumeId, path);
@@ -186,7 +190,8 @@ const effects = {
sleep,
runDaemon,
signalGroup,
runRsync
runRsync,
readDir
};
const defaults = {

View File

@@ -281,6 +281,7 @@ impl JsExecutionEnvironment {
fns::remove_file::decl(),
fns::create_dir::decl(),
fns::remove_dir::decl(),
fns::read_dir::decl(),
fns::current_function::decl(),
fns::log_trace::decl(),
fns::log_warn::decl(),
@@ -844,6 +845,49 @@ mod fns {
tokio::fs::create_dir_all(new_file).await?;
Ok(())
}
#[op]
async fn read_dir(
state: Rc<RefCell<OpState>>,
volume_id: VolumeId,
path_in: PathBuf,
) -> Result<Vec<String>, AnyError> {
let volume_path = {
let state = state.borrow();
let ctx: &JsContext = state.borrow();
let volume_path = ctx
.volumes
.path_for(&ctx.datadir, &ctx.package_id, &ctx.version, &volume_id)
.ok_or_else(|| anyhow!("There is no {} in volumes", volume_id))?;
volume_path
};
let new_file = volume_path.join(path_in);
// With the volume check
if !is_subset(&volume_path, &new_file).await? {
bail!(
"Path '{}' has broken away from parent '{}'",
new_file.to_string_lossy(),
volume_path.to_string_lossy(),
);
}
let mut reader = tokio::fs::read_dir(&new_file).await?;
let mut paths: Vec<String> = Vec::new();
let origin_path = format!("{}/", new_file.to_str().unwrap_or_default());
let remove_new_file = |other_path: String| other_path.replacen(&origin_path, "", 1);
let has_origin_path = |other_path: &String| other_path.starts_with(&origin_path);
while let Some(entry) = reader.next_entry().await? {
entry
.path()
.to_str()
.into_iter()
.map(ToString::to_string)
.filter(&has_origin_path)
.map(&remove_new_file)
.for_each(|x| paths.push(x));
}
paths.sort();
Ok(paths)
}
#[op]
fn current_function(state: &mut OpState) -> Result<String, AnyError> {