Skip to content

Commit

Permalink
创建项目
Browse files Browse the repository at this point in the history
  • Loading branch information
tbontb-iaq committed Jun 16, 2023
0 parents commit 85a3d7b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "cef-detector"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CEF Detector for Linux

> 给 Linux 用户一点小小的 Chromium 震撼
使用 `locate` 命令查找文件,`du` 命令计算文件夹大小

创意来自 [@ShirasawaSama](https://github.com/ShirasawaSama)[CEF Detector X](https://github.com/ShirasawaSama/CefDetectorX)
106 changes: 106 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
fs::metadata,
io::{BufRead, BufReader},
path::PathBuf,
process::{Command, Stdio},
};

struct App {
path: String,
size: u128,
cef_files: HashSet<String>,
}

const CEF_FILES: [&str; 5] = [
r"chrome_100_percent\.pak",
r"chrome_crashpad_handler",
r"chrome-sandbox",
r"libcef\.so",
r"resources\.pak",
];

fn locate(regex: &str) -> HashMap<String, HashSet<String>> {
let mut map = HashMap::new();

let child = Command::new("locate")
.args(["-r", regex])
.stdout(Stdio::piped())
.spawn()
.unwrap();

let reader = BufReader::new(child.stdout.unwrap());

for line in reader.lines() {
let path = line.unwrap();
let metadata = metadata(path.as_str()).unwrap();
if metadata.is_file() {
let path_buf = PathBuf::from(path.as_str());
let parent = path_buf.parent().unwrap().to_str().unwrap();

map.entry(String::from(parent))
.or_insert(HashSet::new())
.insert(path);
}
}

map
}

fn disk_usage(folder: &str) -> u128 {
let output = Command::new("du").args(["-s", folder]).output().unwrap();
let output = String::from_utf8_lossy(&output.stdout);
let size_str = output.split_ascii_whitespace().next().unwrap();
let size = size_str.parse::<u128>().unwrap();
size
}

fn format_size(size: u128) -> String {
let units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let mut size = size as f64;
let mut unit_index = 0;
while size >= 1024.0 && unit_index < units.len() - 1 {
size /= 1024.0;
unit_index += 1;
}
format!("{:.2}{}", size, units[unit_index])
}

fn main() {
println!("locating...");

let mut map = BTreeMap::<String, App>::new();

for file in CEF_FILES {
let locate_result = locate(&format!(r"\/{}$", file));
for (path, set) in locate_result {
let size = disk_usage(path.as_str());
map.entry(path.clone())
.or_insert(App {
size,
path,
cef_files: HashSet::new(),
})
.cef_files
.extend(set);
}
}

let mut total_size = 0u128;

for app in map.values() {
println!("软件目录:{}", app.path);
println!("软件大小:{}", format_size(app.size));
for path in &app.cef_files {
println!("\t{}", path);
}
total_size += app.size;
println!();
}

println!(
"此电脑中共有 {} 个 Chromium 内核应用 ({})",
map.len(),
format_size(total_size)
);
}

0 comments on commit 85a3d7b

Please sign in to comment.