Skip to content

Commit

Permalink
Fix/OpenSky Master File: Cleanup old database files (Lin/Max only)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Feb 13, 2024
1 parent 97f1d53 commit 2282137
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Include/DataRefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,9 @@ class DataRefs
int DecNumAc();

// Get XP System Path
inline std::string GetXPSystemPath() const { return XPSystemPath; }
inline std::string GetLTPluginPath() const { return LTPluginPath; }
inline std::string GetDirSeparator() const { return DirSeparator; }
const std::string& GetXPSystemPath() const { return XPSystemPath; }
const std::string& GetLTPluginPath() const { return LTPluginPath; }
const std::string& GetDirSeparator() const { return DirSeparator; }

// Load/save config file (basically a subset of LT dataRefs)
bool LoadConfigFile();
Expand Down
1 change: 1 addition & 0 deletions Include/LTOpenSky.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ constexpr size_t OPSKY_MD_TEXT_VEHICLE_LEN = 20; ///< length after which cate

#define OPSKY_MD_DB_NAME "OpenSky Masterdata File"
#define OPSKY_MD_DB_URL "https://opensky-network.org/datasets/metadata/"
#define OPSKY_MD_DB_FILE_BEGIN "aircraft-database-complete-"
#define OPSKY_MD_DB_FILE "aircraft-database-complete-%04d-%02d.csv"

#define OPSKY_ROUTE_URL "https://opensky-network.org/api/routes?callsign="
Expand Down
4 changes: 4 additions & 0 deletions Include/LiveTraffic.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ bool RemoteFileDownload (const std::string& url, const std::string& path);
std::string& str_toupper(std::string& s);
/// return a std::string copy converted to uppercase
std::string str_toupper_c(const std::string& s);
/// Case-insensitive equal
bool striequal (const std::string& a, const std::string& b);
/// Case-insensitive begins with
bool stribeginwith (const std::string& s, const std::string& begin);
// are all chars alphanumeric?
bool str_isalnum(const std::string& s);
// limits text to m characters, replacing the last ones with ... if too long
Expand Down
19 changes: 19 additions & 0 deletions Src/LTMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,25 @@ std::string str_toupper_c(const std::string& s)
return c;
}

// Case-insensitive equal
/// @see https://stackoverflow.com/a/4119881
bool striequal (const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](unsigned char x, unsigned char y)
{ return std::tolower(x) == std::tolower(y); });
}

// Case-insensitive begins with
bool stribeginwith (const std::string& s, const std::string& begin)
{
if (begin.size() > s.size()) return false;
return std::equal(begin.begin(), begin.end(), s.begin(),
[](unsigned char x, unsigned char y)
{ return std::tolower(x) == std::tolower(y); });
}


bool str_isalnum(const std::string& s)
{
return std::all_of(s.cbegin(), s.cend(), [](unsigned char c){return isalnum(c);});
Expand Down
40 changes: 36 additions & 4 deletions Src/LTOpenSky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
// All includes are collected in one header
#include "LiveTraffic.h"

#if IBM
#else
#include <dirent.h>
#endif

//
//MARK: OpenSky
//
Expand Down Expand Up @@ -851,10 +856,8 @@ bool OpenSkyAcMasterFile::TryOpenDbFile (int year, int month)

try {
// Is the file available already?
std::string filePath = dataRefs.GetLTPluginPath();
filePath += PATH_RESOURCES;
filePath += '/';
filePath += fileName;
const std::string fileDir = dataRefs.GetLTPluginPath() + PATH_RESOURCES + '/';
const std::string filePath = fileDir + fileName;

// Just try to open and see what happens
fAcDb.open(filePath);
Expand Down Expand Up @@ -917,6 +920,35 @@ bool OpenSkyAcMasterFile::TryOpenDbFile (int year, int month)

// looks good!
fAcDb.clear();

// Lastly, we remove all _other_ database files given that each takes up 50MB of disk space
// (Can't use XPLMGetDirectoryContents in non-main thread,
// using std::filesystem crashed CURL...
// so we go back to basic POSIX C and native Windows)
{
std::vector<std::string> vToBeDeleted;
#if IBM
#error Traversing directory to be implemented
#else
DIR *d = nullptr;
struct dirent *dir = nullptr;
d = opendir(fileDir.c_str());
if (d) {
while ((dir = readdir(d)) != NULL) {
// If begins like a database file but is not the one we just processed
std::string f = dir->d_name;
if (stribeginwith(f, OPSKY_MD_DB_FILE_BEGIN) &&
!striequal(f, fileName))
vToBeDeleted.emplace_back(std::move(f));
}
closedir(d);
}
#endif
// Now delete what we remembered
for (const std::string& p: vToBeDeleted)
std::remove((fileDir+p).c_str());
}

return true;

} catch (const std::exception& e) {
Expand Down
5 changes: 4 additions & 1 deletion docs/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ <h3>v3.5.1</h3>
<P>Change log:</P>

<ul>
<li>Not designated a Beta version. No usage time limit.</li>
<li>Not designated a Beta version as v3.5.0 accidently was. No usage time limit.</li>
<li>
RealTraffic with historic data:
<ul>
Expand All @@ -156,6 +156,9 @@ <h3>v3.5.1</h3>
historic timestamp when modifying timestamp again.</li>
</ul>
</li>
<li>
OpenSky Master File: Cleanup old database files.
</li>
</ul>

<h3>v3.5.0</h3>
Expand Down

0 comments on commit 2282137

Please sign in to comment.