From 4402d7b81d0a0183e569da995dc4bcb9bfd42619 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Fri, 4 Sep 2020 12:29:45 +0100
Subject: [PATCH] NavCache: only remove if the file exists.

Otherwise we block trying to re-create the cache, which is dumb. This
showed up as Sentry issue:
https://sentry.io/organizations/flightgear/issues/1875854826

Will back-port to 2020.2 once verified.
---
 src/Navaids/NavDataCache.cxx | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/Navaids/NavDataCache.cxx b/src/Navaids/NavDataCache.cxx
index c6b93f635..f45bee087 100644
--- a/src/Navaids/NavDataCache.cxx
+++ b/src/Navaids/NavDataCache.cxx
@@ -1212,14 +1212,13 @@ NavDataCache::NavDataCache()
         os << "navdata_" << versionParts[0] << "_" << versionParts[1] << ".cache";
     }
 
-    homePath.append(os.str());
-
     // permit additional DB connections from the same process
     sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
 
     for (int t=0; t < MAX_TRIES; ++t) {
+        SGPath cachePath = homePath / os.str();
         try {
-            d.reset(new NavDataCachePrivate(homePath, this));
+            d.reset(new NavDataCachePrivate(cachePath, this));
             d->init();
             //d->checkCacheFile();
             // reached this point with no exception, success
@@ -1239,14 +1238,14 @@ NavDataCache::NavDataCache()
             d.reset();
 
             // only wipe the existing if not readonly
-            if (!fgGetBool("/sim/fghome-readonly", false)) {
-                bool ok = homePath.remove();
+            if (cachePath.exists() && !fgGetBool("/sim/fghome-readonly", false)) {
+                bool ok = cachePath.remove();
                 if (!ok) {
                     SG_LOG(SG_NAVCACHE, SG_ALERT, "NavCache: failed to remove previous cache file");
                     flightgear::fatalMessageBoxThenExit(
                       "Unable to re-create navigation cache",
                       "Attempting to remove the old cache failed.",
-                      "Location: "  + homePath.utf8Str());
+                      "Location: "  + cachePath.utf8Str());
                 }
             }
         }