@@ -5,7 +5,7 @@ const READ_CAPACITY: usize = 1024 * 1024 * 8; // Read file in chunks of 8MB
5
5
6
6
// To get file checksums
7
7
use hex:: { encode, decode} ;
8
- use sha3:: { Sha3_512 , Digest } ;
8
+ use sha3:: Digest ;
9
9
// To log the program process
10
10
use log:: * ;
11
11
// To manage hex to ascii conversion
@@ -16,44 +16,103 @@ use std::path::Path;
16
16
// To read file content
17
17
use std:: io:: { BufRead , BufReader } ;
18
18
19
- // To calculate file content hash in sha512 format (SHA3 implementation)
20
- pub fn get_checksum ( filename : String , read_limit : usize ) -> String {
21
- let mut hasher = Sha3_512 :: new ( ) ;
19
+ // To calculate file content hash (SHA3 implementation)
20
+ pub fn get_checksum < T : Digest > ( filename : String , read_limit : usize , mut hasher : T ) -> String {
21
+ // let mut hasher = Sha3_512::new();
22
22
let mut length = 1 ;
23
23
let mut iteration = 0 ;
24
24
let mut data_read = 0 ;
25
+ let limit: u64 = ( read_limit * 1024 * 1024 ) . try_into ( ) . unwrap ( ) ;
25
26
26
27
if Path :: new ( & filename) . is_file ( ) {
27
28
debug ! ( "Getting hash of file: {}" , filename) ;
28
29
match File :: open ( filename. clone ( ) ) {
29
30
Ok ( file) => {
31
+ let size = file. metadata ( ) . unwrap ( ) . len ( ) ;
30
32
let mut reader = BufReader :: with_capacity ( READ_CAPACITY , file) ;
31
33
32
- while length > 0 && data_read <= read_limit {
33
- if iteration == 2 {
34
- debug ! ( "Big file detected, the hash will take a while" ) ;
35
- }
36
-
37
- length = {
38
- match reader. fill_buf ( ) {
39
- Ok ( buffer) =>{
40
- hasher. update ( buffer) ;
41
- buffer. len ( )
42
- } ,
43
- Err ( e) => {
44
- debug ! ( "Cannot read file. Checksum set to 'UNKNOWN', error: {}" , e) ;
45
- 0
46
- }
34
+
35
+ if size > limit {
36
+ info ! ( "File '{}' checksum skipped. File size is above limit." , filename) ;
37
+ String :: from ( "UNKNOWN" )
38
+ } else {
39
+ while length > 0 && data_read <= read_limit {
40
+ if iteration == 2 {
41
+ debug ! ( "Big file detected, the hash will take a while" ) ;
47
42
}
43
+
44
+ length = {
45
+ match reader. fill_buf ( ) {
46
+ Ok ( buffer) =>{
47
+ hasher. update ( buffer) ;
48
+ buffer. len ( )
49
+ } ,
50
+ Err ( e) => {
51
+ debug ! ( "Cannot read file. Checksum set to 'UNKNOWN', error: {}" , e) ;
52
+ 0
53
+ }
54
+ }
55
+ } ;
56
+ reader. consume ( length) ;
57
+ data_read += length / ( 1024 * 1024 ) ;
58
+ iteration += 1 ;
48
59
} ;
49
- reader. consume ( length) ;
50
- data_read += length / ( 1024 * 1024 ) ;
51
- iteration += 1 ;
52
- } ;
53
- if data_read > read_limit {
60
+ encode ( hasher. finalize ( ) )
61
+ }
62
+ } ,
63
+ Err ( e) => {
64
+ debug ! ( "Cannot open file to get checksum, error: {:?}" , e) ;
65
+ String :: from ( "UNKNOWN" )
66
+ }
67
+ }
68
+ } else {
69
+ debug ! ( "Cannot produce checksum of a removed file or directory." ) ;
70
+ String :: from ( "UNKNOWN" )
71
+ }
72
+ }
73
+
74
+ // ----------------------------------------------------------------------------
75
+
76
+ pub fn get_checksumv2 < T : Digest > ( filename : String , read_limit : usize , mut hasher : T ) -> String {
77
+ //let mut hasher = Sha3_512::new();
78
+ let mut length = 1 ;
79
+ let mut iteration = 0 ;
80
+ let mut data_read = 0 ;
81
+ let limit: usize = read_limit * 1024 * 1024 ;
82
+
83
+ if Path :: new ( & filename) . is_file ( ) {
84
+ debug ! ( "Getting hash of file: {}" , filename) ;
85
+ match File :: open ( filename. clone ( ) ) {
86
+ Ok ( file) => {
87
+ let size: usize = file. metadata ( ) . unwrap ( ) . len ( ) as usize ;
88
+ let mut reader = BufReader :: with_capacity ( READ_CAPACITY , file) ;
89
+
90
+
91
+ if size > limit {
54
92
info ! ( "File '{}' checksum skipped. File size is above limit." , filename) ;
55
93
String :: from ( "UNKNOWN" )
56
94
} else {
95
+ while length > 0 && data_read <= limit {
96
+ if iteration == 2 {
97
+ debug ! ( "Big file detected, the hash will take a while" ) ;
98
+ }
99
+
100
+ length = {
101
+ match reader. fill_buf ( ) {
102
+ Ok ( buffer) =>{
103
+ hasher. update ( buffer) ;
104
+ buffer. len ( )
105
+ } ,
106
+ Err ( e) => {
107
+ debug ! ( "Cannot read file. Checksum set to 'UNKNOWN', error: {}" , e) ;
108
+ 0
109
+ }
110
+ }
111
+ } ;
112
+ reader. consume ( length) ;
113
+ data_read += length;
114
+ iteration += 1 ;
115
+ } ;
57
116
encode ( hasher. finalize ( ) )
58
117
}
59
118
} ,
0 commit comments