Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect file type #1935

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions application/apps/indexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion application/apps/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
resolver = "2"

members = [
"addon",
"addons/dlt-tools",
"addons/file-tools",
"indexer_base",
"indexer_cli",
"merging",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "addon"
name = "dlt-tools"
version = "0.1.0"
edition = "2021"

[dependencies]
dlt-core = "0.14"
futures = "0.3"
indexer_base = { path = "../indexer_base" }
indexer_base = { path = "../../indexer_base" }
log = "0.4.17"
parsers = { path = "../parsers" }
sources = { path = "../sources" }
parsers = { path = "../../parsers" }
sources = { path = "../../sources" }
tokio = { version = "1.17", features = ["full"] }
tokio-util = { version = "0.7", features = ["codec", "net"] }
tokio-stream = "0.1.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
use super::*;
use std::path::Path;

const DLT_FT_SAMPLE: &str = "../../../../application/developing/resources/attachments.dlt";
const DLT_FT_SAMPLE: &str = "../../../../../application/developing/resources/attachments.dlt";

#[tokio::test]
async fn test_scan_dlt_ft() {
Expand Down
9 changes: 9 additions & 0 deletions application/apps/indexer/addons/file-tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "file-tools"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
101 changes: 101 additions & 0 deletions application/apps/indexer/addons/file-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use anyhow::Result;
use std::{
fs::{metadata, File},
io::Read,
path::Path,
str::from_utf8,
};

const BYTES_TO_READ: u64 = 10240;

pub fn is_binary(file_path: String) -> Result<bool> {
let chunks = fetch_starting_chunk(Path::new(&file_path));
let buffer = match chunks {
Ok(buffer) => buffer,
Err(err) => return Err(err),
};

Ok(from_utf8(&buffer).map_or(true, |_file_content| false))
}

fn fetch_starting_chunk(file_path: &Path) -> Result<Vec<u8>> {
let bytes_to_read: u64 = (metadata(file_path)?.len().max(1) - 1).min(BYTES_TO_READ);

let mut buffer = Vec::new();
File::open(file_path)?
.take(bytes_to_read)
.read_to_end(&mut buffer)?;
Ok(buffer)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_fetch_starting_chunk() -> Result<()> {
let chunks: Vec<u8> = fetch_starting_chunk(Path::new(
"../../../../developing/resources/chinese_poem.txt",
))?;
assert_eq!(chunks[0..5], [32, 32, 32, 32, 229]);
Ok(())
}

#[test]
fn test_fetch_starting_chunk_when_file_is_missing() -> Result<()> {
assert!(
fetch_starting_chunk(Path::new("../../developing/resources/chinese_poem.txt")).is_err()
);
Ok(())
}

#[test]
fn test_fetch_starting_chunk_when_file_is_empty() -> Result<()> {
let chunks: Vec<u8> =
fetch_starting_chunk(Path::new("../../../../developing/resources/empty.txt"))?;
assert_eq!(chunks, []);
Ok(())
}

#[test]
fn test_is_binary_when_file_is_binary() -> Result<()> {
assert!(is_binary(String::from(
"../../../../developing/resources/attachments.dlt"
))?);
assert!(is_binary(String::from(
"../../../../developing/resources/someip.pcap"
))?);
assert!(is_binary(String::from(
"../../../../developing/resources/someip.pcapng"
))?);
Ok(())
}

#[test]
fn test_is_binary_when_file_is_not_binary() -> Result<()> {
assert!(!is_binary(String::from(
"../../../../developing/resources/chinese_poem.txt"
))?);
assert!(!is_binary(String::from(
"../../../../developing/resources/sample_utf_8.txt"
))?);
assert!(!is_binary(String::from(
"../../../../developing/resources/someip.xml"
))?);
Ok(())
}

#[test]
fn test_is_binary_when_wrong_file_path_is_given() -> Result<()> {
assert!(is_binary(String::from("../../developing/resources/empty.text")).is_err());
Ok(())
}

#[test]
fn test_is_binary_when_file_is_empty() -> Result<()> {
assert!(!is_binary(String::from(
"../../../../developing/resources/empty.txt"
))?);
Ok(())
}
}
2 changes: 1 addition & 1 deletion application/apps/indexer/indexer_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ indexer_base = { path = "../indexer_base" }
indicatif = "0.17"
lazy_static = "1.4"
log = "0.4"
addon = { path = "../addon" }
dlt-tools = { path = "../addons/dlt-tools" }
parsers = { path = "../parsers" }
processor = { path = "../processor" }
session = { path = "../session" }
Expand Down
2 changes: 1 addition & 1 deletion application/apps/indexer/indexer_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ extern crate lazy_static;
mod interactive;

use crate::interactive::handle_interactive_session;
use addon::{extract_dlt_ft, scan_dlt_ft};
use anyhow::{anyhow, Result};
use dlt_core::{
fibex::FibexConfig,
filtering::{read_filter_options, DltFilterConfig},
parse::DltParseError,
statistics::{collect_dlt_stats, count_dlt_messages as count_dlt_messages_old},
};
use dlt_tools::{extract_dlt_ft, scan_dlt_ft};
use env_logger::Env;
use futures::{pin_mut, stream::StreamExt};
use indexer_base::config::*;
Expand Down
1 change: 1 addition & 0 deletions application/apps/indexer/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crossbeam-channel = "0.5"
dirs = "5.0"
dlt-core = "0.14"
envvars = "0.1"
file-tools = { path = "../addons/file-tools" }
futures = "0.3"
indexer_base = { path = "../indexer_base" }
lazy_static = "1.4"
Expand Down
10 changes: 10 additions & 0 deletions application/apps/indexer/session/src/unbound/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ impl UnboundSessionAPI {
.await
}

pub async fn is_file_binary(
&self,
id: u64,
file_path: String,
) -> Result<CommandOutcome<bool>, 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,
Expand Down
9 changes: 9 additions & 0 deletions application/apps/indexer/session/src/unbound/commands/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::{CommandOutcome, CommandOutcome::Finished};
use crate::events::{ComputationError, ComputationError::OperationNotSupported};
use file_tools::is_binary;

pub fn is_file_binary(file_path: String) -> Result<CommandOutcome<bool>, ComputationError> {
is_binary(file_path)
.map(Finished)
.map_err(|err| OperationNotSupported(err.to_string()))
}
8 changes: 8 additions & 0 deletions application/apps/indexer/session/src/unbound/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod cancel_test;
mod checksum;
mod dlt;
mod file;
mod folder;
mod process;
mod regex;
Expand Down Expand Up @@ -79,6 +80,10 @@ pub enum Command {
GetShellProfiles(oneshot::Sender<Result<CommandOutcome<String>, ComputationError>>),
GetContextEnvvars(oneshot::Sender<Result<CommandOutcome<String>, ComputationError>>),
SerialPortsList(oneshot::Sender<Result<CommandOutcome<Vec<String>>, ComputationError>>),
IsFileBinary(
String,
oneshot::Sender<Result<CommandOutcome<bool>, ComputationError>>,
),
CancelTest(
i64,
i64,
Expand All @@ -103,6 +108,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",
}
)
}
Expand Down Expand Up @@ -137,6 +143,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(),
Expand All @@ -158,6 +165,7 @@ pub 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}");
Expand Down
8 changes: 8 additions & 0 deletions application/apps/rustcore/rs-bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions application/apps/rustcore/rs-bindings/src/js/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ impl UnboundJobs {
.map(CommandOutcomeWrapper)
}

#[node_bindgen]
async fn is_file_binary(
&self,
id: i64,
file_path: String,
) -> Result<CommandOutcomeWrapper<bool>, ComputationErrorWrapper> {
self.api
.as_ref()
.ok_or(ComputationError::SessionUnavailable)?
.is_file_binary(u64_from_i64(id)?, file_path)
.await
.map_err(ComputationErrorWrapper)
.map(CommandOutcomeWrapper)
}

#[node_bindgen]
async fn spawn_process(
&self,
Expand Down
7 changes: 3 additions & 4 deletions application/apps/rustcore/ts-bindings/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "Jasmine Current File",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
"args": ["--config=${workspaceFolder}/spec/support/jasmine.json", "${file}"],
"program": "${workspaceFolder}/node_modules/.bin/jasmine",
"args": ["${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
}
Loading
Loading