if [ -z "$local_abi" ]; then echo"Cannot find local AppArmor ABI under /etc/apparmor.d/abi; refusing to fetch upstream profile." >&2 exit 1 fi
if dpkg --compare-versions "$local_abi" lt 4.0; then echo"Local AppArmor ABI is $local_abi, which is too old for bwrap-userns-restrict/userns policy; aborting." >&2 exit 1 fi
if ! wget -qO "$tmp""$url"; then echo"Failed to fetch bwrap-userns-restrict from branch ${branch}." >&2 echo"Do not fall back to master automatically; check the AppArmor version/branch manually." >&2 exit 1 fi fi
# 简单检查一下下载/复制到的文件确实像 bwrap-userns-restrict profile grep -qE '^[[:space:]]*profile[[:space:]]+bwrap[[:space:]]+/usr/bin/bwrap'"$tmp" || { echo"File does not look like bwrap-userns-restrict: missing bwrap profile." >&2 exit 1 }
grep -qE '^[[:space:]]*profile[[:space:]]+unpriv_bwrap[[:space:]]'"$tmp" || { echo"File does not look like bwrap-userns-restrict: missing unpriv_bwrap profile." >&2 exit 1 }
# 兜底处理 “Could not open 'abi/5.0'” 这类问题。 # 正常情况下,按本机 ABI 选择 branch 后不应该触发; # 但如果 profile 里声明的 abi 版本本机不存在,就把它改成本机实际存在的最高 ABI。 local_abi="$(detect_local_abi)" policy_abi="$( sed -nE 's/^[[:space:]]*abi[[:space:]]+<abi\/([^>]+)>[[:space:]]*,[[:space:]]*$/\1/p' "$tmp" \ | head -n1 || true )"
if [ -n "$policy_abi" ] && [ ! -e "/etc/apparmor.d/abi/$policy_abi" ]; then if [ -z "$local_abi" ]; then echo"Profile requires abi/$policy_abi, but no local ABI file was found." >&2 exit 1 fi
echo"Profile requires abi/$policy_abi, but local system has abi/$local_abi; rewriting ABI line." sed -i -E "0,/^[[:space:]]*abi[[:space:]]+<abi\/[^>]+>[[:space:]]*,[[:space:]]*$/s//abi <abi\/${local_abi}>,/""$tmp" fi