Skip to content

srtm: fix for old cached srtm files#3676

Merged
meee1 merged 1 commit intomasterfrom
srtmfix
Mar 12, 2026
Merged

srtm: fix for old cached srtm files#3676
meee1 merged 1 commit intomasterfrom
srtmfix

Conversation

@meee1
Copy link
Collaborator

@meee1 meee1 commented Mar 2, 2026

No description provided.

@meee1 meee1 requested a review from Copilot March 12, 2026 05:26
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a cleanup step for cached SRTM files to address issues with older/invalid cache entries.

Changes:

  • Deletes zero-length cached files in the SRTM data directory during static initialization.
  • Purges cached files older than a fixed cutoff date to mitigate the “srtm3 bug cache” issue referenced in the linked discussion.

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

Comment on lines +103 to +111
Directory.GetFiles(datadirectory).ToList().ForEach(x =>
{
var fi = new FileInfo(x);
if (fi.Length == 0)
File.Delete(x);
// fix srtm3 bug cache - delete old files https://discuss.ardupilot.org/t/serious-terrain-data-error-and-how-to-fix-your-vehicle/142593
if (fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))
File.Delete(x);
});
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

This runs in a static constructor; any IOException/UnauthorizedAccessException (e.g., directory missing, file locked, permissions) will bubble up as a TypeInitializationException and can break any feature that touches this type. Wrap the enumeration and per-file delete in a try/catch (ideally per file), and log failures rather than letting initialization fail.

Suggested change
Directory.GetFiles(datadirectory).ToList().ForEach(x =>
{
var fi = new FileInfo(x);
if (fi.Length == 0)
File.Delete(x);
// fix srtm3 bug cache - delete old files https://discuss.ardupilot.org/t/serious-terrain-data-error-and-how-to-fix-your-vehicle/142593
if (fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))
File.Delete(x);
});
try
{
if (!Directory.Exists(datadirectory))
{
log.Warn($"SRTM data directory does not exist: '{datadirectory}'");
return;
}
Directory.GetFiles(datadirectory).ToList().ForEach(x =>
{
try
{
var fi = new FileInfo(x);
if (fi.Length == 0)
File.Delete(x);
// fix srtm3 bug cache - delete old files https://discuss.ardupilot.org/t/serious-terrain-data-error-and-how-to-fix-your-vehicle/142593
if (fi.LastWriteTimeUtc < new DateTime(2026, 03, 01, 0, 0, 0, DateTimeKind.Utc))
File.Delete(x);
}
catch (IOException ex)
{
log.Warn($"Failed to process SRTM file '{x}'", ex);
}
catch (UnauthorizedAccessException ex)
{
log.Warn($"Access denied processing SRTM file '{x}'", ex);
}
});
}
catch (IOException ex)
{
log.Warn($"Failed to enumerate SRTM data directory '{datadirectory}'", ex);
}
catch (UnauthorizedAccessException ex)
{
log.Warn($"Access denied enumerating SRTM data directory '{datadirectory}'", ex);
}

Copilot uses AI. Check for mistakes.
Comment on lines +103 to +111
Directory.GetFiles(datadirectory).ToList().ForEach(x =>
{
var fi = new FileInfo(x);
if (fi.Length == 0)
File.Delete(x);
// fix srtm3 bug cache - delete old files https://discuss.ardupilot.org/t/serious-terrain-data-error-and-how-to-fix-your-vehicle/142593
if (fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))
File.Delete(x);
});
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Using ToList().ForEach() for side effects forces an unnecessary list allocation and is harder to debug/step through. Prefer a simple foreach loop over Directory.GetFiles(datadirectory) to avoid the allocation and improve readability.

Copilot uses AI. Check for mistakes.
if (fi.Length == 0)
File.Delete(x);
// fix srtm3 bug cache - delete old files https://discuss.ardupilot.org/t/serious-terrain-data-error-and-how-to-fix-your-vehicle/142593
if (fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The cleanup deletes all files in datadirectory that are older than the cutoff date, regardless of filename/extension. If that directory can contain anything besides SRTM cache files, this risks deleting unrelated files. Consider filtering by known cache filename pattern/extension (or checking expected prefixes) before applying the size/date deletion rules.

Suggested change
if (fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))
if (fi.Extension.Equals(".hgt", StringComparison.OrdinalIgnoreCase) &&
fi.LastWriteTimeUtc < new DateTime(2026,03,01,0,0,0, DateTimeKind.Utc))

Copilot uses AI. Check for mistakes.
@meee1 meee1 merged commit d2ce21b into master Mar 12, 2026
5 of 12 checks passed
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