@@ -6,38 +6,18 @@ use crate::config::File;
6
6
use std:: fs;
7
7
use std:: fs:: File as fsFile;
8
8
use std:: path:: Path ;
9
- use tracing:: { debug, error } ;
9
+ use tracing:: { debug} ;
10
10
//use fs_extra::dir::get_size;
11
11
12
12
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
+ } ,
23
17
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) ?
29
19
}
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
+ }
41
21
}
42
22
43
23
// 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>> {
84
64
// }
85
65
86
66
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)
90
123
}
91
124
92
125
fn calculate_hash ( path : & str ) -> Result < String , Box < dyn std:: error:: Error > > {
93
126
let bytes = fs:: read ( path) ?;
94
127
let digest = sha256:: digest ( & bytes) ;
95
-
96
128
Ok ( digest)
97
129
}
0 commit comments