|
@@ -3,12 +3,17 @@
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
use super::InvokeResponse;
|
|
|
-use crate::api::path::BaseDirectory;
|
|
|
+use crate::{
|
|
|
+ api::{
|
|
|
+ dir, file,
|
|
|
+ path::{resolve_path, BaseDirectory},
|
|
|
+ },
|
|
|
+ Config,
|
|
|
+};
|
|
|
|
|
|
-use crate::api::{dir, file, path::resolve_path};
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
-use std::{fs, fs::File, io::Write, path::PathBuf};
|
|
|
+use std::{fs, fs::File, io::Write, path::PathBuf, sync::Arc};
|
|
|
|
|
|
/// The options for the directory functions on the file system API.
|
|
|
#[derive(Deserialize)]
|
|
@@ -96,17 +101,22 @@ pub enum Cmd {
|
|
|
}
|
|
|
|
|
|
impl Cmd {
|
|
|
- pub fn run(self) -> crate::Result<InvokeResponse> {
|
|
|
+ #[allow(unused_variables)]
|
|
|
+ pub fn run(self, config: Arc<Config>) -> crate::Result<InvokeResponse> {
|
|
|
match self {
|
|
|
#[cfg(fs_read_text_file)]
|
|
|
- Self::ReadTextFile { path, options } => read_text_file(path, options).map(Into::into),
|
|
|
+ Self::ReadTextFile { path, options } => {
|
|
|
+ read_text_file(&config, path, options).map(Into::into)
|
|
|
+ }
|
|
|
#[cfg(not(fs_read_text_file))]
|
|
|
Self::ReadTextFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > readTextFile".to_string(),
|
|
|
)),
|
|
|
|
|
|
#[cfg(fs_read_binary_file)]
|
|
|
- Self::ReadBinaryFile { path, options } => read_binary_file(path, options).map(Into::into),
|
|
|
+ Self::ReadBinaryFile { path, options } => {
|
|
|
+ read_binary_file(&config, path, options).map(Into::into)
|
|
|
+ }
|
|
|
#[cfg(not(fs_read_binary_file))]
|
|
|
Self::ReadBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"readBinaryFile".to_string(),
|
|
@@ -117,7 +127,7 @@ impl Cmd {
|
|
|
path,
|
|
|
contents,
|
|
|
options,
|
|
|
- } => write_file(path, contents, options).map(Into::into),
|
|
|
+ } => write_file(&config, path, contents, options).map(Into::into),
|
|
|
#[cfg(not(fs_write_file))]
|
|
|
Self::WriteFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > writeFile".to_string(),
|
|
@@ -128,14 +138,14 @@ impl Cmd {
|
|
|
path,
|
|
|
contents,
|
|
|
options,
|
|
|
- } => write_binary_file(path, contents, options).map(Into::into),
|
|
|
+ } => write_binary_file(&config, path, contents, options).map(Into::into),
|
|
|
#[cfg(not(fs_write_binary_file))]
|
|
|
Self::WriteBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"writeBinaryFile".to_string(),
|
|
|
)),
|
|
|
|
|
|
#[cfg(fs_read_dir)]
|
|
|
- Self::ReadDir { path, options } => read_dir(path, options).map(Into::into),
|
|
|
+ Self::ReadDir { path, options } => read_dir(&config, path, options).map(Into::into),
|
|
|
#[cfg(not(fs_read_dir))]
|
|
|
Self::ReadDir { .. } => Err(crate::Error::ApiNotAllowlisted("fs > readDir".to_string())),
|
|
|
|
|
@@ -144,26 +154,26 @@ impl Cmd {
|
|
|
source,
|
|
|
destination,
|
|
|
options,
|
|
|
- } => copy_file(source, destination, options).map(Into::into),
|
|
|
+ } => copy_file(&config, source, destination, options).map(Into::into),
|
|
|
#[cfg(not(fs_copy_file))]
|
|
|
Self::CopyFile { .. } => Err(crate::Error::ApiNotAllowlisted("fs > copyFile".to_string())),
|
|
|
|
|
|
#[cfg(fs_create_dir)]
|
|
|
- Self::CreateDir { path, options } => create_dir(path, options).map(Into::into),
|
|
|
+ Self::CreateDir { path, options } => create_dir(&config, path, options).map(Into::into),
|
|
|
#[cfg(not(fs_create_dir))]
|
|
|
Self::CreateDir { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > createDir".to_string(),
|
|
|
)),
|
|
|
|
|
|
#[cfg(fs_remove_dir)]
|
|
|
- Self::RemoveDir { path, options } => remove_dir(path, options).map(Into::into),
|
|
|
+ Self::RemoveDir { path, options } => remove_dir(&config, path, options).map(Into::into),
|
|
|
#[cfg(not(fs_remove_dir))]
|
|
|
Self::RemoveDir { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > removeDir".to_string(),
|
|
|
)),
|
|
|
|
|
|
#[cfg(fs_remove_file)]
|
|
|
- Self::RemoveFile { path, options } => remove_file(path, options).map(Into::into),
|
|
|
+ Self::RemoveFile { path, options } => remove_file(&config, path, options).map(Into::into),
|
|
|
#[cfg(not(fs_remove_file))]
|
|
|
Self::RemoveFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > removeFile".to_string(),
|
|
@@ -174,7 +184,7 @@ impl Cmd {
|
|
|
old_path,
|
|
|
new_path,
|
|
|
options,
|
|
|
- } => rename_file(old_path, new_path, options).map(Into::into),
|
|
|
+ } => rename_file(&config, old_path, new_path, options).map(Into::into),
|
|
|
#[cfg(not(fs_rename_file))]
|
|
|
Self::RenameFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
|
|
"fs > renameFile".to_string(),
|
|
@@ -182,7 +192,7 @@ impl Cmd {
|
|
|
|
|
|
#[cfg(fs_path)]
|
|
|
Self::ResolvePath { path, directory } => {
|
|
|
- resolve_path_handler(path, directory).map(Into::into)
|
|
|
+ resolve_path_handler(&config, path, directory).map(Into::into)
|
|
|
}
|
|
|
#[cfg(not(fs_path))]
|
|
|
Self::ResolvePath { .. } => Err(crate::Error::ApiNotAllowlisted("fs > pathApi".to_string())),
|
|
@@ -193,6 +203,7 @@ impl Cmd {
|
|
|
/// Reads a directory.
|
|
|
#[cfg(fs_read_dir)]
|
|
|
pub fn read_dir(
|
|
|
+ config: &Config,
|
|
|
path: PathBuf,
|
|
|
options: Option<DirOperationOptions>,
|
|
|
) -> crate::Result<Vec<dir::DiskEntry>> {
|
|
@@ -201,20 +212,22 @@ pub fn read_dir(
|
|
|
} else {
|
|
|
(false, None)
|
|
|
};
|
|
|
- dir::read_dir(resolve_path(path, dir)?, recursive).map_err(crate::Error::FailedToExecuteApi)
|
|
|
+ dir::read_dir(resolve_path(config, path, dir)?, recursive)
|
|
|
+ .map_err(crate::Error::FailedToExecuteApi)
|
|
|
}
|
|
|
|
|
|
/// Copies a file.
|
|
|
#[cfg(fs_copy_file)]
|
|
|
pub fn copy_file(
|
|
|
+ config: &Config,
|
|
|
source: PathBuf,
|
|
|
destination: PathBuf,
|
|
|
options: Option<FileOperationOptions>,
|
|
|
) -> crate::Result<()> {
|
|
|
let (src, dest) = match options.and_then(|o| o.dir) {
|
|
|
Some(dir) => (
|
|
|
- resolve_path(source, Some(dir.clone()))?,
|
|
|
- resolve_path(destination, Some(dir))?,
|
|
|
+ resolve_path(config, source, Some(dir.clone()))?,
|
|
|
+ resolve_path(config, destination, Some(dir))?,
|
|
|
),
|
|
|
None => (source, destination),
|
|
|
};
|
|
@@ -224,13 +237,17 @@ pub fn copy_file(
|
|
|
|
|
|
/// Creates a directory.
|
|
|
#[cfg(fs_create_dir)]
|
|
|
-pub fn create_dir(path: PathBuf, options: Option<DirOperationOptions>) -> crate::Result<()> {
|
|
|
+pub fn create_dir(
|
|
|
+ config: &Config,
|
|
|
+ path: PathBuf,
|
|
|
+ options: Option<DirOperationOptions>,
|
|
|
+) -> crate::Result<()> {
|
|
|
let (recursive, dir) = if let Some(options_value) = options {
|
|
|
(options_value.recursive, options_value.dir)
|
|
|
} else {
|
|
|
(false, None)
|
|
|
};
|
|
|
- let resolved_path = resolve_path(path, dir)?;
|
|
|
+ let resolved_path = resolve_path(config, path, dir)?;
|
|
|
if recursive {
|
|
|
fs::create_dir_all(resolved_path)?;
|
|
|
} else {
|
|
@@ -242,13 +259,17 @@ pub fn create_dir(path: PathBuf, options: Option<DirOperationOptions>) -> crate:
|
|
|
|
|
|
/// Removes a directory.
|
|
|
#[cfg(fs_remove_dir)]
|
|
|
-pub fn remove_dir(path: PathBuf, options: Option<DirOperationOptions>) -> crate::Result<()> {
|
|
|
+pub fn remove_dir(
|
|
|
+ config: &Config,
|
|
|
+ path: PathBuf,
|
|
|
+ options: Option<DirOperationOptions>,
|
|
|
+) -> crate::Result<()> {
|
|
|
let (recursive, dir) = if let Some(options_value) = options {
|
|
|
(options_value.recursive, options_value.dir)
|
|
|
} else {
|
|
|
(false, None)
|
|
|
};
|
|
|
- let resolved_path = resolve_path(path, dir)?;
|
|
|
+ let resolved_path = resolve_path(config, path, dir)?;
|
|
|
if recursive {
|
|
|
fs::remove_dir_all(resolved_path)?;
|
|
|
} else {
|
|
@@ -260,8 +281,12 @@ pub fn remove_dir(path: PathBuf, options: Option<DirOperationOptions>) -> crate:
|
|
|
|
|
|
/// Removes a file
|
|
|
#[cfg(fs_remove_file)]
|
|
|
-pub fn remove_file(path: PathBuf, options: Option<FileOperationOptions>) -> crate::Result<()> {
|
|
|
- let resolved_path = resolve_path(path, options.and_then(|o| o.dir))?;
|
|
|
+pub fn remove_file(
|
|
|
+ config: &Config,
|
|
|
+ path: PathBuf,
|
|
|
+ options: Option<FileOperationOptions>,
|
|
|
+) -> crate::Result<()> {
|
|
|
+ let resolved_path = resolve_path(config, path, options.and_then(|o| o.dir))?;
|
|
|
fs::remove_file(resolved_path)?;
|
|
|
Ok(())
|
|
|
}
|
|
@@ -269,14 +294,15 @@ pub fn remove_file(path: PathBuf, options: Option<FileOperationOptions>) -> crat
|
|
|
/// Renames a file.
|
|
|
#[cfg(fs_rename_file)]
|
|
|
pub fn rename_file(
|
|
|
+ config: &Config,
|
|
|
old_path: PathBuf,
|
|
|
new_path: PathBuf,
|
|
|
options: Option<FileOperationOptions>,
|
|
|
) -> crate::Result<()> {
|
|
|
let (old, new) = match options.and_then(|o| o.dir) {
|
|
|
Some(dir) => (
|
|
|
- resolve_path(old_path, Some(dir.clone()))?,
|
|
|
- resolve_path(new_path, Some(dir))?,
|
|
|
+ resolve_path(config, old_path, Some(dir.clone()))?,
|
|
|
+ resolve_path(config, new_path, Some(dir))?,
|
|
|
),
|
|
|
None => (old_path, new_path),
|
|
|
};
|
|
@@ -286,11 +312,12 @@ pub fn rename_file(
|
|
|
/// Writes a text file.
|
|
|
#[cfg(fs_write_file)]
|
|
|
pub fn write_file(
|
|
|
+ config: &Config,
|
|
|
path: PathBuf,
|
|
|
contents: String,
|
|
|
options: Option<FileOperationOptions>,
|
|
|
) -> crate::Result<()> {
|
|
|
- File::create(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
+ File::create(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(crate::Error::Io)
|
|
|
.and_then(|mut f| f.write_all(contents.as_bytes()).map_err(|err| err.into()))?;
|
|
|
Ok(())
|
|
@@ -299,6 +326,7 @@ pub fn write_file(
|
|
|
/// Writes a binary file.
|
|
|
#[cfg(fs_write_binary_file)]
|
|
|
pub fn write_binary_file(
|
|
|
+ config: &Config,
|
|
|
path: PathBuf,
|
|
|
contents: String,
|
|
|
options: Option<FileOperationOptions>,
|
|
@@ -306,7 +334,7 @@ pub fn write_binary_file(
|
|
|
base64::decode(contents)
|
|
|
.map_err(crate::Error::Base64Decode)
|
|
|
.and_then(|c| {
|
|
|
- File::create(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
+ File::create(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(Into::into)
|
|
|
.and_then(|mut f| f.write_all(&c).map_err(|err| err.into()))
|
|
|
})?;
|
|
@@ -316,29 +344,32 @@ pub fn write_binary_file(
|
|
|
/// Reads a text file.
|
|
|
#[cfg(fs_read_text_file)]
|
|
|
pub fn read_text_file(
|
|
|
+ config: &Config,
|
|
|
path: PathBuf,
|
|
|
options: Option<FileOperationOptions>,
|
|
|
) -> crate::Result<String> {
|
|
|
- file::read_string(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
+ file::read_string(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(crate::Error::FailedToExecuteApi)
|
|
|
}
|
|
|
|
|
|
/// Reads a binary file.
|
|
|
#[cfg(fs_read_binary_file)]
|
|
|
pub fn read_binary_file(
|
|
|
+ config: &Config,
|
|
|
path: PathBuf,
|
|
|
options: Option<FileOperationOptions>,
|
|
|
) -> crate::Result<Vec<u8>> {
|
|
|
- file::read_binary(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
+ file::read_binary(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(crate::Error::FailedToExecuteApi)
|
|
|
}
|
|
|
|
|
|
#[cfg(fs_path)]
|
|
|
pub fn resolve_path_handler(
|
|
|
+ config: &Config,
|
|
|
path: String,
|
|
|
directory: Option<BaseDirectory>,
|
|
|
) -> crate::Result<PathBuf> {
|
|
|
- resolve_path(path, directory).map_err(Into::into)
|
|
|
+ resolve_path(config, path, directory).map_err(Into::into)
|
|
|
}
|
|
|
|
|
|
// test webview functionality.
|