Skip to content

Conversation

@smoser
Copy link
Contributor

@smoser smoser commented Dec 12, 2025

This fixes a bug in dirFS where WriteFile would cache an empty buffer in the memory overlay even though it wrote the correct data to disk. This caused subsequent ReadFile operations to return zeros instead of the actual file content.

Root cause:

  • WriteFile used conditional logic where memContent was left empty when createOnDisk(name) returned true
  • The empty buffer was then stored in the overlay via f.overrides.WriteFile(name, memContent, mode)
  • ReadFile would return this cached empty buffer when caseSensitiveOnDisk returned false

Impact:

  • Any code that wrote a file and then read it back through dirFS could receive zeros instead of actual data
  • This particularly affected updateScriptsTar in pkg/apk/apk/installed.go which would corrupt scripts.tar files during package installation

Solution:

  • Always cache the actual data in the memory overlay, not an empty buffer
  • Simplified WriteFile by removing memContent variable and conditional
  • Updated comment to clarify the new behavior

Added comprehensive unit tests to verify:

  • Basic write/read cycles work correctly
  • Multiple writes/reads maintain data integrity
  • Stat calls don't affect caching behavior
  • OpenFile/Write/Close patterns work correctly
  • The exact updateScriptsTar pattern preserves data

🤖 Generated with Claude Code

@smoser
Copy link
Contributor Author

smoser commented Dec 12, 2025

Some background.... I'm trying to make a clone of apk using apko.
In that work, I hit a bug where writing /usr/lib/apk/scripts.tar was getting corrupted.

I'm not certain that claude root-caused this correctly, but the test cases it added seem valid.

jonjohnsonjr
jonjohnsonjr previously approved these changes Dec 12, 2025
This fixes a bug in dirFS where WriteFile would cache an empty buffer
in the memory overlay even though it wrote the correct data to disk.
This caused subsequent ReadFile operations to return zeros instead of
the actual file content.

Root cause:
- WriteFile used conditional logic where memContent was left empty when
  createOnDisk(name) returned true
- The empty buffer was then stored in the overlay via
  f.overrides.WriteFile(name, memContent, mode)
- ReadFile would return this cached empty buffer when
  caseSensitiveOnDisk returned false

Impact:
- Any code that wrote a file and then read it back through dirFS could
  receive zeros instead of actual data
- This particularly affected updateScriptsTar in pkg/apk/apk/installed.go
  which would corrupt scripts.tar files during package installation

Solution:
- Always cache the actual data in the memory overlay, not an empty buffer
- Simplified WriteFile by removing memContent variable and conditional
- Updated comment to clarify the new behavior

Added comprehensive unit tests to verify:
- Basic write/read cycles work correctly
- Multiple writes/reads maintain data integrity
- Stat calls don't affect caching behavior
- OpenFile/Write/Close patterns work correctly
- The exact updateScriptsTar pattern preserves data

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@smoser smoser enabled auto-merge (squash) December 12, 2025 19:43
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