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
use std::fs::File;
use std::io::Write;
use std::path::Path;

use byteorder::{LittleEndian, WriteBytesExt};
use clap::ArgMatches;

use crate::{AnyResult, ImageInfo, HEADER, SECTOR_SIZE};

pub fn main(matches: &ArgMatches) -> AnyResult<()> {
    let img = matches.value_of("img").unwrap();
    let force = matches.is_present("force");

    if Path::new(img).exists() && !force {
        return Err("File exists. Use `--force` to overwrite".into());
    }
    let mut file = File::options()
        .truncate(true)
        .create(true)
        .write(true)
        .open(img)?;

    let image_info = ImageInfo::new(SECTOR_SIZE, Vec::new());

    let image_info_json = serde_json::to_string(&image_info).unwrap();

    file.write_all(HEADER)?;
    file.write_u32::<LittleEndian>(image_info_json.len() as u32)?;

    file.write_all(image_info_json.as_bytes())?;

    println!("Done");

    Ok(())
}