From 4020f062efbd5943e3b4c61e9d56ad51bfb1623e Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 7 Jul 2021 18:27:41 +0200 Subject: [PATCH 1/2] 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(()) + } +} From 9aae14feae41de7df997d7e88e0f8370ad2e0cbe Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 7 Jul 2021 19:19:19 +0200 Subject: [PATCH 2/2] Refactor commands to support different backends Therefore also the `sysv` backend is intoduced to prepare for future support. The man page and the readme are updated so far. --- README.md | 30 ++++++++++++++++++++---------- mqrs.1 | 43 ++++++++++++++++++++++++++----------------- src/main.rs | 35 ++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index db90769..d274168 100644 --- a/README.md +++ b/README.md @@ -46,31 +46,38 @@ from `target/{debug,release}/mqrs` depending on which one you built. ## Using `mqrs` -`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. +Depending on which backend you want to use there are different subsets +of subcommands. Following backends are supported: +- `posix`: Uses POSIX message queues +- `sysv`: Uses SysV IPC message queues -### Create a message queue +If a command is clearly distinguishable from all the others, +it does not have to be completed further. + +### POSIX message queues +The POSIX backend supports six commands: `create`, `info`, `list`, +`unlink`, `send` and `recv`. + +#### Create a message queue Use the `create` command to create a new POSIX message queue. Following optional arguments are supported: - `-c`, `--capacity`: Maximum number of messages in the queue - `-p`, `--permissions`: Permissions (octal) to create the queue with - `-s`, `--msgsize`: Message size in bytes -### Print information about a message queue +#### Print information about a message queue Use the `info` command to print further information about a message queue. -### List all message queues +#### 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 +#### Delete a message queue Use the `unlink` command to delete a message queue. -### Send a message to a queue +#### Send a message to a queue Use the `send` command to send a message to a message queue. Following optional arguments are supported: - `-n`, `--non-blocking`: Do not block @@ -80,7 +87,7 @@ optional arguments are supported: default, priority >= 0 [default: 0] - `-o`, `--timeout `: As for example in "5h 23min 42ms" -### Receive a message from a queue +#### Receive a message from a queue Use the `recv` command to receive one or more messages from a message queue. Following optional arguments are supported: - `-f`, `--follow`: Print messages as they are received @@ -89,3 +96,6 @@ queue. Following optional arguments are supported: - `-d`, `--deadline `: Deadline until messages are received (format: `%Y-%m-%d %H:%M:%S`) - `-o,` `--timeout `: As for example in "5h 23min 42ms" + +### SysV IPC message queues +The SysV IPC backend supports no commands yet. diff --git a/mqrs.1 b/mqrs.1 index a7dceca..9934081 100644 --- a/mqrs.1 +++ b/mqrs.1 @@ -1,12 +1,34 @@ .\" Manpage for mqrs -.TH man 1 "27 June 2021" "0.1.1" "mqrs man page" +.TH man 1 "7 July 2021" "0.1.1" "mqrs man page" .SH NAME mqrs \- Handle POSIX message queues .SH SYNOPSIS .B mqrs [\-h] [\-\-help] [\-v] [\-\-verbose] [\-V] [\-\-version] [SUBCOMMAND] .SH DESCRIPTION .B mqrs -is a small cli program to handle POSIX message queues. Supported commands are +is a small cli program to handle message queues. Depending on which +backend you want to use there are different subsets of +subcommands. Following backends are supported: +.B posix\ +and +.B sysv\ +. + +If a command is clearly distinguishable from all the others, it does +not have to be completed further. +.SH OPTIONS +.TP 8 +.B \-h, \-\-help +Prints help information +.TP 8 +.B \-v, \-\-verbose +Produce verbose output, multiple -v options increase the verbosity +(max. 3) +.TP 8 +.B \-V, \-\-version +Prints version information +.SH POSIX MESSAGE QUEUE SUBCOMMANDS +The POSIX backend supports six commands: .B create\ , .B info\ @@ -19,21 +41,6 @@ is a small cli program to handle POSIX message queues. Supported commands are and .B recv . -All commands do not have to be specified fully. If the command is -clearly distinguishable from all the others, it is not needed to -complete it further. -.SH OPTIONS -.TP 8 -.B \-h, \-\-help -Prints help information -.TP 8 -.B \-v, \-\-verbose -Produce verbose output, multiple -v options increase the verbosity -(max. 3) -.TP 8 -.B \-V, \-\-version -Prints version information -.SH SUBCOMMANDS .SS create [FLAGS] [OPTIONS] \fI\fP Create a new POSIX message queue. .TP 8 @@ -202,6 +209,8 @@ Prints help information .B \-v, \-\-verbose Produce verbose output .RE +.SH SYSV IPC MESSAGE QUEUE SUBCOMMANDS +The SysV IPC backend supports no commands yet. .SH SEE ALSO mq_overview(7) .SH BUGS diff --git a/src/main.rs b/src/main.rs index bf3fb61..cc1d185 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,15 @@ use clap::{crate_authors, crate_version, AppSettings, Clap}; mod posix; #[derive(Clap, Debug)] -enum Command { +enum Backend { + /// Handle POSIX message queues + Posix(PosixCommand), + /// Handle SysV message queues + Sysv(SysvCommand), +} + +#[derive(Clap, Debug)] +enum PosixCommand { Create(posix::Create), Info(posix::Info), List(posix::List), @@ -13,6 +21,10 @@ enum Command { Recv(posix::Recv), } +#[derive(Clap, Debug)] +enum SysvCommand { +} + #[derive(Clap, Debug)] #[clap( version = crate_version!(), @@ -25,8 +37,9 @@ struct Opts { /// Produce verbose output, multiple -v options increase the verbosity (max. 3) #[clap(short, long, global = true, parse(from_occurrences))] verbose: u32, + /// Backend to be used #[clap(subcommand)] - command: Command, + backend: Backend, } fn main() -> Result<()> { @@ -42,13 +55,17 @@ fn main() -> Result<()> { )) .init(); - match opts.command { - Command::Create(c) => c.run()?, - Command::Info(i) => i.run()?, - Command::Unlink(u) => u.run()?, - Command::Send(s) => s.run()?, - Command::Recv(r) => r.run()?, - Command::List(l) => l.run()?, + match opts.backend { + Backend::Posix(p) => match p { + PosixCommand::Create(c) => c.run()?, + PosixCommand::Info(i) => i.run()?, + PosixCommand::List(l) => l.run()?, + PosixCommand::Unlink(u) => u.run()?, + PosixCommand::Send(s) => s.run()?, + PosixCommand::Recv(r) => r.run()?, + }, + Backend::Sysv(s) => match s { + }, } Ok(())