子命令 db 功能初步实现
Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
parent
edb907941b
commit
2b8afdb2ea
4
.gitignore
vendored
4
.gitignore
vendored
@ -19,3 +19,7 @@ Cargo.lock
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
# Test files
|
||||
sainfos
|
||||
cves
|
||||
|
78
src/lib.rs
78
src/lib.rs
@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use cvrf_xmlparser::{
|
||||
CVRF,
|
||||
@ -67,7 +68,80 @@ fn covert(cli: &cli::ConvertCli) -> Result<()> {
|
||||
}
|
||||
|
||||
fn sadb(cli: &cli::SaDbCli) -> Result<()> {
|
||||
let _ = cli;
|
||||
let source = &cli.from;
|
||||
// 只要文件
|
||||
let files = walk_dir(source, true);
|
||||
|
||||
let mut sadb = HashMap::new();
|
||||
let mut cvedb = HashMap::new();
|
||||
|
||||
for _file in files {
|
||||
match _file.extension() {
|
||||
Some(tail) => {
|
||||
if tail != "xml" {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 从 xml 中读取 cvrf 并转换为 cusa
|
||||
let file = if let Some(file) = _file.to_str() {
|
||||
file
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
let mut cvrf = CVRF::new();
|
||||
let _ = cvrf.load_xml(file);
|
||||
let sa = cvrf.sainfo();
|
||||
|
||||
// cvedb
|
||||
sa.cves.clone().into_iter().for_each(|cve| {
|
||||
cvedb.insert(cve.id.clone(), cve);
|
||||
});
|
||||
|
||||
// sadb
|
||||
// 很烦,华为干的这事儿
|
||||
// 2022/cvrf-openEuler-SA-2022-2063.xml 里的 ID 为 openEuler-SA-2022-2046
|
||||
// 暂不处理
|
||||
// TODO
|
||||
if let Some(_) = sadb.insert(sa.id.clone(), sa) {
|
||||
// unimplemented!();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// 写入 sadb 文件
|
||||
let data = serde_json::to_string_pretty(&sadb)?;
|
||||
let mut sadb_file = fs::OpenOptions::new().read(true).write(true).create(true).open(&cli.sa)?;
|
||||
sadb_file.write(data.as_bytes())?;
|
||||
sadb_file.flush()?;
|
||||
|
||||
// 写入 cvedb 文件
|
||||
let data = serde_json::to_string_pretty(&cvedb)?;
|
||||
let mut cvedb_file = fs::OpenOptions::new().read(true).write(true).create(true).open(&cli.cve)?;
|
||||
cvedb_file.write(data.as_bytes())?;
|
||||
cvedb_file.flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 递归去读一个路径,返回一个文件列表
|
||||
#[allow(dead_code)]
|
||||
fn walk_dir<P: AsRef<Path>>(path: P, nodir: bool) -> Vec<PathBuf>
|
||||
{
|
||||
let mut res = vec![];
|
||||
for entry in std::fs::read_dir(path).expect("read_dir call failed!") {
|
||||
let entry = entry.unwrap().path();
|
||||
if entry.is_dir() {
|
||||
if !nodir {
|
||||
res.push(entry.clone());
|
||||
}
|
||||
res.append(&mut walk_dir(entry, nodir));
|
||||
} else {
|
||||
// 暂不处理链接等情况
|
||||
res.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user