Implement info command for SysV IPC mqs

The `list` command was refactored a little bit as well and the man
page and readme were updated.
This commit is contained in:
finga 2021-07-08 17:44:46 +02:00
parent 4c82d41f8e
commit 9666e0788d
7 changed files with 113 additions and 16 deletions

View file

@ -25,6 +25,7 @@ enum PosixCommand {
#[derive(Clap, Debug)]
enum SysvCommand {
Create(sysv::Create),
Info(sysv::Info),
List(sysv::List),
Unlink(sysv::Unlink),
}
@ -70,6 +71,7 @@ fn main() -> Result<()> {
},
Backend::Sysv(s) => match s {
SysvCommand::Create(c) => c.run()?,
SysvCommand::Info(i) => i.run()?,
SysvCommand::List(l) => l.run()?,
SysvCommand::Unlink(u) => u.run()?,
},

View file

@ -1,7 +1,9 @@
mod create;
mod info;
mod list;
mod unlink;
pub use create::Create;
pub use info::Info;
pub use list::List;
pub use unlink::Unlink;

53
src/sysv/info.rs Normal file
View file

@ -0,0 +1,53 @@
use anyhow::Result;
use clap::Clap;
use std::{
fs::File,
io::{BufRead, BufReader},
};
/// Print information about an existing message queue
#[derive(Clap, Debug)]
pub struct Info {
/// Id of the queue
#[clap(short, long, required_unless_present_any = &["key"], conflicts_with = "key")]
id: Option<i32>,
/// Key of the queue
#[clap(short, long, required_unless_present_any = &["id"], conflicts_with = "id")]
key: Option<i32>,
}
fn print_line(line: &str) {
for field in line.split_whitespace().collect::<Vec<&str>>() {
print!("{0: <10}", field);
}
println!();
}
impl Info {
pub fn run(&self) -> Result<()> {
let mut lines = BufReader::new(File::open("/proc/sysvipc/msg")?).lines();
print_line(&lines.nth(0).unwrap_or(Ok(String::new()))?);
for line in lines {
let line = line?;
if let Some(id) = self.id {
if id == line.split_whitespace().collect::<Vec<&str>>()[1].parse::<i32>()? {
print_line(&line);
break;
}
} else if let Some(key) = self.key {
if key == line.split_whitespace().collect::<Vec<&str>>()[0].parse::<i32>()? {
print_line(&line);
break;
}
}
}
Ok(())
}
}

View file

@ -11,9 +11,7 @@ pub struct List {}
impl List {
pub fn run(&self) -> Result<()> {
let file = BufReader::new(File::open("/proc/sysvipc/msg")?);
for line in file.lines() {
for line in BufReader::new(File::open("/proc/sysvipc/msg")?).lines() {
for field in line?.split_whitespace().collect::<Vec<&str>>() {
print!("{0: <10}", field);
}

View file

@ -7,14 +7,14 @@ use log::info;
pub struct Unlink {
/// Id of the queue
#[clap(
long,
short,
long,
required_unless_present_any = &["key"],
conflicts_with = "key"
)]
id: Option<i32>,
/// Key of the queue
#[clap(long, short, required_unless_present_any = &["id"], conflicts_with = "id")]
#[clap(short, long, required_unless_present_any = &["id"], conflicts_with = "id")]
key: Option<i32>,
}