From 59df6db74d28507a3a469d70560fd093c587925c Mon Sep 17 00:00:00 2001 From: sudeeptarlekar Date: Mon, 8 Jan 2024 15:56:25 +0100 Subject: [PATCH] Add function in rust core Added function in rs bindings to that it will be available in the node bindings on frontend. --- application/apps/indexer/session/Cargo.toml | 1 + .../apps/indexer/session/src/unbound/api.rs | 18 ++++++++++++++++++ .../session/src/unbound/commands/file.rs | 13 +++++++++++++ .../session/src/unbound/commands/mod.rs | 7 +++++++ .../apps/rustcore/rs-bindings/Cargo.lock | 8 ++++++++ .../rustcore/rs-bindings/src/js/jobs/mod.rs | 18 ++++++++++++++++++ 6 files changed, 65 insertions(+) create mode 100644 application/apps/indexer/session/src/unbound/commands/file.rs diff --git a/application/apps/indexer/session/Cargo.toml b/application/apps/indexer/session/Cargo.toml index e8efd0c62..b90ef46a6 100644 --- a/application/apps/indexer/session/Cargo.toml +++ b/application/apps/indexer/session/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] crossbeam-channel = "0.5" dirs = "5.0" +file-tools = { path = "../addons/file-tools" } futures = "0.3" indexer_base = { path = "../indexer_base" } lazy_static = "1.4" diff --git a/application/apps/indexer/session/src/unbound/api.rs b/application/apps/indexer/session/src/unbound/api.rs index 66a802f7a..96264c213 100644 --- a/application/apps/indexer/session/src/unbound/api.rs +++ b/application/apps/indexer/session/src/unbound/api.rs @@ -1,6 +1,7 @@ use crate::events::ComputationError; use processor::search::filter::SearchFilter; use serde::Serialize; +use std::path::Path; use tokio::sync::{mpsc::UnboundedSender, oneshot}; use super::commands::{Command, CommandOutcome}; @@ -103,6 +104,23 @@ impl UnboundSessionAPI { .await } + pub async fn is_file_binary( + &self, + id: u64, + file_path: &'static Path + ) -> Result, ComputationError> { + let (tx_results, rx_results) = oneshot::channel(); + self.process_command( + id, + rx_results, + Command::IsFileBinary( + file_path, + tx_results + ) + ) + .await + } + pub async fn spawn_process( &self, id: u64, diff --git a/application/apps/indexer/session/src/unbound/commands/file.rs b/application/apps/indexer/session/src/unbound/commands/file.rs new file mode 100644 index 000000000..456e6d0e4 --- /dev/null +++ b/application/apps/indexer/session/src/unbound/commands/file.rs @@ -0,0 +1,13 @@ +use super::{CommandOutcome, CommandOutcome::Finished}; +use crate::events::{ComputationError, ComputationError::OperationNotSupported}; +use std::path::Path; +use file_tools::is_binary; + +pub fn is_file_binary( + file_path: &Path +) -> Result, ComputationError> { + match is_binary(file_path) { + Ok(is_binary) => Ok(Finished(is_binary)), + Err(err) => Err(OperationNotSupported(err.to_string())) + } +} diff --git a/application/apps/indexer/session/src/unbound/commands/mod.rs b/application/apps/indexer/session/src/unbound/commands/mod.rs index 04b545644..64f86164c 100644 --- a/application/apps/indexer/session/src/unbound/commands/mod.rs +++ b/application/apps/indexer/session/src/unbound/commands/mod.rs @@ -1,6 +1,7 @@ mod cancel_test; mod checksum; mod dlt; +mod file; mod folder; mod process; mod regex; @@ -8,6 +9,8 @@ mod serial; mod shells; mod someip; +use std::path::Path; + use crate::{events::ComputationError, unbound::commands::someip::get_someip_statistic}; use log::{error, trace}; @@ -73,6 +76,7 @@ pub enum Command { GetShellProfiles(oneshot::Sender, ComputationError>>), GetContextEnvvars(oneshot::Sender, ComputationError>>), SerialPortsList(oneshot::Sender>, ComputationError>>), + IsFileBinary(&'static Path, oneshot::Sender, ComputationError>>), CancelTest( i64, i64, @@ -96,6 +100,7 @@ impl std::fmt::Display for Command { Command::GetDltStats(_, _) => "Getting dlt stats", Command::GetSomeipStatistic(_, _) => "Getting someip statistic", Command::GetRegexError(_, _) => "Checking regex", + Command::IsFileBinary(_, _) => "Checking if file is binary", } ) } @@ -129,6 +134,7 @@ pub async fn process(command: Command, signal: Signal) { Command::GetShellProfiles(tx) => tx.send(shells::get_valid_profiles(signal)).is_err(), Command::GetContextEnvvars(tx) => tx.send(shells::get_context_envvars(signal)).is_err(), Command::SerialPortsList(tx) => tx.send(serial::available_ports(signal)).is_err(), + Command::IsFileBinary(file_path, tx) => tx.send(file::is_file_binary(file_path)).is_err(), Command::CancelTest(a, b, tx) => tx .send(cancel_test::cancel_test(a, b, signal).await) .is_err(), @@ -149,6 +155,7 @@ pub async fn err(command: Command, err: ComputationError) { Command::GetShellProfiles(tx) => tx.send(Err(err)).is_err(), Command::GetContextEnvvars(tx) => tx.send(Err(err)).is_err(), Command::SerialPortsList(tx) => tx.send(Err(err)).is_err(), + Command::IsFileBinary(_filepath, tx) => tx.send(Err(err)).is_err(), Command::CancelTest(_a, _b, tx) => tx.send(Err(err)).is_err(), } { error!("Fail to send error response for command: {cmd}"); diff --git a/application/apps/rustcore/rs-bindings/Cargo.lock b/application/apps/rustcore/rs-bindings/Cargo.lock index f81b20160..c837eff9b 100644 --- a/application/apps/rustcore/rs-bindings/Cargo.lock +++ b/application/apps/rustcore/rs-bindings/Cargo.lock @@ -855,6 +855,13 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "file-tools" +version = "0.1.0" +dependencies = [ + "anyhow", +] + [[package]] name = "fluvio-future" version = "0.6.0" @@ -2568,6 +2575,7 @@ dependencies = [ "dirs", "dlt-core", "envvars", + "file-tools", "futures 0.3.28", "indexer_base", "lazy_static", diff --git a/application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs b/application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs index 627ee28bc..13e03b75a 100644 --- a/application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs +++ b/application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs @@ -15,6 +15,7 @@ use session::{ use std::{convert::TryFrom, thread}; use tokio::runtime::Runtime; use tokio_util::sync::CancellationToken; +use std::path::Path; struct UnboundJobs { api: Option, @@ -135,6 +136,23 @@ impl UnboundJobs { .map(CommandOutcomeWrapper) } + async fn is_file_binary( + &self, + id: i64, + file_path: &'static Path, + ) -> Result, ComputationErrorWrapper> { + self.api + .as_ref() + .ok_or(ComputationError::SessionUnavailable)? + .is_file_binary( + id_from_i64(id)?, + file_path + ) + .await + .map_err(ComputationErrorWrapper) + .map(CommandOutcomeWrapper) + } + #[node_bindgen] async fn spawn_process( &self,