Skip to content

Commit f7afcb3

Browse files
committed
[WIP] File hashing
1 parent dbefa83 commit f7afcb3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

build_system/hash.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

build_system/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod bench;
1313
mod build_backend;
1414
mod build_sysroot;
1515
mod config;
16+
mod hash;
1617
mod path;
1718
mod prepare;
1819
mod rustc_info;

0 commit comments

Comments
 (0)