rust-fatfs/examples/ls.rs
Henry Gabryjelski 2d689a668d Add more sanity checks and fix size formatting in ls example (#11)
* Fix copy/paste errors in ls example so file sizes are correctly output.
* BPB updates.

Fix bug in bpb.active_fat(), because active_fat is
only valid when mirroring is disabled.

Add additional santiy checks to BPB deserialization:
1. bytes per sector must be a power of 2
2. 512 <= bytes per sector <= 4096
3. sectors per cluster must be a power of 2
4. 1 <= sectors per cluster <= 128

Also added some comments relating to conditions that
may be useful to WARN about for BPB deserialization:
Z. bpb.reserved_sectors should be 1
Y. bpb.fats should be either 1 or 2
X. bpb.fs_version should be validated as zero

Add syntactic sugar for:
A. bpb.is_fat32()
B. bpb.sectors_per_fat()
C. bpb.total_sectors()
2018-09-24 20:58:02 +02:00

45 lines
1.2 KiB
Rust

extern crate chrono;
extern crate fatfs;
extern crate fscommon;
use std::env;
use std::fs::File;
use std::io;
use chrono::{DateTime, Local};
use fatfs::{FileSystem, FsOptions};
use fscommon::BufStream;
fn format_file_size(size: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = 1024 * KB;
const GB: u64 = 1024 * MB;
if size < KB {
format!("{}B", size)
} else if size < MB {
format!("{}KB", size / KB)
} else if size < GB {
format!("{}MB", size / MB)
} else {
format!("{}GB", size / GB)
}
}
fn main() -> io::Result<()> {
let file = File::open("resources/fat32.img")?;
let buf_rdr = BufStream::new(file);
let fs = FileSystem::new(buf_rdr, FsOptions::new())?;
let root_dir = fs.root_dir();
let dir = match env::args().nth(1) {
None => root_dir,
Some(ref path) if path == "." => root_dir,
Some(ref path) => root_dir.open_dir(&path)?,
};
for r in dir.iter() {
let e = r?;
let modified = DateTime::<Local>::from(e.modified()).format("%Y-%m-%d %H:%M:%S").to_string();
println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name());
}
Ok(())
}