Fix, if db-file is readonly (#1082)

This commit is contained in:
LordGrey 2020-11-14 17:40:15 +01:00 committed by GitHub
parent 56f45a4930
commit d28540a7fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 37 deletions

View File

@ -263,7 +263,7 @@ int main(int argc, char** argv)
QDir destDir(exportEfxOption.value(parser)); QDir destDir(exportEfxOption.value(parser));
if (directory.exists() && destDir.exists()) if (directory.exists() && destDir.exists())
{ {
std::cout << "Extract to folder: " << std::endl; std::cout << "Extract to folder: " << destDir.absolutePath().toStdString() << std::endl;
QStringList filenames = directory.entryList(QStringList() << "*", QDir::Files, QDir::Name | QDir::IgnoreCase); QStringList filenames = directory.entryList(QStringList() << "*", QDir::Files, QDir::Name | QDir::IgnoreCase);
QString destFileName; QString destFileName;
for (const QString & filename : filenames) for (const QString & filename : filenames)
@ -280,8 +280,8 @@ int main(int argc, char** argv)
} }
else else
{ {
std::cout << "Error, aborting" << std::endl; std::cout << "Error, aborting" << std::endl;
return 1; return 1;
} }
} }
return 0; return 0;
@ -294,39 +294,36 @@ int main(int argc, char** argv)
int rc = 1; int rc = 1;
bool readonlyMode = false; bool readonlyMode = false;
QString userDataPath(userDataOption.value(parser));
QDir userDataDirectory(userDataPath);
QFileInfo dbFile(userDataDirectory.absolutePath() +"/db/hyperion.db");
try try
{ {
// handle and create userDataPath for user data, default path is home directory + /.hyperion
QString userDataPath(userDataOption.value(parser));
QDir mDir(userDataPath); if (dbFile.exists())
QFileInfo mFi(userDataPath);
if(!mDir.mkpath(userDataPath) || !mFi.isWritable())
{ {
if ( !mDir.isReadable() ) if (!dbFile.isReadable())
{ {
throw std::runtime_error("The user data path '"+mDir.absolutePath().toStdString()+"' can't be created or isn't read/writeable. Please setup permissions correctly!"); throw std::runtime_error("Configuration database '" + dbFile.absoluteFilePath().toStdString() + "' is not readable. Please setup permissions correctly!");
} }
else else
{ {
QFileInfo mFiDB(userDataPath + "/db/hyperion.db"); if (!dbFile.isWritable())
if ( !mFiDB.exists() )
{ {
throw std::runtime_error("Configuration database '"+mFiDB.absoluteFilePath().toStdString()+"' does not exist!"); readonlyMode = true;
} }
else }
}
else
{
if (!userDataDirectory.mkpath(dbFile.absolutePath()))
{
if (!userDataDirectory.isReadable() || !dbFile.isWritable())
{ {
if ( !mFiDB.isReadable() ) throw std::runtime_error("The user data path '" + userDataDirectory.absolutePath().toStdString() + "' can't be created or isn't read/writeable. Please setup permissions correctly!");
{
throw std::runtime_error("Configuration database '"+mFiDB.absoluteFilePath().toStdString()+"' is not readable. Please setup permissions correctly!");
}
else
{
if ( !mFiDB.isWritable() )
{
readonlyMode = true;
}
}
} }
} }
} }
@ -336,12 +333,12 @@ int main(int argc, char** argv)
{ {
if ( readonlyMode ) if ( readonlyMode )
{ {
Error(log,"Password reset is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(mDir.absolutePath())); Error(log,"Password reset is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(userDataDirectory.absolutePath()));
throw std::runtime_error("Password reset failed"); throw std::runtime_error("Password reset failed");
} }
else else
{ {
AuthTable* table = new AuthTable(userDataPath); AuthTable* table = new AuthTable(userDataDirectory.absolutePath());
if(table->resetHyperionUser()){ if(table->resetHyperionUser()){
Info(log,"Password reset successful"); Info(log,"Password reset successful");
delete table; delete table;
@ -359,15 +356,14 @@ int main(int argc, char** argv)
{ {
if ( readonlyMode ) if ( readonlyMode )
{ {
Error(log,"Deleting the configuration database is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(mDir.absolutePath())); Error(log,"Deleting the configuration database is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(dbFile.absolutePath()));
throw std::runtime_error("Deleting the configuration database failed"); throw std::runtime_error("Deleting the configuration database failed");
} }
else else
{ {
const QString dbFile = mDir.absolutePath() + "/db/hyperion.db"; if (QFile::exists(dbFile.absoluteFilePath()))
if (QFile::exists(dbFile))
{ {
if (!QFile::remove(dbFile)) if (!QFile::remove(dbFile.absoluteFilePath()))
{ {
Info(log,"Failed to delete Database!"); Info(log,"Failed to delete Database!");
exit(1); exit(1);
@ -379,7 +375,7 @@ int main(int argc, char** argv)
} }
else else
{ {
Warning(log,"Configuration database [%s] does not exist!", QSTRING_CSTR(dbFile)); Warning(log,"Configuration database [%s] does not exist!", QSTRING_CSTR(dbFile.absoluteFilePath()));
} }
} }
} }
@ -389,17 +385,17 @@ int main(int argc, char** argv)
if ( !readonlyMode ) if ( !readonlyMode )
{ {
Info(log, "Set user data path to '%s'", QSTRING_CSTR(mDir.absolutePath())); Info(log, "Set user data path to '%s'", QSTRING_CSTR(userDataDirectory.absolutePath()));
} }
else else
{ {
Warning(log,"The user data path '%s' is not writeable. Hyperion starts in read-only mode. Configuration updates will not be persisted!", QSTRING_CSTR(mDir.absolutePath())); Warning(log,"The user data path '%s' is not writeable. Hyperion starts in read-only mode. Configuration updates will not be persisted!", QSTRING_CSTR(userDataDirectory.absolutePath()));
} }
HyperionDaemon* hyperiond = nullptr; HyperionDaemon* hyperiond = nullptr;
try try
{ {
hyperiond = new HyperionDaemon(userDataPath, qApp, bool(logLevelCheck), readonlyMode); hyperiond = new HyperionDaemon(userDataDirectory.absolutePath(), qApp, bool(logLevelCheck), readonlyMode);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@ -430,5 +426,13 @@ int main(int argc, char** argv)
// delete components // delete components
Logger::deleteInstance(); Logger::deleteInstance();
#ifdef _WIN32
if (parser.isSet(consoleOption))
{
system("pause");
}
#endif
return rc; return rc;
} }