From 4020f062efbd5943e3b4c61e9d56ad51bfb1623e Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 7 Jul 2021 18:27:41 +0200 Subject: [PATCH] Add new subcommand `list` Also add it to the readme and manpage. --- README.md | 13 +++++++---- mqrs.1 | 21 +++++++++++++++-- src/main.rs | 2 ++ src/posix.rs | 2 ++ src/posix/list.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/posix/list.rs diff --git a/README.md b/README.md index ebc9031..db90769 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,10 @@ from `target/{debug,release}/mqrs` depending on which one you built. ## Using `mqrs` -`mqrs` supports five commands: `create`, `info`, `unlink`, `send` and -`recv`. All commands do not have to be specified fully. If the -command is clearly distinguishable from all the others, it does not -have to be completed further. +`mqrs` supports five commands: `create`, `info`, `list`, `unlink`, +`send` and `recv`. All commands do not have to be specified fully. If +the command is clearly distinguishable from all the others, it does +not have to be completed further. ### Create a message queue Use the `create` command to create a new POSIX message queue. Following @@ -62,6 +62,11 @@ optional arguments are supported: Use the `info` command to print further information about a message queue. +### List all message queues +Use the `list` command to print a list of all message +queues. Following option argument is supported: +- `-a`, `--all`: Print all available information + ### Delete a message queue Use the `unlink` command to delete a message queue. diff --git a/mqrs.1 b/mqrs.1 index 50fa2d0..a7dceca 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -11,6 +11,8 @@ is a small cli program to handle POSIX message queues. Supported commands are , .B info\ , +.B list\ +, .B unlink\ , .B send @@ -18,8 +20,8 @@ and .B recv . All commands do not have to be specified fully. If the command is -clearly distinguishable from all the others, it does not have to be -completed further. +clearly distinguishable from all the others, it is not needed to +complete it further. .SH OPTIONS .TP 8 .B \-h, \-\-help @@ -90,6 +92,21 @@ Prints help information .B \-v, \-\-verbose Produce verbose output .RE +.SS list [FLAGS] +Print a list of all existing POSIX message queues. +.TP 8 +.SS FLAGS +.RS +.TP 8 +.B \-h, \-\-help +Prints help information +.TP 8 +.B \-v, \-\-verbose +Produce verbose output +.TP 8 +.B \-a, \-\-all +Print all available information +.RE .SS recv [FLAGS] [OPTIONS] \fI\fP Receive and print one or more messages message from a message queue. .TP 8 diff --git a/src/main.rs b/src/main.rs index 40296fb..bf3fb61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod posix; enum Command { Create(posix::Create), Info(posix::Info), + List(posix::List), Unlink(posix::Unlink), Send(posix::Send), Recv(posix::Recv), @@ -47,6 +48,7 @@ fn main() -> Result<()> { Command::Unlink(u) => u.run()?, Command::Send(s) => s.run()?, Command::Recv(r) => r.run()?, + Command::List(l) => l.run()?, } Ok(()) diff --git a/src/posix.rs b/src/posix.rs index d9e6266..c5855c8 100644 --- a/src/posix.rs +++ b/src/posix.rs @@ -1,11 +1,13 @@ mod create; mod info; +mod list; mod recv; mod send; mod unlink; pub use create::Create; pub use info::Info; +pub use list::List; pub use recv::Recv; pub use send::Send; pub use unlink::Unlink; diff --git a/src/posix/list.rs b/src/posix/list.rs new file mode 100644 index 0000000..54c031a --- /dev/null +++ b/src/posix/list.rs @@ -0,0 +1,58 @@ +use anyhow::{anyhow, Result}; +use chrono::{DateTime, Local}; +use clap::Clap; +use log::warn; +use std::{fs, os::unix::fs::PermissionsExt}; + +/// Print information about an existing message queue +#[derive(Clap, Debug)] +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 = metadata.modified()?.into(); + let accessed: DateTime = 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(()) + } +}