chore: 能够通过比较文件内容,处理包

This commit is contained in:
2025-11-17 09:39:11 +08:00
parent 84c4933ece
commit 27f10afd78
3 changed files with 73 additions and 20 deletions

View File

@@ -68,15 +68,42 @@ 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用于签名验证

View File

@@ -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 # 日志文件备份数量

View File

@@ -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>"