From b002bea6475feb403b2dc4bfa5855804331f59f9 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:12:30 +0800 Subject: [PATCH 01/13] cli: optimize cli arguments Signed-off-by: renoseven --- build/src/cli/args.rs | 2 +- build/src/cli/cli.rs | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/build/src/cli/args.rs b/build/src/cli/args.rs index 1d6fb3b..a91eac1 100644 --- a/build/src/cli/args.rs +++ b/build/src/cli/args.rs @@ -42,7 +42,7 @@ pub struct CliArguments { /// Debug info (vmlinux for kernel) #[arg(short, long)] - pub debug_info: Option, + pub debug_info: Option, /// Working directory #[arg(long, default_value=CLI_DEFAULT_WORK_DIR)] diff --git a/build/src/cli/cli.rs b/build/src/cli/cli.rs index 85e6e9e..c0d9a40 100644 --- a/build/src/cli/cli.rs +++ b/build/src/cli/cli.rs @@ -28,14 +28,11 @@ impl PatchBuildCLI { fn check_arguments(&self) -> std::io::Result<()> { let args = &self.cli_args; match &args.source { - CliPath::File(file_path) => fs::check_file(file_path)?, + CliPath::File(file_path) => fs::check_file(file_path)?, CliPath::Directory(dir_path) => fs::check_dir(dir_path)?, } - if let Some(debug_info_path) = &args.debug_info { - match debug_info_path { - CliPath::File(file_path) => fs::check_file(file_path)?, - CliPath::Directory(dir_path) => fs::check_dir(dir_path)?, - } + if let Some(file_path) = &args.debug_info { + fs::check_file(file_path)? } if let Some(file_path) = &args.kconfig { fs::check_file(file_path)?; @@ -69,13 +66,14 @@ impl PatchBuildCLI { // Collect patch version info from patched source package let patch_version_file = fs::find_file(rpm_source_dir, PKG_PATCH_VERSION_FILE_NAME, false, false); if let Ok(file_path) = &patch_version_file { - let version_str = fs::read_file_to_string(file_path)?; - let current_patch_version: u32 = args.patch_version.parse().expect("Parse patch version failed"); - let package_patch_version: u32 = version_str.parse().expect("Parse patch version failed"); + let arg_version = args.patch_version.parse::(); + let pkg_version = fs::read_file_to_string(file_path)?.parse::(); - let max_patch_version = u32::max(current_patch_version, package_patch_version + 1); - if max_patch_version > current_patch_version { - args.patch_version = max_patch_version.to_string(); + if let (Ok(arg_ver), Ok(pkg_ver)) = (arg_version, pkg_version) { + let max_ver = u32::max(arg_ver, pkg_ver + 1); + if max_ver > arg_ver { + args.patch_version = max_ver.to_string(); + } } } @@ -155,7 +153,7 @@ impl PatchBuildCLI { )?; let kernel_file = KernelPatchHelper::build_kernel(&source_dir, jobs)?; - args.debug_info = Some(CliPath::File(kernel_file)); + args.debug_info = Some(kernel_file); } Ok(()) -- Gitee From f75725a036d2a0735ce5d80b372ff83d21db2226 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:17:54 +0800 Subject: [PATCH 02/13] package: fix 'rpm with dependencies extraction failed' issue Signed-off-by: renoseven --- build/src/package/rpm_extractor.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build/src/package/rpm_extractor.rs b/build/src/package/rpm_extractor.rs index 93692ea..d9c7847 100644 --- a/build/src/package/rpm_extractor.rs +++ b/build/src/package/rpm_extractor.rs @@ -7,7 +7,13 @@ pub struct RpmExtractor; impl RpmExtractor { fn install_package(pkg_path: &str, root_path: &str) -> std::io::Result<()> { - let exit_status = RPM.execvp([ "--install", "--allfiles", "--root", root_path, pkg_path ])?; + let exit_status = RPM.execvp([ + "--install", + "--nodeps", + "--allfiles", + "--root", root_path, + pkg_path + ])?; let exit_code = exit_status.exit_code(); if exit_code != 0 { -- Gitee From 09c155f53954f564374cde271d2ecf898e82d28c Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:23:08 +0800 Subject: [PATCH 03/13] sys: remove constants Signed-off-by: renoseven --- build/src/util/sys.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/build/src/util/sys.rs b/build/src/util/sys.rs index 64fc277..1b5dce4 100644 --- a/build/src/util/sys.rs +++ b/build/src/util/sys.rs @@ -1,8 +1,5 @@ use super::fs; -const PROC_SELF_PATH: &str = "/proc/self"; -const PROC_SELF_EXE_PATH: &str = "/proc/self/exe"; - lazy_static::lazy_static! { static ref PROCESS_ID: u32 = SysInitializer::init_process_id(); static ref PROCESS_PATH: String = SysInitializer::init_process_path(); @@ -15,35 +12,33 @@ struct SysInitializer; impl SysInitializer { pub fn init_process_id() -> u32 { fs::stringtify_path( - std::fs::read_link(PROC_SELF_PATH).expect("Get process id failed") + std::fs::read_link("/proc/self").expect("Get process id failed") ).parse::().expect("Parse process id failed") } pub fn init_cpu_num() -> usize { - const SYS_INFO_SPLITER: char = '-'; - const SYS_INFO_ONLINE_CPU_FILE_PATH: &str = "/sys/devices/system/cpu/online"; + let cpu_online_info = fs::read_file_to_string("/sys/devices/system/cpu/online") + .expect("Read cpu number failed"); - let cpu_online_info = fs::read_file_to_string(SYS_INFO_ONLINE_CPU_FILE_PATH).expect("Read cpu number failed"); let max_cpu_id = cpu_online_info - .split(SYS_INFO_SPLITER) + .split('-') .last() .unwrap_or_default() .parse::() - .unwrap_or_default(); + .unwrap_or_default() + 1; // cpu id start from 0 - // cpu id start from 0 - max_cpu_id + 1 + max_cpu_id } pub fn init_process_path() -> String { fs::stringtify_path( - std::fs::read_link(PROC_SELF_EXE_PATH).expect("Read process id failed") + std::fs::read_link("/proc/self/exe").expect("Read process id failed") ) } pub fn init_process_name() -> String { fs::stringtify_path( - std::fs::read_link(PROC_SELF_EXE_PATH).expect("Read process name failed") + std::fs::read_link("/proc/self/exe").expect("Read process name failed") .file_name().expect("Parse process name failed") ) } -- Gitee From 82b27537a614f8523d788d2eb13773cbfe48fc14 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:25:12 +0800 Subject: [PATCH 04/13] build: rename statics to contants Signed-off-by: renoseven --- build/src/cli/args.rs | 2 +- build/src/cli/cli.rs | 2 +- build/src/{statics.rs => constants.rs} | 0 build/src/lib.rs | 2 +- build/src/package/package_info.rs | 2 +- build/src/package/rpm_builder.rs | 2 +- build/src/package/rpm_buildroot.rs | 2 +- build/src/package/rpm_extractor.rs | 2 +- build/src/package/rpm_helper.rs | 2 +- build/src/package/rpm_spec_generator.rs | 2 +- build/src/package/rpm_spec_helper.rs | 2 +- build/src/package/rpm_spec_parser.rs | 2 +- build/src/patch/kpatch_builder.rs | 2 +- build/src/patch/kpatch_helper.rs | 2 +- build/src/patch/patch_helper.rs | 2 +- build/src/patch/patch_info.rs | 2 +- 16 files changed, 15 insertions(+), 15 deletions(-) rename build/src/{statics.rs => constants.rs} (100%) diff --git a/build/src/cli/args.rs b/build/src/cli/args.rs index a91eac1..89f7492 100644 --- a/build/src/cli/args.rs +++ b/build/src/cli/args.rs @@ -1,6 +1,6 @@ use clap::Parser; -use crate::statics::*; +use crate::constants::*; use crate::util::sys; use super::CliPath; diff --git a/build/src/cli/cli.rs b/build/src/cli/cli.rs index c0d9a40..34c6016 100644 --- a/build/src/cli/cli.rs +++ b/build/src/cli/cli.rs @@ -5,7 +5,7 @@ use crate::patch::{PatchInfo, PatchName, PatchType}; use crate::patch::{PatchBuilderFactory, PatchBuilderOptions}; use crate::patch::{PatchHelper, UserPatchHelper, KernelPatchHelper}; -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use super::path::CliPath; diff --git a/build/src/statics.rs b/build/src/constants.rs similarity index 100% rename from build/src/statics.rs rename to build/src/constants.rs diff --git a/build/src/lib.rs b/build/src/lib.rs index 358cc43..3e6ba4e 100644 --- a/build/src/lib.rs +++ b/build/src/lib.rs @@ -1,4 +1,4 @@ -pub mod statics; +pub mod constants; pub mod util; pub mod cmd; pub mod patch; diff --git a/build/src/package/package_info.rs b/build/src/package/package_info.rs index 7682540..9448128 100644 --- a/build/src/package/package_info.rs +++ b/build/src/package/package_info.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use super::rpm_helper::RpmHelper; diff --git a/build/src/package/rpm_builder.rs b/build/src/package/rpm_builder.rs index c375f6d..8cf501a 100644 --- a/build/src/package/rpm_builder.rs +++ b/build/src/package/rpm_builder.rs @@ -1,6 +1,6 @@ use crate::patch::PatchInfo; -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use super::rpm_spec_generator::RpmSpecGenerator; diff --git a/build/src/package/rpm_buildroot.rs b/build/src/package/rpm_buildroot.rs index 4a0631a..1ae4863 100644 --- a/build/src/package/rpm_buildroot.rs +++ b/build/src/package/rpm_buildroot.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use crate::util::fs; pub struct RpmBuildRoot { diff --git a/build/src/package/rpm_extractor.rs b/build/src/package/rpm_extractor.rs index d9c7847..4ab198b 100644 --- a/build/src/package/rpm_extractor.rs +++ b/build/src/package/rpm_extractor.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use super::rpm_buildroot::RpmBuildRoot; diff --git a/build/src/package/rpm_helper.rs b/build/src/package/rpm_helper.rs index 73a2f8d..d6b8ffe 100644 --- a/build/src/package/rpm_helper.rs +++ b/build/src/package/rpm_helper.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use crate::util::fs; pub struct RpmHelper; diff --git a/build/src/package/rpm_spec_generator.rs b/build/src/package/rpm_spec_generator.rs index 1e89765..707ed52 100644 --- a/build/src/package/rpm_spec_generator.rs +++ b/build/src/package/rpm_spec_generator.rs @@ -1,7 +1,7 @@ use std::io::{Write, LineWriter}; use std::ffi::OsStr; -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use crate::patch::PatchInfo; diff --git a/build/src/package/rpm_spec_helper.rs b/build/src/package/rpm_spec_helper.rs index f7aa798..40bbdef 100644 --- a/build/src/package/rpm_spec_helper.rs +++ b/build/src/package/rpm_spec_helper.rs @@ -1,6 +1,6 @@ use std::collections::BTreeSet; -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use crate::patch::PatchInfo; diff --git a/build/src/package/rpm_spec_parser.rs b/build/src/package/rpm_spec_parser.rs index b30af73..0b9da6c 100644 --- a/build/src/package/rpm_spec_parser.rs +++ b/build/src/package/rpm_spec_parser.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; #[derive(PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug)] diff --git a/build/src/patch/kpatch_builder.rs b/build/src/patch/kpatch_builder.rs index 80eb280..181f428 100644 --- a/build/src/patch/kpatch_builder.rs +++ b/build/src/patch/kpatch_builder.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::statics::*; +use crate::constants::*; use super::{PatchBuilder, PatchBuilderOptions}; diff --git a/build/src/patch/kpatch_helper.rs b/build/src/patch/kpatch_helper.rs index ba3e5ec..38b0394 100644 --- a/build/src/patch/kpatch_helper.rs +++ b/build/src/patch/kpatch_helper.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use crate::util::fs; pub struct KernelPatchHelper; diff --git a/build/src/patch/patch_helper.rs b/build/src/patch/patch_helper.rs index 26c114b..e5575c3 100644 --- a/build/src/patch/patch_helper.rs +++ b/build/src/patch/patch_helper.rs @@ -1,4 +1,4 @@ -use crate::statics::*; +use crate::constants::*; use crate::util::fs; pub struct PatchHelper; diff --git a/build/src/patch/patch_info.rs b/build/src/patch/patch_info.rs index 7335722..8458040 100644 --- a/build/src/patch/patch_info.rs +++ b/build/src/patch/patch_info.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::path::Path; -use crate::statics::*; +use crate::constants::*; use crate::util::fs; use crate::cli::CliArguments; -- Gitee From f4e92c81785f6a22da8d4520d3b4ba8edbe5d28b Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:30:19 +0800 Subject: [PATCH 05/13] cmd: save only last stdout / stderr Signed-off-by: renoseven --- build/src/cmd/ext_cmd.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/build/src/cmd/ext_cmd.rs b/build/src/cmd/ext_cmd.rs index 0316c3d..2f2ac96 100644 --- a/build/src/cmd/ext_cmd.rs +++ b/build/src/cmd/ext_cmd.rs @@ -56,8 +56,8 @@ impl ExternCommand<'_> { #[inline(always)] fn execute_command(&self, command: &mut Command) -> std::io::Result { - let mut stdout = String::new(); - let mut stderr = String::new(); + let mut last_stdout = String::new(); + let mut last_stderr = String::new(); let mut child_process = command .stdout(Stdio::piped()) @@ -71,26 +71,25 @@ impl ExternCommand<'_> { let process_stdout = child_process.stdout.as_mut().expect("Pipe stdout failed"); for read_line in BufReader::new(process_stdout).lines() { - let line = read_line?; - - writeln!(writer, "{}", line)?; - stdout.push_str(&line); + last_stdout = read_line?; + writeln!(writer, "{}", last_stdout)?; } let process_stderr = child_process.stderr.as_mut().expect("Pipe stderr failed"); for read_line in BufReader::new(process_stderr).lines() { - let line = read_line?; - - writeln!(writer, "{}", line)?; - stderr.push_str(&line); + last_stderr = read_line?; + writeln!(writer, "{}", last_stderr)?; } let exit_code = child_process.wait()?.code().expect("Get process exit code failed"); writeln!(writer, "Process '{}' ({}) exited, exit_code={}", process_name, process_id, exit_code)?; writeln!(writer)?; - let exit_status = ExternCommandExitStatus { exit_code, stdout, stderr }; - Ok(exit_status) + Ok(ExternCommandExitStatus { + exit_code, + stdout: last_stdout, + stderr: last_stderr, + }) } } -- Gitee From b69c635b9f8e04554d3ae980eca3abde53ec9732 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 15:52:24 +0800 Subject: [PATCH 06/13] fs: implement realpath Signed-off-by: renoseven --- build/src/util/fs.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build/src/util/fs.rs b/build/src/util/fs.rs index 808a6a7..1707027 100644 --- a/build/src/util/fs.rs +++ b/build/src/util/fs.rs @@ -244,6 +244,13 @@ pub fn copy_all_files, Q: AsRef>(src_dir: P, dst_dir: Q) -> Ok(()) } +pub fn realpath>(path: P) -> std::io::Result { + let orig_path = path.as_ref(); + + self::check_exist(orig_path)?; + Ok(orig_path.canonicalize()?) +} + pub fn read_file_to_string>(file_path: P) -> std::io::Result { self::check_file(file_path.as_ref())?; -- Gitee From 284e2ffc3b18d75e958c9c6354b9d7b65ac4d4c8 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:06:47 +0800 Subject: [PATCH 07/13] fs: rename get_file_ext to file_ext Signed-off-by: renoseven --- build/src/patch/patch_info.rs | 2 +- build/src/util/fs.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build/src/patch/patch_info.rs b/build/src/patch/patch_info.rs index 8458040..63a0daf 100644 --- a/build/src/patch/patch_info.rs +++ b/build/src/patch/patch_info.rs @@ -216,7 +216,7 @@ impl PatchInfo { let file_digest = patch_file.get_digest().to_owned(); // Check file extension - if fs::get_file_ext(&file_path)? != PATCH_FILE_EXTENSION { + if fs::file_ext(&file_path)? != PATCH_FILE_EXTENSION { eprintln!("Warning: file '{}' is not a patch", file_path); continue; } diff --git a/build/src/util/fs.rs b/build/src/util/fs.rs index 1707027..d9c6ebc 100644 --- a/build/src/util/fs.rs +++ b/build/src/util/fs.rs @@ -63,7 +63,7 @@ pub fn create_dir_all>(dir_path: P) -> std::io::Result<()> { Ok(()) } -pub fn get_file_ext>(file_path: P) -> std::io::Result { +pub fn file_ext>(file_path: P) -> std::io::Result { let file = file_path.as_ref(); self::check_file(file)?; -- Gitee From 350db1703c40c41af25ce82498b2357af4eccef8 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:08:38 +0800 Subject: [PATCH 08/13] fs: rearrange code Signed-off-by: renoseven --- build/src/util/fs.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/build/src/util/fs.rs b/build/src/util/fs.rs index d9c6ebc..9e38a11 100644 --- a/build/src/util/fs.rs +++ b/build/src/util/fs.rs @@ -63,18 +63,6 @@ pub fn create_dir_all>(dir_path: P) -> std::io::Result<()> { Ok(()) } -pub fn file_ext>(file_path: P) -> std::io::Result { - let file = file_path.as_ref(); - self::check_file(file)?; - - let file_ext = file.extension() - .and_then(OsStr::to_str) - .unwrap_or_default() - .to_string(); - - Ok(file_ext) -} - pub fn list_all_dirs>(directory: P, recursive: bool) -> std::io::Result> { let search_path = directory.as_ref(); @@ -244,6 +232,18 @@ pub fn copy_all_files, Q: AsRef>(src_dir: P, dst_dir: Q) -> Ok(()) } +pub fn file_ext>(file_path: P) -> std::io::Result { + let file = file_path.as_ref(); + self::check_file(file)?; + + let file_ext = file.extension() + .and_then(OsStr::to_str) + .unwrap_or_default() + .to_string(); + + Ok(file_ext) +} + pub fn realpath>(path: P) -> std::io::Result { let orig_path = path.as_ref(); -- Gitee From 8df5d876911b13ab6d676e4309934e2c328b66db Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:19:54 +0800 Subject: [PATCH 09/13] cli: canonicalize arguments before use Signed-off-by: renoseven --- build/src/cli/cli.rs | 35 +++++++++++++++++++++++++---------- build/src/cli/workdir.rs | 12 +++++++----- build/src/patch/patch_info.rs | 12 +++++------- build/src/util/fs.rs | 15 ++++++--------- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/build/src/cli/cli.rs b/build/src/cli/cli.rs index 34c6016..9abb4dd 100644 --- a/build/src/cli/cli.rs +++ b/build/src/cli/cli.rs @@ -25,22 +25,37 @@ impl PatchBuildCLI { } } - fn check_arguments(&self) -> std::io::Result<()> { - let args = &self.cli_args; - match &args.source { - CliPath::File(file_path) => fs::check_file(file_path)?, - CliPath::Directory(dir_path) => fs::check_dir(dir_path)?, - } + fn check_and_canonicalize_arguments(&mut self) -> std::io::Result<()> { + let args = &mut self.cli_args; + + args.source = match &args.source { + CliPath::File(file_path) => { + fs::check_file(file_path)?; + CliPath::File(fs::stringtify_path(fs::realpath(file_path)?)) + }, + CliPath::Directory(dir_path) => { + fs::check_dir(dir_path)?; + CliPath::Directory(fs::stringtify_path(fs::realpath(dir_path)?)) + }, + }; + if let Some(file_path) = &args.debug_info { - fs::check_file(file_path)? + fs::check_file(file_path)?; + args.debug_info = Some(fs::stringtify_path(fs::realpath(file_path)?)); } + if let Some(file_path) = &args.kconfig { fs::check_file(file_path)?; + args.kconfig = Some(fs::stringtify_path(fs::realpath(file_path)?)) } - for file_path in &args.patches { - fs::check_file(file_path)?; + + for patch in &mut args.patches { + fs::check_file(patch.as_str())?; + *patch = fs::stringtify_path(fs::realpath(patch.as_str())?); } + fs::check_dir(&args.output_dir)?; + args.output_dir = fs::stringtify_path(fs::realpath(&args.output_dir)?); Ok(()) } @@ -233,7 +248,7 @@ impl PatchBuildCLI { } pub fn run(&mut self) { - self.check_arguments().expect("Check arguments failed"); + self.check_and_canonicalize_arguments().expect("Check arguments failed"); self.work_dir.create(&self.cli_args.work_dir).expect("Create working directory failed"); diff --git a/build/src/cli/workdir.rs b/build/src/cli/workdir.rs index 2e48554..ce8ebe1 100644 --- a/build/src/cli/workdir.rs +++ b/build/src/cli/workdir.rs @@ -1,5 +1,3 @@ -use std::path::Path; - use crate::util::sys; use crate::util::fs; @@ -11,9 +9,12 @@ struct WorkDir { } impl WorkDir { - pub fn new>(base_dir: P) -> std::io::Result { - let base_dir_path = fs::stringtify_path(base_dir.as_ref().canonicalize()?); - let work_dir = format!("{}/{}.{}", base_dir_path, sys::get_process_name(), sys::get_process_id()); + pub fn new(base_dir: &str) -> std::io::Result { + let process_id = sys::get_process_id(); + let process_name = sys::get_process_name(); + let base_dir_path = fs::realpath(base_dir)?; + + let work_dir = format!("{}/{}.{}", base_dir_path.display(), process_id, process_name); let patch_build_root = format!("{}/patch_root", work_dir); let patch_output_dir = format!("{}/patch_output", patch_build_root); let package_build_root = format!("{}/pkg_root", work_dir); @@ -68,6 +69,7 @@ impl CliWorkDir { impl CliWorkDir { pub fn create(&mut self, base_dir: &str) -> std::io::Result<()> { + fs::check_dir(base_dir)?; self.inner = Some(WorkDir::new(base_dir)?); Ok(()) diff --git a/build/src/patch/patch_info.rs b/build/src/patch/patch_info.rs index 63a0daf..199dd23 100644 --- a/build/src/patch/patch_info.rs +++ b/build/src/patch/patch_info.rs @@ -90,13 +90,11 @@ impl std::fmt::Display for PatchFile { } impl PatchFile { - pub fn new>(file: P) -> std::io::Result { - fs::check_file(&file)?; - - let file_path = file.as_ref().canonicalize()?; - let name = fs::stringtify_path(file_path.file_name().expect("Get patch name failed")); - let path = fs::stringtify_path(file_path.as_path().canonicalize()?); - let digest = fs::sha256_digest_file(file_path)?[..PATCH_VERSION_DIGITS].to_owned(); + pub fn new>(file_path: P) -> std::io::Result { + let file = fs::realpath(file_path)?; + let name = fs::stringtify_path(file.file_name().expect("Get patch name failed")); + let path = fs::stringtify_path(file.as_path()); + let digest = fs::sha256_digest_file(file)?[..PATCH_VERSION_DIGITS].to_owned(); Ok(Self { name, path, digest: digest }) } diff --git a/build/src/util/fs.rs b/build/src/util/fs.rs index 9e38a11..b0d8f2d 100644 --- a/build/src/util/fs.rs +++ b/build/src/util/fs.rs @@ -75,7 +75,7 @@ pub fn list_all_dirs>(directory: P, recursive: bool) -> std::io:: if !current_path.is_dir() { continue; } - dir_list.push(current_path.canonicalize()?); + dir_list.push(self::realpath(current_path.as_path())?); } } @@ -101,10 +101,10 @@ pub fn list_all_files>(directory: P, recursive: bool) -> std::io: let current_path_type = current_path.metadata()?.file_type(); if current_path_type.is_file() { - file_list.push(current_path.canonicalize()?) + file_list.push(self::realpath(current_path.as_path())?); } if current_path_type.is_dir() { - dir_list.push(current_path.canonicalize()?); + dir_list.push(self::realpath(current_path.as_path())?); } } } @@ -133,11 +133,11 @@ pub fn list_all_files_ext>(directory: P, file_ext: &str, recursiv if current_path_type.is_file() { let current_path_ext = current_path.extension().unwrap_or_default(); if current_path_ext == file_ext { - file_list.push(current_path.canonicalize()?); + file_list.push(self::realpath(current_path.as_path())?); } } if current_path_type.is_dir() { - dir_list.push(current_path.canonicalize()?); + dir_list.push(self::realpath(current_path.as_path())?); } } } @@ -245,10 +245,7 @@ pub fn file_ext>(file_path: P) -> std::io::Result { } pub fn realpath>(path: P) -> std::io::Result { - let orig_path = path.as_ref(); - - self::check_exist(orig_path)?; - Ok(orig_path.canonicalize()?) + path.as_ref().canonicalize() } pub fn read_file_to_string>(file_path: P) -> std::io::Result { -- Gitee From 6fa2bbc289165407e136079119d9273b7af11bd6 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:23:28 +0800 Subject: [PATCH 10/13] sys: format imports Signed-off-by: renoseven --- build/src/util/sys.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build/src/util/sys.rs b/build/src/util/sys.rs index 1b5dce4..db0361b 100644 --- a/build/src/util/sys.rs +++ b/build/src/util/sys.rs @@ -1,6 +1,8 @@ +use lazy_static::*; + use super::fs; -lazy_static::lazy_static! { +lazy_static! { static ref PROCESS_ID: u32 = SysInitializer::init_process_id(); static ref PROCESS_PATH: String = SysInitializer::init_process_path(); static ref PROCESS_NAME: String = SysInitializer::init_process_name(); -- Gitee From 55de05d9503a07d90b94182f4678f1d0e912c331 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:27:16 +0800 Subject: [PATCH 11/13] build: format constants Signed-off-by: renoseven --- build/src/constants.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/build/src/constants.rs b/build/src/constants.rs index 60b4d3a..70d7a53 100644 --- a/build/src/constants.rs +++ b/build/src/constants.rs @@ -1,6 +1,6 @@ use crate::cmd::ExternCommand; -// CLI Defaults +// CLI defaults pub const CLI_DEFAULT_PATCH_VERSION: &str = "1"; pub const CLI_DEFAULT_PATCH_SUMMARY: &str = "Syscare Patch"; pub const CLI_DEFAULT_WORK_DIR: &str = "."; @@ -8,35 +8,35 @@ pub const CLI_DEFAULT_OUTPUT_DIR: &str = "."; pub const CLI_DEFAULT_SKIP_COMPILER_CHECK: &str = "false"; // External commands +pub const MAKE: ExternCommand = ExternCommand::new("/usr/bin/make"); pub const RPM: ExternCommand = ExternCommand::new("/usr/bin/rpm"); pub const RPM_BUILD: ExternCommand = ExternCommand::new("/usr/bin/rpmbuild"); -pub const MAKE: ExternCommand = ExternCommand::new("/usr/bin/make"); pub const KPATCH_BUILD: ExternCommand = ExternCommand::new("/usr/bin/kpatch-build"); pub const UPATCH_BUILD: ExternCommand = ExternCommand::new("/usr/bin/upatch-build"); // Patch -pub const PATCH_VERSION_DIGITS: usize = 8; -pub const PATCH_FILE_EXTENSION: &str = "patch"; pub const PATCH_FILE_PREFIX: &str = "syscare-patch"; -pub const PATCH_FILE_INSTALL_PATH: &str = "/usr/lib/syscare/patches"; +pub const PATCH_FILE_EXTENSION: &str = "patch"; pub const PATCH_DIR_PERMISSION: &str = "755"; pub const PATCH_FILE_PERMISSION: &str = "644"; +pub const PATCH_FILE_INSTALL_PATH: &str = "/usr/lib/syscare/patches"; pub const PATCH_UNDEFINED_VALUE: &str = "Undefined"; pub const PATCH_INFO_FILE_NAME: &str = "patch_info"; +pub const PATCH_VERSION_DIGITS: usize = 8; // Package -pub const PKG_FILE_EXTENSION: &str = "rpm"; -pub const PKG_NAME_SPLITER: char = '-'; -pub const PKG_FLAG_SOURCE_PKG: &str = "(none)"; -pub const PKG_FLAG_PATCHED_SOURCE_PKG: &str = "patched"; -pub const PKG_PATCH_VERSION_FILE_NAME: &str = "syscare-patch-version"; -pub const PKG_PATCH_TARGET_FILE_NAME: &str = "syscare-patch-target"; -pub const PKG_SPEC_FILE_EXTENSION: &str = "spec"; -pub const PKG_SPEC_TAG_SPLITER: char = ':'; -pub const PKG_SPEC_TAG_NAME_RELEASE: &str = "Release:"; -pub const PKG_SPEC_TAG_NAME_SOURCE: &str = "Source"; -pub const PKG_SPEC_TAG_NAME_BUILD_REQUIRES: &str = "BuildRequires:"; -pub const PKG_SPEC_TAG_VALUE_GROUP: &str = "Patch"; +pub const PKG_FILE_EXTENSION: &str = "rpm"; +pub const PKG_NAME_SPLITER: char = '-'; +pub const PKG_FLAG_SOURCE_PKG: &str = "(none)"; +pub const PKG_FLAG_PATCHED_SOURCE_PKG: &str = "patched"; +pub const PKG_PATCH_VERSION_FILE_NAME: &str = "syscare-patch-version"; +pub const PKG_PATCH_TARGET_FILE_NAME: &str = "syscare-patch-target"; +pub const PKG_SPEC_FILE_EXTENSION: &str = "spec"; +pub const PKG_SPEC_TAG_SPLITER: char = ':'; +pub const PKG_SPEC_TAG_NAME_RELEASE: &str = "Release:"; +pub const PKG_SPEC_TAG_NAME_SOURCE: &str = "Source"; +pub const PKG_SPEC_TAG_NAME_BUILD_REQUIRES: &str = "BuildRequires:"; +pub const PKG_SPEC_TAG_VALUE_GROUP: &str = "Patch"; // Kernel pub const KERNEL_PKG_NAME: &str = "kernel"; -- Gitee From 18d9464a9582c06cf4f6226194eadb9b9ec521ae Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 18:31:32 +0800 Subject: [PATCH 12/13] patch: change patch installed permission 1. file: 644 -> 640 2. directory: 755 -> 750 Signed-off-by: renoseven --- build/src/constants.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/src/constants.rs b/build/src/constants.rs index 70d7a53..5b06edc 100644 --- a/build/src/constants.rs +++ b/build/src/constants.rs @@ -17,8 +17,8 @@ pub const UPATCH_BUILD: ExternCommand = ExternCommand::new("/usr/bin/upatch-buil // Patch pub const PATCH_FILE_PREFIX: &str = "syscare-patch"; pub const PATCH_FILE_EXTENSION: &str = "patch"; -pub const PATCH_DIR_PERMISSION: &str = "755"; -pub const PATCH_FILE_PERMISSION: &str = "644"; +pub const PATCH_DIR_PERMISSION: &str = "750"; +pub const PATCH_FILE_PERMISSION: &str = "640"; pub const PATCH_FILE_INSTALL_PATH: &str = "/usr/lib/syscare/patches"; pub const PATCH_UNDEFINED_VALUE: &str = "Undefined"; pub const PATCH_INFO_FILE_NAME: &str = "patch_info"; -- Gitee From 07b28d5c72863fb5b0fef68ff67501f73eb8a1d7 Mon Sep 17 00:00:00 2001 From: renoseven Date: Thu, 17 Nov 2022 21:45:05 +0800 Subject: [PATCH 13/13] package: fix 'patched source rpm spec has duplicated source tag' issue Signed-off-by: renoseven --- build/src/package/rpm_spec_helper.rs | 57 +++++++++++++++++----------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/build/src/package/rpm_spec_helper.rs b/build/src/package/rpm_spec_helper.rs index 40bbdef..97f4396 100644 --- a/build/src/package/rpm_spec_helper.rs +++ b/build/src/package/rpm_spec_helper.rs @@ -1,6 +1,7 @@ use std::collections::BTreeSet; use crate::constants::*; +use crate::util::sys; use crate::util::fs; use crate::patch::PatchInfo; @@ -29,35 +30,49 @@ impl RpmSpecHelper { } fn create_new_source_tags(start_tag_id: usize, patch_info: &PatchInfo) -> Vec { - let mut source_tag_list = Vec::new(); + let tag_name = PKG_SPEC_TAG_NAME_SOURCE; - let tag_name = PKG_SPEC_TAG_NAME_SOURCE.to_owned(); + let mut source_tag_list = Vec::new(); let mut tag_id = start_tag_id + 1; + let mut is_patched_pkg = false; + for patch_file in patch_info.get_file_list() { + // File path contains pid (in workdir) means some of patches are come from source package + match patch_file.get_path().contains(&sys::get_process_id().to_string()) { + true => { + // Exclude patches from patched source package + // and leave a flag to identify this + is_patched_pkg = true; + }, + false => { + source_tag_list.push(RpmSpecTag::new_id_tag( + tag_name.to_owned(), + tag_id, + patch_file.get_name().to_owned() + )); + } + } + + tag_id += 1; + } + + // If the package is patched, generate files to record + // patch target name and patch version + if !is_patched_pkg { source_tag_list.push(RpmSpecTag::new_id_tag( tag_name.to_owned(), tag_id, - patch_file.get_name().to_owned() + PKG_PATCH_VERSION_FILE_NAME.to_owned() )); tag_id += 1; - } - // Add patch version file - source_tag_list.push(RpmSpecTag::new_id_tag( - tag_name.to_owned(), - tag_id, - PKG_PATCH_VERSION_FILE_NAME.to_owned() - )); - tag_id += 1; - - // Add patch target file - source_tag_list.push(RpmSpecTag::new_id_tag( - tag_name.to_owned(), - tag_id, - PKG_PATCH_TARGET_FILE_NAME.to_owned() - )); - // tag_id += 1; + source_tag_list.push(RpmSpecTag::new_id_tag( + tag_name.to_owned(), + tag_id, + PKG_PATCH_TARGET_FILE_NAME.to_owned() + )); + } source_tag_list } @@ -79,15 +94,14 @@ impl RpmSpecHelper { if release_tag.is_none() { if let Some(tag) = RpmSpecParser::parse_tag(¤t_line, PKG_SPEC_TAG_NAME_RELEASE) { release_tag = Some((current_line_num, tag)); - current_line_num += 1; continue; // Since parsed release tag, the other tag would not be parsed } } + // Add parsed source tag into the btree set if let Some(tag) = RpmSpecParser::parse_parse_id_tag(¤t_line, PKG_SPEC_TAG_NAME_SOURCE) { source_tags.insert(tag); - current_line_num += 1; continue; } @@ -105,7 +119,6 @@ impl RpmSpecHelper { // Modify 'Release' tag if let Some((line_num, tag)) = Self::create_new_release_tag(release_tag.unwrap(), patch_info) { - // lines_to_write.insert((line_num, tag.to_string())); spec_file_content.remove(line_num); spec_file_content.insert(line_num, tag.to_string()); } -- Gitee