Skip to content

refactor container cp task to be simpler and move logic to container service and common filesystem helpers - #41668

Merged
ggarzia-MSFT merged 16 commits into
masterfrom
user/ggarzia/refactor-container-cp
Oct 2, 2026
Merged

ggarzia-MSFT merged 16 commits into
masterfrom
user/ggarzia/refactor-container-cp

Conversation

@ggarzia-MSFT

Copy link
Copy Markdown
Contributor

This pull request refactors and improves the filesystem utilities used for staging directories, file name validation, and archive extraction. The main enhancements include encapsulating staging directory cleanup in a RAII class, centralizing file/directory move logic, and adding robust validation and tests for file name representability. This results in safer, more maintainable, and more reliable file operations.

This pull request also moves most of the logic out of the ContainerCp method in ContainerTasks.cpp into the Copy method in ContainerService.cpp. Additionally, the parseContainerPath lambda in Copy has been updated to throw if given a bad container path rather than silently deforming the path

Filesystem utility enhancements:

  • Added IsRepresentableFileName to determine if a given string can be used as a Windows file name, preventing invalid names from being used in file operations.
  • Introduced the StagingDirectory RAII class, which automatically cleans up temporary directories when they go out of scope, reducing manual cleanup code and risk of leftover files.
  • Centralized and improved the logic for moving files/directories with the MoveOver function, now handling more edge cases and used consistently.

Archive extraction and staging improvements:

  • Added ExtractArchiveInto and StageDereferencedTree utilities to streamline extraction of tar streams and dereferencing of symlinks, making archive handling more robust and less error-prone.
  • Refactored ContainerCp to use the new utilities, resulting in simpler, safer, and more maintainable code for container copy operations.

Testing improvements:

  • Added comprehensive unit tests for IsRepresentableFileName and StagingDirectory, ensuring correct behavior and preventing regressions.

Copilot AI lite review requested due to automatic review settings September 21, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

It introduces correctness and build/maintainability issues (filename validation gaps, non-static helper symbols, missing header include, and brace-style inconsistency) that should be addressed before merging.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 3 Medium severity · 2 Low severity

Open (5)
What changed in this PR

Refactors the wslc cp implementation by moving the bulk of copy logic from the CLI task layer into ContainerService, and introduces/expands shared filesystem helpers (staging, archive extraction, and filename validation) to make copy/extraction safer and easier to maintain.

Changes:

  • Moved ContainerCp logic into ContainerService::Copy() and simplified the CLI task wrapper.
  • Added filesystem utilities: StagingDirectory (RAII cleanup), ExtractArchiveInto, StageDereferencedTree, and IsRepresentableFileName.
  • Added/extended unit tests for staging directory lifetime and filename representability.
File Description
test/​windows/​FilesystemUnitTests.cpp Adds unit coverage for new filename validation and RAII staging directory behavior.
src/​windows/​wslc/​tasks/​ContainerTasks.cpp Simplifies ContainerCp to delegate to ContainerService::Copy.
src/​windows/​wslc/​services/​ContainerService.h Declares new ContainerService::Copy entry point.
src/​windows/​wslc/​services/​ContainerService.cpp Implements the refactored copy behavior and uses new filesystem helpers.
src/​windows/​common/​filesystem.hpp Declares new shared filesystem helpers for staging/extraction/validation.
src/​windows/​common/​filesystem.cpp Implements new filesystem helpers used by container copy operations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/windows/common/filesystem.cpp
Comment thread src/windows/common/filesystem.hpp
Comment thread test/windows/FilesystemUnitTests.cpp Outdated
Comment thread src/windows/common/filesystem.cpp Outdated
Comment thread test/windows/FilesystemUnitTests.cpp
Copilot AI review requested due to automatic review settings September 22, 2026 00:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

IsRepresentableFileName currently does not enforce key Win32 filename constraints (device names, trailing dot/space) and helper functions in filesystem.cpp leak as global symbols.

Review effort: Lite
Findings: 3 Medium severity · 2 Low severity

Open (5)
Previously missed (1)

In code that hasn't changed since last review

Low severity Give ExtractTarStream internal linkage

src/​windows/​common/​filesystem.cpp:1050

ExtractTarStream() is a file-local helper but is currently emitted as a global symbol from filesystem.cpp. Give it internal linkage (e.g. make it static or put it in an unnamed namespace).

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings September 22, 2026 17:22
Added additional reserved filenames to the test case for better coverage.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Address the two unresolved moderate findings in filesystem.cpp.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
Resolved since last review (4)

Copilot AI review requested due to automatic review settings September 22, 2026 18:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Archive entries are not validated before direct extraction, leaving a moderate unresolved issue.

Review effort: Lite
Findings: None

