summaryrefslogtreecommitdiff
path: root/osdep
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-08 10:18:12 -0800
committerAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-08 10:18:12 -0800
commit9c1a9f984b2bdffe94e147e3548080dfc7c2ef72 (patch)
tree47c8b4ec87b1cecda3faa4fd3becc5d6700f4672 /osdep
parent04c7adea07d3df6684f7776999150631226f19a5 (diff)
downloadinfinitytier-9c1a9f984b2bdffe94e147e3548080dfc7c2ef72.tar.gz
infinitytier-9c1a9f984b2bdffe94e147e3548080dfc7c2ef72.zip
Clean iddb.d periodically.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/OSUtils.cpp46
-rw-r--r--osdep/OSUtils.hpp10
2 files changed, 56 insertions, 0 deletions
diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp
index fc02109a..69d368ef 100644
--- a/osdep/OSUtils.cpp
+++ b/osdep/OSUtils.cpp
@@ -145,6 +145,52 @@ std::map<std::string,char> OSUtils::listDirectoryFull(const char *path)
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)
{
#ifdef __WINDOWS__
diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp
index 2fe01979..adf1488e 100644
--- a/osdep/OSUtils.hpp
+++ b/osdep/OSUtils.hpp
@@ -120,6 +120,16 @@ public:
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
*
* @param path Path to delete