Skip to content

Commit

Permalink
Fixing every clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
LeChatP committed Sep 10, 2024
1 parent 7e1371b commit 1cae07e
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = ["xtask", "rar-common"]
name = "RootAsRole"
# The project version is managed on json file in resources/rootasrole.json
version = "3.0.0"
rust-version = "1.74.1"
rust-version = "1.76.0"
authors = ["Eddie Billoir <[email protected]>"]
edition = "2021"
default-run = "sr"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

### Prerequisites

* [Rust](https://www.rust-lang.org/tools/install) >= 1.70.0
* [Rust](https://www.rust-lang.org/tools/install) >= 1.76.0
* You can install Rust by running the following command:
```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Expand Down
8 changes: 8 additions & 0 deletions rar-common/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::sync::Mutex;

use capctl::CapSet;

#[cfg(feature = "finder")]
use serde_json::Value;
use strum::EnumIs;
#[cfg(feature = "finder")]
use tracing::debug;

#[cfg(feature = "finder")]
Expand Down Expand Up @@ -93,6 +95,12 @@ pub struct PluginManager {
complex_command_parsers: Vec<ComplexCommandParser>,
}

impl Default for PluginManager {
fn default() -> Self {
Self::new()
}
}

impl PluginManager {
pub fn new() -> Self {
PluginManager {
Expand Down
6 changes: 3 additions & 3 deletions rar-common/src/database/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ fn match_args(input_args: &[String], role_args: &[String]) -> Result<CmdMin, Box
debug!("Matching args {:?} with {:?}", commandline, role_args);
if commandline != role_args {
debug!("test regex");
return evaluate_regex_cmd(role_args, commandline).inspect_err(|e| {
evaluate_regex_cmd(role_args, commandline).inspect_err(|e| {
debug!("{:?},No match for args {:?}", e, input_args);
});
})
} else {
return Ok(CmdMin::Match);
Ok(CmdMin::Match)
}
}

Expand Down
6 changes: 4 additions & 2 deletions rar-common/src/database/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use tracing::debug;

use crate::version::PACKAGE_VERSION;

type MigrationFn<T> = fn(&Migration<T>, &mut T) -> Result<(), Box<dyn Error>>;

pub struct Migration<T> {
pub from: fn() -> Version,
pub to: fn() -> Version,
pub up: fn(&Self, &mut T) -> Result<(), Box<dyn Error>>,
pub down: fn(&Self, &mut T) -> Result<(), Box<dyn Error>>,
pub up: MigrationFn<T>,
pub down: MigrationFn<T>,
}

#[derive(PartialEq, Eq, Debug)]
Expand Down
89 changes: 52 additions & 37 deletions rar-common/src/database/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::collections::HashMap;
use std::{borrow::Borrow, cell::RefCell, path::PathBuf, rc::Rc};
use std::{borrow::Borrow, cell::RefCell, rc::Rc};
#[cfg(feature = "finder")]
use std::path::PathBuf;

use chrono::Duration;

#[cfg(feature = "finder")]
use libc::PATH_MAX;
use linked_hash_set::LinkedHashSet;

Expand All @@ -11,7 +14,10 @@ use pcre2::bytes::Regex;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
use strum::{Display, EnumIs, EnumIter, FromRepr};
use tracing::{debug, warn};

use tracing::debug;
#[cfg(feature = "finder")]
use tracing::warn;

use crate::rc_refcell;

Expand Down Expand Up @@ -129,9 +135,9 @@ pub struct EnvKey {
value: String,
}

impl ToString for EnvKey {
fn to_string(&self) -> String {
self.value.clone()
impl std::fmt::Display for EnvKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}

Expand Down Expand Up @@ -222,9 +228,10 @@ pub struct Opt {

impl Opt {
pub fn new(level: Level) -> Self {
let mut opt = Self::default();
opt.level = level;
opt
Opt {
level,
..Default::default()
}
}

pub fn level_default() -> Self {
Expand Down Expand Up @@ -274,9 +281,11 @@ impl Opt {
.into_iter()
.collect();
opt.env = Some(env);
let mut timeout = STimeout::default();
timeout.type_field = Some(TimestampType::PPID);
timeout.duration = Some(Duration::minutes(5));
let timeout = STimeout {
type_field: Some(TimestampType::PPID),
duration: Some(Duration::minutes(5)),
..Default::default()
};
opt.timeout = Some(timeout);
opt.wildcard_denied = Some(";&|".to_string());
opt
Expand Down Expand Up @@ -433,9 +442,10 @@ impl Default for SEnvOptions {

impl SEnvOptions {
pub fn new(behavior: EnvBehavior) -> Self {
let mut res = SEnvOptions::default();
res.default_behavior = behavior;
res
SEnvOptions {
default_behavior: behavior,
..Default::default()
}
}
}

Expand Down Expand Up @@ -474,10 +484,11 @@ fn check_wildcarded(_wildcarded: &EnvKey, _s: &String) -> bool {
true
}

#[cfg(feature = "finder")]
fn tz_is_safe(tzval: &str) -> bool {
// tzcode treats a value beginning with a ':' as a path.
let tzval = if tzval.starts_with(':') {
&tzval[1..]
let tzval = if let Some(val) = tzval.strip_prefix(':') {
val
} else {
tzval
};
Expand Down Expand Up @@ -522,6 +533,7 @@ fn tz_is_safe(tzval: &str) -> bool {
true
}

#[cfg(feature = "finder")]
fn check_env(key: &str, value: &str) -> bool {
debug!("Checking env: {}={}", key, value);
match key {
Expand All @@ -538,6 +550,20 @@ pub struct OptStack {
task: Option<Rc<RefCell<STask>>>,
}

type FinalPath = (
PathBehavior,
Rc<RefCell<LinkedHashSet<String>>>,
Rc<RefCell<LinkedHashSet<String>>>,
);

type FinalEnv = (
EnvBehavior,
HashMap<String, String>,
LinkedHashSet<EnvKey>,
LinkedHashSet<EnvKey>,
LinkedHashSet<EnvKey>,
);

impl OptStack {
pub fn from_task(task: Rc<RefCell<STask>>) -> Self {
let mut stack = OptStack::from_role(
Expand Down Expand Up @@ -584,10 +610,12 @@ impl OptStack {

fn new(roles: Rc<RefCell<SConfig>>) -> OptStack {
let mut res = OptStack::default();
let mut opt = Opt::default();
opt.level = Level::Global;
opt.root = Some(SPrivileged::User);
opt.bounding = Some(SBounding::Strict);
let mut opt = Opt{
level: Level::Global,
root: Some(SPrivileged::User),
bounding: Some(SBounding::Strict),
..Default::default()
};
let mut env = SEnvOptions::new(EnvBehavior::Delete);
env.check = ["TZ".into(), "LOGNAME".into(), "LOGIN".into(), "USER".into()]
.iter()
Expand Down Expand Up @@ -633,6 +661,7 @@ impl OptStack {
}
}

#[cfg(feature = "finder")]
fn calculate_path(&self) -> String {
let (final_behavior, final_add, final_sub) = self.get_final_path();
let final_add = final_add
Expand Down Expand Up @@ -669,11 +698,7 @@ impl OptStack {

fn get_final_path(
&self,
) -> (
PathBehavior,
Rc<RefCell<LinkedHashSet<String>>>,
Rc<RefCell<LinkedHashSet<String>>>,
) {
) -> FinalPath {
let mut final_behavior = PathBehavior::Delete;
let final_add = rc_refcell!(LinkedHashSet::new());
// Cannot use HashSet as we need to keep order
Expand Down Expand Up @@ -724,11 +749,7 @@ impl OptStack {
#[cfg(not(tarpaulin_include))]
fn union_all_path(
&self,
) -> (
PathBehavior,
Rc<RefCell<LinkedHashSet<String>>>,
Rc<RefCell<LinkedHashSet<String>>>,
) {
) -> FinalPath {
let mut final_behavior = PathBehavior::Delete;
let final_add = rc_refcell!(LinkedHashSet::new());
// Cannot use HashSet as we need to keep order
Expand Down Expand Up @@ -858,13 +879,7 @@ impl OptStack {

fn get_final_env(
&self,
) -> (
EnvBehavior,
HashMap<String, String>,
LinkedHashSet<EnvKey>,
LinkedHashSet<EnvKey>,
LinkedHashSet<EnvKey>,
) {
) -> FinalEnv {
let mut final_behavior = EnvBehavior::default();
let mut final_set = HashMap::new();
let mut final_keep = LinkedHashSet::new();
Expand Down
35 changes: 20 additions & 15 deletions rar-common/src/database/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ pub struct SConfig {
pub options: OptWrapper,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub roles: Vec<Rc<RefCell<SRole>>>,
#[serde(skip)]
storage: (),
#[serde(default)]
#[serde(flatten, skip_serializing_if = "Map::is_empty")]
pub _extra_fields: Map<String, Value>,
Expand Down Expand Up @@ -87,6 +85,9 @@ impl SGroups {
SGroups::Multiple(groups) => groups.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, EnumIs)]
Expand Down Expand Up @@ -220,7 +221,6 @@ impl Default for SConfig {
SConfig {
options: Some(Rc::new(RefCell::new(Opt::default()))),
roles: Vec::new(),
storage: (),
_extra_fields: Map::default(),
}
}
Expand Down Expand Up @@ -323,9 +323,10 @@ impl From<&str> for SCommand {

impl From<CapSet> for SCapabilities {
fn from(capset: CapSet) -> Self {
let mut c = SCapabilities::default();
c.add = capset;
c
SCapabilities {
add: capset,
..Default::default()
}
}
}

Expand Down Expand Up @@ -388,10 +389,11 @@ impl SConfig {

impl SRole {
pub fn new(name: String, config: Weak<RefCell<SConfig>>) -> Self {
let mut ret = SRole::default();
ret.name = name;
ret._config = Some(config);
ret
SRole {
name,
_config: Some(config),
..Default::default()
}
}
pub fn config(&self) -> Option<Rc<RefCell<SConfig>>> {
self._config.as_ref()?.upgrade()
Expand All @@ -405,10 +407,11 @@ impl SRole {

impl STask {
pub fn new(name: IdTask, role: Weak<RefCell<SRole>>) -> Self {
let mut ret = STask::default();
ret.name = name;
ret._role = Some(role);
ret
STask {
name,
_role: Some(role),
..Default::default()
}
}
pub fn role(&self) -> Option<Rc<RefCell<SRole>>> {
self._role.as_ref()?.upgrade()
Expand Down Expand Up @@ -597,7 +600,9 @@ impl PartialOrd<Vec<SActorType>> for SGroups {
}
}
SGroups::Multiple(groups) => {
if groups.len() == other.len() {
if groups.is_empty() && other.is_empty() {
return Some(Ordering::Equal);
} else if groups.len() == other.len() {
if groups.iter().all(|x| other.iter().any(|y| x == y)) {
return Some(Ordering::Equal);
}
Expand Down
1 change: 1 addition & 0 deletions rar-common/src/plugin/hashchecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct HashChecker {
command: SCommand,
}

#[cfg(feature = "finder")]
fn compute(hashtype: &HashType, hash: &[u8]) -> Vec<u8> {
match hashtype {
HashType::SHA224 => {
Expand Down
1 change: 1 addition & 0 deletions rar-common/src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "finder")]
mod hashchecker;
#[cfg(feature = "finder")]
mod hierarchy;
Expand Down
9 changes: 6 additions & 3 deletions rar-common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use strum::EnumIs;
use tracing::{debug, warn, Level};
use tracing_subscriber::util::SubscriberInitExt;

use crate::{api::PluginManager, database::structs::SCommand};
use crate::database::structs::SCommand;
#[cfg(feature = "finder")]
use crate::api::PluginManager;

pub const RST: &str = "\x1B[0m";
pub const BOLD: &str = "\x1B[1m";
Expand Down Expand Up @@ -76,14 +78,15 @@ fn immutable_required_privileges(file: &File, effective: bool) -> Result<(), cap
}

fn read_or_dac_override(effective: bool) -> Result<(), capctl::Error> {
Ok(match effective {
match effective {
false => {
read_effective(false).and(dac_override_effective(false))?;
}
true => {
read_effective(true).or(dac_override_effective(true))?;
}
})
}
Ok(())
}

/// Set or unset the immutable flag on a file
Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by build.rs
// Do not edit this file directly
// Instead edit build.rs and run cargo build
pub const PACKAGE_VERSION: &'static str = "3.0.0-alpha.5";
pub const PACKAGE_VERSION: &str = "3.0.0-alpha.5";
Loading

0 comments on commit 1cae07e

Please sign in to comment.