most done.
This commit is contained in:
123
src/meta/file.rs
Normal file
123
src/meta/file.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use ed2k::digest::Digest;
|
||||
use ed2k::Ed2k;
|
||||
use md5::Md5;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha1::Sha1;
|
||||
use sha2::Sha256;
|
||||
use xxhash_rust::xxh3::Xxh3;
|
||||
|
||||
use crate::constants::{DEFAULT_BUFFER_SIZE, HEAD_115_BYTES, HEAD_BAIDU_BYTES};
|
||||
use crate::head_hash::{calc_head_115, calc_head_baidu, HeadChunk};
|
||||
use crate::utils::{basename, friendly_size, hex_upper};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileMeta {
|
||||
pub basename: String,
|
||||
pub size: u64,
|
||||
pub friendly_size: String,
|
||||
pub mtime: i64,
|
||||
pub head_115: String,
|
||||
pub head_baidu: String,
|
||||
pub ed2k: String,
|
||||
pub md5: String,
|
||||
pub sha1: String,
|
||||
pub sha256: String,
|
||||
pub xxh128: String,
|
||||
}
|
||||
|
||||
impl FileMeta {
|
||||
pub fn from_path(path: &Path) -> Result<Self> {
|
||||
let info =
|
||||
fs::metadata(path).with_context(|| format!("无法读取文件信息: {}", path.display()))?;
|
||||
if !info.is_file() {
|
||||
return Err(anyhow!("{} 不是文件", path.display()));
|
||||
}
|
||||
|
||||
let basename_str = basename(
|
||||
path.file_name()
|
||||
.ok_or_else(|| anyhow!("{} 缺少文件名", path.display()))?,
|
||||
);
|
||||
let size = info.len();
|
||||
let friendly = friendly_size(size);
|
||||
let mtime = info
|
||||
.modified()
|
||||
.unwrap_or(SystemTime::UNIX_EPOCH)
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs() as i64)
|
||||
.unwrap_or(0);
|
||||
|
||||
let mut file =
|
||||
File::open(path).with_context(|| format!("无法打开文件: {}", path.display()))?;
|
||||
|
||||
let mut buffer = vec![0u8; DEFAULT_BUFFER_SIZE];
|
||||
let mut md5_hasher = Md5::new();
|
||||
let mut sha1_hasher = Sha1::new();
|
||||
let mut sha256_hasher = Sha256::new();
|
||||
let mut xxh_hasher = Xxh3::new();
|
||||
let mut ed2k_hasher = Ed2k::new();
|
||||
let mut head115 = HeadChunk::new(HEAD_115_BYTES);
|
||||
let mut head_baidu = HeadChunk::new(HEAD_BAIDU_BYTES);
|
||||
|
||||
loop {
|
||||
let read_len = file.read(&mut buffer)?;
|
||||
if read_len == 0 {
|
||||
break;
|
||||
}
|
||||
let chunk = &buffer[..read_len];
|
||||
md5_hasher.update(chunk);
|
||||
sha1_hasher.update(chunk);
|
||||
sha256_hasher.update(chunk);
|
||||
xxh_hasher.update(chunk);
|
||||
ed2k_hasher.update(chunk);
|
||||
|
||||
head115.feed(chunk);
|
||||
head_baidu.feed(chunk);
|
||||
}
|
||||
|
||||
let head_115 = calc_head_115(head115.as_slice());
|
||||
let head_baidu = calc_head_baidu(head_baidu.as_slice());
|
||||
|
||||
let md5_hex = hex_upper(md5_hasher.finalize());
|
||||
let sha1_hex = hex_upper(sha1_hasher.finalize());
|
||||
let sha256_hex = hex_upper(sha256_hasher.finalize());
|
||||
let xxh_hex = hex_upper(xxh_hasher.digest128().to_be_bytes());
|
||||
let ed2k_hex = hex_upper(ed2k_hasher.finalize());
|
||||
|
||||
Ok(Self {
|
||||
basename: basename_str,
|
||||
size,
|
||||
friendly_size: friendly,
|
||||
mtime,
|
||||
head_115,
|
||||
head_baidu,
|
||||
ed2k: ed2k_hex,
|
||||
md5: md5_hex,
|
||||
sha1: sha1_hex,
|
||||
sha256: sha256_hex,
|
||||
xxh128: xxh_hex,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_reader<R: Read>(reader: R) -> Result<Self> {
|
||||
Ok(serde_json::from_reader(reader)?)
|
||||
}
|
||||
|
||||
pub fn to_pretty_json(&self) -> Result<String> {
|
||||
Ok(serde_json::to_string_pretty(self)?)
|
||||
}
|
||||
|
||||
pub fn matches(&self, other: &Self) -> bool {
|
||||
self.size == other.size
|
||||
&& self.ed2k == other.ed2k
|
||||
&& self.md5 == other.md5
|
||||
&& self.sha1 == other.sha1
|
||||
&& self.sha256 == other.sha256
|
||||
&& self.xxh128 == other.xxh128
|
||||
}
|
||||
}
|
||||
5
src/meta/mod.rs
Normal file
5
src/meta/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod file;
|
||||
mod tree;
|
||||
|
||||
pub use file::FileMeta;
|
||||
pub use tree::DirSnapshot;
|
||||
76
src/meta/tree.rs
Normal file
76
src/meta/tree.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::file::FileMeta;
|
||||
use crate::constants::META_VERSION;
|
||||
use crate::utils::{basename, should_skip_dir, should_skip_file};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DirSnapshot {
|
||||
pub dir_name: String,
|
||||
pub dirs: Vec<DirSnapshot>,
|
||||
pub files: Vec<FileMeta>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub v: Option<String>,
|
||||
}
|
||||
|
||||
impl DirSnapshot {
|
||||
pub fn build_root(path: &Path) -> Result<Self> {
|
||||
let mut node = Self::build_node(path)?;
|
||||
node.v = Some(META_VERSION.to_string());
|
||||
Ok(node)
|
||||
}
|
||||
|
||||
fn build_node(path: &Path) -> Result<Self> {
|
||||
let dir_name = path
|
||||
.file_name()
|
||||
.map(basename)
|
||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||
|
||||
let mut dirs = Vec::new();
|
||||
let mut files = Vec::new();
|
||||
|
||||
let mut entries = fs::read_dir(path)
|
||||
.with_context(|| format!("无法遍历目录: {}", path.display()))?
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.with_context(|| format!("读取目录失败: {}", path.display()))?;
|
||||
|
||||
entries.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
|
||||
|
||||
for entry in entries {
|
||||
let file_name = entry.file_name();
|
||||
let name = file_name.to_string_lossy().to_string();
|
||||
let full_path = entry.path();
|
||||
let file_type = entry
|
||||
.file_type()
|
||||
.with_context(|| format!("无法读取类型: {}", full_path.display()))?;
|
||||
|
||||
if file_type.is_dir() {
|
||||
if should_skip_dir(&name) {
|
||||
continue;
|
||||
}
|
||||
println!("目录: {}", full_path.display());
|
||||
dirs.push(Self::build_node(&full_path)?);
|
||||
continue;
|
||||
}
|
||||
|
||||
if should_skip_file(&name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let meta = FileMeta::from_path(&full_path)?;
|
||||
println!("文件: {} {}", meta.friendly_size, full_path.display());
|
||||
files.push(meta);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
dir_name,
|
||||
dirs,
|
||||
files,
|
||||
v: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user