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
1 change: 1 addition & 0 deletions src/uu/chcon/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ chcon-error-no-files-specified = No files are specified
chcon-error-data-out-of-range = Data is out of range
chcon-error-operation-failed = { $operation } failed
chcon-error-operation-failed-on = { $operation } failed on { $operand }
chcon-error-write = write error: { $error }

# Error messages - argument validation
chcon-error-invalid-context = Invalid security context '{ $context }'.
Expand Down
15 changes: 12 additions & 3 deletions src/uu/chcon/src/chcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use core::ffi::CStr;
use std::borrow::Cow;
use std::collections::HashSet;
use std::ffi::{CString, OsStr, OsString};
use std::io::Write;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd};
use std::path::{Path, PathBuf};
use std::{fs, io};
Expand Down Expand Up @@ -519,6 +520,7 @@ fn relabel_all(
// Directories reached on the way down, so a symlink loop is noticed
// instead of followed forever.
open_ancestors: HashSet::new(),
write_error: None,
errors,
};

Expand All @@ -527,6 +529,10 @@ fn relabel_all(
walk.visit(&start, operand.as_os_str(), operand, behavior, true);
}

if let Some(err) = walk.write_error {
walk.errors.push(Error::Write(err));
}

walk.errors
}

Expand All @@ -537,6 +543,7 @@ struct Walk<'a> {
context: &'a SELinuxSecurityContext<'a>,
root_id: Option<DeviceAndINode>,
open_ancestors: HashSet<DeviceAndINode>,
write_error: Option<io::Error>,
errors: Vec<Error>,
}

Expand Down Expand Up @@ -645,11 +652,13 @@ impl Walk<'_> {
}

fn relabel(&mut self, parent: &DirFd, name: &OsStr, display: &Path) {
if self.options.verbose {
println!(
if self.options.verbose && self.write_error.is_none() {
self.write_error = writeln!(
io::stdout(),
"{}",
translate!("chcon-verbose-changing-context", "util_name" => "chcon", "file" => display.quote())
);
)
.err();
}

if let Err(err) = apply_context(
Expand Down
4 changes: 4 additions & 0 deletions src/uu/chcon/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::fmt::Write;
use std::io;

use uucore::display::Quotable;
use uucore::error::strip_errno;
use uucore::translate;

pub(crate) type Result<T> = std::result::Result<T, Error>;
Expand All @@ -31,6 +32,9 @@ pub(crate) enum Error {
#[error(transparent)]
CommandLine(#[from] clap::Error),

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

#[error("{}", translate!("chcon-error-operation-failed", "operation" => operation))]
SELinux {
operation: String,
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_chcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ fn recursive_errors() {
}
}

#[test]
#[cfg(target_os = "linux")]
fn verbose_write_error() {
let (at, mut cmd) = at_and_ucmd!();
at.touch("a");
at.touch("b");
let dev_full = std::fs::OpenOptions::new()
.write(true)
.open("/dev/full")
.unwrap();

let result = cmd
.args(&["--verbose", "--type=etc_t", "a", "b", "missing"])
.set_stdout(dev_full)
.fails_with_code(1);
result
.stderr_contains("write error: No space left on device")
.stderr_contains("Accessing failed on 'missing'");

assert_eq!(result.stderr_str().matches("write error:").count(), 1);
}

#[test]
fn valid_context() {
let (dir, mut cmd) = at_and_ucmd!();
Expand Down
Loading