From 84c4933eceff67cccac9ca0a7e14ae5c43450ae4 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Sat, 15 Nov 2025 18:34:28 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E8=8E=B7=E5=BE=97?= =?UTF-8?q?=E6=96=87=E4=BB=B6hash=E7=9A=84=E5=A4=84=E7=90=86=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/patch_tools/docker-compose.yml | 4 +- scripts/patch_tools/patch_config.sh | 5 +- scripts/patch_tools/patch_generator.sh | 68 +++++++++++++++++--------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/scripts/patch_tools/docker-compose.yml b/scripts/patch_tools/docker-compose.yml index 12c69ec51..fa3a0066a 100644 --- a/scripts/patch_tools/docker-compose.yml +++ b/scripts/patch_tools/docker-compose.yml @@ -12,5 +12,5 @@ services: volumes: - ./:/working_dir/scripts - ../patch-dir:/working_dir/patchs - - ../../ftp-src/app:/working_dir/old-shop-src - - ../../src/app:/working_dir/new-shop-src + - ../../ftp-src/config:/working_dir/old-shop-src + - ../../src/config:/working_dir/new-shop-src diff --git a/scripts/patch_tools/patch_config.sh b/scripts/patch_tools/patch_config.sh index 9646c6822..d4393aa83 100644 --- a/scripts/patch_tools/patch_config.sh +++ b/scripts/patch_tools/patch_config.sh @@ -73,6 +73,7 @@ HASH_ALGORITHM="sha256" # 哈希算法,用于文件内容比较 TIME_PRECISION="second" # 时间精度,用于文件时间比较 COMPARE_PERMISSIONS=false # 是否比较文件权限 COMPARE_OWNERSHIP=false # 是否比较文件所有者 +IGNORE_LINE_ENDINGS=true # 是否忽略换行符格式不同时(比如Windows的 \r\n vs Linux的 \n) # ============================================================================== # 增量配置 - 定义是否启用增量补丁 @@ -163,9 +164,9 @@ NAMING_PATTERN="patch-{name}-{version}-{timestamp}-{git_commit}.{format}" # 文 # ============================================================================== # 日志配置 -LOG_LEVEL="INFO" # 日志级别,DEBUG, INFO, WARN, ERROR +LOG_LEVEL="TRACE" # 日志级别,DEBUG, INFO, WARN, ERROR, TRACE; DEBUG 会开启终端调试输出,TRACE 只会开启详细日志输出 LOG_FILE="/var/log/patch_system/patch.log" # 日志文件路径 -LOG_MAX_SIZE="100MB" # 日志文件最大大小 +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 0bd80b858..bc1163034 100644 --- a/scripts/patch_tools/patch_generator.sh +++ b/scripts/patch_tools/patch_generator.sh @@ -31,6 +31,7 @@ log() { "WARN") color="$YELLOW" ;; "ERROR") color="$RED" ;; "DEBUG") color="$BLUE" ;; + "TRACE") color="$PURPLE" ;; *) color="$NC" ;; esac @@ -40,6 +41,9 @@ log() { "DEBUG") # DEBUG级别:输出所有日志 ;; + "TRACE") + # TRACE级别:输出所有日志 + ;; "INFO") # INFO级别:只输出INFO、WARN和ERROR日志 if [[ "$level" == "DEBUG" ]]; then @@ -67,10 +71,12 @@ log() { echo -e "${color}[$timestamp] [$level]${NC} $message" | tee -a "$LOG_FILE" } +debug() { log "DEBUG" "$1"; } +trace() { log "TRACE" "$1"; } info() { log "INFO" "$1"; } warn() { log "WARN" "$1"; } error() { log "ERROR" "$1"; } -debug() { log "DEBUG" "$1"; } + # 配置加载 load_config() { @@ -97,6 +103,7 @@ load_config() { : ${COMPARISON_METHOD:="both"} : ${TIME_PRECISION:="second"} : ${LONG_PATH_SUPPORT:="true"} + : ${IGNORE_LINE_ENDINGS:="false"} source "$CONFIG_FILE" info "配置文件加载完成" @@ -109,13 +116,8 @@ setup_environment() { mkdir -p "$OUTPUT_DIRECTORY" mkdir -p "$TEMP_DIR" - # 设置日志级别 - case "$LOG_LEVEL" in - "DEBUG") set -x ;; - "INFO") ;; - "WARN") ;; - "ERROR") ;; - esac + # 注意:不在环境设置中启用调试模式,以免影响日志输出 + # 如果需要详细调试,可以在脚本开头手动设置 set -x info "环境设置完成" info "日志文件: $LOG_FILE" @@ -126,7 +128,7 @@ setup_environment() { # 清理函数 cleanup() { if [[ -d "$TEMP_DIR" ]]; then - # rm -rf "$TEMP_DIR" + rm -rf "$TEMP_DIR" info "临时目录已清理: $TEMP_DIR" fi } @@ -173,16 +175,25 @@ parse_size() { esac } + get_file_hash() { local file_path="$1" local algorithm="${2:-sha256}" - + + # 对复合形式的HASH要做处理,只比较内容,不比较时间戳,权限等 + case "$algorithm" in - "md5") md5sum "$file_path" | cut -d' ' -f1 ;; - "sha1") sha1sum "$file_path" | cut -d' ' -f1 ;; - "sha256") sha256sum "$file_path" | cut -d' ' -f1 ;; - *) sha256sum "$file_path" | cut -d' ' -f1 ;; - esac + "md5") + cat "$file_path" | tr -d '\r\n' | md5sum | cut -d' ' -f1 | cut -d'|' -f6;; + "sha1") + cat "$file_path" | tr -d '\r\n' | sha1sum | cut -d' ' -f1 | cut -d'|' -f6;; + "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;; + *) + cat "$file_path" | tr -d '\r\n' | sha256sum | cut -d' ' -f1 | cut -d'|' -f6;; + esac } get_file_info() { @@ -226,7 +237,7 @@ should_include_file() { for pattern in "${INCLUDE_PATTERNS[@]}"; do if [[ "$file_path" == $pattern ]] || [[ "$file_path" =~ $pattern ]]; then include_match=true - debug "文件匹配包含模式<$pattern>: $file_path" + trace "文件匹配包含模式<$pattern>: $file_path" break fi done @@ -381,6 +392,15 @@ compare_files() { local output_file="$3" info "开始比较文件差异" + + # 输出差异比较规则 + info "比较规则: " + info "比较方法配置: $COMPARISON_METHOD" + info "哈希算法配置: $HASH_ALGORITHM" + info "时间精度:$TIME_PRECISION" + info "是否忽略换行符不同: $IGNORE_LINE_ENDINGS" + + declare -A old_files declare -A new_files @@ -427,7 +447,7 @@ compare_files() { case "$COMPARISON_METHOD" in "content") [[ "$old_hash" != "$new_hash" ]] && is_modified=true - debug "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" + info "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" ;; "time") IFS='|' read -r old_size old_mtime old_ctime old_perm old_uid old_gid <<< "$old_info" @@ -435,10 +455,10 @@ compare_files() { if [[ "$TIME_PRECISION" == "second" ]]; then [[ $old_mtime -ne $new_mtime ]] && is_modified=true - debug "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" else [[ $(echo "$old_mtime != $new_mtime" | bc) -eq 1 ]] && is_modified=true - debug "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" fi ;; "both") @@ -447,13 +467,13 @@ compare_files() { if [[ "$old_hash" != "$new_hash" ]]; then is_modified=true - debug "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" + trace "检测到修改文件: $path | 哈希值变化: <$old_hash> => <$new_hash>" elif [[ "$TIME_PRECISION" == "second" ]] && [[ $old_mtime -ne $new_mtime ]]; then is_modified=true - debug "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" elif [[ "$TIME_PRECISION" == "millisecond" ]] && [[ $(echo "$old_mtime != $new_mtime" | bc) -eq 1 ]]; then is_modified=true - debug "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" + trace "检测到修改文件: $path | 时间变化: <$old_mtime> => <$new_mtime>" fi ;; esac @@ -708,7 +728,7 @@ generate_rollback_package() { mkdir -p "$dest_dir" if handle_long_path_copy "$source_file" "$dest_file"; then rollback_files+=("$path") - debug "回滚文件: $source_file" + trace "回滚文件: $source_file" else warn "无法复制回滚文件: $source_file" fi @@ -905,7 +925,7 @@ generate_patch() { mkdir -p "$dest_dir" if handle_long_path_copy "$source_file" "$dest_file"; then ((copied_count++)) - debug "复制文件: $source_file -> $dest_file" + trace "复制文件: $source_file -> $dest_file" else warn "文件复制失败: $source_file" fi