Skip to content

Commit 8b316b4

Browse files
committed
v1 with basic implementation
0 parents  commit 8b316b4

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
.vscode/

Cargo.lock

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "yet-another-disk-mapper"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rmp-serde = "1.3.0"
8+
serde = "1.0.219"
9+
10+
[profile.custom-opt]
11+
inherits = "release"
12+
opt-level = 3
13+
lto = true
14+
codegen-units = 1

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Yet Another Disk Mapper (YADM)
2+
YADM is a little Disk Mapper for some future projects.

src/main.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use std::fs;
2+
use std::time::{Duration, Instant};
3+
use std::path::{PathBuf};
4+
use std::ffi::OsStr;
5+
6+
use rmp_serde::{encode};
7+
use std::collections::HashMap;
8+
use std::fs::File;
9+
use std::io::Write;
10+
11+
fn format_file_size(size: u64, unit: &str) -> String {
12+
match unit {
13+
"o" => format!("{} octets", size),
14+
"Ko" => format!("{:.2} Ko", size as f64 / 1024.0),
15+
"Mo" => format!("{:.2} Mo", size as f64 / (1024.0 * 1024.0)),
16+
"Go" => format!("{:.2} Go", size as f64 / (1024.0 * 1024.0 * 1024.0)),
17+
_ => format!("{} octets", size),
18+
}
19+
}
20+
21+
fn scan_dir(path: &str) -> Vec<PathBuf> {
22+
let mut result = Vec::new();
23+
if let Ok(entries) = fs::read_dir(path) {
24+
for entry in entries {
25+
if let Ok(entry) = entry {
26+
let path = entry.path();
27+
if path.is_dir() {
28+
// Récursivement scanner les sous-répertoires
29+
let mut sub_dir_results = scan_dir(path.to_str().unwrap());
30+
result.append(&mut sub_dir_results);
31+
} else {
32+
// Ajouter le fichier à la liste des résultats
33+
result.push(path);
34+
}
35+
}
36+
}
37+
}
38+
result
39+
}
40+
41+
fn scan_dirs(path: &str) -> Vec<PathBuf> {
42+
let result = scan_dir(path);
43+
result
44+
}
45+
46+
fn parsing_to_hashmap(entries: &Vec<PathBuf>) -> Vec<HashMap<String, String>> {
47+
let mut dictionaries: Vec<HashMap<String, String>> = Vec::new();
48+
49+
for entry in entries {
50+
let mut hashmap = HashMap::new();
51+
52+
// Convertir Option<&OsStr> en String pour "name"
53+
if let Some(name) = entry.file_name().and_then(OsStr::to_str) {
54+
hashmap.insert(String::from("name"), name.to_string());
55+
} else {
56+
hashmap.insert(String::from("name"), String::from(""));
57+
}
58+
59+
// Convertir Option<&OsStr> en String pour "ext"
60+
if let Some(ext) = entry.extension().and_then(OsStr::to_str) {
61+
hashmap.insert(String::from("ext"), ext.to_string());
62+
} else {
63+
hashmap.insert(String::from("ext"), String::from(""));
64+
}
65+
66+
// Convertir Option<&str> en String pour "path"
67+
if let Some(path) = entry.to_str() {
68+
hashmap.insert(String::from("path"), path.to_string());
69+
} else {
70+
hashmap.insert(String::from("path"), String::from(""));
71+
}
72+
73+
dictionaries.push(hashmap);
74+
}
75+
76+
dictionaries
77+
}
78+
79+
80+
fn main() {
81+
println!("1. Scanning folders...");
82+
let start_time: Instant = Instant::now();
83+
84+
let paths: Vec<PathBuf> = scan_dirs("C:/Users/ederv/");
85+
86+
let end_time: Duration = start_time.elapsed();
87+
88+
println!("Time taken: {:?}", end_time);
89+
println!("Elements found: {:?}", paths.len());
90+
91+
if paths.len() > 0 {
92+
let medium_time_per_element = end_time.as_nanos() / paths.len() as u128;
93+
println!("Medium time by element: {:?} ns", medium_time_per_element);
94+
} else {
95+
println!("No elements found, cannot calculate medium time by element.");
96+
}
97+
98+
//println!("2. Parsing to a list of dicts...");
99+
let hashmap = parsing_to_hashmap(&paths);
100+
101+
// Sérialiser le HashMap en MessagePack
102+
let encoded = encode::to_vec(&hashmap).unwrap();
103+
104+
// Écrire le MessagePack dans un fichier
105+
let mut file = File::create("output.msgpack").expect("Impossible de créer le fichier");
106+
file.write_all(&encoded).expect("Impossible d'écrire dans le fichier");
107+
108+
// Obtenir la taille du fichier
109+
let metadata = file.metadata().expect("Impossible d'obtenir les métadonnées du fichier");
110+
let file_size = metadata.len();
111+
112+
//println!("MessagePack sauvegardé dans output.msgpack");
113+
114+
// Afficher la taille du fichier dans différentes unités
115+
println!("Taille du fichier : {}", format_file_size(file_size, "o"));
116+
println!("Taille du fichier : {}", format_file_size(file_size, "Ko"));
117+
println!("Taille du fichier : {}", format_file_size(file_size, "Mo"));
118+
println!("Taille du fichier : {}", format_file_size(file_size, "Go"));
119+
120+
}

0 commit comments

Comments
 (0)