From d76280f71d3203279900bb00196197b23ee50050 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Wed, 29 Jul 2026 11:49:00 -0700 Subject: [PATCH] Switch to 2024 edition Signed-off-by: Maksym Pavlenko --- Cargo.toml | 16 +++--- crates/runc-shim/src/console.rs | 24 ++++----- crates/runc-shim/src/runc.rs | 74 ++++++++++++-------------- crates/runc/src/asynchronous/runc.rs | 24 ++++----- crates/shim/src/asynchronous/mod.rs | 16 +++--- crates/shim/src/logger.rs | 21 +++++--- crates/shim/src/mount_linux.rs | 38 ++++++------- crates/shim/src/synchronous/mod.rs | 10 ++-- crates/shim/src/synchronous/monitor.rs | 5 +- rust-toolchain.toml | 2 +- rustfmt.toml | 1 + 11 files changed, 116 insertions(+), 115 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e265a2b..d985277a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [workspace] members = [ - "crates/client", - "crates/logging", - "crates/runc", - "crates/runc-shim", - "crates/shim", - "crates/shim-protos", - "crates/snapshots", + "crates/client", + "crates/logging", + "crates/runc", + "crates/runc-shim", + "crates/shim", + "crates/shim-protos", + "crates/snapshots", ] resolver = "2" @@ -21,7 +21,7 @@ panic = 'abort' license = "Apache-2.0" repository = "https://github.com/containerd/rust-extensions" homepage = "https://containerd.io" -edition = "2021" +edition = "2024" # Common dependencies for all crates [workspace.dependencies] diff --git a/crates/runc-shim/src/console.rs b/crates/runc-shim/src/console.rs index 853e5e84..7f563ab4 100644 --- a/crates/runc-shim/src/console.rs +++ b/crates/runc-shim/src/console.rs @@ -57,18 +57,18 @@ impl ConsoleSocket { // async drop is not supported yet, we can only call clean manually after socket received pub async fn clean(self) { - if self.rmdir { - if let Some(tmp_socket_dir) = self.path.parent() { - tokio::fs::remove_dir_all(tmp_socket_dir) - .await - .unwrap_or_else(|e| { - warn!( - "remove tmp console socket path {} : {}", - tmp_socket_dir.display(), - e - ) - }) - } + if self.rmdir + && let Some(tmp_socket_dir) = self.path.parent() + { + tokio::fs::remove_dir_all(tmp_socket_dir) + .await + .unwrap_or_else(|e| { + warn!( + "remove tmp console socket path {} : {}", + tmp_socket_dir.display(), + e + ) + }) } } } diff --git a/crates/runc-shim/src/runc.rs b/crates/runc-shim/src/runc.rs index 60a9c994..031abfd9 100644 --- a/crates/runc-shim/src/runc.rs +++ b/crates/runc-shim/src/runc.rs @@ -307,12 +307,9 @@ impl ProcessLifecycle for RuncInitLifecycle { Some(&runc::options::DeleteOpts { force: true }), ) .await + && !e.to_string().to_lowercase().contains("does not exist") { - if !e.to_string().to_lowercase().contains("does not exist") { - return Err( - runtime_error(&p.lifecycle.bundle, e, "OCI runtime delete failed").await, - ); - } + return Err(runtime_error(&p.lifecycle.bundle, e, "OCI runtime delete failed").await); } umount_recursive(Path::new(&self.bundle).join("rootfs").to_str(), 0)?; self.exit_signal.signal(); @@ -698,36 +695,36 @@ pub async fn copy_io(pio: &ProcessIO, stdio: &Stdio, exit_signal: Arc i32 { subject: Subject::Pid(epid), exit_code: code, }) = s.rx.recv().await + && pid == epid { - if pid == epid { - monitor_unsubscribe(s.id).await.unwrap_or_default(); - return code; - } + monitor_unsubscribe(s.id).await.unwrap_or_default(); + return code; } } } diff --git a/crates/runc/src/asynchronous/runc.rs b/crates/runc/src/asynchronous/runc.rs index 040cc110..db722fd9 100644 --- a/crates/runc/src/asynchronous/runc.rs +++ b/crates/runc/src/asynchronous/runc.rs @@ -55,18 +55,18 @@ impl Runc { unsafe { cmd.pre_exec(move || { #[cfg(target_os = "linux")] - if let Ok(thp) = std::env::var("THP_DISABLED") { - if let Ok(thp_disabled) = thp.parse::() { - let ret = libc::prctl( - libc::PR_SET_THP_DISABLE, - if thp_disabled { 1u64 } else { 0u64 }, - 0, - 0, - 0, - ); - if ret < 0 { - debug!("set_thp_disable err: {}", std::io::Error::last_os_error()); - } + if let Ok(thp) = std::env::var("THP_DISABLED") + && let Ok(thp_disabled) = thp.parse::() + { + let ret = libc::prctl( + libc::PR_SET_THP_DISABLE, + if thp_disabled { 1u64 } else { 0u64 }, + 0, + 0, + 0, + ); + if ret < 0 { + debug!("set_thp_disable err: {}", std::io::Error::last_os_error()); } } Ok(()) diff --git a/crates/shim/src/asynchronous/mod.rs b/crates/shim/src/asynchronous/mod.rs index 114677ee..515d223c 100644 --- a/crates/shim/src/asynchronous/mod.rs +++ b/crates/shim/src/asynchronous/mod.rs @@ -515,14 +515,14 @@ async fn remove_socket_silently(address: &str) { #[cfg_attr(feature = "tracing", tracing::instrument(level = "info"))] async fn remove_socket(address: &str) -> Result<()> { let path = parse_sockaddr(address); - if let Ok(md) = Path::new(path).metadata() { - if md.file_type().is_socket() { - tokio::fs::remove_file(path).await.map_err(io_error!( - e, - "failed to remove socket {}", - address - ))?; - } + if let Ok(md) = Path::new(path).metadata() + && md.file_type().is_socket() + { + tokio::fs::remove_file(path).await.map_err(io_error!( + e, + "failed to remove socket {}", + address + ))?; } Ok(()) } diff --git a/crates/shim/src/logger.rs b/crates/shim/src/logger.rs index 77ee3c44..47bf6a89 100644 --- a/crates/shim/src/logger.rs +++ b/crates/shim/src/logger.rs @@ -221,6 +221,11 @@ mod tests { use super::*; use crate::Config; + // Only this test touches `LOG_ENV`, and it does so from a single thread. + fn set_log_env(value: &str) { + unsafe { std::env::set_var(LOG_ENV, value) }; + } + #[test] fn test_init_log_level() -> Result<(), Error> { let config = Config::default(); @@ -233,38 +238,38 @@ mod tests { assert_eq!(log::LevelFilter::Debug, log::max_level()); // ENV different than default - std::env::set_var(LOG_ENV, "error"); + set_log_env("error"); configure_logging_level(false, &config.default_log_level); assert_eq!(log::LevelFilter::Error, log::max_level()); - std::env::set_var(LOG_ENV, "warn"); + set_log_env("warn"); configure_logging_level(false, &config.default_log_level); assert_eq!(log::LevelFilter::Warn, log::max_level()); - std::env::set_var(LOG_ENV, "off"); + set_log_env("off"); configure_logging_level(false, &config.default_log_level); assert_eq!(log::LevelFilter::Off, log::max_level()); - std::env::set_var(LOG_ENV, "trace"); + set_log_env("trace"); configure_logging_level(false, &config.default_log_level); assert_eq!(log::LevelFilter::Trace, log::max_level()); - std::env::set_var(LOG_ENV, "debug"); + set_log_env("debug"); configure_logging_level(false, &config.default_log_level); // ENV Different than default from debug flag configure_logging_level(true, &config.default_log_level); assert_eq!(log::LevelFilter::Debug, log::max_level()); - std::env::set_var(LOG_ENV, "trace"); + set_log_env("trace"); configure_logging_level(true, &config.default_log_level); assert_eq!(log::LevelFilter::Trace, log::max_level()); - std::env::set_var(LOG_ENV, "info"); + set_log_env("info"); configure_logging_level(true, &config.default_log_level); assert_eq!(log::LevelFilter::Debug, log::max_level()); - std::env::set_var(LOG_ENV, "off"); + set_log_env("off"); configure_logging_level(true, &config.default_log_level); assert_eq!(log::LevelFilter::Debug, log::max_level()); Ok(()) diff --git a/crates/shim/src/mount_linux.rs b/crates/shim/src/mount_linux.rs index 4a20095b..6f24d307 100644 --- a/crates/shim/src/mount_linux.rs +++ b/crates/shim/src/mount_linux.rs @@ -896,10 +896,10 @@ pub fn umount_recursive(target: Option<&str>, flags: i32) -> Result<()> { fn umount_all(target: Option, flags: i32) -> Result<()> { if let Some(target) = target { - if let Err(e) = std::fs::metadata(target.clone()) { - if e.kind() == std::io::ErrorKind::NotFound { - return Ok(()); - } + if let Err(e) = std::fs::metadata(target.clone()) + && e.kind() == std::io::ErrorKind::NotFound + { + return Ok(()); } loop { if let Err(e) = nix::mount::umount2( @@ -997,11 +997,11 @@ where } mount_info.major = str::parse::(major_minor[0]).ok()?; mount_info.minor = str::parse::(major_minor[1]).ok()?; - if let Some(f) = &f { - if f(mount_info.clone()) { - // skip this mountpoint. This mountpoint is not the container's mountpoint - return None; - } + if let Some(f) = &f + && f(mount_info.clone()) + { + // skip this mountpoint. This mountpoint is not the container's mountpoint + return None; } Some(mount_info) }) @@ -1139,11 +1139,11 @@ mod tests { ]; // mount target. let result = mount_rootfs(Some("overlay"), Some("overlay"), &options, &target); - if let Err(err) = &result { - if crate::error::is_permission_error(err) { - eprintln!("skipping test_mount_rootfs_umount_recursive: {err}"); - return; - } + if let Err(err) = &result + && crate::error::is_permission_error(err) + { + eprintln!("skipping test_mount_rootfs_umount_recursive: {err}"); + return; } assert!(result.is_ok(), "{result:?}"); let mut mountinfo = get_mounts(Some(prefix_filter( @@ -1182,11 +1182,11 @@ mod tests { direct: true, }; let result = setup_loop(backing_file, params); - if let Err(err) = &result { - if crate::error::is_permission_error(err) { - eprintln!("skipping test_setup_loop_dev: {err}"); - return; - } + if let Err(err) = &result + && crate::error::is_permission_error(err) + { + eprintln!("skipping test_setup_loop_dev: {err}"); + return; } assert!(result.is_ok(), "{result:?}"); } diff --git a/crates/shim/src/synchronous/mod.rs b/crates/shim/src/synchronous/mod.rs index 2629c65a..759df268 100644 --- a/crates/shim/src/synchronous/mod.rs +++ b/crates/shim/src/synchronous/mod.rs @@ -371,7 +371,7 @@ fn setup_signals(_config: &Config) -> Option { #[cfg(windows)] unsafe extern "system" fn signal_handler(_: u32) -> i32 { - ReleaseSemaphore(SEMAPHORE, 1, ptr::null_mut()); + unsafe { ReleaseSemaphore(SEMAPHORE, 1, ptr::null_mut()) }; 1 } @@ -462,10 +462,10 @@ fn remove_socket(address: &str) -> Result<()> { #[cfg(unix)] { let path = parse_sockaddr(address); - if let Ok(md) = Path::new(path).metadata() { - if md.file_type().is_socket() { - fs::remove_file(path).map_err(io_error!(e, "remove socket"))?; - } + if let Ok(md) = Path::new(path).metadata() + && md.file_type().is_socket() + { + fs::remove_file(path).map_err(io_error!(e, "remove socket"))?; } } diff --git a/crates/shim/src/synchronous/monitor.rs b/crates/shim/src/synchronous/monitor.rs index da35dd03..76ac136e 100644 --- a/crates/shim/src/synchronous/monitor.rs +++ b/crates/shim/src/synchronous/monitor.rs @@ -141,10 +141,9 @@ pub fn wait_pid(pid: i32, s: Subscription) -> i32 { subject: Subject::Pid(epid), exit_code: code, }) = s.rx.recv() + && pid == epid { - if pid == epid { - return code; - } + return code; } } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 54e5b5ec..ee956aee 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "1.91" -components = ["rustfmt", "clippy", "llvm-tools"] +components = ["rustfmt", "clippy", "llvm-tools", "rust-analyzer"] diff --git a/rustfmt.toml b/rustfmt.toml index 80eda749..f37178a3 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,5 @@ newline_style = "Unix" +style_edition = "2021" # Keep 2021 formatting (mainly import sort order) across the 2024 edition bump unstable_features = true # Cargo fmt now needs to be called with `cargo +nightly fmt` group_imports = "StdExternalCrate" # Create 3 groups: std, external crates, and self. imports_granularity = "Crate" # Merge imports from the same crate into a single use statement