From 27f10afd788664d90784ce1b6e24336a435abe02 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 17 Nov 2025 09:39:11 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=83=BD=E5=A4=9F=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9=EF=BC=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/patch_tools/install_patch_system.sh | 33 ++++++++++-- scripts/patch_tools/patch_config.sh | 2 +- scripts/patch_tools/patch_generator.sh | 58 +++++++++++++++------ 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/scripts/patch_tools/install_patch_system.sh b/scripts/patch_tools/install_patch_system.sh index 88ae022ab..026182ad9 100644 --- a/scripts/patch_tools/install_patch_system.sh +++ b/scripts/patch_tools/install_patch_system.sh @@ -67,16 +67,43 @@ install_dependencies() { local sudo_prefix sudo_prefix=$(get_cmd_prefix) + + + local will_install_dependencies=false + local dependencies=( + "tar" + "gzip" + "jq" + "gpg" + "bc" + ) + + for dep in "${dependencies[@]}"; do + if command -v "$dep" >/dev/null 2>&1; then + info "系统依赖 $dep 已安装" + else + warn "系统依赖 $dep 未安装" + will_install_dependencies=true + fi + done + + if ! $will_install_dependencies; then + info "系统依赖已安装" + return 0 + fi + + # 关键依赖 + local keys_deps = " coreutils findutils util-linux " if command -v apt-get >/dev/null 2>&1; then # Debian/Ubuntu $sudo_prefix apt-get update - $sudo_prefix apt-get install -y tar gzip jq coreutils findutils util-linux bc + $sudo_prefix apt-get install -y $keys_deps $(printf "%s " "${dependencies[@]}") elif command -v yum >/dev/null 2>&1; then # CentOS/RHEL - $sudo_prefix yum install -y tar gzip jq coreutils findutils util-linux bc + $sudo_prefix yum install -y $keys_deps $(printf "%s " "${dependencies[@]}") else - warn "无法自动安装依赖,请手动安装: tar, gzip, jq, coreutils, findutils, util-linux, bc" + warn "无法自动安装依赖,请手动安装: $keys_deps $(printf "%s " "${dependencies[@]}")" fi # 安装GPG(用于签名验证) diff --git a/scripts/patch_tools/patch_config.sh b/scripts/patch_tools/patch_config.sh index d4393aa83..7b8e7c1ce 100644 --- a/scripts/patch_tools/patch_config.sh +++ b/scripts/patch_tools/patch_config.sh @@ -164,7 +164,7 @@ NAMING_PATTERN="patch-{name}-{version}-{timestamp}-{git_commit}.{format}" # 文 # ============================================================================== # 日志配置 -LOG_LEVEL="TRACE" # 日志级别,DEBUG, INFO, WARN, ERROR, TRACE; DEBUG 会开启终端调试输出,TRACE 只会开启详细日志输出 +LOG_LEVEL="INFO" # 日志级别,DEBUG, INFO, WARN, ERROR, TRACE; DEBUG 会开启终端调试输出,TRACE 只会开启详细日志输出 LOG_FILE="/var/log/patch_system/patch.log" # 日志文件路径 LOG_MAX_SIZE="10MB" # 日志文件最大大小 LOG_BACKUP_COUNT=10 # 日志文件备份数量 diff --git a/scripts/patch_tools/patch_generator.sh b/scripts/patch_tools/patch_generator.sh index bc1163034..892812421 100644 --- a/scripts/patch_tools/patch_generator.sh +++ b/scripts/patch_tools/patch_generator.sh @@ -46,13 +46,13 @@ log() { ;; "INFO") # INFO级别:只输出INFO、WARN和ERROR日志 - if [[ "$level" == "DEBUG" ]]; then + if [[ "$level" == "DEBUG" ]] || [[ "$level" == "TRACE" ]]; then return 0 fi ;; "WARN") # WARN级别:只输出WARN和ERROR日志 - if [[ "$level" == "DEBUG" ]] || [[ "$level" == "INFO" ]]; then + if [[ "$level" == "DEBUG" ]] || [[ "$level" == "TRACE" ]] || [[ "$level" == "INFO" ]]; then return 0 fi ;; @@ -137,7 +137,7 @@ trap cleanup EXIT # 依赖检查 check_dependencies() { - local deps=("tar" "gzip" "find" "stat" "sha256sum" "date" "mkdir" "cp") + local deps=("tar" "gzip" "jq" "find" "stat" "sha256sum" "date" "mkdir" "cp" "bc") local missing=() for dep in "${deps[@]}"; do @@ -180,19 +180,37 @@ get_file_hash() { local file_path="$1" local algorithm="${2:-sha256}" - # 对复合形式的HASH要做处理,只比较内容,不比较时间戳,权限等 + # 根据 $IGNORE_LINE_ENDINGS 是否为true,决定是否忽略行尾的换行符 + local cmd_tr="" + if [[ "$IGNORE_LINE_ENDINGS" == "true" ]]; then + cmd_tr="tr -d '\r\n'" + fi case "$algorithm" in "md5") - cat "$file_path" | tr -d '\r\n' | md5sum | cut -d' ' -f1 | cut -d'|' -f6;; + if [[ -n "$cmd_tr" ]]; then + cat "$file_path" | $cmd_tr | md5sum | cut -d' ' -f1 + else + cat "$file_path" | md5sum | cut -d' ' -f1 + fi;; "sha1") - cat "$file_path" | tr -d '\r\n' | sha1sum | cut -d' ' -f1 | cut -d'|' -f6;; + if [[ -n "$cmd_tr" ]]; then + cat "$file_path" | $cmd_tr | sha1sum | cut -d' ' -f1 + else + cat "$file_path" | sha1sum | cut -d' ' -f1 + fi;; "sha256") - cat "$file_path" | tr -d '\r\n' | sha256sum | cut -d' ' -f1 | cut -d'|' -f6;; - "sha512") - cat "$file_path" | tr -d '\r\n' | sha512sum | cut -d' ' -f1 | cut -d'|' -f6;; + if [[ -n "$cmd_tr" ]]; then + cat "$file_path" | $cmd_tr | sha256sum | cut -d' ' -f1 + else + cat "$file_path" | sha256sum | cut -d' ' -f1 + fi;; *) - cat "$file_path" | tr -d '\r\n' | sha256sum | cut -d' ' -f1 | cut -d'|' -f6;; + if [[ -n "$cmd_tr" ]]; then + cat "$file_path" | $cmd_tr | sha256sum | cut -d' ' -f1 + else + cat "$file_path" | sha256sum | cut -d' ' -f1 + fi;; esac } @@ -443,11 +461,15 @@ compare_files() { IFS='|' read -r new_info new_hash <<< "${new_files[$path]}" local is_modified=false + local old_short_hash="${old_hash##*|}" # 使用短哈希值,不使用复合哈希值,因为复合哈希值包含权限,用户和组信息, + local new_short_hash="${new_hash##*|}" # 使用短哈希值,不使用复合哈希值,因为复合哈希值包含权限,用户和组信息, case "$COMPARISON_METHOD" in "content") - [[ "$old_hash" != "$new_hash" ]] && is_modified=true - info "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" + [[ "$old_short_hash" != "$new_short_hash" ]] && is_modified=true + if $is_modified; then + trace "检测到修改文件: $path | 哈希值变化: <$old_short_hash> => <$new_short_hash>" + fi ;; "time") IFS='|' read -r old_size old_mtime old_ctime old_perm old_uid old_gid <<< "$old_info" @@ -455,19 +477,23 @@ compare_files() { if [[ "$TIME_PRECISION" == "second" ]]; then [[ $old_mtime -ne $new_mtime ]] && is_modified=true - trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + if $is_modified; then + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + fi else [[ $(echo "$old_mtime != $new_mtime" | bc) -eq 1 ]] && is_modified=true - trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + if $is_modified; then + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + fi fi ;; "both") IFS='|' read -r old_size old_mtime old_ctime old_perm old_uid old_gid <<< "$old_info" IFS='|' read -r new_size new_mtime new_ctime new_perm new_uid new_gid <<< "$new_info" - if [[ "$old_hash" != "$new_hash" ]]; then + if [[ "$old_short_hash" != "$new_short_hash" ]]; then is_modified=true - trace "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" + trace "检测到修改文件: $path | 哈希值变化: <$old_short_hash> => <$new_short_hash>" elif [[ "$TIME_PRECISION" == "second" ]] && [[ $old_mtime -ne $new_mtime ]]; then is_modified=true trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>"