diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea45189..60ce919 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,7 @@ find_package(Boost REQUIRED) add_definitions(-DTIXML_USE_STL) set (SOURCES + Dir.cpp FileOps.cpp Log.cpp ProcessUtils.cpp @@ -14,6 +15,7 @@ set (SOURCES ) set (HEADERS + Dir.h FileOps.h Log.h ProcessUtils.h diff --git a/src/Dir.cpp b/src/Dir.cpp new file mode 100644 index 0000000..9cc89a8 --- /dev/null +++ b/src/Dir.cpp @@ -0,0 +1,39 @@ +#include "Dir.h" + +#ifdef PLATFORM_UNIX + #include +#endif + +Dir::Dir(const char* path) +: m_dir(0) +{ + m_path = path; + m_dir = opendir(path); +} + +Dir::~Dir() +{ + closedir(m_dir); +} + +bool Dir::next() +{ + m_entry = readdir(m_dir); + return m_entry != 0; +} + +std::string Dir::fileName() const +{ + return m_entry->d_name; +} + +std::string Dir::filePath() const +{ + return m_path + '/' + fileName(); +} + +bool Dir::isDir() const +{ + return m_entry->d_type == DT_DIR; +} + diff --git a/src/Dir.h b/src/Dir.h new file mode 100644 index 0000000..53162f2 --- /dev/null +++ b/src/Dir.h @@ -0,0 +1,34 @@ +#pragma once + +#include "Platform.h" + +#include + +#ifdef PLATFORM_UNIX +#include +#endif + +class Dir +{ + public: + Dir(const char* path); + ~Dir(); + + // iterate to the next entry in the directory + bool next(); + + // methods to return information about + // the current entry + std::string fileName() const; + std::string filePath() const; + bool isDir() const; + + private: + std::string m_path; + +#ifdef PLATFORM_UNIX + DIR* m_dir; + dirent* m_entry; +#endif +}; + diff --git a/src/FileOps.cpp b/src/FileOps.cpp index 8497d12..822b8fb 100644 --- a/src/FileOps.cpp +++ b/src/FileOps.cpp @@ -1,5 +1,6 @@ #include "FileOps.h" +#include "Dir.h" #include "Platform.h" #include "StringUtils.h" @@ -208,3 +209,27 @@ void FileOps::touch(const char* path) throw (IOException) #endif } +void FileOps::rmdirRecursive(const char* path) throw (IOException) +{ + // remove dir contents + Dir dir(path); + while (dir.next()) + { + std::string name = dir.fileName(); + if (name != "." && name != "..") + { + if (dir.isDir()) + { + rmdir(dir.filePath().c_str()); + } + else + { + removeFile(dir.filePath().c_str()); + } + } + } + + // remove the directory itself + rmdir(path); +} + diff --git a/src/FileOps.h b/src/FileOps.h index b3a777e..8a848a6 100644 --- a/src/FileOps.h +++ b/src/FileOps.h @@ -35,5 +35,6 @@ class FileOps static void createSymLink(const char* link, const char* target) throw (IOException); static void touch(const char* path) throw (IOException); static std::string dirname(const char* path); + static void rmdirRecursive(const char* dir) throw (IOException); }; diff --git a/src/UpdateInstaller.cpp b/src/UpdateInstaller.cpp index 0ea7ed5..0582162 100644 --- a/src/UpdateInstaller.cpp +++ b/src/UpdateInstaller.cpp @@ -119,7 +119,7 @@ void UpdateInstaller::run() throw () void UpdateInstaller::cleanup() { - // TODO - Remove the temp dir which the installer was copied to + FileOps::rmdirRecursive(m_packageDir.c_str()); } void UpdateInstaller::revert()