diff options
Diffstat (limited to 'controller/FileDB.cpp')
-rw-r--r-- | controller/FileDB.cpp | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/controller/FileDB.cpp b/controller/FileDB.cpp index b2f26e7c..6942a16e 100644 --- a/controller/FileDB.cpp +++ b/controller/FileDB.cpp @@ -75,25 +75,32 @@ void FileDB::save(nlohmann::json *orig,nlohmann::json &record) { char p1[4096],p2[4096],pb[4096]; try { - nlohmann::json rec(record); - const std::string objtype = rec["objtype"]; + if (orig) { + if (*orig != record) { + record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1; + } + } else { + record["revision"] = 1; + } + + const std::string objtype = record["objtype"]; if (objtype == "network") { - const uint64_t nwid = OSUtils::jsonIntHex(rec["id"],0ULL); + const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL); if (nwid) { nlohmann::json old; get(nwid,old); OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid); OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1))) + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); OSUtils::rename(p1,p2); - _networkChanged(old,rec,true); + _networkChanged(old,record,true); } } else if (objtype == "member") { - const uint64_t id = OSUtils::jsonIntHex(rec["id"],0ULL); - const uint64_t nwid = OSUtils::jsonIntHex(rec["nwid"],0ULL); + const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL); + const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL); if ((id)&&(nwid)) { nlohmann::json network,old; get(nwid,network,id,old); @@ -101,20 +108,20 @@ void FileDB::save(nlohmann::json *orig,nlohmann::json &record) OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid); OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json.new",pb,(unsigned long long)id); OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1))) { + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) { OSUtils::mkdir(pb); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1))) + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); } OSUtils::rename(p1,p2); - _memberChanged(old,rec,true); + _memberChanged(old,record,true); } } else if (objtype == "trace") { - const std::string id = rec["id"]; + const std::string id = record["id"]; if (id.length() > 0) { OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "trace" ZT_PATH_SEPARATOR_S "%s.json",_path.c_str(),id.c_str()); - OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1)); + OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)); } } } catch ( ... ) {} // drop invalid records missing fields |