Skip to content

Commit 9aed5bb

Browse files
committed
implement BuildStamp that is stricter impl for build stamps
Signed-off-by: onur-ozkan <[email protected]>
1 parent 6afee11 commit 9aed5bb

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::{
2+
fs, io,
3+
path::{Path, PathBuf},
4+
};
5+
6+
#[derive(Clone)]
7+
pub struct BuildStamp {
8+
path: PathBuf,
9+
stamp: String,
10+
}
11+
12+
impl From<BuildStamp> for PathBuf {
13+
fn from(value: BuildStamp) -> Self {
14+
value.path
15+
}
16+
}
17+
18+
impl AsRef<Path> for BuildStamp {
19+
fn as_ref(&self) -> &Path {
20+
&self.path
21+
}
22+
}
23+
24+
impl BuildStamp {
25+
pub fn new(dir: &Path) -> Self {
26+
Self { path: dir.join(".stamp"), stamp: String::new() }
27+
}
28+
29+
pub fn with_stamp(mut self, stamp: String) -> Self {
30+
self.stamp = stamp;
31+
self
32+
}
33+
34+
pub fn with_prefix(mut self, prefix: &str) -> Self {
35+
assert!(
36+
!prefix.starts_with('.') && !prefix.ends_with('.'),
37+
"prefix can not start or end with '.'"
38+
);
39+
40+
let stamp_filename = self.path.components().last().unwrap().as_os_str().to_str().unwrap();
41+
let stamp_filename = stamp_filename.strip_prefix('.').unwrap_or(stamp_filename);
42+
self.path.set_file_name(format!(".{prefix}-{stamp_filename}"));
43+
44+
self
45+
}
46+
47+
pub fn remove(self) -> io::Result<()> {
48+
match fs::remove_file(&self.path) {
49+
Ok(()) => Ok(()),
50+
Err(e) => {
51+
if e.kind() == io::ErrorKind::NotFound {
52+
Ok(())
53+
} else {
54+
Err(e)
55+
}
56+
}
57+
}
58+
}
59+
60+
pub fn write(&self) -> io::Result<()> {
61+
fs::write(&self.path, &self.stamp)
62+
}
63+
64+
pub fn is_up_to_date(&self) -> bool {
65+
match fs::read(&self.path) {
66+
Ok(h) => self.stamp.as_bytes() == h.as_slice(),
67+
Err(e) if e.kind() == io::ErrorKind::NotFound => false,
68+
Err(e) => {
69+
panic!("failed to read stamp file `{}`: {}", self.path.display(), e);
70+
}
71+
}
72+
}
73+
}

src/bootstrap/src/utils/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
//! support for a wide range of tasks and operations such as caching, tarballs, release
33
//! channels, job management, etc.
44
5+
pub(crate) mod build_stamp;
56
pub(crate) mod cache;
67
pub(crate) mod cc_detect;
78
pub(crate) mod change_tracker;
89
pub(crate) mod channel;
910
pub(crate) mod exec;
1011
pub(crate) mod helpers;
1112
pub(crate) mod job;
12-
#[cfg(feature = "build-metrics")]
13-
pub(crate) mod metrics;
1413
pub(crate) mod render_tests;
1514
pub(crate) mod shared_helpers;
1615
pub(crate) mod tarball;
16+
17+
#[cfg(feature = "build-metrics")]
18+
pub(crate) mod metrics;
19+
1720
#[cfg(test)]
1821
mod tests;

0 commit comments

Comments
 (0)