Skip to content

Commit

Permalink
Adjust retrieve_tty to return anyhow errors
Browse files Browse the repository at this point in the history
When we devised the retrieve_tty function we skimped on the error
reporting because 1) we never actually evaluate the error in any way and
2) it's just a major pain to work with errno (in general, but in Rust
specifically).
However, now that we have simplified the function down to a single call
into the standard library we may as well do it right and bubble up
errors appropriately, because, even if not used most of the time, it can
still help tremendously with debugging. This change does so by
manifesting an anyhow error, as we do in most parts of the crate.
  • Loading branch information
d-e-s-o committed Jun 17, 2022
1 parent 4787248 commit b015986
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/tty/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use std::io;
use std::os::unix::io::AsRawFd as _;
use std::path;

use anyhow::Context as _;

/// Retrieve a path to the TTY used for stdin, if any.
///
/// This function works on a best effort basis and skips any advanced
/// error reporting, knowing that callers do not care.
pub(crate) fn retrieve_tty() -> Result<path::PathBuf, ()> {
pub(crate) fn retrieve_tty() -> anyhow::Result<path::PathBuf> {
let fd = io::stdin().as_raw_fd();
let fd_path = format!("/proc/self/fd/{}", fd);
fs::read_link(fd_path).map_err(|_| ())
fs::read_link(&fd_path).with_context(|| format!("Failed to read symbolic link {}", fd_path))
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/tty/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

use std::path;

pub(crate) fn retrieve_tty() -> Result<path::PathBuf, ()> {
pub(crate) fn retrieve_tty() -> anyhow::Result<path::PathBuf> {
Err(())
}

0 comments on commit b015986

Please sign in to comment.