ktimezoned: clean zone information directory path before using it

fixes symlink strategy when installation path from KStandardDirs query exists

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-07-07 21:49:00 +02:00
parent 62c3a8ff03
commit 50b3b5bbad
2 changed files with 25 additions and 16 deletions

View file

@ -131,6 +131,7 @@ ClockHelper::CH_Error ClockHelper::tz( const QString& selectedzone )
foreach (const QString &zonedir, zoneDirs) { foreach (const QString &zonedir, zoneDirs) {
if (QDir(zonedir).exists()) { if (QDir(zonedir).exists()) {
ZONE_INFO_DIR = zonedir; ZONE_INFO_DIR = zonedir;
break;
} }
} }

View file

@ -94,12 +94,16 @@ void KTimeZoned::init(bool restart)
if (restart) if (restart)
config.reparseConfiguration(); config.reparseConfiguration();
KConfigGroup group(&config, "TimeZones"); KConfigGroup group(&config, "TimeZones");
mZoneinfoDir = group.readEntry(ZONEINFO_DIR); mZoneinfoDir = QDir::cleanPath(group.readEntry(ZONEINFO_DIR));
mZoneTab = group.readEntry(ZONE_TAB); mZoneTab = group.readEntry(ZONE_TAB);
mConfigLocalZone = group.readEntry(LOCAL_ZONE); mConfigLocalZone = group.readEntry(LOCAL_ZONE);
if (mZoneinfoDir.length() > 1 && mZoneinfoDir.endsWith('/')) if (mZoneinfoDir.length() > 1 && mZoneinfoDir.endsWith('/'))
mZoneinfoDir.truncate(mZoneinfoDir.length() - 1); // strip trailing '/' mZoneinfoDir.truncate(mZoneinfoDir.length() - 1); // strip trailing '/'
kDebug(1221) << ZONEINFO_DIR << mZoneinfoDir;
kDebug(1221) << ZONE_TAB << mZoneTab;
kDebug(1221) << LOCAL_ZONE << mConfigLocalZone;
// For Unix, read zone.tab. // For Unix, read zone.tab.
QString oldZoneinfoDir = mZoneinfoDir; QString oldZoneinfoDir = mZoneinfoDir;
@ -193,6 +197,7 @@ bool KTimeZoned::findZoneTab(QFile& f)
foreach (const QString &zonedir, zoneDirs) { foreach (const QString &zonedir, zoneDirs) {
if (QDir(zonedir).exists()) { if (QDir(zonedir).exists()) {
ZONE_INFO_DIR = zonedir; ZONE_INFO_DIR = zonedir;
break;
} }
} }
@ -202,8 +207,8 @@ bool KTimeZoned::findZoneTab(QFile& f)
// make a note if the dir exists; whether it contains zone.tab or not // make a note if the dir exists; whether it contains zone.tab or not
if (dir.exists(zoneinfoDir)) if (dir.exists(zoneinfoDir))
{ {
mZoneinfoDir = zoneinfoDir; mZoneinfoDir = QDir::cleanPath(zoneinfoDir);
f.setFileName(zoneinfoDir + ZONE_TAB_FILE); f.setFileName(mZoneinfoDir + ZONE_TAB_FILE);
if (f.open(QIODevice::ReadOnly)) if (f.open(QIODevice::ReadOnly))
return true; return true;
kDebug(1221) << "Can't open " << f.fileName(); kDebug(1221) << "Can't open " << f.fileName();
@ -212,8 +217,8 @@ bool KTimeZoned::findZoneTab(QFile& f)
zoneinfoDir = ::getenv("TZDIR"); zoneinfoDir = ::getenv("TZDIR");
if (!zoneinfoDir.isEmpty() && dir.exists(zoneinfoDir)) if (!zoneinfoDir.isEmpty() && dir.exists(zoneinfoDir))
{ {
mZoneinfoDir = zoneinfoDir; mZoneinfoDir = QDir::cleanPath(zoneinfoDir);
f.setFileName(zoneinfoDir + ZONE_TAB_FILE); f.setFileName(mZoneinfoDir + ZONE_TAB_FILE);
if (f.open(QIODevice::ReadOnly)) if (f.open(QIODevice::ReadOnly))
return true; return true;
kDebug(1221) << "Can't open " << f.fileName(); kDebug(1221) << "Can't open " << f.fileName();
@ -269,7 +274,9 @@ void KTimeZoned::findLocalZone()
const char *envtz = ::getenv("TZ"); const char *envtz = ::getenv("TZ");
if (checkTZ(envtz)) { if (checkTZ(envtz)) {
mSavedTZ = envtz; mSavedTZ = envtz;
if (!mLocalZone.isEmpty()) kDebug(1221)<<"TZ: "<<mLocalZone; if (!mLocalZone.isEmpty()) {
kDebug(1221) << "TZ: " << mLocalZone;
}
} }
if (mLocalZone.isEmpty() && !mZoneinfoDir.isEmpty()) { if (mLocalZone.isEmpty() && !mZoneinfoDir.isEmpty()) {
@ -309,7 +316,7 @@ void KTimeZoned::findLocalZone()
if (candidateOffset < bestOffset && zone.parse()) { if (candidateOffset < bestOffset && zone.parse()) {
QList<QByteArray> abbrs = zone.abbreviations(); QList<QByteArray> abbrs = zone.abbreviations();
if (abbrs.contains(tzname0) && abbrs.contains(tzname1)) { if (abbrs.contains(tzname0) && abbrs.contains(tzname1)) {
// kDebug(1221) << "local=" << zone.name(); kDebug(1221) << "local=" << zone.name();
mLocalZone = zone.name(); mLocalZone = zone.name();
bestOffset = candidateOffset; bestOffset = candidateOffset;
if (!bestOffset) if (!bestOffset)
@ -319,13 +326,15 @@ void KTimeZoned::findLocalZone()
} }
KSystemTimeZoneSource::endParseBlock(); KSystemTimeZoneSource::endParseBlock();
if (!mLocalZone.isEmpty()) { if (!mLocalZone.isEmpty()) {
kDebug(1221)<<"tzname: "<<mLocalZone; kDebug(1221) << "tzname:" << mLocalZone;
} }
} }
if (mLocalZone.isEmpty()) { if (mLocalZone.isEmpty()) {
// SOLUTION 6: FAILSAFE. // SOLUTION 6: FAILSAFE.
mLocalZone = KTimeZone::utc().name(); mLocalZone = KTimeZone::utc().name();
if (!mLocalZone.isEmpty()) kDebug(1221)<<"Failsafe: "<<mLocalZone; if (!mLocalZone.isEmpty()) {
kDebug(1221) << "Failsafe:" << mLocalZone;
}
} }
// Finally, if the local zone identity has changed, store // Finally, if the local zone identity has changed, store
@ -438,7 +447,7 @@ bool KTimeZoned::checkTimezone()
return false; return false;
} }
mLocalIdFile = f.fileName(); mLocalIdFile = f.fileName();
kDebug(1221)<<"/etc/timezone: "<<mLocalZone; kDebug(1221) << "/etc/timezone:" << mLocalZone;
return true; return true;
} }
@ -461,7 +470,7 @@ bool KTimeZoned::matchZoneFile(const QString &path)
// Note that some systems (e.g. Gentoo) have zones under zoneinfo which // Note that some systems (e.g. Gentoo) have zones under zoneinfo which
// are not in zone.tab, so don't validate against mZones. // are not in zone.tab, so don't validate against mZones.
mLocalZone = zoneInfoFileName.mid(mZoneinfoDir.length() + 1); mLocalZone = zoneInfoFileName.mid(mZoneinfoDir.length() + 1);
// kDebug(1221) << "local=" << mLocalZone; kDebug(1221) << "local=" << mLocalZone;
} else { } else {
// It isn't a zoneinfo file or a copy thereof. // It isn't a zoneinfo file or a copy thereof.
// Use the absolute path as the time zone name. // Use the absolute path as the time zone name.
@ -469,11 +478,10 @@ bool KTimeZoned::matchZoneFile(const QString &path)
} }
mLocalIdFile = f.fileName(); mLocalIdFile = f.fileName();
mLocalZoneDataFile = zoneInfoFileName; mLocalZoneDataFile = zoneInfoFileName;
kDebug(1221)<<mLocalIdFile<<": "<<mLocalZone; kDebug(1221) << "symlink" << mLocalIdFile << ":" << mLocalZone;
return true; return true;
} }
} } else if (f.open(QIODevice::ReadOnly)) {
else if (f.open(QIODevice::ReadOnly)) {
// SOLUTION 3: DEFINITIVE. // SOLUTION 3: DEFINITIVE.
// Try to match the file against the list of zoneinfo files. // Try to match the file against the list of zoneinfo files.
@ -535,7 +543,7 @@ bool KTimeZoned::matchZoneFile(const QString &path)
f.close(); f.close();
if (success) { if (success) {
mLocalIdFile = f.fileName(); mLocalIdFile = f.fileName();
kDebug(1221)<<mLocalIdFile<<": "<<mLocalZone; kDebug(1221) << "file" << mLocalIdFile << ":" << mLocalZone;
return true; return true;
} }
} }