Resolved since last review (1)

@dkbennett David Bennett (dkbennett) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this PR a lot, but I think we can do a little more refactoring in the service tasks, possibly break it up into multiple tasks or at least a few helpers. The logic is still hard to read, which means it may have bugs lurking in there.

Comment thread src/windows/wslc/services/ContainerService.cpp Outdated
Comment thread src/windows/common/filesystem.cpp
Comment thread test/windows/FilesystemUnitTests.cpp
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp
Copilot AI review requested due to automatic review settings September 23, 2026 17:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Unresolved path-validation, filename-length, and public API issues remain.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)

Comment thread src/windows/wslc/services/ContainerService.cpp Outdated
Comment thread src/windows/wslc/services/ContainerService.h Outdated
@ggarzia-MSFT
ggarzia-MSFT marked this pull request as ready for review September 23, 2026 20:38
@ggarzia-MSFT
ggarzia-MSFT requested review from a team as code owners September 23, 2026 20:38
Copilot AI review requested due to automatic review settings September 23, 2026 20:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Four moderate issues remain unresolved in filesystem validation, archive handling, symlink replacement, and path classification.

Review effort: Lite
Findings: None

Resolved since last review (2)

@OneBlue Blue (OneBlue) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Change looks great, minor comments

Comment thread src/windows/wslc/services/ContainerService.cpp Outdated
Comment thread src/windows/common/filesystem.cpp
Comment thread src/windows/wslc/services/ContainerService.cpp Outdated
Comment thread test/windows/wslc/e2e/WSLCE2EContainerCpTests.cpp Outdated
Comment thread test/windows/FilesystemUnitTests.cpp Outdated
Comment thread test/windows/FilesystemUnitTests.cpp Outdated
Copilot AI lite review requested due to automatic review settings September 29, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Unresolved critical and moderate filesystem and path-handling issues remain.

Review effort: Lite
Findings: 1 High severity

Open (1)

Comment thread src/windows/common/filesystem.cpp

@ranm-msft ranm-msft 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.

The shape here is a clear improvement - the CLI task delegating into a helper, file destinations staging before they are moved into place, followed links keeping the requested basename, and the reserved-name and trailing dot or space checks handled up front instead of producing broken host entries.

The one thing I would like before I sign off is a regression test for the symlink form of tar traversal, not only dir/../file. This helper now centralizes archive extraction, and container cp consumes an archive built from container-controlled filesystem state, so the classic escape is an entry link -> <path outside the destination> followed by an entry under link/child. An absolute-path entry is worth pinning too. If tar.exe already refuses both, even better - having that stated in FilesystemUnitTests is what makes the helper safe to reuse later without someone re-deriving the argument.

The existing coverage for reserved Windows names, trailing dot and space, .. extraction, trailing slash handling, the stdout and stdin guards, and the directory-to-file rejection all look right to me, and CI is green at the head. This is a small ask on top of work that is already going the right direction.

Copilot AI lite review requested due to automatic review settings October 1, 2026 22:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Critical archive extraction and member-collision issues remain unresolved, along with filename validation and test inconsistencies.

Review effort: Lite
Findings: 2 High severity · 1 Medium severity

Open (3)
Resolved since last review (1)

Comment thread src/windows/common/filesystem.cpp Outdated
Comment thread src/windows/common/filesystem.cpp Outdated
Comment thread src/windows/common/filesystem.cpp Outdated

@ranm-msft ranm-msft 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.

Took a pass over the archive extraction path. The two bot comments on filesystem.cpp are not noise - I reproduced both behaviours against the installed Windows tar.exe on this box, so I have left the evidence inline rather than just agreeing with them.

Nothing else jumped out; the rest reads fine. Holding off on a sign-off until wsl-github-pr comes back green and those two are settled.

Comment thread src/windows/common/filesystem.cpp Outdated
Comment thread src/windows/common/filesystem.cpp Outdated
Copilot AI lite review requested due to automatic review settings October 1, 2026 23:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Unresolved archive extraction safety, temporary-storage, and filename-validation issues must be addressed.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (3)

Comment thread src/windows/common/filesystem.cpp Outdated
Copilot AI lite review requested due to automatic review settings October 2, 2026 00:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Critical archive path and hard-link validation issues remain unresolved.

Review effort: Lite
Findings: 2 High severity

Open (2)
Resolved since last review (1)

Comment thread src/windows/common/filesystem.cpp
Comment thread src/windows/common/filesystem.cpp
@ggarzia-MSFT
ggarzia-MSFT merged commit b0a14e5 into master Oct 2, 2026
12 checks passed
@ggarzia-MSFT
ggarzia-MSFT deleted the user/ggarzia/refactor-container-cp branch October 2, 2026 18:47
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.

5 participants