2018-06-29 00:13:07 +08:00
|
|
|
extern crate chrono;
|
2017-10-06 23:00:38 +08:00
|
|
|
extern crate fatfs;
|
2018-06-16 23:57:29 +08:00
|
|
|
extern crate fscommon;
|
2017-09-24 20:34:23 +08:00
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
2018-06-01 03:40:40 +08:00
|
|
|
use std::io;
|
2017-09-24 20:34:23 +08:00
|
|
|
|
2018-06-29 00:13:07 +08:00
|
|
|
use chrono::{DateTime, Local};
|
2018-06-16 23:57:29 +08:00
|
|
|
use fatfs::{FileSystem, FsOptions};
|
|
|
|
use fscommon::BufStream;
|
2017-09-24 20:34:23 +08:00
|
|
|
|
|
|
|
fn format_file_size(size: u64) -> String {
|
|
|
|
const KB: u64 = 1024;
|
2018-06-29 00:13:07 +08:00
|
|
|
const MB: u64 = 1024 * KB;
|
|
|
|
const GB: u64 = 1024 * MB;
|
2018-09-25 02:58:02 +08:00
|
|
|
if size < KB {
|
2017-09-24 20:34:23 +08:00
|
|
|
format!("{}B", size)
|
|
|
|
} else if size < MB {
|
|
|
|
format!("{}KB", size / KB)
|
|
|
|
} else if size < GB {
|
2018-09-25 02:58:02 +08:00
|
|
|
format!("{}MB", size / MB)
|
2017-09-24 20:34:23 +08:00
|
|
|
} else {
|
2018-09-25 02:58:02 +08:00
|
|
|
format!("{}GB", size / GB)
|
2017-09-24 20:34:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 03:40:40 +08:00
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
let file = File::open("resources/fat32.img")?;
|
2018-06-14 05:26:13 +08:00
|
|
|
let buf_rdr = BufStream::new(file);
|
|
|
|
let fs = FileSystem::new(buf_rdr, FsOptions::new())?;
|
2018-06-12 06:07:30 +08:00
|
|
|
let 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,
|
2018-06-01 03:40:40 +08:00
|
|
|
Some(ref path) => root_dir.open_dir(&path)?,
|
2017-09-24 20:34:23 +08:00
|
|
|
};
|
2017-10-06 22:07:11 +08:00
|
|
|
for r in dir.iter() {
|
2018-06-01 03:40:40 +08:00
|
|
|
let e = r?;
|
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());
|
|
|
|
}
|
2018-06-01 03:40:40 +08:00
|
|
|
Ok(())
|
2017-09-24 20:34:23 +08:00
|
|
|
}
|