From 31f1d3d18566d1ae97daaedbd17132a11a7ab5aa Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sun, 31 May 2020 16:29:40 -0700 Subject: start aft-ls --- src/bin/aft-ls.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/bin/aft-ls.rs (limited to 'src/bin') diff --git a/src/bin/aft-ls.rs b/src/bin/aft-ls.rs new file mode 100644 index 0000000..3118c08 --- /dev/null +++ b/src/bin/aft-ls.rs @@ -0,0 +1,36 @@ + +use std::{fs,env,path}; +use std::error::Error; +use std::os::unix::fs::MetadataExt; + +fn main() -> Result<(), Box> { + + let args: Vec = env::args().collect(); + let dir = match args.len() { + 0 | 1 => path::Path::new("."), + _other => path::Path::new(&args[2]), + }; + + // header is basically a constant + print!("\x01"); + println!("{}\x1D", ["file_name", "size", "mtime"].join("\x1E")); + println!("{}\x1D", ["string", "integer+bytes", "datetime"].join("\x1E")); + print!("\x02"); + + for entry in fs::read_dir(dir)? { + let entry = entry?; + let metadata = entry.metadata()?; + let filename: String = entry.file_name() + .into_string() + .or_else(|_| Err(format!("Invalid entry")))?; + let mtime = chrono::DateTime::::from_utc( + chrono::NaiveDateTime::from_timestamp(metadata.mtime(), 0), chrono::Utc); + println!("{}\x1D", + [ + filename, + metadata.size().to_string(), + mtime.to_rfc3339_opts(chrono::SecondsFormat::AutoSi, true), + ].join("\x1E")); + } + Ok(()) +} -- cgit v1.2.3