Skip to content

Commit

Permalink
fix: handle permission errors
Browse files Browse the repository at this point in the history
Resolve #1
  • Loading branch information
Noxturnix committed Oct 12, 2022
1 parent 1822d52 commit f0d5086
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub fn handle_file_read_error(err: Error, file_path: &String) {
exit(1);
}

pub fn handle_file_create_error(err: Error) {
match err.raw_os_error().unwrap() {
5 => eprintln!("Error: Not enough permissions to write to file"),
19 => eprintln!("Error: The file is in write-protected media"),
_ => eprintln!("Error: Can't write to file"),
}
exit(1);
}

pub fn handle_flipnote_id_error(err: FlipnoteDataError) {
match err {
FlipnoteDataError::InvalidSize => eprintln!("Error: Invalid file size"),
Expand Down
24 changes: 13 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use cli::{Actions, Cli};

use file::{handle_file_read_error, handle_flipnote_id_error};
use file::{handle_file_create_error, handle_file_read_error, handle_flipnote_id_error};
use flipnote_id::{compute_checksum, extract_id_with_checksum, set_fsid};

use std::{
Expand All @@ -27,16 +27,18 @@ fn main() {
}
}

let mut fp = File::create(&cli.file).unwrap();
match fp.write_all(&new_data) {
Ok(_) => println!(
"Successfully set FSID to {:016X}",
extract_id_with_checksum(&new_data).unwrap().id
),
Err(_) => {
eprintln!("Error: Can't write to file");
exit(1);
}
match File::create(&cli.file) {
Ok(mut fp) => match fp.write_all(&new_data) {
Ok(_) => println!(
"Successfully set FSID to {:016X}",
extract_id_with_checksum(&new_data).unwrap().id
),
Err(_) => {
eprintln!("Error: Can't write to file");
exit(1);
}
},
Err(err) => handle_file_create_error(err),
}
}
Err(_) => {
Expand Down

0 comments on commit f0d5086

Please sign in to comment.