Skip to content

Commit c2f9135

Browse files
Refactor
1 parent a29b5b5 commit c2f9135

File tree

1 file changed

+63
-31
lines changed

1 file changed

+63
-31
lines changed

resources/fs/src/file_helper.rs

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,18 @@ use crate::config::File;
66
use std::fs;
77
use std::fs::File as fsFile;
88
use std::path::Path;
9-
use tracing::{debug, error};
9+
use tracing::{debug};
1010
//use fs_extra::dir::get_size;
1111

1212
pub fn get_file(file: &File) -> Result<File, Box<dyn std::error::Error>> {
13-
let resolved_path = Path::new(file.path.as_str());
14-
debug!("Resolved path: {:?}", resolved_path);
15-
match resolved_path.is_dir() {
16-
true => {
17-
return Ok(File { path: file.path.to_string(), size: None, hash: String::new(), exist: Some(false) });
18-
}
19-
false => {}
20-
}
21-
let f: fsFile = match fsFile::open(resolved_path) {
22-
Ok(f) => f,
13+
match compare_file_state(file) {
14+
Ok(f) => {
15+
Ok(f)
16+
},
2317
Err(e) => {
24-
if e.kind() == std::io::ErrorKind::NotFound {
25-
return Ok(File { path: file.path.to_string(), size: None, hash: String::new(), exist: Some(false) });
26-
} else {
27-
return Err(e)?
28-
}
18+
Err(e)?
2919
}
30-
};
31-
32-
let metadata = f.metadata()?;
33-
let hash = calculate_hash(file.path.as_str())?;
34-
35-
let mut updated_file = file.clone();
36-
updated_file.path = file.path.clone();
37-
updated_file.size = Some(metadata.len());
38-
updated_file.hash = hash;
39-
updated_file.exist = Some(true);
40-
Ok(updated_file)
20+
}
4121
}
4222

4323
// pub fn export_path(path: &str) -> Result<Directory, Box<dyn std::error::Error>> {
@@ -84,14 +64,66 @@ pub fn get_file(file: &File) -> Result<File, Box<dyn std::error::Error>> {
8464
// }
8565

8666
pub fn delete_file(file: &File) -> Result<(), Box<dyn std::error::Error>> {
87-
let path = file.path.as_str();
88-
fs::remove_file(path)?;
89-
Ok(())
67+
match compare_file_state(file) {
68+
Ok(f) => {
69+
debug!("Deleting file: {:?}", f.path);
70+
fs::remove_file(f.path)?;
71+
Ok(())
72+
},
73+
Err(e) => {
74+
Err(e)?
75+
}
76+
}
77+
}
78+
79+
fn compare_file_state(file: &File) -> Result<File, Box<dyn std::error::Error>> {
80+
let resolved_path = Path::new(file.path.as_str());
81+
debug!("Resolved path: {:?}", resolved_path);
82+
match resolved_path.is_dir() {
83+
true => {
84+
debug!("Path is a directory");
85+
let mut updated_file = file.clone();
86+
updated_file.exist = Some(false);
87+
return Ok(updated_file)
88+
}
89+
false => {}
90+
}
91+
let f: fsFile = match fsFile::open(resolved_path) {
92+
Ok(f) => {
93+
debug!("File found: {:?}", file.path);
94+
f
95+
},
96+
Err(e) => {
97+
if e.kind() == std::io::ErrorKind::NotFound {
98+
debug!("File not found: {:?}", file.path);
99+
let mut updated_file = file.clone();
100+
updated_file.exist = Some(false);
101+
return Ok(updated_file)
102+
} else {
103+
return Err(e)?
104+
}
105+
}
106+
};
107+
108+
let hash = calculate_hash(file.path.as_str())?;
109+
110+
if hash.to_lowercase() != file.hash.to_lowercase() {
111+
debug!("Hash mismatch: {:?}", file.path);
112+
let mut updated_file = file.clone();
113+
updated_file.exist = Some(false);
114+
return Ok(updated_file)
115+
}
116+
117+
let metadata = f.metadata()?;
118+
let mut updated_file = file.clone();
119+
updated_file.size = Some(metadata.len());
120+
updated_file.exist = Some(true);
121+
122+
Ok(updated_file)
90123
}
91124

92125
fn calculate_hash(path: &str) -> Result<String, Box<dyn std::error::Error>> {
93126
let bytes = fs::read(path)?;
94127
let digest = sha256::digest(&bytes);
95-
96128
Ok(digest)
97129
}

0 commit comments

Comments
 (0)