apko
apko copied to clipboard
fix(fs): fix dirFS caching bug where ReadFile returns zeros
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