Skip to content

Commit 3ca81ca

Browse files
committed
Emulate flock() on Solaris via fcntl().
This should resolve problem with Rust build: rust-lang/rust#103630
1 parent 2f58d9f commit 3ca81ca

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

src/backend/libc/fs/syscalls.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ use crate::fs::Advice;
8686
target_os = "solaris",
8787
)))]
8888
use crate::fs::FallocateFlags;
89-
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
89+
#[cfg(not(target_os = "wasi"))]
9090
use crate::fs::FlockOperation;
9191
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
9292
use crate::fs::MemfdFlags;
@@ -968,6 +968,37 @@ pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result
968968
unsafe { ret(c::flock(borrowed_fd(fd), operation as c::c_int)) }
969969
}
970970

971+
#[cfg(target_os = "solaris")]
972+
pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> {
973+
// Solaris lacks flock(), so try to emulate using fcntl()
974+
let flag = operation as c::c_int;
975+
let mut flock = c::flock {
976+
l_type: 0,
977+
l_whence: 0,
978+
l_start: 0,
979+
l_len: 0,
980+
l_sysid: 0,
981+
l_pid: 0,
982+
l_pad: [0, 0, 0, 0],
983+
};
984+
flock.l_type = if flag & libc::LOCK_UN != 0 {
985+
libc::F_UNLCK
986+
} else if flag & libc::LOCK_EX != 0 {
987+
libc::F_WRLCK
988+
} else if flag & libc::LOCK_SH != 0 {
989+
libc::F_RDLCK
990+
} else {
991+
panic!("unexpected flock() operation")
992+
};
993+
994+
let mut cmd = libc::F_SETLKW;
995+
if (flag & libc::LOCK_NB) != 0 {
996+
cmd = libc::F_SETLK;
997+
}
998+
999+
unsafe { ret(c::fcntl(borrowed_fd(fd), cmd, &flock)) }
1000+
}
1001+
9711002
pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result<Stat> {
9721003
// 32-bit and mips64 Linux: `struct stat64` is not y2038 compatible; use
9731004
// `statx`.

src/backend/libc/fs/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ bitflags! {
850850
/// `LOCK_*` constants for use with [`flock`]
851851
///
852852
/// [`flock`]: crate::fs::flock
853-
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
853+
#[cfg(not(target_os = "wasi"))]
854854
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
855855
#[repr(i32)]
856856
pub enum FlockOperation {

src/fs/fd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::process::{Gid, Uid};
88
use crate::{backend, io};
99
use backend::fd::{AsFd, BorrowedFd};
1010

11-
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
11+
#[cfg(not(target_os = "wasi"))]
1212
pub use backend::fs::types::FlockOperation;
1313

1414
#[cfg(not(any(
@@ -343,7 +343,7 @@ pub fn ftruncate<Fd: AsFd>(fd: Fd, length: u64) -> io::Result<()> {
343343
/// - [Linux]
344344
///
345345
/// [Linux]: https://man7.org/linux/man-pages/man2/flock.2.html
346-
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
346+
#[cfg(not(target_os = "wasi"))]
347347
#[inline]
348348
pub fn flock<Fd: AsFd>(fd: Fd, operation: FlockOperation) -> io::Result<()> {
349349
backend::fs::syscalls::flock(fd.as_fd(), operation)

src/fs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub use fd::fdatasync;
162162
pub use fd::{fallocate, FallocateFlags};
163163
#[cfg(not(target_os = "wasi"))]
164164
pub use fd::{fchmod, fchown};
165-
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
165+
#[cfg(not(target_os = "wasi"))]
166166
pub use fd::{flock, FlockOperation};
167167
pub use fd::{fstat, fsync, ftruncate, futimens, is_file_read_write, seek, tell, Stat, Timestamps};
168168
#[cfg(not(any(

0 commit comments

Comments
 (0)