feat: Enhance Linux ID modifier with advanced file modification checks

- Improved random ID generation to ensure single-line output
- Added chattr attribute removal for immutable files before modification
- Enhanced file modification reliability by handling read-only file attributes
- Implemented pre-modification checks to prevent potential configuration errors
This commit is contained in:
煎饼果子卷鲨鱼辣椒 2025-02-19 11:19:29 +08:00
parent e8e37b3f89
commit 644d663a4e

View File

@ -152,8 +152,8 @@ backup_config() {
# 生成随机 ID # 生成随机 ID
generate_random_id() { generate_random_id() {
# 生成32字节(64个十六进制字符)的随机数 # 生成32字节(64个十六进制字符)的随机数,并确保一行输出
head -c 32 /dev/urandom | xxd -p head -c 32 /dev/urandom | xxd -p -c 32
} }
# 生成随机 UUID # 生成随机 UUID
@ -176,6 +176,15 @@ modify_or_add_config() {
return 1 return 1
fi fi
# 检查并移除chattr只读属性如果存在
if lsattr "$file" 2>/dev/null | grep -q '^....i'; then
log_debug "移除文件不可变属性..."
sudo chattr -i "$file" || {
log_error "无法移除文件不可变属性"
return 1
}
fi
# 确保文件可写 # 确保文件可写
chmod 644 "$file" || { chmod 644 "$file" || {
log_error "无法修改文件权限: $file" log_error "无法修改文件权限: $file"