Skip to content

Commit

Permalink
Open fifo twice to have independent file descriptor
Browse files Browse the repository at this point in the history
So that setting `O_NONBLOCK` would not affect other.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Jul 16, 2024
1 parent 588029f commit 423c058
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
11 changes: 3 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub enum FromEnvErrorKind {
NotAPipe,
/// Jobserver inheritance is not supported on this platform.
Unsupported,
/// Cannot clone the jobserver fifo fd
CannotClone,
}

impl FromEnvError {
Expand All @@ -52,7 +50,6 @@ impl FromEnvError {
FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd,
FromEnvErrorInner::NotAPipe(..) => FromEnvErrorKind::NotAPipe,
FromEnvErrorInner::Unsupported => FromEnvErrorKind::Unsupported,
FromEnvErrorInner::CannotClone(..) => FromEnvErrorKind::CannotClone,
}
}
}
Expand All @@ -69,17 +66,16 @@ impl std::fmt::Display for FromEnvError {
FromEnvErrorInner::NotAPipe(fd, None) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe"),
FromEnvErrorInner::NotAPipe(fd, Some(err)) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe: {err}"),
FromEnvErrorInner::Unsupported => write!(f, "jobserver inheritance is not supported on this platform"),
FromEnvErrorInner::CannotClone(fd, err) => write!(f, "file descriptor {fd} created fromjobserver environment variable value cannot be cloned: {err}"),
}
}
}
impl std::error::Error for FromEnvError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.inner {
FromEnvErrorInner::CannotOpenPath(_, err) => Some(err),
FromEnvErrorInner::NotAPipe(_, Some(err))
| FromEnvErrorInner::CannotOpenFd(_, err)
| FromEnvErrorInner::CannotClone(_, err) => Some(err),
FromEnvErrorInner::NotAPipe(_, Some(err)) | FromEnvErrorInner::CannotOpenFd(_, err) => {
Some(err)
}
_ => None,
}
}
Expand All @@ -96,5 +92,4 @@ pub(crate) enum FromEnvErrorInner {
NegativeFd(RawFd),
NotAPipe(RawFd, Option<std::io::Error>),
Unsupported,
CannotClone(RawFd, std::io::Error),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ mod test {
client.try_acquire().unwrap().unwrap();
}

#[cfg(any(not(unix), linux))]
#[cfg(any(windows, target_os = "linux"))]
#[test]
fn test_try_acquire() {
let client = Client::new(0).unwrap();
Expand Down
18 changes: 9 additions & 9 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ impl Client {
})?;
let path = Path::new(path_str);

let file = OpenOptions::new()
.read(true)
.write(true)
.open(path)
.map_err(|err| FromEnvErrorInner::CannotOpenPath(path_str.to_string(), err))?;
let open_file = || {
OpenOptions::new()
.read(true)
.write(true)
.open(path)
.map_err(|err| FromEnvErrorInner::CannotOpenPath(path_str.to_string(), err))
};

Ok(Some(Client {
read: file
.try_clone()
.map_err(|err| FromEnvErrorInner::CannotClone(file.as_raw_fd(), err))?,
write: file,
read: open_file()?,
write: open_file()?,
creation_arg: ClientCreationArg::Fifo(path.into()),
is_non_blocking: Some(AtomicBool::new(false)),
}))
Expand Down

0 comments on commit 423c058

Please sign in to comment.