Create a message queue
The create command creates a new message queue with support of the typical parameters `mode`, `capacity` and `msgsize`. This command also supports the global argument `verbose`.
This commit is contained in:
parent
2b96bcc562
commit
fdacba1eec
5 changed files with 101 additions and 3 deletions
|
@ -1,3 +1,4 @@
|
|||
use crate::create::Create;
|
||||
use clap::{crate_authors, crate_version, AppSettings, Clap};
|
||||
|
||||
#[derive(Clap, Debug)]
|
||||
|
@ -12,4 +13,11 @@ pub struct Opts {
|
|||
/// Produce verbose output
|
||||
#[clap(short, long, global = true)]
|
||||
pub verbose: bool,
|
||||
#[clap(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Clap, Debug)]
|
||||
pub enum Command {
|
||||
Create(Create),
|
||||
}
|
||||
|
|
63
src/create.rs
Normal file
63
src/create.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use anyhow::Result;
|
||||
use clap::Clap;
|
||||
use posixmq::PosixMq;
|
||||
use std::fs;
|
||||
|
||||
/// Create a POSIX message queue
|
||||
#[derive(Clap, Debug)]
|
||||
pub struct Create {
|
||||
/// Permissions (octal) to create the queue with
|
||||
#[clap(short = 'p', long)]
|
||||
mode: Option<String>,
|
||||
/// Maximum number of messages in the queue
|
||||
#[clap(short, long)]
|
||||
capacity: Option<usize>,
|
||||
/// Message size in bytes
|
||||
#[clap(short = 's', long)]
|
||||
msgsize: Option<usize>,
|
||||
/// Name of the queue
|
||||
#[clap(value_name = "QNAME")]
|
||||
queue: String,
|
||||
}
|
||||
|
||||
fn msgsize_default() -> usize {
|
||||
match fs::read_to_string("/proc/sys/fs/mqueue/msgsize_default") {
|
||||
Ok(m) => m.trim().parse::<usize>().unwrap(), // should never fail
|
||||
_ => 8192,
|
||||
}
|
||||
}
|
||||
|
||||
fn msg_default() -> usize {
|
||||
match fs::read_to_string("/proc/sys/fs/mqueue/msg_default") {
|
||||
Ok(m) => m.trim().parse::<usize>().unwrap(), // should never fail
|
||||
_ => 10,
|
||||
}
|
||||
}
|
||||
|
||||
impl Create {
|
||||
pub fn run(&self, verbose: bool) -> Result<()> {
|
||||
let mq = &mut posixmq::OpenOptions::readonly();
|
||||
|
||||
if let Some(m) = &self.mode {
|
||||
mq.mode(u32::from_str_radix(&m, 8)?);
|
||||
}
|
||||
|
||||
mq.max_msg_len(self.msgsize.unwrap_or_else(msgsize_default))
|
||||
.capacity(self.capacity.unwrap_or_else(msg_default))
|
||||
.create_new()
|
||||
.open(&self.queue)?;
|
||||
|
||||
if verbose {
|
||||
let mq = PosixMq::open(&self.queue)?;
|
||||
let attributes = mq.attributes()?;
|
||||
|
||||
println!("Created message queue: {} with attributes msgsize: {}, capacity: {}, current_messages: {}",
|
||||
&self.queue,
|
||||
&attributes.max_msg_len,
|
||||
&attributes.capacity,
|
||||
&attributes.current_messages);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
14
src/main.rs
14
src/main.rs
|
@ -1,9 +1,17 @@
|
|||
use anyhow::Result;
|
||||
use clap::Clap;
|
||||
|
||||
mod cli;
|
||||
mod create;
|
||||
|
||||
use cli::Opts;
|
||||
use cli::{Command, Opts};
|
||||
|
||||
fn main() {
|
||||
Opts::parse();
|
||||
fn main() -> Result<()> {
|
||||
let opts: Opts = Opts::parse();
|
||||
|
||||
match opts.command {
|
||||
Command::Create(c) => c.run(opts.verbose)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue