Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
361 changes: 120 additions & 241 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,267 +1441,146 @@ impl File {
}
}

#[cfg(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
))]
pub fn lock(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
return Ok(());
}

#[cfg(target_os = "solaris")]
pub fn lock(&self) -> io::Result<()> {
let mut flock: libc::flock = unsafe { mem::zeroed() };
flock.l_type = libc::F_WRLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
cvt(unsafe { libc::fcntl(self.as_raw_fd(), libc::F_SETLKW, &flock) })?;
Ok(())
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
)))]
pub fn lock(&self) -> io::Result<()> {
Err(io::const_error!(io::ErrorKind::Unsupported, "lock() not supported"))
}

#[cfg(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
))]
pub fn lock_shared(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
return Ok(());
}

#[cfg(target_os = "solaris")]
pub fn lock_shared(&self) -> io::Result<()> {
let mut flock: libc::flock = unsafe { mem::zeroed() };
flock.l_type = libc::F_RDLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
cvt(unsafe { libc::fcntl(self.as_raw_fd(), libc::F_SETLKW, &flock) })?;
Ok(())
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
)))]
pub fn lock_shared(&self) -> io::Result<()> {
Err(io::const_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
}

#[cfg(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
))]
pub fn try_lock(&self) -> Result<(), TryLockError> {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
cfg_select! {
any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
) => {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
return Ok(());
}
_ => {
Err(io::const_error!(io::ErrorKind::Unsupported, "lock() not supported"))
}
} else {
Ok(())
}
}

#[cfg(target_os = "solaris")]
pub fn try_lock(&self) -> Result<(), TryLockError> {
let mut flock: libc::flock = unsafe { mem::zeroed() };
flock.l_type = libc::F_WRLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
let result = cvt(unsafe { libc::fcntl(self.as_raw_fd(), libc::F_SETLK, &flock) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
pub fn lock_shared(&self) -> io::Result<()> {
cfg_select! {
any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
) => {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
return Ok(());
}
_ => {
Err(io::const_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
}
} else {
Ok(())
}
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
)))]
pub fn try_lock(&self) -> Result<(), TryLockError> {
Err(TryLockError::Error(io::const_error!(
io::ErrorKind::Unsupported,
"try_lock() not supported"
)))
}

#[cfg(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
))]
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
cfg_select! {
any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
) => {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
}
} else {
Ok(())
}
}
_ => {
Err(TryLockError::Error(io::const_error!(
io::ErrorKind::Unsupported,
"try_lock() not supported"
)))
}
} else {
Ok(())
}
}

#[cfg(target_os = "solaris")]
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
let mut flock: libc::flock = unsafe { mem::zeroed() };
flock.l_type = libc::F_RDLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
let result = cvt(unsafe { libc::fcntl(self.as_raw_fd(), libc::F_SETLK, &flock) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
cfg_select! {
any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
) => {
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
if let Err(err) = result {
if err.kind() == io::ErrorKind::WouldBlock {
Err(TryLockError::WouldBlock)
} else {
Err(TryLockError::Error(err))
}
} else {
Ok(())
}
}
_ => {
Err(TryLockError::Error(io::const_error!(
io::ErrorKind::Unsupported,
"try_lock_shared() not supported"
)))
}
} else {
Ok(())
}
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
)))]
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
Err(TryLockError::Error(io::const_error!(
io::ErrorKind::Unsupported,
"try_lock_shared() not supported"
)))
}

#[cfg(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
))]
pub fn unlock(&self) -> io::Result<()> {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
return Ok(());
}

#[cfg(target_os = "solaris")]
pub fn unlock(&self) -> io::Result<()> {
let mut flock: libc::flock = unsafe { mem::zeroed() };
flock.l_type = libc::F_UNLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
cvt(unsafe { libc::fcntl(self.as_raw_fd(), libc::F_SETLKW, &flock) })?;
Ok(())
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
)))]
pub fn unlock(&self) -> io::Result<()> {
Err(io::const_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
cfg_select! {
any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "illumos",
target_os = "aix",
target_os = "android",
target_vendor = "apple",
) => {
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
return Ok(());
}
_ => {
Err(io::const_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
}
}
}

pub fn truncate(&self, size: u64) -> io::Result<()> {
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/pass/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ fn test_pread_pwrite() {
assert_eq!(&buf1, b" m");
}

// Miri does not support the way this is implemented on Solaris
// (https://github.com/rust-lang/miri/issues/5038).
// Solaris does not support per-handle file locking.
#[cfg(not(target_os = "solaris"))]
fn test_flock() {
let bytes = b"Hello, World!\n";
Expand Down
Loading