Skip to content

Commit

Permalink
Merge pull request #312 from Genio-The-Haiku-IDE/fix/config-no-error
Browse files Browse the repository at this point in the history
config manager creates only the storage provider needed (fix bug #310)
  • Loading branch information
Freaxed authored Feb 10, 2024
2 parents 339f917 + cfd4e03 commit 01c2fc5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/GenioApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ GenioApp::GenioApp()
_PrepareConfig(gCFG);

// Global settings file check.
if (gCFG.LoadFromFile(fConfigurationPath) != B_OK) {
if (gCFG.LoadFromFile({fConfigurationPath}) != B_OK) {
LogInfo("Cannot load global settings file");
}

Expand All @@ -74,7 +74,7 @@ GenioApp::GenioApp()
GenioApp::~GenioApp()
{
// Save settings on quit, anyway
gCFG.SaveToFile(fConfigurationPath);
gCFG.SaveToFile({fConfigurationPath});
LSPServersManager::DisposeLSPServersConfig();
}

Expand Down Expand Up @@ -180,7 +180,7 @@ GenioApp::MessageReceived(BMessage* message)
else if (::strcmp(key, "log_level") == 0)
Logger::SetLevel(log_level(int32(gCFG["log_level"])));
}
gCFG.SaveToFile(fConfigurationPath);
gCFG.SaveToFile({fConfigurationPath});
LogInfo("Configuration file saved! (updating %s)", message->GetString("key", "ERROR!"));
}
break;
Expand Down
113 changes: 81 additions & 32 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,39 @@ class PermanentStorageProvider {
public:
enum kPSPMode { kPSPReadMode, kPSPWriteMode };

PermanentStorageProvider(BPath destination, kPSPMode mode) {};
PermanentStorageProvider() {};
virtual ~PermanentStorageProvider(){};

virtual status_t InitCheck() = 0;
virtual status_t Open(BPath destination, kPSPMode mode) = 0;
virtual status_t Close() = 0;
virtual status_t LoadKey(ConfigManager& manager, const char* key, GMessage& storage, GMessage& parConfig) = 0;
virtual status_t SaveKey(ConfigManager& manager, const char* key, GMessage& storage) = 0;

};

