rust-fatfs/examples/ls.rs
Rafał Harabień 892c3974d3 Move BufStream to fscommon crate (BREAKING CHANGE)
BufStream is universal and can be used with any filesystem so its place is
in different crate. Also moved Partition struct from examples to
fscommon::StreamSlice struct (please note constructor arguments has changed).
2018-06-16 17:57:29 +02:00

45 lines
1.2 KiB
Rust

extern crate fatfs;
extern crate fscommon;
extern crate chrono;
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 < 1024 {
format!("{}B", size)
} else if size < MB {
format!("{}KB", size / KB)
} else if size < GB {
format!("{}KB", size / MB)
} else {
format!("{}KB", 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(())
}