From ee71a992a3ea02c6757b357137a1ab634908ad11 Mon Sep 17 00:00:00 2001 From: Tiann <4233744+tiann@users.noreply.github.com> Date: Mon, 24 Feb 2025 16:53:37 +0800 Subject: [PATCH] ksud: migrate to Rust 2024 edition --- userspace/ksud_magic/Cargo.toml | 2 +- userspace/ksud_magic/src/apk_sign.rs | 2 +- userspace/ksud_magic/src/boot_patch.rs | 6 +++--- userspace/ksud_magic/src/debug.rs | 2 +- userspace/ksud_magic/src/magic_mount.rs | 2 +- userspace/ksud_magic/src/module.rs | 4 ++-- userspace/ksud_magic/src/restorecon.rs | 2 +- userspace/ksud_magic/src/su.rs | 2 +- userspace/ksud_magic/src/utils.rs | 8 ++++---- userspace/ksud_overlayfs/Cargo.toml | 2 +- userspace/ksud_overlayfs/src/apk_sign.rs | 2 +- userspace/ksud_overlayfs/src/boot_patch.rs | 6 +++--- userspace/ksud_overlayfs/src/debug.rs | 2 +- userspace/ksud_overlayfs/src/init_event.rs | 2 +- userspace/ksud_overlayfs/src/module.rs | 4 ++-- userspace/ksud_overlayfs/src/mount.rs | 2 +- userspace/ksud_overlayfs/src/restorecon.rs | 2 +- userspace/ksud_overlayfs/src/sepolicy.rs | 6 +++--- userspace/ksud_overlayfs/src/su.rs | 4 ++-- userspace/ksud_overlayfs/src/utils.rs | 10 +++++----- 20 files changed, 36 insertions(+), 36 deletions(-) diff --git a/userspace/ksud_magic/Cargo.toml b/userspace/ksud_magic/Cargo.toml index 727a89c2..2bb01fb9 100644 --- a/userspace/ksud_magic/Cargo.toml +++ b/userspace/ksud_magic/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ksud" version = "0.1.0" -edition = "2021" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/userspace/ksud_magic/src/apk_sign.rs b/userspace/ksud_magic/src/apk_sign.rs index 6d047cca..4b2c4a98 100644 --- a/userspace/ksud_magic/src/apk_sign.rs +++ b/userspace/ksud_magic/src/apk_sign.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Result}; +use anyhow::{Result, ensure}; use std::io::{Read, Seek, SeekFrom}; pub fn get_apk_signature(apk: &str) -> Result<(u32, String)> { diff --git a/userspace/ksud_magic/src/boot_patch.rs b/userspace/ksud_magic/src/boot_patch.rs index 52306b1c..1737bc3e 100644 --- a/userspace/ksud_magic/src/boot_patch.rs +++ b/userspace/ksud_magic/src/boot_patch.rs @@ -5,11 +5,11 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; +use anyhow::Context; +use anyhow::Result; use anyhow::anyhow; use anyhow::bail; use anyhow::ensure; -use anyhow::Context; -use anyhow::Result; use regex_lite::Regex; use which::which; @@ -97,7 +97,7 @@ pub fn get_current_kmi() -> Result { } fn parse_kmi_from_kernel(kernel: &PathBuf, workdir: &Path) -> Result { - use std::fs::{copy, File}; + use std::fs::{File, copy}; use std::io::{BufReader, Read}; let kernel_path = workdir.join("kernel"); copy(kernel, &kernel_path).context("Failed to copy kernel")?; diff --git a/userspace/ksud_magic/src/debug.rs b/userspace/ksud_magic/src/debug.rs index 8ff851d3..cd367587 100644 --- a/userspace/ksud_magic/src/debug.rs +++ b/userspace/ksud_magic/src/debug.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Context, Ok, Result}; +use anyhow::{Context, Ok, Result, ensure}; use std::{ path::{Path, PathBuf}, process::Command, diff --git a/userspace/ksud_magic/src/magic_mount.rs b/userspace/ksud_magic/src/magic_mount.rs index 61727e48..202d12d0 100644 --- a/userspace/ksud_magic/src/magic_mount.rs +++ b/userspace/ksud_magic/src/magic_mount.rs @@ -4,7 +4,7 @@ use crate::defs::{ use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout}; use crate::restorecon::{lgetfilecon, lsetfilecon}; use crate::utils::ensure_dir_exists; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use extattr::lgetxattr; use rustix::fs::{ bind_mount, chmod, chown, mount, move_mount, unmount, Gid, MetadataExt, Mode, MountFlags, diff --git a/userspace/ksud_magic/src/module.rs b/userspace/ksud_magic/src/module.rs index 87eee351..40f3f002 100644 --- a/userspace/ksud_magic/src/module.rs +++ b/userspace/ksud_magic/src/module.rs @@ -6,7 +6,7 @@ use crate::{ sepolicy, }; -use anyhow::{anyhow, bail, ensure, Context, Result}; +use anyhow::{Context, Result, anyhow, bail, ensure}; use const_format::concatcp; use is_executable::is_executable; use java_properties::PropertiesIter; @@ -16,7 +16,7 @@ use std::fs::{copy, rename}; use std::{ collections::HashMap, env::var as env_var, - fs::{remove_dir_all, remove_file, set_permissions, File, Permissions}, + fs::{File, Permissions, remove_dir_all, remove_file, set_permissions}, io::Cursor, path::{Path, PathBuf}, process::Command, diff --git a/userspace/ksud_magic/src/restorecon.rs b/userspace/ksud_magic/src/restorecon.rs index c396481d..91ccfb11 100644 --- a/userspace/ksud_magic/src/restorecon.rs +++ b/userspace/ksud_magic/src/restorecon.rs @@ -6,7 +6,7 @@ use std::path::Path; #[cfg(any(target_os = "linux", target_os = "android"))] use anyhow::{Context, Ok}; #[cfg(any(target_os = "linux", target_os = "android"))] -use extattr::{lsetxattr, Flags as XattrFlags}; +use extattr::{Flags as XattrFlags, lsetxattr}; pub const SYSTEM_CON: &str = "u:object_r:system_file:s0"; pub const ADB_CON: &str = "u:object_r:adb_data_file:s0"; diff --git a/userspace/ksud_magic/src/su.rs b/userspace/ksud_magic/src/su.rs index 4ce0f3f1..70d96ca0 100644 --- a/userspace/ksud_magic/src/su.rs +++ b/userspace/ksud_magic/src/su.rs @@ -14,7 +14,7 @@ use crate::{ #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process::getuid, - thread::{set_thread_res_gid, set_thread_res_uid, Gid, Uid}, + thread::{Gid, Uid, set_thread_res_gid, set_thread_res_uid}, }; #[cfg(any(target_os = "linux", target_os = "android"))] diff --git a/userspace/ksud_magic/src/utils.rs b/userspace/ksud_magic/src/utils.rs index 5e521ee2..63b392ef 100644 --- a/userspace/ksud_magic/src/utils.rs +++ b/userspace/ksud_magic/src/utils.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Error, Ok, Result}; +use anyhow::{Context, Error, Ok, Result, bail}; use std::{ fs::{create_dir_all, remove_file, write, File, OpenOptions}, io::{ @@ -11,7 +11,7 @@ use std::{ use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; #[allow(unused_imports)] -use std::fs::{set_permissions, Permissions}; +use std::fs::{Permissions, set_permissions}; #[cfg(unix)] use std::os::unix::prelude::PermissionsExt; @@ -20,7 +20,7 @@ use std::path::PathBuf; #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process, - thread::{move_into_link_name_space, LinkNameSpaceType}, + thread::{LinkNameSpaceType, move_into_link_name_space}, }; pub fn ensure_clean_dir(dir: impl AsRef) -> Result<()> { @@ -125,7 +125,7 @@ pub fn get_zip_uncompressed_size(zip_path: &str) -> Result { pub fn switch_mnt_ns(pid: i32) -> Result<()> { use rustix::{ fd::AsFd, - fs::{open, Mode, OFlags}, + fs::{Mode, OFlags, open}, }; let path = format!("/proc/{pid}/ns/mnt"); let fd = open(path, OFlags::RDONLY, Mode::from_raw_mode(0))?; diff --git a/userspace/ksud_overlayfs/Cargo.toml b/userspace/ksud_overlayfs/Cargo.toml index 0d4e3853..9fefb6bb 100644 --- a/userspace/ksud_overlayfs/Cargo.toml +++ b/userspace/ksud_overlayfs/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ksud" version = "0.1.0" -edition = "2021" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/userspace/ksud_overlayfs/src/apk_sign.rs b/userspace/ksud_overlayfs/src/apk_sign.rs index 6d047cca..4b2c4a98 100644 --- a/userspace/ksud_overlayfs/src/apk_sign.rs +++ b/userspace/ksud_overlayfs/src/apk_sign.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Result}; +use anyhow::{Result, ensure}; use std::io::{Read, Seek, SeekFrom}; pub fn get_apk_signature(apk: &str) -> Result<(u32, String)> { diff --git a/userspace/ksud_overlayfs/src/boot_patch.rs b/userspace/ksud_overlayfs/src/boot_patch.rs index 4c6eb9eb..2d2a0eb2 100644 --- a/userspace/ksud_overlayfs/src/boot_patch.rs +++ b/userspace/ksud_overlayfs/src/boot_patch.rs @@ -5,11 +5,11 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; +use anyhow::Context; +use anyhow::Result; use anyhow::anyhow; use anyhow::bail; use anyhow::ensure; -use anyhow::Context; -use anyhow::Result; use regex_lite::Regex; use which::which; @@ -97,7 +97,7 @@ pub fn get_current_kmi() -> Result { } fn parse_kmi_from_kernel(kernel: &PathBuf, workdir: &Path) -> Result { - use std::fs::{copy, File}; + use std::fs::{File, copy}; use std::io::{BufReader, Read}; let kernel_path = workdir.join("kernel"); copy(kernel, &kernel_path).context("Failed to copy kernel")?; diff --git a/userspace/ksud_overlayfs/src/debug.rs b/userspace/ksud_overlayfs/src/debug.rs index 8ff851d3..cd367587 100644 --- a/userspace/ksud_overlayfs/src/debug.rs +++ b/userspace/ksud_overlayfs/src/debug.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Context, Ok, Result}; +use anyhow::{Context, Ok, Result, ensure}; use std::{ path::{Path, PathBuf}, process::Command, diff --git a/userspace/ksud_overlayfs/src/init_event.rs b/userspace/ksud_overlayfs/src/init_event.rs index 7107ff74..46311be1 100644 --- a/userspace/ksud_overlayfs/src/init_event.rs +++ b/userspace/ksud_overlayfs/src/init_event.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use log::{info, warn}; use std::{collections::HashMap, path::Path}; diff --git a/userspace/ksud_overlayfs/src/module.rs b/userspace/ksud_overlayfs/src/module.rs index 96928563..a0b6fbe7 100644 --- a/userspace/ksud_overlayfs/src/module.rs +++ b/userspace/ksud_overlayfs/src/module.rs @@ -6,7 +6,7 @@ use crate::{ sepolicy, utils, }; -use anyhow::{anyhow, bail, ensure, Context, Result}; +use anyhow::{Context, Result, anyhow, bail, ensure}; use const_format::concatcp; use is_executable::is_executable; use java_properties::PropertiesIter; @@ -16,7 +16,7 @@ use std::fs::OpenOptions; use std::{ collections::HashMap, env::var as env_var, - fs::{remove_dir_all, remove_file, set_permissions, File, Permissions}, + fs::{File, Permissions, remove_dir_all, remove_file, set_permissions}, io::Cursor, path::{Path, PathBuf}, process::{Command, Stdio}, diff --git a/userspace/ksud_overlayfs/src/mount.rs b/userspace/ksud_overlayfs/src/mount.rs index 11be898b..bdc486e7 100644 --- a/userspace/ksud_overlayfs/src/mount.rs +++ b/userspace/ksud_overlayfs/src/mount.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, bail, Ok, Result}; +use anyhow::{Ok, Result, anyhow, bail}; #[cfg(any(target_os = "linux", target_os = "android"))] use anyhow::Context; diff --git a/userspace/ksud_overlayfs/src/restorecon.rs b/userspace/ksud_overlayfs/src/restorecon.rs index 152a7c5e..7656357e 100644 --- a/userspace/ksud_overlayfs/src/restorecon.rs +++ b/userspace/ksud_overlayfs/src/restorecon.rs @@ -6,7 +6,7 @@ use std::path::Path; #[cfg(any(target_os = "linux", target_os = "android"))] use anyhow::{Context, Ok}; #[cfg(any(target_os = "linux", target_os = "android"))] -use extattr::{lsetxattr, Flags as XattrFlags}; +use extattr::{Flags as XattrFlags, lsetxattr}; pub const SYSTEM_CON: &str = "u:object_r:system_file:s0"; pub const ADB_CON: &str = "u:object_r:adb_data_file:s0"; diff --git a/userspace/ksud_overlayfs/src/sepolicy.rs b/userspace/ksud_overlayfs/src/sepolicy.rs index 62dedd42..90350cad 100644 --- a/userspace/ksud_overlayfs/src/sepolicy.rs +++ b/userspace/ksud_overlayfs/src/sepolicy.rs @@ -1,11 +1,11 @@ -use anyhow::{bail, Result}; +use anyhow::{Result, bail}; use derive_new::new; use nom::{ + AsChar, IResult, Parser, branch::alt, - bytes::complete::{tag, take_while, take_while1, take_while_m_n}, + bytes::complete::{tag, take_while, take_while_m_n, take_while1}, character::complete::{space0, space1}, combinator::map, - AsChar, IResult, Parser, }; use std::{ffi, path::Path, vec}; diff --git a/userspace/ksud_overlayfs/src/su.rs b/userspace/ksud_overlayfs/src/su.rs index fde12c96..8d79c6ca 100644 --- a/userspace/ksud_overlayfs/src/su.rs +++ b/userspace/ksud_overlayfs/src/su.rs @@ -14,7 +14,7 @@ use crate::{ #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process::getuid, - thread::{set_thread_res_gid, set_thread_res_uid, Gid, Uid}, + thread::{Gid, Uid, set_thread_res_gid, set_thread_res_uid}, }; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -280,6 +280,6 @@ fn add_path_to_env(path: &str) -> Result<()> { let new_path = PathBuf::from(path.trim_end_matches('/')); paths.push(new_path); let new_path_env = env::join_paths(paths)?; - env::set_var("PATH", new_path_env); + unsafe { env::set_var("PATH", new_path_env) }; Ok(()) } diff --git a/userspace/ksud_overlayfs/src/utils.rs b/userspace/ksud_overlayfs/src/utils.rs index e3508b83..1387b38f 100644 --- a/userspace/ksud_overlayfs/src/utils.rs +++ b/userspace/ksud_overlayfs/src/utils.rs @@ -1,6 +1,6 @@ -use anyhow::{bail, Context, Error, Ok, Result}; +use anyhow::{Context, Error, Ok, Result, bail}; use std::{ - fs::{self, create_dir_all, remove_file, write, File, OpenOptions}, + fs::{self, File, OpenOptions, create_dir_all, remove_file, write}, io::{ ErrorKind::{AlreadyExists, NotFound}, Write, @@ -13,7 +13,7 @@ use std::{ use crate::{assets, boot_patch, defs, ksucalls, module, restorecon}; use std::fs::metadata; #[allow(unused_imports)] -use std::fs::{set_permissions, Permissions}; +use std::fs::{Permissions, set_permissions}; #[cfg(unix)] use std::os::unix::prelude::PermissionsExt; @@ -26,7 +26,7 @@ use std::path::PathBuf; #[cfg(any(target_os = "linux", target_os = "android"))] use rustix::{ process, - thread::{move_into_link_name_space, LinkNameSpaceType}, + thread::{LinkNameSpaceType, move_into_link_name_space}, }; pub fn ensure_clean_dir(dir: impl AsRef) -> Result<()> { @@ -131,7 +131,7 @@ pub fn get_zip_uncompressed_size(zip_path: &str) -> Result { pub fn switch_mnt_ns(pid: i32) -> Result<()> { use rustix::{ fd::AsFd, - fs::{open, Mode, OFlags}, + fs::{Mode, OFlags, open}, }; let path = format!("/proc/{pid}/ns/mnt"); let fd = open(path, OFlags::RDONLY, Mode::from_raw_mode(0))?;