mirror of
https://github.com/awalsh128/cache-apt-pkgs-action.git
synced 2026-07-31 10:59:22 +00:00
Fix dpkg not knowing about packages after cache restore
After cache restore, dpkg had no record of the installed packages because: 1. Only preinst/postinst scripts were cached from /var/lib/dpkg/info/, missing .list, .md5sums, .conffiles, and other metadata files 2. The dpkg status database (/var/lib/dpkg/status) was never updated This meant dpkg -s, apt list --installed, and anything checking package state would not see the restored packages. Fix: - Cache all /var/lib/dpkg/info/<package>.* files (not just install scripts) - Save each package's dpkg status entry to a .dpkg-status file - On restore, append status entries to /var/lib/dpkg/status (skipping packages that are already registered) Adds tests verifying dpkg -s reports packages as installed after both fresh install and cache restore. Co-developed-by: Claude Code v2.1.58 (claude-opus-4-6)
This commit is contained in:
parent
f6d423e255
commit
5bddecf9cd
47
.github/workflows/action-tests.yml
vendored
47
.github/workflows/action-tests.yml
vendored
|
|
@ -473,3 +473,50 @@ jobs:
|
||||||
packages: libvips
|
packages: libvips
|
||||||
version: ${{ github.run_id }}-${{ github.run_attempt }}-virtual_package
|
version: ${{ github.run_id }}-${{ github.run_attempt }}-virtual_package
|
||||||
debug: ${{ env.DEBUG }}
|
debug: ${{ env.DEBUG }}
|
||||||
|
|
||||||
|
# === dpkg Registration Tests ===
|
||||||
|
|
||||||
|
dpkg_status_install:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: "dpkg knows about packages after install (phase 1)."
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Execute
|
||||||
|
id: execute
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
packages: rolldice
|
||||||
|
version: ${{ github.run_id }}-${{ github.run_attempt }}-dpkg_status
|
||||||
|
debug: ${{ env.DEBUG }}
|
||||||
|
- name: Verify dpkg knows the package
|
||||||
|
run: |
|
||||||
|
dpkg -s rolldice | grep -q 'Status: install ok installed'
|
||||||
|
echo "dpkg reports rolldice as installed after fresh install."
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
dpkg_status_restore:
|
||||||
|
needs: dpkg_status_install
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
name: "dpkg knows about packages after cache restore (phase 2)."
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Execute
|
||||||
|
id: execute
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
packages: rolldice
|
||||||
|
version: ${{ github.run_id }}-${{ github.run_attempt }}-dpkg_status
|
||||||
|
debug: ${{ env.DEBUG }}
|
||||||
|
- name: Verify cache hit
|
||||||
|
run: test "${{ steps.execute.outputs.cache-hit }}" = "true"
|
||||||
|
shell: bash
|
||||||
|
- name: Verify dpkg knows the package after cache restore
|
||||||
|
run: |
|
||||||
|
dpkg -s rolldice | grep -q 'Status: install ok installed'
|
||||||
|
echo "dpkg reports rolldice as installed after cache restore."
|
||||||
|
shell: bash
|
||||||
|
- name: Verify the binary works
|
||||||
|
run: |
|
||||||
|
rolldice 2d6
|
||||||
|
echo "rolldice binary works after cache restore."
|
||||||
|
shell: bash
|
||||||
|
|
|
||||||
|
|
@ -101,11 +101,15 @@ for installed_package in ${installed_packages}; do
|
||||||
read package_name package_ver < <(get_package_name_ver "${installed_package}")
|
read package_name package_ver < <(get_package_name_ver "${installed_package}")
|
||||||
log " * Caching ${package_name} to ${cache_filepath}..."
|
log " * Caching ${package_name} to ${cache_filepath}..."
|
||||||
|
|
||||||
# Pipe all package files (no folders), including symlinks, their targets, and installation control data to Tar.
|
# Pipe all package files (no folders), including symlinks, their targets,
|
||||||
|
# and all dpkg metadata (info files) to Tar.
|
||||||
tar -cf "${cache_filepath}" -C / --verbatim-files-from --files-from <(
|
tar -cf "${cache_filepath}" -C / --verbatim-files-from --files-from <(
|
||||||
{ dpkg -L "${package_name}" &&
|
{ dpkg -L "${package_name}" &&
|
||||||
get_install_script_filepath "" "${package_name}" "preinst" &&
|
# Include all dpkg info files for this package (list, md5sums,
|
||||||
get_install_script_filepath "" "${package_name}" "postinst" ; } |
|
# conffiles, triggers, preinst, postinst, prerm, postrm, etc.)
|
||||||
|
# so dpkg recognizes the package after cache restore.
|
||||||
|
ls -1 /var/lib/dpkg/info/${package_name}.* 2>/dev/null &&
|
||||||
|
ls -1 /var/lib/dpkg/info/${package_name}:*.* 2>/dev/null ; } |
|
||||||
while IFS= read -r f; do
|
while IFS= read -r f; do
|
||||||
if test -f "${f}" -o -L "${f}"; then
|
if test -f "${f}" -o -L "${f}"; then
|
||||||
get_tar_relpath "${f}"
|
get_tar_relpath "${f}"
|
||||||
|
|
@ -119,6 +123,9 @@ for installed_package in ${installed_packages}; do
|
||||||
done
|
done
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Save the dpkg status entry so we can register the package on restore.
|
||||||
|
dpkg -s "${package_name}" > "${cache_dir}/${installed_package}.dpkg-status" 2>/dev/null || true
|
||||||
|
|
||||||
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
|
log " done (compressed size $(du -h "${cache_filepath}" | cut -f1))."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,3 +59,30 @@ for cached_filepath in ${cached_filepaths}; do
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
log "done"
|
log "done"
|
||||||
|
|
||||||
|
log_empty_line
|
||||||
|
|
||||||
|
# Register packages with dpkg so they appear as installed.
|
||||||
|
# The tar extraction restores dpkg info files (list, md5sums, etc.) but the
|
||||||
|
# main status database (/var/lib/dpkg/status) also needs updating.
|
||||||
|
dpkg_status_dir="${cache_dir}"
|
||||||
|
status_files=$(ls -1 "${dpkg_status_dir}"/*.dpkg-status 2>/dev/null || true)
|
||||||
|
if test -n "${status_files}"; then
|
||||||
|
log "Registering restored packages with dpkg..."
|
||||||
|
for status_file in ${status_files}; do
|
||||||
|
pkg_name=$(head -1 "${status_file}" | sed 's/^Package: //')
|
||||||
|
# Skip if dpkg already knows about this package (e.g., it was pre-installed).
|
||||||
|
if dpkg -s "${pkg_name}" > /dev/null 2>&1; then
|
||||||
|
existing_status=$(dpkg -s "${pkg_name}" 2>/dev/null | grep '^Status:' | head -1)
|
||||||
|
if echo "${existing_status}" | grep -q 'install ok installed'; then
|
||||||
|
log "- ${pkg_name} already registered, skipping."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
# Append the status entry (with blank line separator) to the dpkg database.
|
||||||
|
echo "" | sudo tee -a "${cache_restore_root}var/lib/dpkg/status" > /dev/null
|
||||||
|
cat "${status_file}" | sudo tee -a "${cache_restore_root}var/lib/dpkg/status" > /dev/null
|
||||||
|
log "- ${pkg_name} registered."
|
||||||
|
done
|
||||||
|
log "done"
|
||||||
|
fi
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue