//! Example: Format a file as an ext4 filesystem
//!
//! This example creates a 10MB disk image file and formats it as ext4.

use ext4_mkfs::{mkfs, FsType, IoBlockDevice, MkfsConfig};
use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let path = "test_disk.img";
    let size: u64 = 10 * 1024 * 1024; // 10 MB

    println!("Creating disk image: {} ({} bytes)", path, size);

    // Create and size the file
    {
        let mut file = File::create(path)?;
        file.seek(SeekFrom::Start(size - 1))?;
        file.write_all(&[0])?;
        file.sync_all()?;
    }

    // Open for read/write
    let file = OpenOptions::new().read(true).write(true).open(path)?;

    // Create block device wrapper (512-byte sectors)
    let device = IoBlockDevice::new(file, 512, size);

    // Configure and create filesystem
    let config = MkfsConfig::new()
        .fs_type(FsType::Ext4)
        .block_size(4096)
        .label("test_volume");

    println!("Formatting as ext4...");
    mkfs(device, config)?;

    println!("Done! You can verify with: file {} or dumpe2fs {}", path, path);

    Ok(())
}
