Skip to content

Commit fd6fcbb

Browse files
committed
EIM-117 added copying of openocd rules on linux
1 parent d26907d commit fd6fcbb

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

src-tauri/src/lib/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use reqwest::Client;
88
#[cfg(feature = "userustpython")]
99
use rustpython_vm::literal::char;
1010
use sha2::{Digest, Sha256};
11+
use system_dependencies::copy_openocd_rules;
1112
use std::fs::metadata;
1213
use std::io::BufReader;
1314
use tar::Archive;
@@ -1494,6 +1495,11 @@ pub fn single_version_post_install(
14941495
export_paths,
14951496
env_vars,
14961497
);
1498+
// copy openocd rules (it's noop on macOs)
1499+
match copy_openocd_rules(tool_install_directory) {
1500+
Ok(_) => info!("OpenOCD rules copied successfully"),
1501+
Err(err) => error!("Failed to copy OpenOCD rules: {:?}", err),
1502+
}
14971503
}
14981504
}
14991505
}

src-tauri/src/lib/system_dependencies.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::env;
1+
use std::{env, fs};
2+
use anyhow::{anyhow, Result, Context};
23

34
use log::{debug, trace, warn};
45

5-
use crate::command_executor;
6+
use crate::{command_executor, utils::find_by_name_and_extension};
67

78
/// Determines the package manager installed on the system.
89
///
@@ -608,3 +609,47 @@ fn add_to_path(new_path: &str) -> Result<String, std::io::Error> {
608609

609610
Ok(new_path_string)
610611
}
612+
613+
/// Copies the 60-openocd.rules file to /etc/udev/rules.d/ on Linux.
614+
///
615+
/// This function checks if the rules file already exists. If not, it attempts
616+
/// to find it within the provided `tools_path` and copy it.
617+
///
618+
/// # Arguments
619+
/// * `tools_path` - The path where tool-related files might be located,
620+
/// including the openocd rules file.
621+
///
622+
/// # Returns
623+
/// A `Result` indicating success (`Ok(())`) or an `anyhow::Error` if
624+
/// an error occurs during file operations or if the file is not found.
625+
pub fn copy_openocd_rules(tools_path: &str) -> Result<()> {
626+
let openocd_rules_path = match std::env::consts::OS {
627+
"linux" => "/etc/udev/rules.d/60-openocd.rules",
628+
_ => return Ok(()),
629+
};
630+
let openocd_rules_path = std::path::Path::new(openocd_rules_path);
631+
if openocd_rules_path.exists() {
632+
debug!("openocd rules file already exists");
633+
return Ok(());
634+
}
635+
636+
let tools_path = std::path::Path::new(tools_path);
637+
638+
let found_files = find_by_name_and_extension(tools_path, "60-openocd", "rules");
639+
640+
let openocd_rules_source = found_files.first().ok_or_else(|| {
641+
anyhow!(
642+
"60-openocd.rules file not found in {}",
643+
tools_path.display()
644+
)
645+
})?;
646+
fs::copy(openocd_rules_source, openocd_rules_path).with_context(|| {
647+
format!(
648+
"Failed to copy {} to {}",
649+
openocd_rules_source,
650+
openocd_rules_path.display()
651+
)
652+
})?;
653+
654+
Ok(())
655+
}

src-tauri/src/lib/utils.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,46 @@ pub fn find_directories_by_name(path: &Path, name: &str) -> Vec<String> {
8989
filter_directories(filter_subpaths(search))
9090
}
9191

92+
/// Searches for files within a specified directory that match a given name and extension.
93+
///
94+
/// This function constructs a search query using `SearchBuilder` to find files.
95+
/// The search is strict, case-insensitive, includes hidden files, and looks for
96+
/// an exact match for the file name and extension.
97+
///
98+
/// # Arguments
99+
///
100+
/// * `path` - A reference to a `Path` indicating the directory where the search should begin.
101+
/// * `name` - A string slice representing the exact name of the file to search for (without the extension).
102+
/// * `extension` - A string slice representing the exact extension of the file to search for (e.g., "txt", "rs").
103+
///
104+
/// # Returns
105+
///
106+
/// A `Vec<String>` containing the paths of all files found that match the criteria.
107+
/// The paths are returned as strings. If no files are found, an empty vector is returned.
108+
///
109+
/// # Examples
110+
///
111+
/// ```no_run
112+
/// use std::path::Path;
113+
///
114+
/// let search_path = Path::new("./my_directory");
115+
/// let found_files = find_by_name_and_extension(search_path, "document", "pdf");
116+
/// for file in found_files {
117+
/// println!("Found: {}", file);
118+
/// }
119+
/// ```
120+
pub fn find_by_name_and_extension(path: &Path, name: &str, extension: &str) -> Vec<String> {
121+
SearchBuilder::default()
122+
.location(path)
123+
.search_input(name)
124+
.ext(extension)
125+
.strict()
126+
.ignore_case()
127+
.hidden()
128+
.build()
129+
.collect()
130+
}
131+
92132
/// Checks if the given path is a valid ESP-IDF directory.
93133
///
94134
/// # Purpose

0 commit comments

Comments
 (0)