dmesg: add the field record separator to the the struct dmesg

Add the field kmsg_record_separator to the struct Dmesg so it can be
changed from \0 to \n when dmesg reads from a file.
This commit is contained in:
Goffredo Baroncelli 2025-01-02 17:03:11 +01:00
parent f94d7c1d89
commit f878075a74

@ -30,6 +30,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?; let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
if let Some(kmsg_file) = matches.get_one::<String>(options::KMSG_FILE) { if let Some(kmsg_file) = matches.get_one::<String>(options::KMSG_FILE) {
dmesg.kmsg_file = kmsg_file; dmesg.kmsg_file = kmsg_file;
dmesg.kmsg_record_separator = 0;
} }
if matches.get_flag(options::JSON) { if matches.get_flag(options::JSON) {
dmesg.output_format = OutputFormat::Json; dmesg.output_format = OutputFormat::Json;
@ -184,6 +185,7 @@ mod options {
struct Dmesg<'a> { struct Dmesg<'a> {
kmsg_file: &'a str, kmsg_file: &'a str,
kmsg_record_separator: u8,
output_format: OutputFormat, output_format: OutputFormat,
time_format: TimeFormat, time_format: TimeFormat,
facility_filters: Option<HashSet<Facility>>, facility_filters: Option<HashSet<Facility>>,
@ -196,6 +198,7 @@ impl Dmesg<'_> {
fn new() -> Self { fn new() -> Self {
Dmesg { Dmesg {
kmsg_file: "/dev/kmsg", kmsg_file: "/dev/kmsg",
kmsg_record_separator: 10, // '\n'
output_format: OutputFormat::Normal, output_format: OutputFormat::Normal,
time_format: TimeFormat::Raw, time_format: TimeFormat::Raw,
facility_filters: None, facility_filters: None,
@ -259,7 +262,10 @@ impl Dmesg<'_> {
let file = File::open(self.kmsg_file) let file = File::open(self.kmsg_file)
.map_err_context(|| format!("cannot open {}", self.kmsg_file))?; .map_err_context(|| format!("cannot open {}", self.kmsg_file))?;
let file_reader = BufReader::new(file); let file_reader = BufReader::new(file);
Ok(RecordIterator { file_reader }) Ok(RecordIterator {
file_reader,
kmsg_record_separator: self.kmsg_record_separator
})
} }
fn is_record_in_set<T>( fn is_record_in_set<T>(
@ -359,6 +365,7 @@ enum Level {
struct RecordIterator { struct RecordIterator {
file_reader: BufReader<File>, file_reader: BufReader<File>,
kmsg_record_separator: u8,
} }
impl Iterator for RecordIterator { impl Iterator for RecordIterator {
@ -379,7 +386,7 @@ impl Iterator for RecordIterator {
impl RecordIterator { impl RecordIterator {
fn read_record_line(&mut self) -> UResult<Option<String>> { fn read_record_line(&mut self) -> UResult<Option<String>> {
let mut buf = vec![]; let mut buf = vec![];
let num_bytes = self.file_reader.read_until(0, &mut buf)?; let num_bytes = self.file_reader.read_until(self.kmsg_record_separator, &mut buf)?;
match num_bytes { match num_bytes {
0 => Ok(None), 0 => Ok(None),
_ => Ok(Some(String::from_utf8_lossy(&buf).to_string())), _ => Ok(Some(String::from_utf8_lossy(&buf).to_string())),