mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-02-21 10:01:46 +00:00
Clean iddb.d periodically.
This commit is contained in:
parent
04c7adea07
commit
9c1a9f984b
@ -145,6 +145,52 @@ std::map<std::string,char> OSUtils::listDirectoryFull(const char *path)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long OSUtils::cleanDirectory(const char *path,const uint64_t olderThan)
|
||||||
|
{
|
||||||
|
long cleaned = 0;
|
||||||
|
|
||||||
|
#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,".."))) {
|
||||||
|
}
|
||||||
|
} while (FindNextFileA(hFind,&ffd));
|
||||||
|
FindClose(hFind);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
struct dirent de;
|
||||||
|
struct dirent *dptr;
|
||||||
|
struct stat st;
|
||||||
|
char tmp[4096];
|
||||||
|
DIR *d = opendir(path);
|
||||||
|
if (!d)
|
||||||
|
return -1;
|
||||||
|
dptr = (struct dirent *)0;
|
||||||
|
for(;;) {
|
||||||
|
if (readdir_r(d,&de,&dptr))
|
||||||
|
break;
|
||||||
|
if (dptr) {
|
||||||
|
if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))&&(dptr->d_type == DT_REG)) {
|
||||||
|
Utils::snprintf(tmp,sizeof(tmp),"%s/%s",path,dptr->d_name);
|
||||||
|
if (stat(tmp,&st) == 0) {
|
||||||
|
uint64_t mt = (uint64_t)(st.st_mtime);
|
||||||
|
if ((mt > 0)&&((mt * 1000) < olderThan)) {
|
||||||
|
printf("%s\n",tmp);
|
||||||
|
if (unlink(tmp) == 0)
|
||||||
|
++cleaned;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
closedir(d);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return cleaned;
|
||||||
|
}
|
||||||
|
|
||||||
bool OSUtils::rmDashRf(const char *path)
|
bool OSUtils::rmDashRf(const char *path)
|
||||||
{
|
{
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
|
@ -119,6 +119,16 @@ public:
|
|||||||
*/
|
*/
|
||||||
static std::map<std::string,char> listDirectoryFull(const char *path);
|
static std::map<std::string,char> listDirectoryFull(const char *path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean a directory of files whose last modified time is older than this
|
||||||
|
*
|
||||||
|
* This ignores directories, symbolic links, and other special files.
|
||||||
|
*
|
||||||
|
* @param olderThan Last modified older than timestamp (ms since epoch)
|
||||||
|
* @return Number of cleaned files or negative on fatal error
|
||||||
|
*/
|
||||||
|
static long cleanDirectory(const char *path,const uint64_t olderThan);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a directory and all its files and subdirectories recursively
|
* Delete a directory and all its files and subdirectories recursively
|
||||||
*
|
*
|
||||||
|
@ -147,6 +147,9 @@ namespace ZeroTier { typedef BSDEthernetTap EthernetTap; }
|
|||||||
// How often to check for local interface addresses
|
// How often to check for local interface addresses
|
||||||
#define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
|
#define ZT_LOCAL_INTERFACE_CHECK_INTERVAL 60000
|
||||||
|
|
||||||
|
// Clean files from iddb.d that are older than this (60 days)
|
||||||
|
#define ZT_IDDB_CLEANUP_AGE 5184000000ULL
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@ -826,6 +829,7 @@ public:
|
|||||||
uint64_t lastBindRefresh = 0;
|
uint64_t lastBindRefresh = 0;
|
||||||
uint64_t lastUpdateCheck = clockShouldBe;
|
uint64_t lastUpdateCheck = clockShouldBe;
|
||||||
uint64_t lastLocalInterfaceAddressCheck = (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to configure and other things time to settle
|
uint64_t lastLocalInterfaceAddressCheck = (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to configure and other things time to settle
|
||||||
|
uint64_t lastCleanedIddb = 0;
|
||||||
for(;;) {
|
for(;;) {
|
||||||
_run_m.lock();
|
_run_m.lock();
|
||||||
if (!_run) {
|
if (!_run) {
|
||||||
@ -840,6 +844,12 @@ public:
|
|||||||
|
|
||||||
const uint64_t now = OSUtils::now();
|
const uint64_t now = OSUtils::now();
|
||||||
|
|
||||||
|
// Clean iddb.d on start and every 24 hours
|
||||||
|
if ((now - lastCleanedIddb) > 86400000) {
|
||||||
|
lastCleanedIddb = now;
|
||||||
|
OSUtils::cleanDirectory((_homePath + ZT_PATH_SEPARATOR_S "iddb.d").c_str(),now - ZT_IDDB_CLEANUP_AGE);
|
||||||
|
}
|
||||||
|
|
||||||
// Attempt to detect sleep/wake events by detecting delay overruns
|
// Attempt to detect sleep/wake events by detecting delay overruns
|
||||||
bool restarted = false;
|
bool restarted = false;
|
||||||
if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
|
if ((now > clockShouldBe)&&((now - clockShouldBe) > 10000)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user