Remove pub
declarations and move to posix::*
This is in order to prepare also for SysV message queues.
This commit is contained in:
parent
606b4de524
commit
c2ad74ff15
7 changed files with 24 additions and 18 deletions
63
src/posix/create.rs
Normal file
63
src/posix/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, 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, verbose: bool) -> 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 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(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue