File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ #[ allow( deprecated) ]
2
+ use std:: hash:: SipHasher ;
3
+ use std:: hash:: { Hash , Hasher } ;
4
+ use std:: path:: Path ;
5
+
6
+ #[ derive( Copy , Clone , Debug ) ]
7
+ pub ( crate ) enum IsDirty {
8
+ Clean ,
9
+ Dirty ,
10
+ }
11
+
12
+ impl IsDirty {
13
+ pub ( crate ) fn check_file ( file : & Path , expected_hash : u64 ) -> Self {
14
+ let contents = std:: fs:: read ( file) . unwrap ( ) ;
15
+
16
+ #[ allow( deprecated) ]
17
+ let mut hasher = SipHasher :: new ( ) ;
18
+ contents. hash ( & mut hasher) ;
19
+ let actual_hash = hasher. finish ( ) ;
20
+
21
+ if actual_hash == expected_hash { IsDirty :: Clean } else { IsDirty :: Dirty }
22
+ }
23
+ }
24
+
25
+ pub ( crate ) struct HashedFile {
26
+ contents : Vec < u8 > ,
27
+ hash : u64 ,
28
+ }
29
+
30
+ impl HashedFile {
31
+ pub ( crate ) fn read_file ( file : & Path ) -> Self {
32
+ let contents = std:: fs:: read ( file) . unwrap ( ) ;
33
+
34
+ #[ allow( deprecated) ]
35
+ let mut hasher = SipHasher :: new ( ) ;
36
+ contents. hash ( & mut hasher) ;
37
+ let hash = hasher. finish ( ) ;
38
+
39
+ HashedFile { contents, hash }
40
+ }
41
+
42
+ pub ( crate ) fn write_if_changed ( & self , target : & Path ) -> IsDirty {
43
+ let is_dirty = IsDirty :: check_file ( target, self . hash ) ;
44
+ if let IsDirty :: Dirty = is_dirty {
45
+ std:: fs:: write ( target, & self . contents ) . unwrap ( ) ;
46
+ }
47
+ is_dirty
48
+ }
49
+ }
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ mod bench;
13
13
mod build_backend;
14
14
mod build_sysroot;
15
15
mod config;
16
+ mod hash;
16
17
mod path;
17
18
mod prepare;
18
19
mod rustc_info;
You can’t perform that action at this time.
0 commit comments