Skip to content

Commit aa90bb0

Browse files
committed
Update chanfix_core.cpp
1 parent cfcf200 commit aa90bb0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

modules/third/ChanFix/chanfix_core.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using cf_channel_map = Anope::unordered_map<CFChannelData *>;
1111
static Serialize::Checker<cf_channel_map> ChanFixChannelList(CHANFIX_CHANNEL_DATA_TYPE);
1212

1313
static constexpr const char* LEGACY_DB_MAGIC = "chanfix";
14+
static constexpr unsigned LEGACY_DB_VERSION = 1;
1415

1516
static Anope::string GetLegacyDBPath()
1617
{
@@ -190,6 +191,7 @@ ChanFixCore::~ChanFixCore()
190191

191192
void ChanFixCore::LegacyImportIfNeeded()
192193
{
194+
// Only import if there are no records loaded via the configured DB backend.
193195
if (!ChanFixChannelList->empty())
194196
return;
195197

@@ -205,10 +207,10 @@ void ChanFixCore::LegacyImportIfNeeded()
205207
std::string raw;
206208
Anope::string line;
207209
bool header_ok = false;
208-
std::string raw;
210+
unsigned int imported = 0;
211+
while (std::getline(in, raw))
209212
{
210213
line = Anope::string(raw);
211-
unsigned int imported = 0;
212214
line.trim();
213215
if (line.empty())
214216
continue;
@@ -219,6 +221,10 @@ void ChanFixCore::LegacyImportIfNeeded()
219221
sepstream(line, '|').GetTokens(parts);
220222
if (parts.size() >= 2 && parts[0].equals_ci(LEGACY_DB_MAGIC))
221223
{
224+
unsigned int ver = 0;
225+
try { ver = Anope::Convert<unsigned int>(parts[1], 0); } catch (...) { ver = 0; }
226+
if (ver != LEGACY_DB_VERSION)
227+
return;
222228
header_ok = true;
223229
continue;
224230
}
@@ -300,6 +306,7 @@ void ChanFixCore::LegacyImportIfNeeded()
300306
}
301307
}
302308

309+
// Queue all imported objects for persistence.
303310
for (const auto& [_, rec] : *ChanFixChannelList)
304311
if (rec)
305312
rec->QueueUpdate();
@@ -311,6 +318,7 @@ void ChanFixCore::LegacyImportIfNeeded()
311318
if (imported > 0)
312319
this->legacy_import_needs_save = true;
313320

321+
// Move the legacy file out of the way so we don't re-import.
314322
const fs::path migrated = path.string() + ".migrated";
315323
fs::rename(path, migrated, ec);
316324
}
@@ -761,9 +769,10 @@ void ChanFixCore::ExpireTick()
761769
for (auto it = ChanFixChannelList->begin(); it != ChanFixChannelList->end();)
762770
{
763771
CFChannelData* recp = it->second;
764-
++it;
772+
++it; // Advance early in case we delete the record.
765773
if (!recp)
766774
continue;
775+
767776
CFChannelData& rec = *recp;
768777
bool dirty = false;
769778

0 commit comments

Comments
 (0)