mirror of
https://github.com/minetest/minetest.git
synced 2025-03-21 01:32:34 +01:00
Optimize folder handling in 'files' mod storage backend
This regressed in bf22569019749e421e8ffe0a73cff988a9a9c846.
This commit is contained in:
@ -430,35 +430,28 @@ void ModMetadataDatabaseFiles::beginSave()
|
|||||||
|
|
||||||
void ModMetadataDatabaseFiles::endSave()
|
void ModMetadataDatabaseFiles::endSave()
|
||||||
{
|
{
|
||||||
|
if (m_modified.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
if (!fs::CreateAllDirs(m_storage_dir)) {
|
if (!fs::CreateAllDirs(m_storage_dir)) {
|
||||||
errorstream << "ModMetadataDatabaseFiles: Unable to save. '" << m_storage_dir
|
errorstream << "ModMetadataDatabaseFiles: Unable to save. '"
|
||||||
<< "' tree cannot be created." << std::endl;
|
<< m_storage_dir << "' cannot be created." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fs::IsDir(m_storage_dir)) {
|
||||||
|
errorstream << "ModMetadataDatabaseFiles: Unable to save. '"
|
||||||
|
<< m_storage_dir << "' is not a directory." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = m_modified.begin(); it != m_modified.end();) {
|
for (auto it = m_modified.begin(); it != m_modified.end();) {
|
||||||
const std::string &modname = *it;
|
const std::string &modname = *it;
|
||||||
|
|
||||||
if (!fs::PathExists(m_storage_dir)) {
|
|
||||||
if (!fs::CreateAllDirs(m_storage_dir)) {
|
|
||||||
errorstream << "ModMetadataDatabaseFiles[" << modname
|
|
||||||
<< "]: Unable to save. '" << m_storage_dir
|
|
||||||
<< "' tree cannot be created." << std::endl;
|
|
||||||
++it;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else if (!fs::IsDir(m_storage_dir)) {
|
|
||||||
errorstream << "ModMetadataDatabaseFiles[" << modname << "]: Unable to save. '"
|
|
||||||
<< m_storage_dir << "' is not a directory." << std::endl;
|
|
||||||
++it;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Json::Value &json = m_mod_meta[modname];
|
const Json::Value &json = m_mod_meta[modname];
|
||||||
|
|
||||||
if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) {
|
if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) {
|
||||||
errorstream << "ModMetadataDatabaseFiles[" << modname
|
errorstream << "ModMetadataDatabaseFiles[" << modname
|
||||||
<< "]: failed write file." << std::endl;
|
<< "]: failed to write file." << std::endl;
|
||||||
++it;
|
++it;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -484,29 +477,25 @@ void ModMetadataDatabaseFiles::listMods(std::vector<std::string> *res)
|
|||||||
Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modname)
|
Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modname)
|
||||||
{
|
{
|
||||||
auto found = m_mod_meta.find(modname);
|
auto found = m_mod_meta.find(modname);
|
||||||
if (found == m_mod_meta.end()) {
|
if (found != m_mod_meta.end())
|
||||||
fs::CreateAllDirs(m_storage_dir);
|
|
||||||
|
|
||||||
Json::Value meta(Json::objectValue);
|
|
||||||
|
|
||||||
std::string path = m_storage_dir + DIR_DELIM + modname;
|
|
||||||
if (fs::PathExists(path)) {
|
|
||||||
std::ifstream is(path.c_str(), std::ios_base::binary);
|
|
||||||
|
|
||||||
Json::CharReaderBuilder builder;
|
|
||||||
builder.settings_["collectComments"] = false;
|
|
||||||
std::string errs;
|
|
||||||
|
|
||||||
if (!Json::parseFromStream(builder, is, &meta, &errs)) {
|
|
||||||
errorstream << "ModMetadataDatabaseFiles[" << modname
|
|
||||||
<< "]: failed read data (Json decoding failure). Message: "
|
|
||||||
<< errs << std::endl;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &(m_mod_meta[modname] = meta);
|
|
||||||
} else {
|
|
||||||
return &found->second;
|
return &found->second;
|
||||||
|
|
||||||
|
Json::Value meta(Json::objectValue);
|
||||||
|
|
||||||
|
std::string path = m_storage_dir + DIR_DELIM + modname;
|
||||||
|
if (fs::PathExists(path)) {
|
||||||
|
std::ifstream is(path.c_str(), std::ios_base::binary);
|
||||||
|
|
||||||
|
Json::CharReaderBuilder builder;
|
||||||
|
builder.settings_["collectComments"] = false;
|
||||||
|
std::string errs;
|
||||||
|
|
||||||
|
if (!Json::parseFromStream(builder, is, &meta, &errs)) {
|
||||||
|
errorstream << "ModMetadataDatabaseFiles[" << modname
|
||||||
|
<< "]: failed to decode data: " << errs << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &(m_mod_meta[modname] = meta);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user