mqrs/src/posix/list.rs
finga 1aab989000 Update build dependencies and edition
Use the 2021 Rust edition. To use current version of clap changes were
adapted.
2021-12-03 14:50:23 +01:00

59 lines
1.7 KiB
Rust

use anyhow::{anyhow, Result};
use chrono::{DateTime, Local};
use clap::Parser;
use log::warn;
use std::{fs, os::unix::fs::PermissionsExt};
/// Print a list of existing message queues
#[derive(Debug, Parser)]
pub struct List {
/// Show all parameters
#[clap(short, long)]
all: bool,
}
impl List {
pub fn run(&self) -> Result<()> {
match self.all {
false => println!("Name"),
true => println!(
"{0: <10} {1: <10} {2: <12} {3: <26} {4: <26}",
"Name", "Size", "Permissions", "Modified", "Accessed",
),
}
for mq in fs::read_dir("/dev/mqueue")? {
match mq {
Ok(mq) => {
print!(
"/{0: <10}",
mq.file_name().into_string().map_err(|e| anyhow!(
"Could not convert queue name to string: {:?}",
e
))?
);
if self.all {
let metadata = mq.metadata()?;
let modified: DateTime<Local> = metadata.modified()?.into();
let accessed: DateTime<Local> = metadata.accessed()?.into();
print!(
"{0: <10} {1: <12o} {2: <26} {3: <26}",
metadata.len(),
metadata.permissions().mode(),
modified,
accessed,
);
}
println!();
}
Err(e) => warn!("Could not read file: {:?}", e),
}
}
Ok(())
}
}