mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 20:13:44 +02:00
GitHub issue #460
This commit is contained in:
parent
e10325e133
commit
4f3f471b4c
5 changed files with 13 additions and 67 deletions
|
@ -533,11 +533,10 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET(
|
||||||
Mutex::Lock _l(_db_m);
|
Mutex::Lock _l(_db_m);
|
||||||
|
|
||||||
responseBody = "{";
|
responseBody = "{";
|
||||||
std::string pfx(std::string("network/") + nwids + "member/");
|
_db.filter((std::string("network/") + nwids + "/member/"),ZT_NETCONF_DB_CACHE_TTL,[&responseBody](const std::string &n,const json &member) {
|
||||||
_db.filter(pfx,ZT_NETCONF_DB_CACHE_TTL,[&responseBody](const std::string &n,const json &member) {
|
if ((member.is_object())&&(member.size() > 0)) {
|
||||||
if (member.size() > 0) {
|
|
||||||
responseBody.append((responseBody.length() == 1) ? "\"" : ",\"");
|
responseBody.append((responseBody.length() == 1) ? "\"" : ",\"");
|
||||||
responseBody.append(OSUtils::jsonString(member["id"],""));
|
responseBody.append(OSUtils::jsonString(member["id"],"0"));
|
||||||
responseBody.append("\":");
|
responseBody.append("\":");
|
||||||
responseBody.append(OSUtils::jsonString(member["revision"],"0"));
|
responseBody.append(OSUtils::jsonString(member["revision"],"0"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,22 +126,14 @@ void JSONDB::erase(const std::string &n)
|
||||||
_db.erase(n);
|
_db.erase(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JSONDB::_reload(const std::string &p)
|
void JSONDB::_reload(const std::string &p,const std::string &b)
|
||||||
{
|
{
|
||||||
std::map<std::string,char> l(OSUtils::listDirectoryFull(p.c_str()));
|
std::vector<std::string> dl(OSUtils::listDirectory(p.c_str()));
|
||||||
for(std::map<std::string,char>::iterator li(l.begin());li!=l.end();++li) {
|
for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
|
||||||
if (li->second == 'f') {
|
if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
|
||||||
// assume p starts with _basePath, which it always does -- will throw otherwise
|
this->get(b + di->substr(0,di->length() - 5),0);
|
||||||
std::string n(p.substr(_basePath.length()));
|
} else {
|
||||||
while ((n.length() > 0)&&(n[0] == ZT_PATH_SEPARATOR)) n = n.substr(1);
|
this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,13 +45,13 @@ public:
|
||||||
JSONDB(const std::string &basePath) :
|
JSONDB(const std::string &basePath) :
|
||||||
_basePath(basePath)
|
_basePath(basePath)
|
||||||
{
|
{
|
||||||
_reload(_basePath);
|
_reload(_basePath,std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void reload()
|
inline void reload()
|
||||||
{
|
{
|
||||||
_db.clear();
|
_db.clear();
|
||||||
_reload(_basePath);
|
_reload(_basePath,std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool writeRaw(const std::string &n,const std::string &obj);
|
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)); }
|
inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _reload(const std::string &p);
|
void _reload(const std::string &p,const std::string &b);
|
||||||
bool _isValidObjectName(const std::string &n);
|
bool _isValidObjectName(const std::string &n);
|
||||||
std::string _genPath(const std::string &n,bool create);
|
std::string _genPath(const std::string &n,bool create);
|
||||||
|
|
||||||
|
|
|
@ -108,43 +108,6 @@ std::vector<std::string> OSUtils::listDirectory(const char *path)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string,char> OSUtils::listDirectoryFull(const char *path)
|
|
||||||
{
|
|
||||||
std::map<std::string,char> 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 OSUtils::cleanDirectory(const char *path,const uint64_t olderThan)
|
||||||
{
|
{
|
||||||
long cleaned = 0;
|
long cleaned = 0;
|
||||||
|
|
|
@ -111,14 +111,6 @@ public:
|
||||||
*/
|
*/
|
||||||
static std::vector<std::string> listDirectory(const char *path);
|
static std::vector<std::string> 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<std::string,char> listDirectoryFull(const char *path);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean a directory of files whose last modified time is older than this
|
* Clean a directory of files whose last modified time is older than this
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue