1
1
use std:: ffi:: OsStr ;
2
2
use std:: fs;
3
+ use std:: hash:: { Hash , Hasher } ;
3
4
use std:: path:: { Path , PathBuf } ;
4
5
use std:: process:: Command ;
5
6
@@ -71,7 +72,11 @@ fn hash_file(file: &std::path::Path) -> u64 {
71
72
let contents = std:: fs:: read ( file) . unwrap ( ) ;
72
73
#[ allow( deprecated) ]
73
74
let mut hasher = std:: hash:: SipHasher :: new ( ) ;
74
- std:: hash:: Hash :: hash ( & contents, & mut hasher) ;
75
+ // The following is equivalent to
76
+ // std::hash::Hash::hash(&contents, &mut hasher);
77
+ // but gives the same result independent of host byte order.
78
+ hasher. write_usize ( contents. len ( ) . to_le ( ) ) ;
79
+ Hash :: hash_slice ( & contents, & mut hasher) ;
75
80
std:: hash:: Hasher :: finish ( & hasher)
76
81
}
77
82
@@ -80,16 +85,26 @@ fn hash_dir(dir: &std::path::Path) -> u64 {
80
85
for entry in std:: fs:: read_dir ( dir) . unwrap ( ) {
81
86
let entry = entry. unwrap ( ) ;
82
87
if entry. file_type ( ) . unwrap ( ) . is_dir ( ) {
83
- sub_hashes
84
- . insert ( entry. file_name ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) , hash_dir ( & entry. path ( ) ) ) ;
88
+ sub_hashes. insert (
89
+ entry. file_name ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) ,
90
+ hash_dir ( & entry. path ( ) ) . to_le ( ) ,
91
+ ) ;
85
92
} else {
86
- sub_hashes
87
- . insert ( entry. file_name ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) , hash_file ( & entry. path ( ) ) ) ;
93
+ sub_hashes. insert (
94
+ entry. file_name ( ) . to_str ( ) . unwrap ( ) . to_owned ( ) ,
95
+ hash_file ( & entry. path ( ) ) . to_le ( ) ,
96
+ ) ;
88
97
}
89
98
}
90
99
#[ allow( deprecated) ]
91
100
let mut hasher = std:: hash:: SipHasher :: new ( ) ;
92
- std:: hash:: Hash :: hash ( & sub_hashes, & mut hasher) ;
101
+ // The following is equivalent to
102
+ // std::hash::Hash::hash(&sub_hashes, &mut hasher);
103
+ // but gives the same result independent of host byte order.
104
+ hasher. write_usize ( sub_hashes. len ( ) . to_le ( ) ) ;
105
+ for elt in sub_hashes {
106
+ elt. hash ( & mut hasher) ;
107
+ }
93
108
std:: hash:: Hasher :: finish ( & hasher)
94
109
}
95
110
0 commit comments