clippy: Fix several clippy issues

In order to enable several clippy lint groups fix several findings.
This commit is contained in:
finga 2023-10-14 12:29:14 +02:00
parent 21bc394c58
commit d6b82f0e72
10 changed files with 97 additions and 105 deletions

View file

@ -10,5 +10,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Changed ### Changed
- Fix several clippy findings in preperation to enable several lint
groups.
- Update to the latest version of **clap**. - Update to the latest version of **clap**.
- Update all dependencies that should not break SemVer. - Update all dependencies that should not break SemVer.

View file

@ -22,17 +22,13 @@ pub struct Create {
} }
fn msgsize_default() -> usize { fn msgsize_default() -> usize {
match fs::read_to_string("/proc/sys/fs/mqueue/msgsize_default") { fs::read_to_string("/proc/sys/fs/mqueue/msgsize_default")
Ok(m) => m.trim().parse::<usize>().expect("can never fail"), .map_or(8192, |m| m.trim().parse::<usize>().expect("can never fail"))
_ => 8192,
}
} }
fn msg_default() -> usize { fn msg_default() -> usize {
match fs::read_to_string("/proc/sys/fs/mqueue/msg_default") { fs::read_to_string("/proc/sys/fs/mqueue/msg_default")
Ok(m) => m.trim().parse::<usize>().expect("can never fail"), .map_or(10, |m| m.trim().parse::<usize>().expect("can never fail"))
_ => 10,
}
} }
impl Create { impl Create {

View file

@ -14,12 +14,13 @@ pub struct List {
impl List { impl List {
pub fn run(&self) -> Result<()> { pub fn run(&self) -> Result<()> {
match self.all { if self.all {
false => println!("Name"), println!(
true => println!(
"{0: <10} {1: <10} {2: <12} {3: <26} {4: <26}", "{0: <10} {1: <10} {2: <12} {3: <26} {4: <26}",
"Name", "Size", "Permissions", "Modified", "Accessed", "Name", "Size", "Permissions", "Modified", "Accessed",
), );
} else {
println!("Name");
} }
for mq in fs::read_dir("/dev/mqueue")? { for mq in fs::read_dir("/dev/mqueue")? {

View file

@ -36,7 +36,7 @@ fn print_message(priority: u32, length: usize, timestamp: bool, msg: &str) {
println!("{}", Local::now()); println!("{}", Local::now());
} }
println!("{}", msg); println!("{msg}");
} }
impl Recv { impl Recv {

View file

@ -3,7 +3,8 @@ use clap::Parser;
use log::info; use log::info;
use sysvmq::SysvMq; use sysvmq::SysvMq;
/// Create a SysV message queue #[allow(clippy::doc_markdown)]
/// Create a SysV IPC message queue
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct Create { pub struct Create {
/// Permissions (octal) to create the queue with (default: 0644) /// Permissions (octal) to create the queue with (default: 0644)

View file

@ -17,8 +17,8 @@ pub struct Info {
} }
fn print_line(line: &str) { fn print_line(line: &str) {
for field in line.split_whitespace().collect::<Vec<&str>>() { for field in line.split_whitespace() {
print!("{0: <10}", field); print!("{field: <10}");
} }
println!(); println!();

View file

@ -10,10 +10,11 @@ use std::{
pub struct List {} pub struct List {}
impl List { impl List {
#[allow(clippy::unused_self)]
pub fn run(&self) -> Result<()> { pub fn run(&self) -> Result<()> {
for line in BufReader::new(File::open("/proc/sysvipc/msg")?).lines() { for line in BufReader::new(File::open("/proc/sysvipc/msg")?).lines() {
for field in line?.split_whitespace().collect::<Vec<&str>>() { for field in line?.split_whitespace() {
print!("{0: <10}", field); print!("{field: <10}");
} }
println!(); println!();

14
sysvmq/CHANGELOG.md Normal file
View file

@ -0,0 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a
Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Changed
- Fix several clippy findings in preperation to enable several lint
groups.

View file

@ -5,6 +5,9 @@ edition = "2018"
authors = ["finga <mqrs@onders.org>"] authors = ["finga <mqrs@onders.org>"]
repository = "https://git.onders.org/finga/mqrs" repository = "https://git.onders.org/finga/mqrs"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
description = "A simple API for SysV IPC message queues."
keywords = ["message_queue", "mq", "mqueue", "queue", "sysv", "ipc"]
categories = ["os"]
[dependencies] [dependencies]
libc = "0.2.98" libc = "0.2.98"

View file

@ -1,6 +1,5 @@
use libc::{ use libc::{
msgctl, msgget, msginfo, msqid_ds, IPC_CREAT, IPC_EXCL, IPC_INFO, IPC_NOWAIT, IPC_PRIVATE, msgctl, msgget, msqid_ds, IPC_CREAT, IPC_INFO, IPC_NOWAIT, IPC_RMID, MSG_INFO,g MSG_STAT,
IPC_RMID, IPC_SET, IPC_STAT, MSG_COPY, MSG_EXCEPT, MSG_INFO, MSG_NOERROR, MSG_STAT,
}; };
use nix::errno::{errno, Errno}; use nix::errno::{errno, Errno};
use std::{marker::PhantomData, mem::MaybeUninit, ptr}; use std::{marker::PhantomData, mem::MaybeUninit, ptr};
@ -12,50 +11,15 @@ pub enum SysvMqError {
ErrnoError(&'static str), ErrnoError(&'static str),
} }
/// IPC bit flags #[allow(clippy::doc_markdown)]
#[repr(i32)] /// Unlink (delete) an existing SysV IPC message queue.
pub enum Flags { ///
/// Create key if key does not exist. /// # Errors
CreateKey = IPC_CREAT, ///
/// Fail if key exists. /// Return an `SysvMqError` when no queue with the given key can be
Exclusive = IPC_EXCL, /// found.
/// Return error on wait.
NoWait = IPC_NOWAIT,
/// No error if message is too big.
NoError = MSG_NOERROR,
/// Receive any message except of specified type.
Except = MSG_EXCEPT,
/// Copy (not remove) all queue messages.
Copy = MSG_COPY,
/// Private key (Special key value).
Private = IPC_PRIVATE,
}
/// Commands for `msgctl()`
#[repr(i32)]
pub enum ControlCommands {
/// Remove identifier (Control command for `msgctl`, `semctl`, and `shmctl`).
Remove = IPC_RMID,
/// Set `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`).
SetPerm = IPC_SET,
/// Get `ipc_perm` options (Control command for `msgctl`, `semctl`, and `shmctl`).
GetPerm = IPC_STAT,
/// See ipcs (Control command for `msgctl`, `semctl`, and `shmctl`).
IpcInfo = IPC_INFO,
/// IPCS control command.
Stat = MSG_STAT,
/// IPCS control command.
MsgInfo = MSG_INFO,
}
pub fn unlink_id(id: i32) -> Result<(), SysvMqError> { pub fn unlink_id(id: i32) -> Result<(), SysvMqError> {
let res = unsafe { let res = unsafe { msgctl(id, IPC_RMID, ptr::null::<msqid_ds>().cast_mut()) };
msgctl(
id,
ControlCommands::Remove as i32,
ptr::null::<msqid_ds>() as *mut msqid_ds,
)
};
match res { match res {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
@ -63,6 +27,14 @@ pub fn unlink_id(id: i32) -> Result<(), SysvMqError> {
} }
} }
#[allow(clippy::doc_markdown)]
/// Get the id of an existing SysV IPC message queue by passing its
/// key.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// found.
pub fn id_from_key(key: i32) -> Result<i32, SysvMqError> { pub fn id_from_key(key: i32) -> Result<i32, SysvMqError> {
let id = unsafe { msgget(key, 0) }; let id = unsafe { msgget(key, 0) };
@ -72,54 +44,43 @@ pub fn id_from_key(key: i32) -> Result<i32, SysvMqError> {
} }
} }
pub fn ipc_info(id: i32) -> Result<(), SysvMqError> { #[allow(clippy::doc_markdown)]
let mut msginfo = MaybeUninit::<msginfo>::uninit(); /// Get the info about an existing SysV IPC message queue.
pub fn ipc_info(id: i32) {
let mut ipc_info = MaybeUninit::<msqid_ds>::uninit();
unsafe { let ipc_info = unsafe {
msgctl( msgctl(id, IPC_INFO, ipc_info.as_mut_ptr());
id, ipc_info.assume_init()
ControlCommands::IpcInfo as i32, };
msginfo.as_mut_ptr() as *mut msqid_ds,
); println!("info: {ipc_info:?}");
} }
let msginfo = unsafe { msginfo.assume_init() }; #[allow(clippy::doc_markdown)]
/// Get the stats about an existing SysV IPC message queue.
pub fn stat_info(id: i32) {
let mut stat_info = MaybeUninit::<msqid_ds>::uninit();
println!("info: {:?}", msginfo); let stat_info = unsafe {
msgctl(id, MSG_STAT, stat_info.as_mut_ptr());
stat_info.assume_init()
};
Ok(()) println!("info: {stat_info:?}");
} }
pub fn stat_info(id: i32) -> Result<(), SysvMqError> { #[allow(clippy::doc_markdown)]
let mut msginfo = MaybeUninit::<msqid_ds>::uninit(); /// Get the message info about an existing SysV IPC message queue.
pub fn msg_info(id: i32) {
let mut msg_info = MaybeUninit::<msqid_ds>::uninit();
unsafe { let msg_info = unsafe {
msgctl(id, ControlCommands::Stat as i32, msginfo.as_mut_ptr()); msgctl(id, MSG_INFO, msg_info.as_mut_ptr());
} msg_info.assume_init()
};
let msginfo = unsafe { msginfo.assume_init() }; println!("info: {msg_info:?}");
println!("info: {:?}", msginfo);
Ok(())
}
pub fn msg_info(id: i32) -> Result<(), SysvMqError> {
let mut msginfo = MaybeUninit::<msginfo>::uninit();
unsafe {
msgctl(
id,
ControlCommands::MsgInfo as i32,
msginfo.as_mut_ptr() as *mut msqid_ds,
);
}
let msginfo = unsafe { msginfo.assume_init() };
println!("info: {:?}", msginfo);
Ok(())
} }
pub struct SysvMq<T> { pub struct SysvMq<T> {
@ -131,9 +92,15 @@ pub struct SysvMq<T> {
} }
impl<T> SysvMq<T> { impl<T> SysvMq<T> {
/// Create a new message queye with the given key.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// created.
pub fn create(&mut self, key: i32) -> Result<&Self, SysvMqError> { pub fn create(&mut self, key: i32) -> Result<&Self, SysvMqError> {
self.key = key; self.key = key;
self.id = unsafe { msgget(self.key, Flags::CreateKey as i32 | self.mode) }; self.id = unsafe { msgget(self.key, IPC_CREAT | self.mode) };
match self.id { match self.id {
-1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())), -1 => Err(SysvMqError::ErrnoError(Errno::from_i32(errno()).desc())),
@ -141,6 +108,12 @@ impl<T> SysvMq<T> {
} }
} }
/// Open an existing message queye with the given key.
///
/// # Errors
///
/// Return an `SysvMqError` when no queue with the given key can be
/// found.
pub fn open(mut self, key: i32) -> Result<Self, SysvMqError> { pub fn open(mut self, key: i32) -> Result<Self, SysvMqError> {
self.key = key; self.key = key;
self.id = unsafe { msgget(self.key, self.mode) }; self.id = unsafe { msgget(self.key, self.mode) };
@ -157,12 +130,13 @@ impl<T> SysvMq<T> {
} }
pub fn nonblocking(&mut self) -> &Self { pub fn nonblocking(&mut self) -> &Self {
self.message_mask |= Flags::NoWait as i32; self.message_mask |= IPC_NOWAIT;
self self
} }
pub fn new() -> Self { #[must_use]
SysvMq { pub const fn new() -> Self {
Self {
id: -1, id: -1,
key: 0, key: 0,
message_mask: 0, message_mask: 0,