diff options
author | sfan5 <sfan5@live.de> | 2022-01-15 17:28:59 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2022-01-17 21:49:18 +0100 |
commit | 42839fa1db85ab6ce61c5a185f91919a5a9c067c (patch) | |
tree | 3e28f2bb0df82f536c6c0fc2c5b31c90b2ce43a6 /src/database | |
parent | b02b381af26c792d7f7feaa93198bb7efb2512de (diff) | |
download | minetest-42839fa1db85ab6ce61c5a185f91919a5a9c067c.tar.gz minetest-42839fa1db85ab6ce61c5a185f91919a5a9c067c.tar.bz2 minetest-42839fa1db85ab6ce61c5a185f91919a5a9c067c.zip |
Optimize folder handling in 'files' mod storage backend
This regressed in bf22569019749e421e8ffe0a73cff988a9a9c846.
Diffstat (limited to 'src/database')
-rw-r--r-- | src/database/database-files.cpp | 63 |
1 files changed, 26 insertions, 37 deletions
diff --git a/src/database/database-files.cpp b/src/database/database-files.cpp index 9021ae61b..7c0dbac28 100644 --- a/src/database/database-files.cpp +++ b/src/database/database-files.cpp @@ -430,35 +430,28 @@ void ModMetadataDatabaseFiles::beginSave() void ModMetadataDatabaseFiles::endSave() { + if (m_modified.empty()) + return; + if (!fs::CreateAllDirs(m_storage_dir)) { - errorstream << "ModMetadataDatabaseFiles: Unable to save. '" << m_storage_dir - << "' tree cannot be created." << std::endl; + errorstream << "ModMetadataDatabaseFiles: Unable to save. '" + << 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; } for (auto it = m_modified.begin(); it != m_modified.end();) { 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]; if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) { errorstream << "ModMetadataDatabaseFiles[" << modname - << "]: failed write file." << std::endl; + << "]: failed to write file." << std::endl; ++it; continue; } @@ -484,29 +477,25 @@ void ModMetadataDatabaseFiles::listMods(std::vector<std::string> *res) Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modname) { auto found = m_mod_meta.find(modname); - if (found == m_mod_meta.end()) { - fs::CreateAllDirs(m_storage_dir); + if (found != m_mod_meta.end()) + return &found->second; - Json::Value meta(Json::objectValue); + 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); + 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; + 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; - } + 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); - } else { - return &found->second; } + + return &(m_mod_meta[modname] = meta); } |