|
1 | | -use std::env; |
| 1 | +use std::{env, fs}; |
| 2 | +use anyhow::{anyhow, Result, Context}; |
2 | 3 |
|
3 | 4 | use log::{debug, trace, warn}; |
4 | 5 |
|
5 | | -use crate::command_executor; |
| 6 | +use crate::{command_executor, utils::find_by_name_and_extension}; |
6 | 7 |
|
7 | 8 | /// Determines the package manager installed on the system. |
8 | 9 | /// |
@@ -608,3 +609,47 @@ fn add_to_path(new_path: &str) -> Result<String, std::io::Error> { |
608 | 609 |
|
609 | 610 | Ok(new_path_string) |
610 | 611 | } |
| 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 | +} |
0 commit comments