Skip to content

Fix/elevation tile cache corruption - #832

Merged
louis-e merged 5 commits into
mainfrom
fix/elevation-tile-cache-corruption
Mar 24, 2026
Merged

louis-e merged 5 commits into
mainfrom
fix/elevation-tile-cache-corruption

Conversation

@louis-e

@louis-e louis-e commented Mar 24, 2026

Copy link
Copy Markdown
Owner

No description provided.

louis-e added 3 commits March 24, 2026 19:12
Restore file size validation for cached tiles (removed in 6ef8169) and
swap the write/validate order in download_tile_once so invalid image
data is never persisted to disk.

When users interrupt the app mid-download (Ctrl+C, closing window),
std::fs::write can leave truncated files on disk. On subsequent runs
these files pass the exists() check but decode as all-black pixels
(RGB 0,0,0 = -32768m), poisoning the height grid and collapsing
elevation range to zero — producing completely flat terrain. Deleting
arnis-tile-cache was the only workaround.

Changes:
- Validate image via load_from_memory before writing to cache, so
  invalid data never gets persisted
- Re-add <1000 byte file size check to detect and remove truncated
  cached tiles on load
Store tile cache in the platform-standard cache directory
(AppData/Local on Windows, ~/.cache on Linux, ~/Library/Caches on
macOS) via dirs::cache_dir() instead of ./arnis-tile-cache relative
to the current working directory.

The CWD-relative path caused silent elevation failures when:
- The working directory lacked write permissions (e.g. Run as Admin
  sets CWD to System32, or Windows Controlled Folder Access blocks
  writes)
- The CWD was unexpected (shortcuts, file managers, terminal launch
  from a different directory)

In these cases, create_dir_all failed, the error propagated to
ground.rs which silently fell back to flat terrain — with no
indication to GUI users since errors only went to stderr.

Also adds migration logic to remove the old CWD-relative cache
directory on startup.
Copilot AI review requested due to automatic review settings March 24, 2026 18:49

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.

Pull request overview

This PR aims to prevent elevation tile cache corruption and make tile caching more robust across different working directories by moving the cache to an OS-standard location and validating tiles before persisting them.

Changes:

  • Switch elevation tile cache path from a CWD-relative directory to an OS cache directory (with fallback).
  • Prevent caching invalid downloads by validating image bytes before writing to disk, and add a truncated-file guard when loading cached tiles.
  • Minor refactor in HTTP status-to-message mapping for download errors.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/retrieve_data.rs Simplifies match arm formatting for server error status messaging.
src/elevation_data.rs Moves tile cache to OS cache dir, adds cache validation/truncation handling, and adds legacy cache migration logic.
Comments suppressed due to low confidence (1)

src/elevation_data.rs:65

  • cleanup_old_cached_tiles() returns early when the new cache directory doesn’t exist, which prevents the legacy-cache migration block later in the function from ever running on first startup. This means an existing ./arnis-tile-cache from previous versions will not be removed/migrated unless the OS cache dir already exists. Consider running the legacy migration before the early return (or splitting migration into a separate pre-check) so it executes even when the new cache dir hasn’t been created yet.
pub fn cleanup_old_cached_tiles() {
    let tile_cache_dir = get_tile_cache_dir();

    if !tile_cache_dir.exists() || !tile_cache_dir.is_dir() {
        return; // Nothing to clean up
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Move the legacy ./arnis-tile-cache removal before the early return
that checks if the new OS cache directory exists. Otherwise on first
startup after upgrade, the new cache dir doesn't exist yet, the
function returns early, and the old CWD-relative cache is never
cleaned up.

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/elevation_data.rs
Comment thread src/elevation_data.rs
Comment thread src/elevation_data.rs Outdated
Comment thread src/elevation_data.rs Outdated
The old ./arnis-tile-cache directory is harmless if left in place.
Users who want to reclaim the space can delete it manually. The new
OS cache directory is now used exclusively for all new downloads.
@louis-e
louis-e merged commit bf79bf6 into main Mar 24, 2026
2 checks passed
@louis-e
louis-e deleted the fix/elevation-tile-cache-corruption branch March 24, 2026 19:09
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