Skip to content

Commit

Permalink
core: 完善文件类型检查
Browse files Browse the repository at this point in the history
  • Loading branch information
bin456789 committed Sep 7, 2024
1 parent 785c813 commit 9fa03c1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 43 deletions.
102 changes: 60 additions & 42 deletions reinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ test_url_grace() {
test_url_real() {
grace=$1
url=$2
expect_type_combo=$3
expect_types=$3
var_to_eval=$4
info test url

Expand Down Expand Up @@ -269,56 +269,74 @@ test_url_real() {
fi
done

if [ -n "$expect_type_combo" ]; then
fix_file_type() {
# gzip的mime有很多种写法
# centos7中显示为 x-gzip,在其他系统中显示为 gzip,可能还有其他
# 所以不用mime判断
# https://www.digipres.org/formats/sources/tika/formats/#application/gzip
# 如果要检查文件类型
if [ -n "$expect_types" ]; then
install_pkg file
real_type=$(file_enhanced $tmp_file)
echo "$real_type"

# 有些 file 版本输出的是 # ISO 9660 CD-ROM filesystem data ,要去掉开头的井号
# 期待值没有.表示要只需判断外侧
if ! grep -Fq . <<<"$expect_types"; then
real_type=$(echo "$real_type" | cut -d. -f2-)
fi

# 下面两种都是 raw
# DOS/MBR boot sector
# x86 boot sector; partition 1: ...
sed 's/^# //' | cut -d' ' -f1 | to_lower |
sed -e 's,dos/mbr,raw,' -e 's,x86,raw,'
}
# 检查
if ! grep -Foq "|$real_type|" <<<"|$expect_types|"; then
failed "$url
expected: $expect_types
actually: $real_type"
fi
fi

file_enhanced() {
file_type=$(file -b $tmp_file | fix_file_type)
if [ "$file_type" = "xz" ] || [ "$file_type" = "gzip" ]; then
# 要安装 xz 或者 gzip,不然会报错
# ERROR:[xz: Wait failed, No child process]
install_pkg "$file_type"

# 加 if 是为了避免以下情况(外面是xz,但是识别不到里面的东西,即使装了xz),
# 即使 file 报错返回值也是 0
# [root@localhost ~]# file -bZ /reinstall-tmp/img-test
# ERROR:[xz: Unexpected end of input]
if file_type_in="$(file -bZ $tmp_file | fix_file_type)" &&
! grep -iq "^Error" <<<"$file_type_in"; then
file_type="$file_type_in.$file_type"
fi
fi
echo "$file_type"
}
# 如果要设置变量
if [ -n "$var_to_eval" ]; then
IFS=. read -r "${var_to_eval?}" "${var_to_eval}_warp" <<<"$real_type"
fi
}

install_pkg file
real_type_combo=$(file_enhanced $tmp_file)
echo "$real_type_combo"
fix_file_type() {
# gzip的mime有很多种写法
# centos7中显示为 x-gzip,在其他系统中显示为 gzip,可能还有其他
# 所以不用mime判断
# https://www.digipres.org/formats/sources/tika/formats/#application/gzip

if ! grep -Foq "|$real_type_combo|" <<<"|$expect_type_combo|"; then
failed "$url expected: $expect_type_combo. actual: $real_type_combo."
fi
# --extension 不靠谱
# file -b /reinstall-tmp/img-test --mime-type
# application/x-qemu-disk
# file -b /reinstall-tmp/img-test --extension
# ???

# 有些 file 版本输出的是 # ISO 9660 CD-ROM filesystem data ,要去掉开头的井号

IFS=. read -r real_type real_type_warp <<<"$real_type_combo"
# 下面两种都是 raw
# DOS/MBR boot sector
# x86 boot sector; partition 1: ...

sed 's/^# //' | awk '{print $1}' | to_lower |
sed -e 's,dos/mbr,raw,' -e 's,x86,raw,'
}

if [ -n "$var_to_eval" ]; then
eval "$var_to_eval=$real_type"
eval "$var_to_eval""_warp=$real_type_warp"
file_enhanced() {
local file=$1
local outside inside

outside=$(file -b $file | fix_file_type)

if [ "$outside" = "xz" ] || [ "$outside" = "gzip" ]; then
# 要安装 xz 或者 gzip,不然会报错
# ERROR:[xz: Wait failed, No child process]
install_pkg "$outside"

# 加 if 是为了避免以下情况(外面是xz,但是识别不到里面的东西,即使装了xz),
# 即使 file 报错返回值也是 0
# [root@localhost ~]# file -bZ /reinstall-tmp/img-test
# ERROR:[xz: Unexpected end of input]
if inside="$(file -bZ $file | fix_file_type)" && ! grep -iq "^Error" <<<"$inside"; then
echo "$inside.$outside"
return
fi
fi
echo "$outside"
}

add_community_repo_for_alpine() {
Expand Down
6 changes: 5 additions & 1 deletion trans.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3700,7 +3700,9 @@ install_windows() {
esac
# 排除 $PLUGINSDIR $TEMP
exclude='$*'
7z x $drv/Citrix_xensetup.exe -o$drv/aws/ -ao$override -x!$exclude
7z x $drv/Citrix_xensetup.exe -o$drv/xen/ -ao$override -x!$exclude

cp_drivers $drv/xen
}

# aws xen
Expand Down Expand Up @@ -3743,6 +3745,8 @@ install_windows() {
download https://xenbits.xenproject.org/pvdrivers/win/$part.tar $drv/$part.tar
tar -xf $drv/$part.tar -C $drv/xen/
done

cp_drivers $drv/xen
}

# kvm
Expand Down

0 comments on commit 9fa03c1

Please sign in to comment.