From 4f3f471b4c7726b9ac66304788613c3c12e09d12 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 17 Mar 2017 18:19:51 -0700 Subject: [PATCH] GitHub issue #460 --- controller/EmbeddedNetworkController.cpp | 7 ++--- controller/JSONDB.cpp | 22 +++++--------- controller/JSONDB.hpp | 6 ++-- osdep/OSUtils.cpp | 37 ------------------------ osdep/OSUtils.hpp | 8 ----- 5 files changed, 13 insertions(+), 67 deletions(-) diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index d2f40b1cf..8fbc1cf8a 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -533,11 +533,10 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( Mutex::Lock _l(_db_m); responseBody = "{"; - std::string pfx(std::string("network/") + nwids + "member/"); - _db.filter(pfx,ZT_NETCONF_DB_CACHE_TTL,[&responseBody](const std::string &n,const json &member) { - if (member.size() > 0) { + _db.filter((std::string("network/") + nwids + "/member/"),ZT_NETCONF_DB_CACHE_TTL,[&responseBody](const std::string &n,const json &member) { + if ((member.is_object())&&(member.size() > 0)) { responseBody.append((responseBody.length() == 1) ? "\"" : ",\""); - responseBody.append(OSUtils::jsonString(member["id"],"")); + responseBody.append(OSUtils::jsonString(member["id"],"0")); responseBody.append("\":"); responseBody.append(OSUtils::jsonString(member["revision"],"0")); } diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp index 1277aabb7..298ac721e 100644 --- a/controller/JSONDB.cpp +++ b/controller/JSONDB.cpp @@ -126,22 +126,14 @@ void JSONDB::erase(const std::string &n) _db.erase(n); } -void JSONDB::_reload(const std::string &p) +void JSONDB::_reload(const std::string &p,const std::string &b) { - std::map l(OSUtils::listDirectoryFull(p.c_str())); - for(std::map::iterator li(l.begin());li!=l.end();++li) { - if (li->second == 'f') { - // assume p starts with _basePath, which it always does -- will throw otherwise - std::string n(p.substr(_basePath.length())); - while ((n.length() > 0)&&(n[0] == ZT_PATH_SEPARATOR)) n = n.substr(1); - if (ZT_PATH_SEPARATOR != '/') std::replace(n.begin(),n.end(),ZT_PATH_SEPARATOR,'/'); - if ((n.length() > 0)&&(n[n.length() - 1] != '/')) n.push_back('/'); - n.append(li->first); - if ((n.length() > 5)&&(n.substr(n.length() - 5) == ".json")) { - this->get(n.substr(0,n.length() - 5),0); // causes load and cache or update - } - } else if (li->second == 'd') { - this->_reload(p + ZT_PATH_SEPARATOR + li->first); + std::vector dl(OSUtils::listDirectory(p.c_str())); + for(std::vector::const_iterator di(dl.begin());di!=dl.end();++di) { + if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) { + this->get(b + di->substr(0,di->length() - 5),0); + } else { + this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR)); } } } diff --git a/controller/JSONDB.hpp b/controller/JSONDB.hpp index 5b7c5e50d..d25491732 100644 --- a/controller/JSONDB.hpp +++ b/controller/JSONDB.hpp @@ -45,13 +45,13 @@ public: JSONDB(const std::string &basePath) : _basePath(basePath) { - _reload(_basePath); + _reload(_basePath,std::string()); } inline void reload() { _db.clear(); - _reload(_basePath); + _reload(_basePath,std::string()); } bool writeRaw(const std::string &n,const std::string &obj); @@ -95,7 +95,7 @@ public: inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); } private: - void _reload(const std::string &p); + void _reload(const std::string &p,const std::string &b); bool _isValidObjectName(const std::string &n); std::string _genPath(const std::string &n,bool create); diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 6b95258c4..aac6bdd82 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -108,43 +108,6 @@ std::vector OSUtils::listDirectory(const char *path) return r; } -std::map OSUtils::listDirectoryFull(const char *path) -{ - std::map r; - -#ifdef __WINDOWS__ - HANDLE hFind; - WIN32_FIND_DATAA ffd; - if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) { - do { - if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,".."))) { - r[ffd.cFileName] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) ? 'd' : 'f'; - } - } while (FindNextFileA(hFind,&ffd)); - FindClose(hFind); - } -#else - struct dirent de; - struct dirent *dptr; - DIR *d = opendir(path); - if (!d) - return r; - dptr = (struct dirent *)0; - for(;;) { - if (readdir_r(d,&de,&dptr)) - break; - if (dptr) { - if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))) { - r[dptr->d_name] = (dptr->d_type == DT_DIR) ? 'd' : 'f'; - } - } else break; - } - closedir(d); -#endif - - return r; -} - long OSUtils::cleanDirectory(const char *path,const uint64_t olderThan) { long cleaned = 0; diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index adf1488ee..2e007ef24 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -111,14 +111,6 @@ public: */ static std::vector listDirectory(const char *path); - /** - * List all contents in a directory - * - * @param path Path to list - * @return Names of things and types, currently just 'f' and 'd' - */ - static std::map listDirectoryFull(const char *path); - /** * Clean a directory of files whose last modified time is older than this *