diff --git a/src/scripts/tools/ppa.sh b/src/scripts/tools/ppa.sh index aa4566dd..f06a4d5b 100644 --- a/src/scripts/tools/ppa.sh +++ b/src/scripts/tools/ppa.sh @@ -46,6 +46,13 @@ update_lists_helper() { fi } +# Search active APT files, including symlinks, without descending into directories. +grep_sources() ( + shopt -s nullglob + local files=("$list_dir"/*.list "$list_dir"/*.sources) + (( ${#files[@]} )) && grep -d skip -D skip "$@" -- "${files[@]}" +) + # Function to update the package lists. update_lists() { local ppa=${1:-} @@ -56,7 +63,7 @@ update_lists() { if [ -f "$ppa_search" ]; then list="$ppa_search" else - list="$(grep -Elr "$ppa_search" "$list_dir" 2>/dev/null | head -n 1)" + list="$(grep_sources -El -e "$ppa_search" 2>/dev/null | head -n 1)" fi [ -n "$list" ] || return 1 status_file="/tmp/os_lists_refresh_${list##*/}" @@ -226,10 +233,10 @@ handle_existing_list() { echo "Repository $ppa ($branches) already exists" && return 1 fi if merged_components="$(merge_components_from_file "$check_lists_file" "$branches")"; then - sudo rm -f "$check_lists_file" && printf '%s\n' "$merged_components" && return 0 + printf '%s\n' "$merged_components" && return 0 fi if [[ "$list_format" = "deb822" && "$check_lists_file" = *.list ]]; then - sudo rm -f "$check_lists_file" && printf '%s\n' "$branches" && return 0 + printf '%s\n' "$branches" && return 0 fi echo "Repository $ppa ($branches) already exists" && return 1 } @@ -243,19 +250,23 @@ write_list() { local components=$5 local key_file=$6 local list_basename="${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$suite" - local arch by_hash= + local arch list dest by_hash= arch="$(dpkg --print-architecture)" case "${url%/}" in "$ppa_sp/sury"|"$ppa_sp/apache2") by_hash=' by-hash=force' ;; esac - sudo rm -f "$list_dir"/"${ppa/\//-}".list "$list_dir"/"${ppa/\//-}".sources "$list_dir"/"$list_basename".list "$list_dir"/"$list_basename".sources || true + dest="$list_dir/$list_basename.$(get_sources_extension "$type")" + for list in "$list_dir/${ppa/\//-}".{list,sources} "$list_dir/$list_basename".{list,sources}; do + [[ "$list" = "$dest" ]] || remove_list_entry "$list" "$url" || return 1 + done + preserve_list "$dest" "$url" || return 1 if [ "$type" = "deb822" ]; then { printf '%s\n' "Types: deb" "URIs: $url" "Suites: $suite" "Components: $components" "Architectures: $arch" "Signed-By: $key_file" if [ -n "$by_hash" ]; then printf '%s\n' 'By-Hash: force'; fi - } | sudo tee "$list_dir"/"$list_basename".sources >/dev/null + } | sudo tee "$dest" >/dev/null else - echo "deb [arch=$arch signed-by=$key_file$by_hash] $url $suite $components" | sudo tee "$list_dir"/"$list_basename".list >/dev/null 2>&1 + echo "deb [arch=$arch signed-by=$key_file$by_hash] $url $suite $components" | sudo tee "$dest" >/dev/null 2>&1 fi } @@ -266,7 +277,7 @@ check_lists() { local match_file= check_lists_file= if [ -n "$pattern" ]; then - match_file=$(grep -Elr "$pattern" "$list_dir" 2>/dev/null | head -n 1) + match_file=$(grep_sources -El -e "$pattern" 2>/dev/null | head -n 1) fi if [ -n "$match_file" ]; then check_lists_file="$match_file" @@ -306,6 +317,7 @@ add_list() { fi [ -e "$key_source" ] && key_file=$key_source || key_file="$key_dir"/"${ppa/\//-}"-keyring.gpg add_key "$ppa" "$ppa_url" "$package_dist" "$key_source" "$key_file" "${6:-}" || return 1 + [[ -z "$list_path" ]] || remove_list_entry "$list_path" "$ppa_url" || return 1 write_list "$list_format" "$ppa" "$ppa_url" "$package_dist" "$branches" "$key_file" || return 1 list_path="$list_dir"/"${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$package_dist"."$list_extension" # A caller requesting an index needs a successful refresh, even if APT has cached lists. @@ -337,29 +349,50 @@ check_ppa() { check_lists "$ppa" "$primary_pattern" "$secondary_pattern" || return 1 } +# Remove matching entries from one APT file and invalidate its cached refresh. +remove_list_entry() { + local list=$1 ppa_url=$2 uri filter + [[ -f "$list" ]] && grep -Fq -- "${ppa_url%/}" "$list" || return 0 + uri=$(escape_regex "${ppa_url%/}") + [[ "$ppa_url" != //* ]] || uri="https?:$uri" + if [[ "$list" = *.sources ]]; then + # Match URIs within one deb822 paragraph, including folded fields. + local gap='([[:blank:]]|\r?\n(#[^\n]*\n)*[[:blank:]])' + filter='/^[[:space:]]*$/b + :stanza + $!{N; /\n[[:blank:]]*\r?$/!b stanza;} + /(^|\n)[Uu][Rr][Ii][Ss]:'"$gap*([^[:space:]]+$gap+)*$uri"'\/*([[:space:]]|$)/d' + else + filter="/^[[:blank:]]*deb(-src)?[[:blank:]]+(\\[[^]]*\\][[:blank:]]+)?$uri\\/*([[:space:]]|$)/d" + fi + sudo sed --follow-symlinks -Ei "$filter" "$list" || return 1 + grep -q '[^[:space:]]' "$list" || sudo rm -f -- "$list" || return 1 + sudo rm -f -- "/tmp/os_lists_refresh_${list##*/}" "${RUNNER_TEMP:-/tmp}/os_lists_${list##*/}" || return 1 +} + +# Keep any other entries at an active sibling path before publishing a managed source. +preserve_list() { + local list=$1 url saved stamp="${RUNNER_TEMP:-/tmp}/os_lists_${1##*/}" + [[ -e "$list" || -L "$list" ]] || return 0 + if [[ -f "$stamp" ]] && tail -n +4 "$stamp" | cmp -s - "$list"; then + sudo rm -f -- "$list" + return + fi + shift + for url in "$@"; do remove_list_entry "$list" "$url" || return 1; done + [[ -e "$list" || -L "$list" ]] || return 0 + saved=$(sudo mktemp --suffix=".${list##*.}" "${list%.*}.XXXXXXXX") && sudo mv -fT -- "$list" "$saved" +} + # Function to remove a PPA. remove_list() { - local ppa=${1-$default_ppa} list ppa_url uri filter + local ppa=${1-$default_ppa} list ppa_url local -a ppa_urls [ -n "$2" ] && ppa_urls=("$2") || ppa_urls=("$lp_ppa/$ppa/ubuntu" "$lpc_ppa/$ppa/ubuntu") for ppa_url in "${ppa_urls[@]}"; do - uri=$(escape_regex "${ppa_url%/}") - [[ "$ppa_url" != //* ]] || uri="https?:$uri" while IFS= read -r -d '' list; do - if [[ "$list" = *.sources ]]; then - # Match URIs within one deb822 paragraph, including folded fields. - local gap='([[:blank:]]|\r?\n(#[^\n]*\n)*[[:blank:]])' - filter='/^[[:space:]]*$/b - :stanza - $!{N; /\n[[:blank:]]*\r?$/!b stanza;} - /(^|\n)[Uu][Rr][Ii][Ss]:'"$gap*([^[:space:]]+$gap+)*$uri"'\/*([[:space:]]|$)/d' - else - filter="/^[[:blank:]]*deb(-src)?[[:blank:]]+(\\[[^]]*\\][[:blank:]]+)?$uri\\/*([[:space:]]|$)/d" - fi - sudo sed -Ei "$filter" "$list" || return 1 - grep -q '[^[:space:]]' "$list" || sudo rm -f -- "$list" || return 1 - sudo rm -f -- "/tmp/os_lists_refresh_${list##*/}" "${RUNNER_TEMP:-/tmp}/os_lists_${list##*/}" || return 1 - done < <(grep -FlrZ --include='*.list' --include='*.sources' --exclude="${3:-}" -- "${ppa_url%/}" "$list_dir") + remove_list_entry "$list" "$ppa_url" || return 1 + done < <(grep_sources -FlZ --exclude="${3:-}" -e "${ppa_url%/}") done [[ ! -e "$key_dir/${ppa/\//-}-keyring" ]] || sudo rm -f "$key_dir/${ppa/\//-}-keyring" } @@ -376,7 +409,7 @@ remove_legacy_mirrors() { for ppa in ondrej/php ondrej/apache2; do old_ppa="//${ppa_sp#*://}/$ppa/ubuntu" old_sp="//${sp#*://}/$ppa/ubuntu" - if grep -Fqr -e "$old_ppa" -e "$old_sp" "$list_dir"; then + if grep_sources -Fq -e "$old_ppa" -e "$old_sp"; then remove_list "$ppa" "$old_ppa" && remove_list "$ppa" "$old_sp" || return 1 fi done @@ -417,7 +450,7 @@ add_list_with_fallback() ( { [[ "${require_mirror_by_hash:-}" != true ]] || ! grep -Fq "${mirror%/}" "$list_dir/$source" || grep -Eq '^By-Hash: force$| by-hash=force( |])' "$list_dir/$source"; }; then # A cache can restore another source file after this archive was selected. - (( $# > 6 )) && grep -qrF --exclude="$source" -f <(printf '%s\n' "${@:7}") "$list_dir" || return 0 + (( $# > 6 )) && grep_sources -qF --exclude="$source" -f <(printf '%s\n' "${@:7}") || return 0 else stage=$(mktemp -d /tmp/setup-php-list.XXXXXXXX) || return 1 trap 'rm -rf "$stage"' EXIT @@ -459,6 +492,7 @@ add_list_with_fallback() ( add_key "$repo" "$primary" "$suite" "$key" "$managed_key" || return 1 key=$managed_key fi + preserve_list "$list_dir/$source" "$primary" "$mirror" "${@:7}" || return 1 sudo install -m 644 "$stage/$source" "$list_dir/$source" || return 1 { printf '%s\n' "$request" && cat "$stage/index" && printf '%s\n' "$key" && cat "$stage/$source"; } > "$stage/stamp" && mv -fT "$stage/stamp" "$stamp" ) @@ -558,7 +592,7 @@ update_ppa() { list_path="$list_dir/${repo%%/*}-$ID-${repo#*/}-$package_dist.$(get_sources_extension "$list_format")" if [[ -n "${2:-}" || ! -s "$list_path" ]]; then IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" - list_path="$(grep -Elr "$primary_pattern" "$list_dir" 2>/dev/null | head -n 1)" + list_path="$(grep_sources -El -e "$primary_pattern" 2>/dev/null | head -n 1)" [ -n "$list_path" ] || return 1 fi local rc=0