1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
use rustypath::RPath;
pub struct File {
path: RPath,
duplicates: Vec<RPath>,
}
pub struct Files {
files: Vec<File>,
}
struct Count {
value: u64,
}
impl Count {
fn get(&self) -> u64 {
self.value
}
fn add(&mut self, val: u64) {
self.value = self.value + val;
}
fn new() -> Self{
Self{
value:0,
}
}
}
impl Files {
pub fn new(path: RPath, recursive: bool) -> Self {
if recursive {
Self {
files: Files::scan_recursive(&path),
}
} else {
Self{
files: Files::scan(&path),
}
}
}
fn scan_recursive(path: &RPath) -> Vec<File> {
let mut file_rpaths: Vec<File> = Vec::new();
if let Ok(entries) = std::fs::read_dir(path.convert_to_pathbuf()) {
for entry in entries.flatten() {
let file_path = entry.path();
if file_path.ends_with(".cargo") || file_path.ends_with(".git") {
continue;
}
if file_path.is_dir() {
// If the entry is a directory, recursively call scan on it
let subdirectory_files = Self::scan_recursive(&RPath::new_from_pbuf(&file_path));
for sub_file in subdirectory_files {
file_rpaths.push(sub_file);
}
} else {
// If the entry is a file, add it to the list of files
if let Some(file_name) = entry.file_name().to_str() {
file_rpaths.push(File {
path: path.join(&file_name),
duplicates: Vec::new(),
});
}
}
}
}
file_rpaths
}
fn scan(path: &RPath) -> Vec<File> {
let mut file_rpaths: Vec<File> = Vec::new();
if let Ok(entries) = std::fs::read_dir(path.convert_to_pathbuf()) {
for entry in entries.filter_map(Result::ok) {
let metadata = entry.metadata().ok();
if let Some(metadata) = metadata {
if metadata.is_file() {
if let Some(_file_name) = entry.file_name().to_str() {
let file_path = entry.path();
file_rpaths.push(File {
path: RPath::new_from_pbuf(&file_path),
duplicates: Vec::new(),
});
}
}
}
}
}
file_rpaths
}
pub fn find_duplicates(&mut self, explicit: bool) {
// PRINCIPLE
// only files that were scanned and that were found as a duplicate will be added to done list.
// if a file is not scanned and not found as a duplicate, it will be subject to either of those things
// therefore they will be scanned as a primary file or they will be found as a duplicate
// MECHANISM
// [FILES], [DONE]
// -> take one file from [FILES] and scan it, also add it to [DONE]
// -> take another file from [FILES] that is neither the same file as the previous file
// nor is added to [DONE]. Then scan it and if duplicate, then add it to the original file's duplicate list
// and push this current file to [DONE]
// -> repeat for files not in [DONE]
let mut done: Vec<String> = Vec::new(); // if a file is scanned, dont scan it again
let mut count = Count::new();
let start_time = std::time::Instant::now();
for i in 0..=self.files.len()-1 {
if !done.contains(&self.files[i].path.convert_to_string()) {
if explicit {
println!("Scanning {}", self.files[i].path.convert_to_string().replace("\\\\?\\", ""));
}
// read each file's content and check with all other files except itself.
let content = std::fs::read(self.files[i].path.convert_to_pathbuf().clone())
.expect(&("Unable to read ".to_string()+&self.files[i].path.convert_to_string()));
done.push(self.files[i].path.clone().convert_to_string());
count.add(1);
// let mut title_count = 0;
// let mut filecounts = 1;
for j in 0..=self.files.len()-1 {
if j == i {
continue;
} else if !done.contains(&self.files[j].path.convert_to_string()){
// read content
let content2 = std::fs::read(self.files[j].path.convert_to_pathbuf())
.expect(&("Unable to read ".to_string()+&self.files[j].path.convert_to_string()));
if content == content2 {
let f = self.files[j].path.clone();
done.push(f.convert_to_string());
count.add(1);
self.files[i].duplicates.push(f);
}
} else {
// if already found as a duplicate of some file, nevermind.
continue;
}
}
}
}
let end_time = std::time::Instant::now();
let runtime = end_time.duration_since(start_time);
println!("");
if runtime.as_secs_f64() >= 60.0 {
println!("Scanned {} files in {:.2}m.", count.get(), runtime.as_secs_f64()/60.0);
} else {
println!("Scanned {} files in {:.3}s.", count.get(), runtime.as_secs_f64());
}
}
pub fn show(&self) {
for file in &self.files {
if file.duplicates.len() > 0 {
let mut string = file.path.convert_to_string().replace("\\\\?\\", "") + " -> {";
let mut count = 0;
for d in &file.duplicates {
if count > 0 {
string = string + ", " + &d.convert_to_string().replace("\\\\?\\", "");
} else {
string = string + " " + &d.convert_to_string().replace("\\\\?\\", "");
}
count += 1;
}
string = string + " }";
if string != file.path.convert_to_string().replace("\\\\?\\", "") + " ->{ }" {
println!("{}", string);
}
}
}
}
pub fn formatted(&self) {
for file in &self.files {
if file.duplicates.len() > 0 {
println!("{}", file.path.convert_to_string().replace("\\\\?\\", ""));
for d in &file.duplicates {
println!(" {} <- duplicate", d.convert_to_string().replace("\\\\?\\", ""));
}
println!("");
}
}
}
pub fn delete_duplicates(&self, formatted: bool) {
for file in &self.files {
if file.duplicates.len() > 0 {
if formatted {
println!("\n[DELETE]");
println!("{}", file.path.convert_to_string());
} else {
println!("\n");
}
for d in &file.duplicates {
// delete
match std::fs::remove_file(d.convert_to_string()) {
Ok(_) => {
if formatted {
println!(" DELETE {}", d.convert_to_string().replace("\\\\?\\", ""));
} else {
println!("DELETE {}", d.convert_to_string().replace("\\\\?\\", ""));
}
},
Err(e) => {
if formatted {
eprintln!(" DELETE ERROR {}: {}", e, d.convert_to_string().replace("\\\\?\\", ""));
} else {
eprintln!("DELETE ERROR {}: {}", e, d.convert_to_string().replace("\\\\?\\", ""));
}
}
}
}
}
}
}
}