1use std::process::Command;
2use std::path::PathBuf;
3
4extern crate exitcode;
5
6
7pub fn write_card(is_blank: bool, source_file: &PathBuf, dest_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
9
10 let mut command = Command::new("nfc-mfclassic");
12
13 if is_blank {
15 command.arg("W");
16 } else {
17 command.arg("w");
18 }
19
20 let write_command = command
22 .arg("a")
23 .arg(source_file)
24 .arg(dest_file)
25 .output()?;
26
27 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
37pub fn dump_card(key_file: Option<&PathBuf>, output_file_name: &str) -> Result<(), Box<dyn std::error::Error>> {
39
40 let mut command = Command::new("mfoc");
42 command.arg("-O")
43 .arg(format!("{}", output_file_name));
44
45 if let Some(file) = key_file {
47 command.arg("-f").arg(file);
48 }
49
50 let dump_command = command.output()?;
51
52 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
62pub fn format_card(file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
64
65 let mut command = Command::new("nfc-mfclassic");
67
68 let format_command = command
70 .arg("f")
71 .arg("B")
72 .arg(file)
73 .arg(file)
74 .arg("f")
75 .output()?;
76
77 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
87pub 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
105pub 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