2017-10-06 23:00:38 +08:00
|
|
|
extern crate fatfs;
|
2017-10-06 22:42:29 +08:00
|
|
|
extern crate chrono;
|
2017-09-24 20:34:23 +08:00
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
2017-10-06 22:42:29 +08:00
|
|
|
use chrono::{DateTime, Local};
|
2017-09-24 20:34:23 +08:00
|
|
|
|
2017-11-09 03:26:50 +08:00
|
|
|
use fatfs::{FileSystem, FsOptions, BufStream};
|
2017-09-24 20:34:23 +08:00
|
|
|
|
|
|
|
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() {
|
|
|
|
let file = File::open("resources/fat32.img").unwrap();
|
2017-10-09 20:59:52 +08:00
|
|
|
let mut buf_rdr = BufStream::new(file);
|
2017-11-09 03:26:50 +08:00
|
|
|
let fs = FileSystem::new(&mut buf_rdr, FsOptions::new()).unwrap();
|
2017-09-24 20:34:23 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-10-02 00:45:58 +08:00
|
|
|
let dir = match env::args().nth(1) {
|
2017-09-24 20:34:23 +08:00
|
|
|
None => root_dir,
|
|
|
|
Some(ref path) if path == "." => root_dir,
|
2017-09-25 04:12:38 +08:00
|
|
|
Some(ref path) => root_dir.open_dir(&path).unwrap(),
|
2017-09-24 20:34:23 +08:00
|
|
|
};
|
2017-10-06 22:07:11 +08:00
|
|
|
for r in dir.iter() {
|
2017-10-02 00:45:58 +08:00
|
|
|
let e = r.unwrap();
|
2017-10-06 22:42:29 +08:00
|
|
|
let modified = DateTime::<Local>::from(e.modified()).format("%Y-%m-%d %H:%M:%S").to_string();
|
2017-09-24 20:34:23 +08:00
|
|
|
println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name());
|
|
|
|
}
|
|
|
|
}
|