Skip to content

Commit

Permalink
Implements cargo file locking using fcntl on Solaris.
Browse files Browse the repository at this point in the history
Fixes #11421.
  • Loading branch information
psumbera committed Nov 29, 2022
1 parent 861110c commit 3ebcecd
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/cargo/util/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
}

Expand Down

0 comments on commit 3ebcecd

Please sign in to comment.