Skip to content

Commit

Permalink
bootstrap: replace fd-lock with util::file_lock
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Dec 3, 2023
1 parent 27b5629 commit fb59860
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 100 deletions.
69 changes: 12 additions & 57 deletions src/bootstrap/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"

[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"

[[package]]
name = "block-buffer"
version = "0.10.2"
Expand All @@ -53,8 +47,8 @@ dependencies = [
"clap",
"clap_complete",
"cmake",
"fd-lock",
"filetime",
"fs2",
"hex",
"home",
"ignore",
Expand Down Expand Up @@ -245,27 +239,6 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"

[[package]]
name = "errno"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860"
dependencies = [
"libc",
"windows-sys",
]

[[package]]
name = "fd-lock"
version = "3.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef033ed5e9bad94e55838ca0ca906db0e043f517adda0c8b79c7a8c66c93c1b5"
dependencies = [
"cfg-if",
"rustix",
"windows-sys",
]

[[package]]
name = "filetime"
version = "0.2.16"
Expand All @@ -284,6 +257,16 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "fs2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
dependencies = [
"libc",
"winapi",
]

[[package]]
name = "generic-array"
version = "0.14.5"
Expand Down Expand Up @@ -374,12 +357,6 @@ version = "0.2.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"

[[package]]
name = "linux-raw-sys"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f"

[[package]]
name = "log"
version = "0.4.17"
Expand Down Expand Up @@ -509,7 +486,7 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
dependencies = [
"bitflags 1.3.2",
"bitflags",
]

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

[[package]]
name = "rustix"
version = "0.38.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed"
dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]

[[package]]
name = "ryu"
version = "1.0.10"
Expand Down Expand Up @@ -757,15 +721,6 @@ dependencies = [
"windows-targets",
]

[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]

[[package]]
name = "windows-targets"
version = "0.48.5"
Expand Down
5 changes: 1 addition & 4 deletions src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ clap = { version = "4.4.7", default-features = false, features = ["std", "usage"
clap_complete = "4.4.3"
cmake = "0.1.38"
filetime = "0.2"
fs2 = "0.4.3"
hex = "0.4"
home = "0.5.4"
ignore = "0.4.10"
Expand All @@ -62,10 +63,6 @@ xz2 = "0.1"
# Dependencies needed by the build-metrics feature
sysinfo = { version = "0.26.0", optional = true }

# Solaris doesn't support flock() and thus fd-lock is not option now
[target.'cfg(not(target_os = "solaris"))'.dependencies]
fd-lock = "3.0.13"

[target.'cfg(windows)'.dependencies.junction]
version = "1.0.0"

Expand Down
50 changes: 17 additions & 33 deletions src/bootstrap/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,40 @@
//! parent directory, and otherwise documentation can be found throughout the `build`
//! directory in each respective module.
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
use std::io::Write;
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
use std::process;
use std::{
env, fs,
io::{self, IsTerminal},
};

use bootstrap::{
find_recent_config_change_ids, t, Build, Config, Subcommand, CONFIG_CHANGE_HISTORY,
find_recent_config_change_ids, t, Build, Config, FileLock, Subcommand, CONFIG_CHANGE_HISTORY,
};

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

#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
let mut build_lock;
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
let _build_lock_guard;
#[cfg(all(any(unix, windows), not(target_os = "solaris")))]
let lock_path = config.out.join("lock");
// Display PID of process holding the lock
// PID will be stored in a lock file
{
let path = config.out.join("lock");
let pid = match fs::read_to_string(&path) {
Ok(contents) => contents,
Err(_) => String::new(),
};

build_lock =
fd_lock::RwLock::new(t!(fs::OpenOptions::new().write(true).create(true).open(&path)));
_build_lock_guard = match build_lock.try_write() {
Ok(mut lock) => {
t!(lock.write(&process::id().to_string().as_ref()));
lock
}
err => {
drop(err);
println!("WARNING: build directory locked by process {pid}, waiting for lock");
let mut lock = t!(build_lock.write());
t!(lock.write(&process::id().to_string().as_ref()));
lock
}
};
}
let pid = fs::read_to_string(&lock_path).unwrap_or_default();

let mut build_lock: FileLock =
t!(fs::OpenOptions::new().write(true).create(true).open(&lock_path)).into();

#[cfg(any(not(any(unix, windows)), target_os = "solaris"))]
println!("WARNING: file locking not supported for target, not locking build directory");
match build_lock.try_lock() {
Ok(mut file) => {
t!(file.write(&process::id().to_string().as_ref()));
}
err => {
drop(err);
println!("WARNING: build directory locked by process {pid}, waiting for lock");
let mut file = t!(build_lock.lock());
t!(file.write(&process::id().to_string().as_ref()));
}
};

// check_version warnings are not printed during setup
let changelog_suggestion =
Expand Down
9 changes: 3 additions & 6 deletions src/bootstrap/src/utils/file_lock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! This module provides a `FileLock` type, serving as a cross-platform file-locking wrapper
//! for `std::fs::File`, built on top of the `fs2` crate.
//!
//! Locks are automatically released by the `Drop` trait implementation when the `FileLock`
//! instance goes out of scope.
//! Locks are automatically released with the `Drop` trait implementation.
use std::{fs::File, io};

Expand All @@ -15,14 +14,12 @@ pub struct FileLock(File);
impl FileLock {
pub fn lock(&mut self) -> io::Result<&File> {
self.0.lock_exclusive()?;

Ok(&mut self.0)
Ok(&self.0)
}

pub fn try_lock(&mut self) -> io::Result<&File> {
self.0.try_lock_exclusive()?;

Ok(&mut self.0)
Ok(&self.0)
}
}

Expand Down

0 comments on commit fb59860

Please sign in to comment.