Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/who/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ doctest = false
[dependencies]
clap = { workspace = true }
rustix = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["utmpx"] }
fluent = { workspace = true }

Expand Down
1 change: 1 addition & 0 deletions src/uu/who/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ who-heading-exit = EXIT

# Error messages
who-canonicalize-error = failed to canonicalize { $host }
who-error-write = write error: { $error }

# Platform-specific messages
who-unsupported-openbsd = unsupported command on OpenBSD
1 change: 1 addition & 0 deletions src/uu/who/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ who-heading-exit = SORTIE

# Error messages
who-canonicalize-error = échec de canonicalisation de { $host }
who-error-write = erreur d'écriture : { $error }

# Platform-specific messages
who-unsupported-openbsd = commande non prise en charge sur OpenBSD
25 changes: 21 additions & 4 deletions src/uu/who/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use crate::options;
use crate::uu_app;

use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::error::{FromIo, UError, UResult, strip_errno};
use uucore::libc::S_IWGRP;
use uucore::translate;

Expand All @@ -25,6 +26,14 @@ fn get_long_usage() -> String {
translate!("who-long-usage", "default_file" => utmpx::DEFAULT_FILE)
}

#[derive(Debug, Error)]
enum WhoError {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are more variants expected? Otherwise a struct seems more appropriate imo..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally define a UtilError enum for each utility. In the case of who, I do expect additional variants in the future, for example, a who-canonicalize-error variant.

#[error("{}", translate!("who-error-write", "error" => strip_errno(.0)))]
Write(std::io::Error),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could derive from?

}

impl UError for WhoError {}

pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches =
uucore::clap_localization::handle_clap_result(uu_app().after_help(get_long_usage()), args)?;
Expand Down Expand Up @@ -209,8 +218,16 @@ impl Who {
.filter(UtmpxRecord::is_user_process)
.map(|ut| ut.user())
.collect::<Vec<_>>();
println!("{}", users.join(" "));
println!("{}", translate!("who-user-count", "count" => users.len()));
let mut out = stdout().lock();
writeln!(out, "{}", users.join(" "))
.and_then(|()| {
writeln!(
out,
"{}",
translate!("who-user-count", "count" => users.len())
)
})
.map_err(WhoError::Write)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the map_err can be skipped

} else {
let records = utmpx::Utmpx::iter_all_records_from(f);

Expand Down Expand Up @@ -450,7 +467,7 @@ impl Who {
if self.include_exit {
write!(buf, " {exit:<12}").unwrap();
}
writeln!(stdout(), "{}", buf.trim_end())?;
writeln!(stdout(), "{}", buf.trim_end()).map_err(WhoError::Write)?;
Ok(())
}

Expand Down
21 changes: 19 additions & 2 deletions tests/by-util/test_who.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,23 @@ fn test_piped_to_dev_full() {
ts.ucmd()
.arg("--heading")
.set_stdout(dev_full)
.fails()
.stderr_is("who: No space left on device\n");
.fails_with_code(1)
.stderr_is("who: write error: No space left on device\n");
}

#[cfg(target_os = "linux")]
#[test]
fn test_count_piped_to_dev_full() {
let ts = TestScenario::new(util_name!());

let dev_full = std::fs::OpenOptions::new()
.write(true)
.open("/dev/full")
.unwrap();

ts.ucmd()
.arg("-q")
.set_stdout(dev_full)
.fails_with_code(1)
.stderr_is("who: write error: No space left on device\n");
}
Loading