class BMessagePSP : public PermanentStorageProvider {
public:
BMessagePSP(BPath dest, kPSPMode mode): PermanentStorageProvider(dest, mode){
status = file.SetTo(dest.Path(), mode == PermanentStorageProvider::kPSPReadMode ? B_READ_ONLY : (B_WRITE_ONLY | B_CREATE_FILE));
BMessagePSP() {}

status_t Open(BPath dest, kPSPMode mode) {
uint32 fileMode = mode == PermanentStorageProvider::kPSPReadMode ? B_READ_ONLY : (B_WRITE_ONLY | B_CREATE_FILE);
status_t status = file.SetTo(dest.Path(), fileMode);
if (status == B_OK && file.IsReadable()) {
status = fromFile.Unflatten(&file);
}
return status;
}
~BMessagePSP() {

status_t Close() {
status_t status = file.InitCheck();
if (status == B_OK && file.IsWritable()) {
fromFile.Flatten(&file);
file.Flush();
status = fromFile.Flatten(&file);
if (status == B_OK) {
status = file.Flush();
}
file.Unset();
return status;
}
}

status_t InitCheck() {
return status;
}

Expand All @@ -59,7 +68,7 @@ class BMessagePSP : public PermanentStorageProvider {
private:
BFile file;
GMessage fromFile;
status_t status = B_ERROR;


bool _SameTypeAndFixedSize(
BMessage* msgL, const char* keyL, BMessage* msgR, const char* keyR) const
Expand All @@ -79,11 +88,10 @@ class BMessagePSP : public PermanentStorageProvider {

class AttributePSP : public PermanentStorageProvider {
public:
AttributePSP(BPath attributeFilePath, kPSPMode mode): PermanentStorageProvider(attributeFilePath, mode){
status = nodeAttr.SetTo(attributeFilePath.Path());
}
AttributePSP(){}

status_t InitCheck() { return status; }
status_t Open(BPath attributeFilePath, kPSPMode mode) { return nodeAttr.SetTo(attributeFilePath.Path()); }
status_t Close() { return B_OK;}
status_t LoadKey(ConfigManager& manager, const char* key, GMessage& storage, GMessage& paramerConfig)
{
BString attrName("genio:");
Expand Down Expand Up @@ -127,7 +135,6 @@ class AttributePSP : public PermanentStorageProvider {
}
private:
BNode nodeAttr;
status_t status = B_ERROR;
};


Expand All @@ -137,6 +144,31 @@ ConfigManager::ConfigManager(const int32 messageWhat)
fWhat(messageWhat)
{
assert(fLocker.InitCheck() == B_OK);
for (int32 i=0;i< kStorageTypeCountNb;i++)
fPSPList[i] = nullptr;
}
ConfigManager::~ConfigManager()
{
for (int32 i=0;i< kStorageTypeCountNb;i++)
if (fPSPList[i] != nullptr) {
delete fPSPList[i];
fPSPList[i] = nullptr;
}
}

PermanentStorageProvider*
ConfigManager::CreatePSPByType(StorageType type)
{
switch (type) {
case kStorageTypeBMessage:
return new BMessagePSP();
case kStorageTypeAttribute:
return new AttributePSP();
default:
LogErrorF("Invalid StorageType! %d", type);
return nullptr;
};
return nullptr;
}


Expand All @@ -156,54 +188,71 @@ ConfigManager::Has(GMessage& msg, const char* key) const


status_t
ConfigManager::LoadFromFile(BPath messageFilePath, BPath attributeFilePath)
ConfigManager::LoadFromFile(std::array<BPath, kStorageTypeCountNb> paths)
{
status_t status = B_OK;
BMessagePSP storageM(messageFilePath, PermanentStorageProvider::kPSPReadMode);
AttributePSP storageA(attributeFilePath, PermanentStorageProvider::kPSPReadMode);
for (int32 i=0;i<kStorageTypeCountNb;i++) {
if (fPSPList[i] != nullptr &&
fPSPList[i]->Open(paths[i], PermanentStorageProvider::kPSPReadMode) != B_OK)

return B_ERROR;
}
GMessage msg;
int i = 0;
while (fConfiguration.FindMessage("config", i++, &msg) == B_OK) {
const char* key = msg["key"];
StorageType storageType = (StorageType)((int32)msg["storage_type"]);
status_t status = B_ERROR;
if (storageType == kStorageTypeBMessage && storageM.InitCheck() == B_OK) {
status = storageM.LoadKey(*this, key, fStorage, msg);
} else if (storageType == kStorageTypeAttribute && storageA.InitCheck() == B_OK) {
status = storageA.LoadKey(*this, key, fStorage, msg);
PermanentStorageProvider* provider = fPSPList[storageType];
if (provider == nullptr) {
LogErrorF("Invalid PermanentStorageProvider (%d)", storageType);
return B_ERROR;
}
status_t status = provider->LoadKey(*this, key, fStorage, msg);

if (status == B_OK) {
LogInfo("Config file: loaded value for key [%s] (StorageType %d)", key, storageType);
} else {
LogError("Config file: unable to get valid key [%s] (%s) (StorageType %d)", key, strerror(status), storageType);
}
}
for (int32 i=0;i<kStorageTypeCountNb;i++) {
if (fPSPList[i] != nullptr)
fPSPList[i]->Close();
}
return status;
}

status_t
ConfigManager::SaveToFile(BPath messageFilePath, BPath attributeFilePath)
ConfigManager::SaveToFile(std::array<BPath, kStorageTypeCountNb> paths)
{
status_t status = B_OK;
BMessagePSP storageM(messageFilePath, PermanentStorageProvider::kPSPWriteMode);
AttributePSP storageA(attributeFilePath, PermanentStorageProvider::kPSPWriteMode);
for (int32 i=0;i<kStorageTypeCountNb;i++) {
if (fPSPList[i] != nullptr &&
fPSPList[i]->Open(paths[i], PermanentStorageProvider::kPSPWriteMode) != B_OK)

return B_ERROR;
}
GMessage msg;
int i = 0;
while (fConfiguration.FindMessage("config", i++, &msg) == B_OK) {
const char* key = msg["key"];
status_t status = B_ERROR;
StorageType storageType = (StorageType)((int32)msg["storage_type"]);
if (storageType == kStorageTypeBMessage && storageM.InitCheck() == B_OK) {
status = storageM.SaveKey(*this, key, fStorage);
} else if (storageType == kStorageTypeAttribute && storageA.InitCheck() == B_OK) {
status = storageA.SaveKey(*this, key, fStorage);
PermanentStorageProvider* provider = fPSPList[storageType];
if (provider == nullptr) {
LogErrorF("Invalid PermanentStorageProvider (%d)", storageType);
return B_ERROR;
}
status_t status = provider->SaveKey(*this, key, fStorage);
if (status == B_OK) {
LogInfo("Config file: saved value for key [%s] (StorageType %d)", key, storageType);
} else {
LogError("Config file: unable to store valid key [%s] (%s) (StorageType %d)", key, strerror(status), storageType);
}
}
for (int32 i=0;i<kStorageTypeCountNb;i++) {
if (fPSPList[i] != nullptr)
fPSPList[i]->Close();
}
return status;
}

Expand Down
12 changes: 10 additions & 2 deletions src/config/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Autolock.h>
#include <Application.h>
#include <Path.h>
#include <array>

#include "GMessage.h"

Expand All @@ -13,10 +14,12 @@ enum StorageType {
kStorageTypeCountNb = 2
};

class PermanentStorageProvider;
class ConfigManagerReturn;
class ConfigManager {
public:
explicit ConfigManager(const int32 messageWhat);
~ConfigManager();

template<typename T>
void AddConfig(const char* group,
Expand All @@ -40,10 +43,13 @@ class ConfigManager {
fStorage[key] = defaultValue;

fConfiguration.AddMessage("config", &configKey);

if (fPSPList[(int32)storageType] == nullptr)
fPSPList[(int32)storageType] = CreatePSPByType(storageType);
}

status_t LoadFromFile(BPath messageFilePath, BPath attributeFilePath = BPath());
status_t SaveToFile(BPath messageFilePath, BPath attributeFilePath = BPath());
status_t SaveToFile(std::array<BPath, kStorageTypeCountNb> paths);
status_t LoadFromFile(std::array<BPath, kStorageTypeCountNb> paths);

void ResetToDefaults();
bool HasAllDefaultValues();
Expand Down Expand Up @@ -88,8 +94,10 @@ friend ConfigManagerReturn;
GMessage fConfiguration;
BLocker fLocker;
int32 fWhat;
PermanentStorageProvider* fPSPList[kStorageTypeCountNb];

bool _CheckKeyIsValid(const char* key) const;
PermanentStorageProvider* CreatePSPByType(StorageType type);
};


Expand Down
4 changes: 2 additions & 2 deletions src/project/ProjectFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ ProjectFolder::LoadSettings()

BPath path(Path());
path.Append(GenioNames::kProjectSettingsFile);
status_t status = fSettings->LoadFromFile(path.Path(), BPath(Path()));
status_t status = fSettings->LoadFromFile({ path.Path(), BPath(Path()) });
if (status != B_OK) {
// Try to load old style settings
status = _LoadOldSettings();
Expand All @@ -195,7 +195,7 @@ ProjectFolder::SaveSettings()

BPath path(Path());
path.Append(GenioNames::kProjectSettingsFile);
status_t status = fSettings->SaveToFile(path.Path(), BPath(Path()));
status_t status = fSettings->SaveToFile({ path.Path(), BPath(Path()) });
if (status != B_OK) {
LogErrorF("Cannot save settings: %s", ::strerror(status));
}
Expand Down

0 comments on commit 01c2fc5

Please sign in to comment.