1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::process::Command;
use std::path::PathBuf;

extern crate exitcode;


/// Writes the given .mfd file to the destinatin .mfd file
pub fn write_card(is_blank: bool, source_file: &PathBuf, dest_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {  

  // Need to store Command::new into its own variable first
  let mut command = Command::new("nfc-mfclassic");

  // Determine write option depending on type of card received
  if is_blank {
      command.arg("W");
  } else {
      command.arg("w");
  }

  // Add remaining arguments
  let write_command = command
                      .arg("a")
                      .arg(source_file)
                      .arg(dest_file)
                      .output()?;

  // Check for errors
  if !write_command.status.success() {
      eprintln!("Error: couldn't write to card");
      std::process::exit(exitcode::USAGE);
  }

  Ok(())
}


/// Dumps the contents of a card into a .mfd with the given output name
pub fn dump_card(key_file: Option<&PathBuf>, output_file_name: &str) -> Result<(), Box<dyn std::error::Error>> {   
    
  // Need to store Command::new into its own variable first
  let mut command = Command::new("mfoc");
  command.arg("-O")
         .arg(format!("{}", output_file_name));

  // Add the key file option, if it exists
  if let Some(file) = key_file {
      command.arg("-f").arg(file);
  }

  let dump_command = command.output()?;

  // Check for errors
  if !dump_command.status.success() {
      eprintln!("Error: couldn't dump contents of card");
      std::process::exit(exitcode::USAGE);
  }

  Ok(())
}


/// Formats a card so that it can be overwritten
pub fn format_card(file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {   
    
    // Need to store Command::new into its own variable first
    let mut command = Command::new("nfc-mfclassic");

    // Add remaining arguments
    let format_command = command
                        .arg("f")
                        .arg("B")
                        .arg(file)
                        .arg(file)
                        .arg("f")
                        .output()?;

    // Check for errors
    if !format_command.status.success() {
        eprintln!("Error: couldn't format card");
        std::process::exit(exitcode::USAGE);
    }

    Ok(())
  }


/// Sets the card to have the given UID
pub fn set_card_uid(uid: &str) -> Result<(), Box<dyn std::error::Error>> {

    let mut command = Command::new("nfc-mfsetuid");
  
    let set_uid_command = command
                         .arg(uid)
                         .output()?;

    if !set_uid_command.status.success() {
        eprintln!("Error: couldn't write uid to card");
        std::process::exit(exitcode::USAGE);
    }

    Ok(())
}


// Sets the card to have the given UID
pub fn remove_generated_file(file_name: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {

    if let Err(_) = std::fs::remove_file(&file_name) {
        eprintln!("Error: could not delete file `{}`", file_name.display());
        std::process::exit(exitcode::USAGE);
    }
  
    Ok(())
}