mfc_cloner/
lib.rs

1use std::process::Command;
2use std::path::PathBuf;
3
4extern crate exitcode;
5
6
7/// Writes the given .mfd file to the destinatin .mfd file
8pub fn write_card(is_blank: bool, source_file: &PathBuf, dest_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {  
9
10  // Need to store Command::new into its own variable first
11  let mut command = Command::new("nfc-mfclassic");
12
13  // Determine write option depending on type of card received
14  if is_blank {
15      command.arg("W");
16  } else {
17      command.arg("w");
18  }
19
20  // Add remaining arguments
21  let write_command = command
22                      .arg("a")
23                      .arg(source_file)
24                      .arg(dest_file)
25                      .output()?;
26
27  // Check for errors
28  if !write_command.status.success() {
29      eprintln!("Error: couldn't write to card");
30      std::process::exit(exitcode::USAGE);
31  }
32
33  Ok(())
34}
35
36
37/// Dumps the contents of a card into a .mfd with the given output name
38pub fn dump_card(key_file: Option<&PathBuf>, output_file_name: &str) -> Result<(), Box<dyn std::error::Error>> {   
39    
40  // Need to store Command::new into its own variable first
41  let mut command = Command::new("mfoc");
42  command.arg("-O")
43         .arg(format!("{}", output_file_name));
44
45  // Add the key file option, if it exists
46  if let Some(file) = key_file {
47      command.arg("-f").arg(file);
48  }
49
50  let dump_command = command.output()?;
51
52  // Check for errors
53  if !dump_command.status.success() {
54      eprintln!("Error: couldn't dump contents of card");
55      std::process::exit(exitcode::USAGE);
56  }
57
58  Ok(())
59}
60
61
62/// Formats a card so that it can be overwritten
63pub fn format_card(file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {   
64    
65    // Need to store Command::new into its own variable first
66    let mut command = Command::new("nfc-mfclassic");
67
68    // Add remaining arguments
69    let format_command = command
70                        .arg("f")
71                        .arg("B")
72                        .arg(file)
73                        .arg(file)
74                        .arg("f")
75                        .output()?;
76
77    // Check for errors
78    if !format_command.status.success() {
79        eprintln!("Error: couldn't format card");
80        std::process::exit(exitcode::USAGE);
81    }
82
83    Ok(())
84  }
85
86
87/// Sets the card to have the given UID
88pub fn set_card_uid(uid: &str) -> Result<(), Box<dyn std::error::Error>> {
89
90    let mut command = Command::new("nfc-mfsetuid");
91  
92    let set_uid_command = command
93                         .arg(uid)
94                         .output()?;
95
96    if !set_uid_command.status.success() {
97        eprintln!("Error: couldn't write uid to card");
98        std::process::exit(exitcode::USAGE);
99    }
100
101    Ok(())
102}
103
104
105// Sets the card to have the given UID
106pub fn remove_generated_file(file_name: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
107
108    if let Err(_) = std::fs::remove_file(&file_name) {
109        eprintln!("Error: could not delete file `{}`", file_name.display());
110        std::process::exit(exitcode::USAGE);
111    }
112  
113    Ok(())
114}
115