From 7d818831a3712e307731bf5be28bc876874f8b39 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 2 Oct 2018 09:31:22 +0300 Subject: Change: really use walkdir crate --- Cargo.lock | 30 ++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 76 ++++++++++++++++++++++++++++++++++++------------------------- 3 files changed, 76 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3db1943..fcf599c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,6 +128,14 @@ name = "redox_syscall" version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "same-file" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "summainrs" version = "0.1.0" @@ -136,6 +144,7 @@ dependencies = [ "crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "users 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -166,6 +175,16 @@ name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "walkdir" +version = "2.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.3.6" @@ -180,6 +199,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -204,10 +231,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum openssl-sys 0.9.36 (registry+https://github.com/rust-lang/crates.io-index)" = "409d77eeb492a1aebd6eb322b2ee72ff7c7496b4434d98b3bf8be038755de65e" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum same-file 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10f7794e2fda7f594866840e95f5c5962e886e228e68b6505885811a94dd728c" "checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum users 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "caa2760fcc10a6ae2c2a35d41c5d69827e4663f0d3889ecfb4d60b343f4139df" "checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "af464bc7be7b785c7ac72e266a6b67c4c9070155606f51655a650a6686204e35" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "afc5508759c5bf4285e61feb862b6083c8480aec864fa17a81fdec6f69b461ab" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 3d413bc..8b20171 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ users = "0.7" chrono = "0.4.6" crypto-hash = "0.3.1" hex = "0.3.2" +walkdir = "2.2.5" diff --git a/src/main.rs b/src/main.rs index cac0646..79736c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ use std::os::unix::fs::PermissionsExt; use std::os::linux::fs::MetadataExt; use std::io::prelude::*; +extern crate walkdir; +use walkdir::{WalkDir, DirEntry}; + extern crate users; extern crate chrono; @@ -16,83 +19,93 @@ use crypto_hash::{Algorithm, Hasher}; extern crate hex; -mod fswalk; -use fswalk::DirTree; - // Command line args are names of dirs, recursively list all files and // dirs them fn main() -> io::Result<()> { for dirname in env::args().skip(1) { - let tree = DirTree::new(&dirname); - for entry in tree { - write_info(&entry.path(), &entry.metadata()); + for e in WalkDir::new(&dirname).into_iter().filter_map(|e| e.ok()) { + write_info(&e); } } Ok(()) } -fn write_info(path: &str, meta: &fs::Metadata) { - println!("Name: {}", path); - println!("Mtime: {}", format_mtime(meta)); - println!("Mode: {}", format_mode(meta)); - println!("Ino: {}", format_inode(meta)); - println!("Dev: {}", format_dev(meta)); - println!("Nlink: {}", format_nlink(meta)); - println!("Size: {}", format_size(meta)); - println!("Uid: {}", format_uid(meta)); - println!("Username: {}", format_username(meta)); - println!("Gid: {}", format_gid(meta)); - println!("Group: {}", format_group(meta)); - println!("SHA256: {}", format_sha256(path)); +fn write_info(e: &DirEntry) { + println!("Name: {}", format_name(e)); + println!("Mtime: {}", format_mtime(e)); + println!("Mode: {}", format_mode(e)); + println!("Ino: {}", format_inode(e)); + println!("Dev: {}", format_dev(e)); + println!("Nlink: {}", format_nlink(e)); + println!("Size: {}", format_size(e)); + println!("Uid: {}", format_uid(e)); + println!("Username: {}", format_username(e)); + println!("Gid: {}", format_gid(e)); + println!("Group: {}", format_group(e)); + println!("SHA256: {}", format_sha256(e)); println!(""); } -fn format_mtime(meta: &fs::Metadata) -> String { +fn format_name(e: &DirEntry) -> String { + e.path().to_string_lossy().to_string() +} + + +fn format_mtime(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); let mtime = meta.modified().unwrap(); let datetime = DateTime::::from(mtime); datetime.format("%Y-%m-%d %H:%M:%S.%f %z").to_string() } -fn format_mode(meta: &fs::Metadata) -> String { +fn format_mode(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); let mode = meta.permissions().mode(); format!("{:06o}", mode) } -fn format_inode(meta: &fs::Metadata) -> String { +fn format_inode(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.st_ino()) } -fn format_dev(meta: &fs::Metadata) -> String { +fn format_dev(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.st_dev()) } -fn format_nlink(meta: &fs::Metadata) -> String { +fn format_nlink(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.st_nlink()) } -fn format_size(meta: &fs::Metadata) -> String { +fn format_size(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.len()) } -fn format_uid(meta: &fs::Metadata) -> String { +fn format_uid(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.st_uid()) } -fn format_gid(meta: &fs::Metadata) -> String { +fn format_gid(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); format!("{}", meta.st_gid()) } -fn format_username(meta: &fs::Metadata) -> String { +fn format_username(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); if let Some(u) = users::get_user_by_uid(meta.st_uid()) { format!("{}", u.name()) } else { @@ -101,7 +114,8 @@ fn format_username(meta: &fs::Metadata) -> String { } -fn format_group(meta: &fs::Metadata) -> String { +fn format_group(e: &DirEntry) -> String { + let meta = e.metadata().unwrap(); if let Some(g) = users::get_group_by_gid(meta.st_gid()) { format!("{}", g.name()) } else { @@ -110,10 +124,10 @@ fn format_group(meta: &fs::Metadata) -> String { } -fn format_sha256(filename: &str) -> String { +fn format_sha256(e: &DirEntry) -> String { let mut ok = true; let mut hasher = Hasher::new(Algorithm::SHA256); - if let Ok(file) = fs::File::open(filename) { + if let Ok(file) = fs::File::open(e.path()) { let mut reader = io::BufReader::new(file); let mut buf = [0; 1024 * 1024]; let mut done = false; -- cgit v1.2.1