use anyhow::Result; use clap::Parser; use log::{info, log_enabled, Level::Info}; use posixmq::PosixMq; use std::fs; /// Create a POSIX message queue #[derive(Debug, Parser)] pub struct Create { /// Permissions (octal) to create the queue with #[clap(short, long)] mode: Option, /// Maximum number of messages in the queue #[clap(short, long)] capacity: Option, /// Message size in bytes #[clap(short = 's', long)] msgsize: Option, /// 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::().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::().expect("can never fail"), _ => 10, } } impl Create { pub fn run(&self) -> 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 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(()) } }