finga
12b117db7e
To supporte more than a single log level `log` and `env_logger` crates are used. For the application to support the different type of argument, the verbose argument definition was adapted.
65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
use anyhow::Result;
|
|
use clap::Clap;
|
|
use log::{info, log_enabled, Level::Info};
|
|
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, long)]
|
|
permissions: 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 new queue
|
|
#[clap(value_name = "QUEUE")]
|
|
queue: String,
|
|
}
|
|
|
|
fn msgsize_default() -> usize {
|
|
match fs::read_to_string("/proc/sys/fs/mqueue/msgsize_default") {
|
|
Ok(m) => m.trim().parse::<usize>().expect("can never fail"),
|
|
_ => 8192,
|
|
}
|
|
}
|
|
|
|
fn msg_default() -> usize {
|
|
match fs::read_to_string("/proc/sys/fs/mqueue/msg_default") {
|
|
Ok(m) => m.trim().parse::<usize>().expect("can never fail"),
|
|
_ => 10,
|
|
}
|
|
}
|
|
|
|
impl Create {
|
|
pub fn run(&self) -> Result<()> {
|
|
let mq = &mut posixmq::OpenOptions::readonly();
|
|
|
|
if let Some(m) = &self.permissions {
|
|
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 log_enabled!(Info) {
|
|
let mq = PosixMq::open(&self.queue)?;
|
|
let attributes = mq.attributes()?;
|
|
|
|
info!("Created message queue: {} with attributes msgsize: {}, capacity: {}, current_messages: {}",
|
|
&self.queue,
|
|
&attributes.max_msg_len,
|
|
&attributes.capacity,
|
|
&attributes.current_messages);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|