most done.
This commit is contained in:
+96
-2
@@ -1,3 +1,97 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod cli;
|
||||
mod constants;
|
||||
mod head_hash;
|
||||
mod meta;
|
||||
mod utils;
|
||||
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::Parser;
|
||||
use meta::{DirSnapshot, FileMeta};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let started = Instant::now();
|
||||
let cli = cli::Cli::parse();
|
||||
let target = cli.resolve_path()?;
|
||||
println!("目标: {}", target.display());
|
||||
|
||||
if target.is_dir() {
|
||||
process_dir(&target)?;
|
||||
} else {
|
||||
process_file(&target)?;
|
||||
}
|
||||
|
||||
println!("耗时: {:?}", started.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_file(path: &Path) -> Result<()> {
|
||||
let meta = FileMeta::from_path(path)?;
|
||||
let meta_dir = path
|
||||
.parent()
|
||||
.map(Path::to_path_buf)
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("meta");
|
||||
fs::create_dir_all(&meta_dir)
|
||||
.with_context(|| format!("无法创建目录: {}", meta_dir.display()))?;
|
||||
|
||||
let save_path = meta_dir.join(format!("{}.json", meta.basename));
|
||||
if !save_path.exists() {
|
||||
let json = meta.to_pretty_json()?;
|
||||
println!("{}", json);
|
||||
fs::write(&save_path, json)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let existing = File::open(&save_path)
|
||||
.with_context(|| format!("无法读取历史元数据: {}", save_path.display()))?;
|
||||
let old_meta = FileMeta::from_reader(existing)?;
|
||||
if meta.matches(&old_meta) {
|
||||
println!("校验通过.");
|
||||
} else {
|
||||
println!("校验失败!");
|
||||
println!("现校验文件:");
|
||||
println!("{}", meta.to_pretty_json()?);
|
||||
println!("原校验文件:");
|
||||
println!("{}", old_meta.to_pretty_json()?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_dir(path: &Path) -> Result<()> {
|
||||
let save_path = path.join("meta.json");
|
||||
let old_path = path.join("meta-old.json");
|
||||
let has_old = save_path.exists();
|
||||
|
||||
if has_old {
|
||||
if old_path.exists() {
|
||||
fs::remove_file(&old_path)?;
|
||||
}
|
||||
fs::rename(&save_path, &old_path)
|
||||
.with_context(|| format!("无法备份旧文件: {}", save_path.display()))?;
|
||||
}
|
||||
|
||||
let snapshot = DirSnapshot::build_root(path)?;
|
||||
let json = serde_json::to_string_pretty(&snapshot)?;
|
||||
let mut file =
|
||||
File::create(&save_path).with_context(|| format!("无法写入: {}", save_path.display()))?;
|
||||
file.write_all(json.as_bytes())?;
|
||||
|
||||
if has_old {
|
||||
let old_meta = FileMeta::from_path(&old_path)?;
|
||||
let new_meta = FileMeta::from_path(&save_path)?;
|
||||
if old_meta.matches(&new_meta) {
|
||||
println!("校验通过.");
|
||||
fs::remove_file(&old_path)?;
|
||||
} else {
|
||||
println!("校验失败!");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user