chore: 优化获得文件hash的处理方式

This commit is contained in:
2025-11-15 18:34:28 +08:00
parent c0252a2dd2
commit 84c4933ece
3 changed files with 49 additions and 28 deletions

View File

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