Read-Only Configuration-Database support (#1046)

This commit is contained in:
LordGrey
2020-11-01 19:47:30 +01:00
committed by GitHub
parent 85a55de28c
commit bb652ade36
35 changed files with 268 additions and 90 deletions

View File

@@ -61,10 +61,10 @@
HyperionDaemon *HyperionDaemon::daemon = nullptr;
HyperionDaemon::HyperionDaemon(const QString rootPath, QObject *parent, bool logLvlOverwrite)
HyperionDaemon::HyperionDaemon(const QString rootPath, QObject *parent, bool logLvlOverwrite, bool readonlyMode)
: QObject(parent), _log(Logger::getInstance("DAEMON"))
, _instanceManager(new HyperionIManager(rootPath, this))
, _authManager(new AuthManager(this))
, _instanceManager(new HyperionIManager(rootPath, this, readonlyMode))
, _authManager(new AuthManager(this, readonlyMode))
#ifdef ENABLE_AVAHI
, _bonjourBrowserWrapper(new BonjourBrowserWrapper())
#endif
@@ -97,7 +97,7 @@ HyperionDaemon::HyperionDaemon(const QString rootPath, QObject *parent, bool log
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
// init settings
_settingsManager = new SettingsManager(0, this);
_settingsManager = new SettingsManager(0, this, readonlyMode);
// set inital log lvl if the loglvl wasn't overwritten by arg
if (!logLvlOverwrite)

View File

@@ -86,7 +86,7 @@ class HyperionDaemon : public QObject
friend SysTray;
public:
HyperionDaemon(QString rootPath, QObject *parent, bool logLvlOverwrite);
HyperionDaemon(QString rootPath, QObject *parent, bool logLvlOverwrite, bool readonlyMode = false);
~HyperionDaemon();
///

View File

@@ -256,8 +256,14 @@ int main(int argc, char** argv)
return 0;
}
if (parser.isSet(versionOption))
{
std::cout
<< "Hyperion Ambilight Daemon" << std::endl
<< "\tVersion : " << HYPERION_VERSION << " (" << HYPERION_BUILD_ID << ")" << std::endl
<< "\tBuild Time: " << __DATE__ << " " << __TIME__ << std::endl;
}
if (parser.isSet(exportEfxOption))
{
Q_INIT_RESOURCE(EffectEngine);
@@ -265,7 +271,7 @@ int main(int argc, char** argv)
QDir destDir(exportEfxOption.value(parser));
if (directory.exists() && destDir.exists())
{
std::cout << "extract to folder: " << std::endl;
std::cout << "Extract to folder: " << std::endl;
QStringList filenames = directory.entryList(QStringList() << "*", QDir::Files, QDir::Name | QDir::IgnoreCase);
QString destFileName;
for (const QString & filename : filenames)
@@ -278,70 +284,129 @@ int main(int argc, char** argv)
if (QFile::copy(QString(":/effects/")+filename, destFileName))
{
QFile::setPermissions(destFileName, PERM0664 );
std::cout << "ok" << std::endl;
std::cout << "OK" << std::endl;
}
else
{
std::cout << "error, aborting" << std::endl;
std::cout << "Error, aborting" << std::endl;
return 1;
}
}
return 0;
}
Error(log, "can not export to %s",exportEfxOption.getCString(parser));
Error(log, "Can not export to %s",exportEfxOption.getCString(parser));
return 1;
}
int rc = 1;
bool readonlyMode = false;
try
{
// handle and create userDataPath for user data, default path is home directory + /.hyperion
// NOTE: No further checks inside Hyperion. FileUtils::writeFile() will resolve permission errors and others that occur during runtime
QString userDataPath(userDataOption.value(parser));
QDir mDir(userDataPath);
QFileInfo mFi(userDataPath);
if(!mDir.mkpath(userDataPath) || !mFi.isWritable() || !mDir.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!");
if(!mDir.mkpath(userDataPath) || !mFi.isWritable())
{
if ( !mDir.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!");
}
else
{
QFileInfo mFiDB(userDataPath + "/db/hyperion.db");
if ( !mFiDB.exists() )
{
throw std::runtime_error("Configuration database '"+mFiDB.absoluteFilePath().toStdString()+"' does not exist!");
}
else
{
if ( !mFiDB.isReadable() )
{
throw std::runtime_error("Configuration database '"+mFiDB.absoluteFilePath().toStdString()+"' is not readable. Please setup permissions correctly!");
}
else
{
if ( !mFiDB.isWritable() )
{
readonlyMode = true;
}
}
}
}
}
// reset Password without spawning daemon
if(parser.isSet(resetPassword))
{
AuthTable* table = new AuthTable(userDataPath);
if(table->resetHyperionUser()){
Info(log,"Password reset successfull");
delete table;
exit(0);
} else {
Error(log,"Failed to reset password!");
delete table;
exit(1);
if ( readonlyMode )
{
Error(log,"Password reset is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(mDir.absolutePath()));
throw std::runtime_error("Password reset failed");
}
else
{
AuthTable* table = new AuthTable(userDataPath);
if(table->resetHyperionUser()){
Info(log,"Password reset successful");
delete table;
exit(0);
} else {
Error(log,"Failed to reset password!");
delete table;
exit(1);
}
}
}
// delete database before start
if(parser.isSet(deleteDB))
{
const QString dbFile = mDir.absolutePath() + "/db/hyperion.db";
if (QFile::exists(dbFile))
if ( readonlyMode )
{
if (!QFile::remove(dbFile))
Error(log,"Deleting the configuration database is not possible. The user data path '%s' is not writeable.", QSTRING_CSTR(mDir.absolutePath()));
throw std::runtime_error("Deleting the configuration database failed");
}
else
{
const QString dbFile = mDir.absolutePath() + "/db/hyperion.db";
if (QFile::exists(dbFile))
{
Info(log,"Failed to delete Database!");
exit(1);
if (!QFile::remove(dbFile))
{
Info(log,"Failed to delete Database!");
exit(1);
}
else
{
Info(log,"Configuration database deleted successfully.");
}
}
else
{
Warning(log,"Configuration database [%s] does not exist!", QSTRING_CSTR(dbFile));
}
}
}
Info(log,"Starting Hyperion - %s, %s, built: %s:%s", HYPERION_VERSION, HYPERION_BUILD_ID, __DATE__, __TIME__);
Info(log, "Set user data path to '%s'", QSTRING_CSTR(mDir.absolutePath()));
if ( !readonlyMode )
{
Info(log, "Set user data path to '%s'", QSTRING_CSTR(mDir.absolutePath()));
}
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()));
}
HyperionDaemon* hyperiond = nullptr;
HyperionDaemon* hyperiond = nullptr;
try
{
hyperiond = new HyperionDaemon(userDataPath, qApp, bool(logLevelCheck));
hyperiond = new HyperionDaemon(userDataPath, qApp, bool(logLevelCheck), readonlyMode);
}
catch (std::exception& e)
{