Skip to content

Commit 125ba2a

Browse files
committed
bootstrap: replace fd-lock with util::file_lock
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 27b5629 commit 125ba2a

File tree

4 files changed

+32
-98
lines changed

4 files changed

+32
-98
lines changed

src/bootstrap/Cargo.lock

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ version = "1.3.2"
2929
source = "registry+https://github.com/rust-lang/crates.io-index"
3030
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
3131

32-
[[package]]
33-
name = "bitflags"
34-
version = "2.4.1"
35-
source = "registry+https://github.com/rust-lang/crates.io-index"
36-
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
37-
3832
[[package]]
3933
name = "block-buffer"
4034
version = "0.10.2"
@@ -53,8 +47,8 @@ dependencies = [
5347
"clap",
5448
"clap_complete",
5549
"cmake",
56-
"fd-lock",
5750
"filetime",
51+
"fs2",
5852
"hex",
5953
"home",
6054
"ignore",
@@ -245,27 +239,6 @@ version = "1.6.1"
245239
source = "registry+https://github.com/rust-lang/crates.io-index"
246240
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
247241

248-
[[package]]
249-
name = "errno"
250-
version = "0.3.5"
251-
source = "registry+https://github.com/rust-lang/crates.io-index"
252-
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860"
253-
dependencies = [
254-
"libc",
255-
"windows-sys",
256-
]
257-
258-
[[package]]
259-
name = "fd-lock"
260-
version = "3.0.13"
261-
source = "registry+https://github.com/rust-lang/crates.io-index"
262-
checksum = "ef033ed5e9bad94e55838ca0ca906db0e043f517adda0c8b79c7a8c66c93c1b5"
263-
dependencies = [
264-
"cfg-if",
265-
"rustix",
266-
"windows-sys",
267-
]
268-
269242
[[package]]
270243
name = "filetime"
271244
version = "0.2.16"
@@ -284,6 +257,16 @@ version = "1.0.7"
284257
source = "registry+https://github.com/rust-lang/crates.io-index"
285258
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
286259

260+
[[package]]
261+
name = "fs2"
262+
version = "0.4.3"
263+
source = "registry+https://github.com/rust-lang/crates.io-index"
264+
checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
265+
dependencies = [
266+
"libc",
267+
"winapi",
268+
]
269+
287270
[[package]]
288271
name = "generic-array"
289272
version = "0.14.5"
@@ -374,12 +357,6 @@ version = "0.2.149"
374357
source = "registry+https://github.com/rust-lang/crates.io-index"
375358
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
376359

377-
[[package]]
378-
name = "linux-raw-sys"
379-
version = "0.4.10"
380-
source = "registry+https://github.com/rust-lang/crates.io-index"
381-
checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f"
382-
383360
[[package]]
384361
name = "log"
385362
version = "0.4.17"
@@ -509,7 +486,7 @@ version = "0.2.13"
509486
source = "registry+https://github.com/rust-lang/crates.io-index"
510487
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
511488
dependencies = [
512-
"bitflags 1.3.2",
489+
"bitflags",
513490
]
514491

515492
[[package]]
@@ -535,19 +512,6 @@ version = "0.6.26"
535512
source = "registry+https://github.com/rust-lang/crates.io-index"
536513
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
537514

538-
[[package]]
539-
name = "rustix"
540-
version = "0.38.19"
541-
source = "registry+https://github.com/rust-lang/crates.io-index"
542-
checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed"
543-
dependencies = [
544-
"bitflags 2.4.1",
545-
"errno",
546-
"libc",
547-
"linux-raw-sys",
548-
"windows-sys",
549-
]
550-
551515
[[package]]
552516
name = "ryu"
553517
version = "1.0.10"
@@ -757,15 +721,6 @@ dependencies = [
757721
"windows-targets",
758722
]
759723

760-
[[package]]
761-
name = "windows-sys"
762-
version = "0.48.0"
763-
source = "registry+https://github.com/rust-lang/crates.io-index"
764-
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
765-
dependencies = [
766-
"windows-targets",
767-
]
768-
769724
[[package]]
770725
name = "windows-targets"
771726
version = "0.48.5"

src/bootstrap/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ clap = { version = "4.4.7", default-features = false, features = ["std", "usage"
3939
clap_complete = "4.4.3"
4040
cmake = "0.1.38"
4141
filetime = "0.2"
42+
fs2 = "0.4.3"
4243
hex = "0.4"
4344
home = "0.5.4"
4445
ignore = "0.4.10"
@@ -62,10 +63,6 @@ xz2 = "0.1"
6263
# Dependencies needed by the build-metrics feature
6364
sysinfo = { version = "0.26.0", optional = true }
6465

65-
# Solaris doesn't support flock() and thus fd-lock is not option now
66-
[target.'cfg(not(target_os = "solaris"))'.dependencies]
67-
fd-lock = "3.0.13"
68-
6966
[target.'cfg(windows)'.dependencies.junction]
7067
version = "1.0.0"
7168

src/bootstrap/src/bin/main.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,40 @@
55
//! parent directory, and otherwise documentation can be found throughout the `build`
66
//! directory in each respective module.
77
8-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
98
use std::io::Write;
10-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
119
use std::process;
1210
use std::{
1311
env, fs,
1412
io::{self, IsTerminal},
1513
};
1614

1715
use bootstrap::{
18-
find_recent_config_change_ids, t, Build, Config, Subcommand, CONFIG_CHANGE_HISTORY,
16+
find_recent_config_change_ids, t, Build, Config, FileLock, Subcommand, CONFIG_CHANGE_HISTORY,
1917
};
2018

2119
fn main() {
2220
let args = env::args().skip(1).collect::<Vec<_>>();
2321
let config = Config::parse(&args);
2422

25-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
26-
let mut build_lock;
27-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
28-
let _build_lock_guard;
29-
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
23+
let lock_path = config.out.join("lock");
3024
// Display PID of process holding the lock
3125
// PID will be stored in a lock file
32-
{
33-
let path = config.out.join("lock");
34-
let pid = match fs::read_to_string(&path) {
35-
Ok(contents) => contents,
36-
Err(_) => String::new(),
37-
};
38-
39-
build_lock =
40-
fd_lock::RwLock::new(t!(fs::OpenOptions::new().write(true).create(true).open(&path)));
41-
_build_lock_guard = match build_lock.try_write() {
42-
Ok(mut lock) => {
43-
t!(lock.write(&process::id().to_string().as_ref()));
44-
lock
45-
}
46-
err => {
47-
drop(err);
48-
println!("WARNING: build directory locked by process {pid}, waiting for lock");
49-
let mut lock = t!(build_lock.write());
50-
t!(lock.write(&process::id().to_string().as_ref()));
51-
lock
52-
}
53-
};
54-
}
26+
let pid = fs::read_to_string(&lock_path).unwrap_or_default();
27+
28+
let mut build_lock: FileLock =
29+
t!(fs::OpenOptions::new().write(true).create(true).open(&lock_path)).into();
5530

56-
#[cfg(any(not(any(unix, windows)), target_os = "solaris"))]
57-
println!("WARNING: file locking not supported for target, not locking build directory");
31+
match build_lock.try_lock() {
32+
Ok(mut file) => {
33+
t!(file.write(&process::id().to_string().as_ref()));
34+
}
35+
err => {
36+
drop(err);
37+
println!("WARNING: build directory locked by process {pid}, waiting for lock");
38+
let mut file = t!(build_lock.lock());
39+
t!(file.write(&process::id().to_string().as_ref()));
40+
}
41+
};
5842

5943
// check_version warnings are not printed during setup
6044
let changelog_suggestion =

src/bootstrap/src/utils/file_lock.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ pub struct FileLock(File);
1515
impl FileLock {
1616
pub fn lock(&mut self) -> io::Result<&File> {
1717
self.0.lock_exclusive()?;
18-
19-
Ok(&mut self.0)
18+
Ok(&self.0)
2019
}
2120

2221
pub fn try_lock(&mut self) -> io::Result<&File> {
2322
self.0.try_lock_exclusive()?;
24-
25-
Ok(&mut self.0)
23+
Ok(&self.0)
2624
}
2725
}
2826

0 commit comments

Comments
 (0)