Working test

This commit is contained in:
2024-09-15 12:56:21 +03:00
parent 56fa91d998
commit 0f771ffbd3
5 changed files with 346 additions and 32 deletions

View File

@@ -1,10 +1,14 @@
use std::env;
use std::{env, fs};
#[allow(unused_imports)]
use std::fs::File;
use std::path::PathBuf;
use std::process::exit;
use std::time::SystemTime;
use chrono::{DateTime, Local};
fn main() -> std::io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
if args.len() < 2 {
usage("");
}
@@ -19,19 +23,57 @@ fn main() -> std::io::Result<()> {
usage(format!("Not a directory: {}", root_dir).as_str());
}
if let Ok(metadata) = root_path.metadata() {
if let Ok(time) = metadata.modified() {
println!("{:?}", time);
} else {
usage("Filesystem mtime not supported on this platform");
}
} else {
usage(format!("No access to path: {}", root_dir).as_str());
if let Some(last_modified) = folder_min_mtime(&root_path)? {
set_mtime(&root_path, last_modified)?;
}
Ok(())
}
fn set_mtime(path: &PathBuf, mtime: SystemTime) -> std::io::Result<()> {
let metadata = path.metadata()?;
let last_modified = metadata.modified()?;
let datetime: DateTime<Local> = mtime.into();
if mtime < last_modified {
// let dest = File::open(path)?;
// dest.set_modified(mtime)?;
println!("SET {} - {:?}", datetime.format("%Y-%m-%d %H:%M:%S"), &path);
} else {
println!(" NO {} - {:?}", datetime.format("%Y-%m-%d %H:%M:%S"), &path);
}
Ok(())
}
fn folder_min_mtime(path: &PathBuf) -> std::io::Result<Option<SystemTime>> {
let mut min_mtime = None;
for entry in fs::read_dir(path)? {
let entry = entry?;
let entry_path = entry.path();
let metadata = entry_path.metadata()?;
let mut last_modified = metadata.modified()?;
if entry_path.is_dir() {
if let Some(mtime) = folder_min_mtime(&entry_path)? {
set_mtime(&entry_path, mtime)?;
last_modified = mtime;
}
}
if let Some(mtime) = min_mtime {
if last_modified < mtime {
min_mtime = Some(last_modified);
}
} else {
min_mtime = Some(last_modified);
}
}
Ok(min_mtime)
}
fn usage(message: &str) {
let app_name = env!("CARGO_PKG_NAME");
if !message.is_empty() {
@@ -40,25 +82,3 @@ fn usage(message: &str) {
eprintln!("Usage: {} path", app_name);
exit(1);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_DIR: &str = "test_folder";
fn prepare(dir: &str) {
let test_dir = PathBuf::from(dir);
let cur_dir = env::current_dir().unwrap();
let root_dir = cur_dir.join(&test_dir);
dbg!(test_dir);
dbg!(cur_dir);
dbg!(root_dir);
}
#[test]
fn test_metadata() {
prepare(TEST_DIR);
assert!(true);
}
}