Skip to content

fix(runtime): remove dropped var_files from var-staging - #445

Open
nicksinas wants to merge 4 commits into
mainfrom
fix/var-staging-stale-files
Open

nicksinas wants to merge 4 commits into
mainfrom
fix/var-staging-stale-files

Conversation

@nicksinas

Copy link
Copy Markdown
Contributor

Problem

var-staging stays between builds, and both the var image and the provision bundle are made from it. The extension and runtime var_files steps only copy files in (cp -a, cp -f). No step records what they copy or removes it later. So a file that you remove from var_files stays in var-staging and gets to the device at the next provision. This occurs when you remove a var_files entry, and also when you remove a file under an entry that is still listed.

With 1.2.0, on a project with var_files: [var/lib/app/**] and overlay/var/lib/app/a.txt:

=== build 1 (var_files listed, a.txt present)
/opt/_avocado/qemux86-64/runtimes/dev/var-staging/lib/app/a.txt
=== build 2 (var_files entry and a.txt removed)
/opt/_avocado/qemux86-64/runtimes/dev/var-staging/lib/app/a.txt

Empty overlay directories in the extension sysroot are left as they are. That is on purpose (see the comment in ext/build.rs), and this PR does not change it.

Change

All code changes are in src/commands/runtime/build.rs.

  • After both copy steps, a record step writes each path that var_files placed, and each file, link and directory under it, to $AVOCADO_PREFIX/.stamps/runtime/<runtime>/var-staging.files. The list is NUL-delimited, like overlay.files.
  • Before anything writes to var-staging, a prune step reads the earlier list in reverse order (LC_ALL=C sort -rzu). It removes each file or link that the current var_files no longer provide. It removes a directory only when the directory is empty. It skips absolute paths and paths that contain .., with the same guard as the overlay sync.
  • Paths that var_files did not place are never touched. This includes docker priming in lib/docker, the lib/avocado manifest and images, and package content. The build does not wipe var-staging.
  • The extension var_files copy shell moves into ext_var_files_script, without a change to the copy itself. A missing source now prints a warning, as the runtime step already does.
  • A runtime source that ends in /. is recorded at its dest, where the shell copy puts its content.

The list is in .stamps/runtime/<runtime>/ so that avocado runtime clean removes it together with var-staging, and so that it never gets into the var image.

Upgrade note (in CHANGELOG.md): files that an older CLI copied are not on a list, so they stay until avocado runtime clean <runtime>, followed by avocado install and avocado build.

Testing

  • var_files_removed_between_builds_leave_var_staging runs the prune, copy and record shell with bash against a tempdir, over three builds. It covers removed files and entries, a path with a space, files that var_files did not place, and ../ and absolute paths in the list. It fails with the old copy-only behavior.
  • var_files_dot_source_is_recorded_where_the_copy_puts_it, var_files_copies_match_where_the_copies_put_files and build_script_prunes_var_staging_first_and_records_after_the_copies cover the path rules and the step order in the full build script.
  • On a real qemux86-64 project, the built binary removes lib/app/a.txt and the empty lib/app at build 2:
    Removed /var/lib/app/a.txt from var staging (no longer in var_files)
    Removed empty directory /var/lib/app from var staging (no longer in var_files)
    
  • cargo fmt --all -- --check and RUST_MIN_STACK=8388608 cargo test pass.
  • cargo clippy --all-targets --all-features -- -D warnings on macOS gives 5 dead_code errors in src/utils/host_mounts.rs. They are on main too. This change adds no warnings.

Var staging persists between builds and the var image is made from it.
The var_files copies only added files, so a file removed from an
extension's or the runtime's var_files stayed in var staging and
reached the device on the next provision.

- Move the extension var_files copy into ext_var_files_script
- Record every path the var_files copies place, NUL-delimited, in
  .stamps/runtime/<runtime>/var-staging.files
- Before anything writes to var staging, remove each recorded path
  that the current var_files no longer provide; remove a recorded
  directory only when it is empty, and never touch unrecorded paths
  or paths that leave var staging
- Test the prune, copy and record steps with bash against a tempdir

Refs: (bug)avocado-cli-S3-removed-var-files-stay-in-var-staging-and-the-var-image.md
Changes from the QA review of the var-staging prune.

- Record a runtime var_files source that ends in `/.` at its dest,
  where the shell copy puts its content
- Sort the list with LC_ALL=C, as the other NUL-delimited sorts do
- Warn when an extension var_files source is not in the sysroot
- Tell users in the CHANGELOG that files from an earlier version
  stay until `avocado runtime clean <runtime>`

Refs: (bug)avocado-cli-S3-removed-var-files-stay-in-var-staging-and-the-var-image.md
Copilot AI balanced review requested due to automatic review settings November 28, 2026 19:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The inventory can delete uncopied files outside staging through preserved symlinks, records skipped dangling links, and leaves root-destination copies untracked and unpruned.

3 open issues
What changed in this PR

This PR addresses stale var_files content in persistent runtime staging, which can otherwise carry removed files into later var images and provision bundles.

Changes:

  • Adds a per-runtime file inventory, pruning before staging writes and recording after both copy steps.
  • Extracts extension copy generation and adds regression tests for removal, path handling, and build-step order.
  • Documents the cleanup required for files copied by older CLI versions.
File Description
src/​commands/​runtime/​build.rs Adds inventory and pruning helpers, integrates build steps, and tests staging cleanup.
CHANGELOG.md Describes the fix and one-time cleanup procedure for existing projects.

Review effort: Balanced

Comment thread src/commands/runtime/build.rs Resolved
Comment thread src/commands/runtime/build.rs Resolved
Comment thread src/commands/runtime/build.rs Resolved
…gh links

Review of the var_files record found three gaps. A runtime source that is
a symlink to a directory is copied as a link, but the record listed the
files of its target, so dropping the entry removed them through the link.
A dangling source link is not copied, but it was recorded, so dropping
the entry could remove a file at its destination that var_files never
placed. A copy into var staging itself (such as seed/conf/. to /) was not
recorded at all, so its files stayed after they left var_files.

- Record a runtime symlink source as one entry, and select sources with
  the same tests the copies use, so a dangling link is never recorded
- Record and prune what a copy places in var staging itself, but never
  list var staging
- Skip a listed path when a directory above it in var staging is a
  symlink, so an older list cannot lead removal through a link
- Add tests for each case

Refs: #445

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cleanup can leave stale staged files untracked when a top-level source becomes a dangling symlink or a copied FIFO or other special file is removed.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Remove copied special files during stale-path cleanup

src/​commands/​runtime/​build.rs:3655

-f only matches regular files, but directory copies using cp -a also preserve FIFOs and other special files, and find records those paths. If a copied directory contains a FIFO and that file or mapping is removed, this branch never deletes it, its parent cannot be removed, and the next inventory forgets it permanently. Remove existing non-directory entries as well as symlinks, while retaining the empty-directory and symlink-ancestor safeguards.

3 resolved since last review

Review effort: Balanced

Comment thread src/commands/runtime/build.rs Outdated
Comment on lines +3603 to +3617
.map(|VarFilesCopy { source, placed, .. }| {
// A copy into var staging itself provides any path its source has.
let (pattern, path) = if placed.is_empty() {
("*".to_string(), format!("{source}/$1"))
} else {
(
format!("\"{placed}\"|\"{placed}\"/*"),
format!("{source}${{1#\"{placed}\"}}"),
)
};
format!(
r#"
case "$1" in
{pattern})
if [ -e "{path}" ] || [ -L "{path}" ]; then

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. The prune now uses the copy step's own source test before it checks the path. A top-level source that became a dangling link no longer counts as provided, so its old copy is removed. -L still counts for links inside a copied directory. Test added. Fixed in f754b14.

A top-level var_files source that became a dangling link still counted as
provided in the prune, while the copy and the record skipped it. Its old
copy then dropped off the list and stayed in var staging for good. A FIFO
or other special file that cp -a copied inside a directory was recorded
but never removed, because the prune removed only regular files and links.

- Use the copy step's own test for the source in both the prune and the
  record, so a source the copy skips provides nothing
- Remove any recorded entry that is not a directory, with the same guards
  against symlinked parents and paths that leave var staging
- Add tests for a source that became a dangling link and for a FIFO that
  left the source directory

Refs: #445
@nicksinas

Copy link
Copy Markdown
Contributor Author

Reply to the Copilot review of e4f85a1:

  • FIFO or other special file never removed: confirmed. The prune now removes any recorded entry that is not a directory, such as a FIFO that cp -a copied. The guards for symlinked parents and for paths that leave var staging are the same. Test added. Fixed in f754b14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants