Skip to content

Commit f3d1db1

Browse files
committed
.github/workflows: add cargo clippy check
Signed-off-by: Kun Lai <[email protected]>
1 parent d319e3f commit f3d1db1

File tree

27 files changed

+246
-175
lines changed

27 files changed

+246
-175
lines changed

.github/workflows/clippy.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Cargo Clippy
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- "v*.*.*"
9+
pull_request:
10+
branches:
11+
- 'master'
12+
13+
jobs:
14+
clippy:
15+
strategy:
16+
fail-fast: false
17+
runs-on: ubuntu-latest
18+
defaults:
19+
run:
20+
shell: bash
21+
container:
22+
image: alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
23+
steps:
24+
- name: Update yum mirror
25+
run: |
26+
set -e
27+
set -x
28+
29+
# replace the mirror
30+
sed -i -E 's|https?://mirrors.openanolis.cn/anolis/|https://mirrors.aliyun.com/anolis/|g' /etc/yum.repos.d/*.repo
31+
sed -i -E 's|https?://mirrors.cloud.aliyuncs.com/|https://mirrors.aliyun.com/|g' /etc/yum.repos.d/*.repo
32+
33+
# install development tools
34+
yum install -y autoconf automake binutils bison flex gcc gcc-c++ gdb glibc-devel libtool make pkgconf pkgconf-m4 pkgconf-pkg-config rpm-build rpm-sign strace asciidoc byacc ctags diffstat elfutils-libelf-devel git intltool patchutils perl-Fedora-VSP perl-Sys-Syslog perl-generators pesign source-highlight systemtap valgrind valgrind-devel cmake expect rpmdevtools rpmlint perl clang
35+
36+
# install rpmdevtools
37+
yum install -y git yum-utils
38+
39+
- name: Checkout repository
40+
uses: actions/checkout@v4
41+
42+
- name: Install Rust toolchain
43+
uses: dtolnay/[email protected]
44+
45+
- name: Install build dependencies
46+
run: yum-builddep -y ./cryptpilot.spec
47+
48+
- name: Install Clippy
49+
run: rustup component add clippy
50+
51+
- name: Cargo Clippy
52+
run: cargo clippy --all-targets --all-features -- -D warnings

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,6 @@ shellcheck:
117117
}
118118
find . -name '*.sh' -exec shellcheck {} \;
119119

120+
.PHONE: clippy
121+
clippy:
122+
cargo clippy --all-targets --all-features -- -D warnings

clippy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
disallowed-methods = [
2+
{path = "std::process::Command::status", reason = "please use 'CheckCommandOutput::run()' instaed"},
3+
{path = "std::process::Command::output", reason = "please use 'CheckCommandOutput::run()' instaed"},
4+
{path = "tokio::process::Command::status", reason = "please use 'CheckCommandOutput::run()' instaed"},
5+
{path = "tokio::process::Command::output", reason = "please use 'CheckCommandOutput::run()' instaed"},
6+
]

src/bin/gen-template/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ where
274274
for (mut key, value) in table.iter_mut() {
275275
// extract docs
276276
let field_name = key.get();
277-
let Ok(docs) = T::get_field_docs(&field_name) else {
277+
let Ok(docs) = T::get_field_docs(field_name) else {
278278
// ignore fields not known to `T`
279279
continue;
280280
};
@@ -383,6 +383,6 @@ fn main() -> Result<()> {
383383
TemplateType::Fde => get_fde_config().as_annotated_toml()?,
384384
};
385385

386-
print!("{}", doc.to_string());
386+
print!("{}", doc);
387387
Ok(())
388388
}

src/cmd/boot_service/initrd_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct InitrdState {
1111
pub fde_config_bundle: FdeConfigBundle,
1212
}
1313

14-
pub const CRYPTPILOT_INITRD_STATE_PATH: &'static str = "/var/run/cryptpilot/initrd_state.toml";
14+
pub const CRYPTPILOT_INITRD_STATE_PATH: &str = "/var/run/cryptpilot/initrd_state.toml";
1515

1616
impl InitrdState {
1717
pub async fn save(&self) -> Result<()> {

src/cmd/boot_service/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ use crate::{
2929

3030
use super::fde::disk::{FdeDisk as _, OnExternalFdeDisk};
3131

32-
const ROOTFS_LOGICAL_VOLUME: &'static str = "/dev/mapper/system-rootfs";
33-
const ROOTFS_LAYER_NAME: &'static str = "rootfs";
34-
const ROOTFS_LAYER_DEVICE: &'static str = "/dev/mapper/rootfs";
35-
const ROOTFS_DECRYPTED_LAYER_DEVICE: &'static str = "/dev/mapper/rootfs_decrypted";
36-
const ROOTFS_DECRYPTED_LAYER_NAME: &'static str = "rootfs_decrypted";
37-
const ROOTFS_HASH_LOGICAL_VOLUME: &'static str = "/dev/mapper/system-rootfs_hash";
38-
const DATA_LOGICAL_VOLUME: &'static str = "/dev/mapper/system-data";
39-
const DATA_LAYER_NAME: &'static str = "data";
40-
const DATA_LAYER_DEVICE: &'static str = "/dev/mapper/data";
32+
const ROOTFS_LOGICAL_VOLUME: &str = "/dev/mapper/system-rootfs";
33+
const ROOTFS_LAYER_NAME: &str = "rootfs";
34+
const ROOTFS_LAYER_DEVICE: &str = "/dev/mapper/rootfs";
35+
const ROOTFS_DECRYPTED_LAYER_DEVICE: &str = "/dev/mapper/rootfs_decrypted";
36+
const ROOTFS_DECRYPTED_LAYER_NAME: &str = "rootfs_decrypted";
37+
const ROOTFS_HASH_LOGICAL_VOLUME: &str = "/dev/mapper/system-rootfs_hash";
38+
const DATA_LOGICAL_VOLUME: &str = "/dev/mapper/system-data";
39+
const DATA_LAYER_NAME: &str = "data";
40+
const DATA_LAYER_DEVICE: &str = "/dev/mapper/data";
4141

4242
pub struct BootServiceCommand {
4343
pub boot_service_options: BootServiceOptions,
@@ -508,7 +508,7 @@ async fn setup_user_provided_volumes(boot_service_options: &BootServiceOptions)
508508
.await
509509
.get_volume_configs()
510510
.await?;
511-
if volume_configs.len() == 0 {
511+
if volume_configs.is_empty() {
512512
tracing::info!("The volume configs is empty, exit now");
513513
return Ok(());
514514
}
@@ -536,7 +536,7 @@ async fn setup_user_provided_volumes(boot_service_options: &BootServiceOptions)
536536
volume_config.volume,
537537
volume_config.dev
538538
);
539-
match super::open::open_for_specific_volume(&volume_config).await {
539+
match super::open::open_for_specific_volume(volume_config).await {
540540
Ok(_) => {
541541
tracing::info!(
542542
"The mapping for volume {} is active now",

src/cmd/close.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ impl super::Command for CloseCommand {
1313
for volume in &self.close_options.volume {
1414
tracing::info!("Close volume {volume} now");
1515

16-
if !crate::fs::luks2::is_active(&volume) {
16+
if !crate::fs::luks2::is_active(volume) {
1717
tracing::info!("The mapping for {} is not active, nothing to do", volume);
1818
continue;
1919
}
2020

2121
tracing::info!("Removing mapping for {volume}");
22-
crate::fs::luks2::close(&volume).await?;
22+
crate::fs::luks2::close(volume).await?;
2323
tracing::info!("The volume {volume} is closed now");
2424
}
2525

src/cmd/fde/disk.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ impl MeasurementedBootComponents {
6767
let grub_authenticode_hashes = self
6868
.grub
6969
.iter()
70-
.map(|bytes| calculate_authenticode_hash::<T>(&bytes))
70+
.map(|bytes| calculate_authenticode_hash::<T>(bytes))
7171
.collect::<Result<Vec<_>>>()?;
7272

7373
let shim_authenticode_hashes = self
7474
.shim
7575
.iter()
76-
.map(|bytes| calculate_authenticode_hash::<T>(&bytes))
76+
.map(|bytes| calculate_authenticode_hash::<T>(bytes))
7777
.collect::<Result<Vec<_>>>()?;
7878

7979
Ok(BootComponentsHashValue {
@@ -217,11 +217,26 @@ pub trait FdeDisk: Send + Sync {
217217

218218
for line in entry_content.lines() {
219219
if line.starts_with("linux ") {
220-
kernel_path = line.splitn(2, ' ').nth(1).unwrap_or("").trim().to_string();
220+
kernel_path = line
221+
.split_once(' ')
222+
.map(|x| x.1)
223+
.unwrap_or("")
224+
.trim()
225+
.to_string();
221226
} else if line.starts_with("options ") {
222-
cmdline = line.splitn(2, ' ').nth(1).unwrap_or("").trim().to_string();
227+
cmdline = line
228+
.split_once(' ')
229+
.map(|x| x.1)
230+
.unwrap_or("")
231+
.trim()
232+
.to_string();
223233
} else if line.starts_with("initrd ") {
224-
initrd_path = line.splitn(2, ' ').nth(1).unwrap_or("").trim().to_string();
234+
initrd_path = line
235+
.split_once(' ')
236+
.map(|x| x.1)
237+
.unwrap_or("")
238+
.trim()
239+
.to_string();
225240
}
226241
}
227242

@@ -245,7 +260,7 @@ pub trait FdeDisk: Send + Sync {
245260

246261
// Make kernel path absolute if needed
247262
if !kernel_path.is_empty() {
248-
if kernel_path.starts_with("/") {
263+
if kernel_path.starts_with('/') {
249264
// Already absolute
250265
} else {
251266
kernel_path = format!("/boot/{}", kernel_path);
@@ -254,7 +269,7 @@ pub trait FdeDisk: Send + Sync {
254269

255270
// Make initrd path absolute if needed
256271
if !initrd_path.is_empty() {
257-
if initrd_path.starts_with("/") {
272+
if initrd_path.starts_with('/') {
258273
// Already absolute
259274
} else {
260275
initrd_path = format!("/boot/{}", initrd_path);
@@ -265,7 +280,7 @@ pub trait FdeDisk: Send + Sync {
265280

266281
// Calculate SHA384 hashes
267282
let kernel = self
268-
.read_file_on_disk(&kernel_path)
283+
.read_file_on_disk(kernel_path)
269284
.await
270285
.with_context(|| format!("Failed to read kernel file at {:?}", kernel_path))?;
271286

@@ -284,7 +299,7 @@ pub trait FdeDisk: Send + Sync {
284299
// Extract partition number from device path
285300
// For example, /dev/sda3 -> (hd0,gpt3) or (hd0,msdos3), /dev/nvme0n1p3 -> (hd0,gpt3) or (hd0,msdos3)
286301
let boot_dev = self.get_boot_dev();
287-
if let Some(partition_num) = boot_dev
302+
if let Ok(partition_num) = boot_dev
288303
.to_string_lossy()
289304
.chars()
290305
.rev()
@@ -294,7 +309,6 @@ pub trait FdeDisk: Send + Sync {
294309
.rev()
295310
.collect::<String>()
296311
.parse::<u32>()
297-
.ok()
298312
{
299313
match partition_type {
300314
PartitionTableType::Gpt => format!("(hd0,gpt{})", partition_num),
@@ -408,29 +422,22 @@ pub trait FdeDisk: Send + Sync {
408422

409423
let mut entries = async_walkdir::WalkDir::new(efi_part_root_dir);
410424

411-
loop {
412-
match entries.next().await {
413-
Some(Ok(entry)) => {
414-
if matches!(entry.file_type().await.map(|e| e.is_file()), Ok(true)) {
415-
let file_name = entry.file_name().to_string_lossy().to_lowercase();
416-
417-
if file_name == "grubx64.efi" {
418-
tracing::debug!(file=?entry.path(), "Found grubx64.efi");
419-
let mut buf = vec![];
420-
let mut file = File::open(entry.path()).await?;
421-
file.read_to_end(&mut buf).await?;
422-
let _ = grub_data.insert(buf);
423-
} else if file_name == "shimx64.efi" {
424-
tracing::debug!(file=?entry.path(), "Found shimx64.efi");
425-
let mut buf = vec![];
426-
let mut file = File::open(entry.path()).await?;
427-
file.read_to_end(&mut buf).await?;
428-
let _ = shim_data.insert(buf);
429-
}
430-
}
431-
}
432-
Some(Err(_)) | None => {
433-
break;
425+
while let Some(Ok(entry)) = entries.next().await {
426+
if matches!(entry.file_type().await.map(|e| e.is_file()), Ok(true)) {
427+
let file_name = entry.file_name().to_string_lossy().to_lowercase();
428+
429+
if file_name == "grubx64.efi" {
430+
tracing::debug!(file=?entry.path(), "Found grubx64.efi");
431+
let mut buf = vec![];
432+
let mut file = File::open(entry.path()).await?;
433+
file.read_to_end(&mut buf).await?;
434+
let _ = grub_data.insert(buf);
435+
} else if file_name == "shimx64.efi" {
436+
tracing::debug!(file=?entry.path(), "Found shimx64.efi");
437+
let mut buf = vec![];
438+
let mut file = File::open(entry.path()).await?;
439+
file.read_to_end(&mut buf).await?;
440+
let _ = shim_data.insert(buf);
434441
}
435442
}
436443
}
@@ -445,8 +452,8 @@ pub trait FdeDisk: Send + Sync {
445452
fn get_efi_part_root_dir(&self) -> &Path;
446453
}
447454

448-
const CRYPTPILOT_CONFIG_DIR_UNTRUSTED_IN_BOOT: &'static str = "cryptpilot/config";
449-
const METADATA_PATH_IN_BOOT: &'static str = "cryptpilot/metadata.toml";
455+
const CRYPTPILOT_CONFIG_DIR_UNTRUSTED_IN_BOOT: &str = "cryptpilot/config";
456+
const METADATA_PATH_IN_BOOT: &str = "cryptpilot/metadata.toml";
450457

451458
async fn load_fde_config_bundle_from_dir(config_dir: &Path) -> Result<FdeConfigBundle> {
452459
Ok(FileSystemConfigSource::new(config_dir)
@@ -662,7 +669,7 @@ impl OnExternalFdeDisk {
662669

663670
let content = String::from_utf8_lossy(&output);
664671
for line in content.lines() {
665-
let fields: Vec<&str> = line.trim().split_whitespace().collect();
672+
let fields: Vec<&str> = line.split_whitespace().collect();
666673
if fields.len() != 2 {
667674
continue;
668675
}

src/cmd/fde/dump_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct ConfigDumpCommand {
1616
impl super::super::Command for ConfigDumpCommand {
1717
async fn run(&self) -> Result<()> {
1818
let fde_disk: Box<dyn FdeDisk + Send> = match &self.disk {
19-
Some(disk) => Box::new(OnExternalFdeDisk::new_from_disk(&disk).await?),
19+
Some(disk) => Box::new(OnExternalFdeDisk::new_from_disk(disk).await?),
2020
None => Box::new(OnCurrentSystemFdeDisk::new().await?),
2121
};
2222

src/cmd/fde/show_reference_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl super::super::Command for ShowReferenceValueCommand {
2525
async fn run(&self) -> Result<()> {
2626
tracing::debug!("Get rootfs reference value");
2727
let fde_disk: Box<dyn FdeDisk + Send + Sync> = match &self.disk {
28-
Some(disk) => Box::new(OnExternalFdeDisk::new_from_disk(&disk).await?),
28+
Some(disk) => Box::new(OnExternalFdeDisk::new_from_disk(disk).await?),
2929
None => Box::new(OnCurrentSystemFdeDisk::new().await?),
3030
};
3131

0 commit comments

Comments
 (0)