From 3ebcecd7dd90c7ade16c0a03781828bbfe6e9744 Mon Sep 17 00:00:00 2001 From: Petr Sumbera Date: Tue, 29 Nov 2022 15:45:54 +0100 Subject: [PATCH] Implements cargo file locking using fcntl on Solaris. Fixes #11421. --- src/cargo/util/flock.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/flock.rs b/src/cargo/util/flock.rs index 2986abb905df..9d73e14bcca3 100644 --- a/src/cargo/util/flock.rs +++ b/src/cargo/util/flock.rs @@ -395,8 +395,38 @@ mod sys { #[cfg(target_os = "solaris")] fn flock(file: &File, flag: libc::c_int) -> Result<()> { - // Solaris lacks flock(), so simply succeed with a no-op - Ok(()) + // Solaris lacks flock(), so try to emulate using fcntl() + let mut flock = libc::flock { + l_type: 0, + l_whence: 0, + l_start: 0, + l_len: 0, + l_sysid: 0, + l_pid: 0, + l_pad: [0, 0, 0, 0], + }; + if (flag & libc::LOCK_SH) != 0 { + flock.l_type |= libc::F_RDLCK; + } + if (flag & libc::LOCK_EX) != 0 { + flock.l_type |= libc::F_WRLCK; + } + if (flag & libc::LOCK_UN) != 0 { + flock.l_type |= libc::F_UNLCK; + } + + let mut ltype = libc::F_SETLKW; + if (flag & libc::LOCK_NB) != 0 { + ltype = libc::F_SETLK; + } + + let ret = unsafe { libc::fcntl(file.as_raw_fd(), ltype, &flock) }; + + if ret < 0 { + Err(Error::last_os_error()) + } else { + Ok(()) + } } }