diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2016-08-16 14:05:17 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2016-08-16 14:05:17 -0700 |
commit | b08ca49580c63abe79a650adbb0d14ca87a1cd24 (patch) | |
tree | 7fc2e04f328cc4eb387dd04d90cd9e90d740b9c1 /osdep | |
parent | bd15262e5459c6003e54bcfd1d98966ff6bd1f97 (diff) | |
download | infinitytier-b08ca49580c63abe79a650adbb0d14ca87a1cd24.tar.gz infinitytier-b08ca49580c63abe79a650adbb0d14ca87a1cd24.zip |
More controller work -- it builds!
Diffstat (limited to 'osdep')
-rw-r--r-- | osdep/OSUtils.cpp | 80 | ||||
-rw-r--r-- | osdep/OSUtils.hpp | 18 |
2 files changed, 97 insertions, 1 deletions
diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 3a04308b..086bb269 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -107,6 +107,86 @@ std::vector<std::string> OSUtils::listDirectory(const char *path) return r; } +std::vector<std::string> OSUtils::listSubdirectories(const char *path) +{ + std::vector<std::string> 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,".."))&&((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) + r.push_back(std::string(ffd.cFileName)); + } 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,".."))&&(dptr->d_type == DT_DIR)) + r.push_back(std::string(dptr->d_name)); + } else break; + } + closedir(d); +#endif + + return r; +} + +bool OSUtils::rmDashRf(const char *path) +{ +#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,".") != 0)&&(strcmp(ffd.cFileName,"..") != 0)) { + if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { + if (DeleteFileA((std::string(path) + ZT_PATH_SEPARATOR_S + ffd.cFileName).c_str()) == FALSE) + return false; + } else { + if (!rmDashRf((std::string(path) + ZT_PATH_SEPARATOR_S + ffd.cFileName).c_str())) + return false; + } + } + } while (FindNextFileA(hFind,&ffd)); + FindClose(hFind); + } + return (RemoveDirectoryA(path) != FALSE); +#else + struct dirent de; + struct dirent *dptr; + DIR *d = opendir(path); + if (!d) + return true; + dptr = (struct dirent *)0; + for(;;) { + if (readdir_r(d,&de,&dptr)) + break; + if ((dptr)&&(strcmp(dptr->d_name,".") != 0)&&(strcmp(dptr->d_name,"..") != 0)) { + std::string p(path); + p.push_back(ZT_PATH_SEPARATOR); + p.append(dptr->d_name); + if (unlink(p.c_str()) != 0) { + if (!rmDashRf(p.c_str())) + return false; + } + } else break; + } + closedir(d); + return (rmdir(path) == 0); +#endif +} + void OSUtils::lockDownFile(const char *path,bool isDir) { #ifdef __UNIX_LIKE__ diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index 25bed9fe..4f74344f 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -105,11 +105,27 @@ public: * This returns only files, not sub-directories. * * @param path Path to list - * @return Names of files in directory + * @return Names of files in directory (without path prepended) */ static std::vector<std::string> listDirectory(const char *path); /** + * List a directory's subdirectories + * + * @param path Path to list + * @return Names of subdirectories (without path prepended) + */ + static std::vector<std::string> listSubdirectories(const char *path); + + /** + * Delete a directory and all its files and subdirectories recursively + * + * @param path Path to delete + * @return True on success + */ + static bool rmDashRf(const char *path); + + /** * Set modes on a file to something secure * * This locks a file so that only the owner can access it. What it actually |