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

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