Fix compilation error on Solaris due to flock usage

PR 130999 added the file_lock feature, but libc does not define
flock() for the Solaris platform leading to a compilation error.

Additionally, I went through all the Tier 2 platforms and read through
their documentation to see whether flock was implemented. This turned up
5 more Unix platforms where flock is not supported, even though it may
exist in the libc crate.
This commit is contained in:
Christopher Berner
2024-11-12 20:02:47 -08:00
parent 81eef2d362
commit 0df3ef4435
2 changed files with 108 additions and 0 deletions

View File

@@ -1254,16 +1254,54 @@ impl File {
}
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
))]
pub fn lock(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
return Ok(());
}
#[cfg(not(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
)))]
pub fn lock(&self) -> io::Result<()> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock() not supported"))
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
))]
pub fn lock_shared(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
return Ok(());
}
#[cfg(not(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
)))]
pub fn lock_shared(&self) -> io::Result<()> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
))]
pub fn try_lock(&self) -> io::Result<bool> {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
if let Err(ref err) = result {
@@ -1275,6 +1313,22 @@ impl File {
return Ok(true);
}
#[cfg(not(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
)))]
pub fn try_lock(&self) -> io::Result<bool> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock() not supported"))
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
))]
pub fn try_lock_shared(&self) -> io::Result<bool> {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
if let Err(ref err) = result {
@@ -1286,11 +1340,37 @@ impl File {
return Ok(true);
}
#[cfg(not(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
)))]
pub fn try_lock_shared(&self) -> io::Result<bool> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock_shared() not supported"))
}
#[cfg(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
))]
pub fn unlock(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
return Ok(());
}
#[cfg(not(any(
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_vendor = "apple",
)))]
pub fn unlock(&self) -> io::Result<()> {
Err(io::const_io_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
}
pub fn truncate(&self, size: u64) -> io::Result<()> {
let size: off64_t =
size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;