Skip to content

Commit 27b5629

Browse files
committed
bootstrap: implement a new utility file_lock
`file_lock` util provides `FileLock` type which is a wrapper around `fs2` and `std::fs::File`. This is useful as `fs2` supports Solaris and has fewer dependencies than `fd-lock`. Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 0919ad1 commit 27b5629

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/bootstrap/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub use core::builder::PathSet;
5151
pub use core::config::flags::Subcommand;
5252
pub use core::config::Config;
5353
pub use utils::change_tracker::{find_recent_config_change_ids, CONFIG_CHANGE_HISTORY};
54+
pub use utils::file_lock::FileLock;
5455

5556
const LLVM_TOOLS: &[&str] = &[
5657
"llvm-cov", // used to generate coverage report

src/bootstrap/src/utils/file_lock.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! This module provides a `FileLock` type, serving as a cross-platform file-locking wrapper
2+
//! for `std::fs::File`, built on top of the `fs2` crate.
3+
//!
4+
//! Locks are automatically released by the `Drop` trait implementation when the `FileLock`
5+
//! instance goes out of scope.
6+
7+
use std::{fs::File, io};
8+
9+
use fs2::FileExt;
10+
11+
use crate::t;
12+
13+
pub struct FileLock(File);
14+
15+
impl FileLock {
16+
pub fn lock(&mut self) -> io::Result<&File> {
17+
self.0.lock_exclusive()?;
18+
19+
Ok(&mut self.0)
20+
}
21+
22+
pub fn try_lock(&mut self) -> io::Result<&File> {
23+
self.0.try_lock_exclusive()?;
24+
25+
Ok(&mut self.0)
26+
}
27+
}
28+
29+
impl Drop for FileLock {
30+
fn drop(&mut self) {
31+
t!(self.0.unlock());
32+
}
33+
}
34+
35+
impl From<File> for FileLock {
36+
fn from(file: File) -> Self {
37+
Self(file)
38+
}
39+
}

src/bootstrap/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub(crate) mod change_tracker;
88
pub(crate) mod channel;
99
pub(crate) mod dylib;
1010
pub(crate) mod exec;
11+
pub(crate) mod file_lock;
1112
pub(crate) mod helpers;
1213
pub(crate) mod job;
1314
#[cfg(feature = "build-metrics")]

0 commit comments

Comments
 (0)