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
4 changes: 4 additions & 0 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ fn file_test_io_seek_and_write() {
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "solaris",
target_vendor = "apple",
))]
fn file_lock_multiple_shared() {
Expand All @@ -249,6 +250,7 @@ fn file_lock_multiple_shared() {
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "solaris",
target_vendor = "apple",
))]
fn file_lock_blocking() {
Expand All @@ -273,6 +275,7 @@ fn file_lock_blocking() {
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "solaris",
target_vendor = "apple",
))]
fn file_lock_drop() {
Expand All @@ -294,6 +297,7 @@ fn file_lock_drop() {
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "solaris",
target_vendor = "apple",
))]
fn file_lock_dup() {
Expand Down
66 changes: 66 additions & 0 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,13 +1293,23 @@ impl File {
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 = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_vendor = "apple",
)))]
pub fn lock(&self) -> io::Result<()> {
Expand All @@ -1320,13 +1330,23 @@ impl File {
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 = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_vendor = "apple",
)))]
pub fn lock_shared(&self) -> io::Result<()> {
Expand Down Expand Up @@ -1355,13 +1375,31 @@ impl File {
}
}

#[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))
}
} else {
Ok(())
}
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_vendor = "apple",
)))]
pub fn try_lock(&self) -> Result<(), TryLockError> {
Expand Down Expand Up @@ -1393,13 +1431,31 @@ impl File {
}
}

#[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))
}
} else {
Ok(())
}
}

#[cfg(not(any(
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_vendor = "apple",
)))]
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
Expand All @@ -1423,13 +1479,23 @@ impl File {
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 = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
target_os = "solaris",
target_vendor = "apple",
)))]
pub fn unlock(&self) -> io::Result<()> {
Expand Down
Loading