Conversation
There was a problem hiding this comment.
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.
| 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); | ||
| }); |
There was a problem hiding this comment.
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.
| 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); | |
| } |
| 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); | ||
| }); |
There was a problem hiding this comment.
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.
| 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)) |
There was a problem hiding this comment.
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.
| 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)) |
No description provided.