From c2b624a8ecbc0a14d9a9c13a73962e4fba5e59de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Wed, 20 Feb 2019 19:55:37 -0800 Subject: [PATCH] Moving file system functions into together. --- include/bx/file.h | 16 ++++ include/bx/filepath.h | 16 ---- src/file.cpp | 176 +++++++++++++++++++++++++++++++++++++++- src/filepath.cpp | 172 +-------------------------------------- tests/filepath_test.cpp | 2 +- 5 files changed, 192 insertions(+), 190 deletions(-) diff --git a/include/bx/file.h b/include/bx/file.h index 25abf46..42c15ba 100644 --- a/include/bx/file.h +++ b/include/bx/file.h @@ -122,6 +122,22 @@ namespace bx /// FIle stat. bool stat(FileInfo& _outFileInfo, const FilePath& _filePath); + /// Creates a directory named `_filePath`. + /// + bool make(const FilePath& _filePath, Error* _err = NULL); + + /// Creates a directory named `_filePath` along with all necessary parents. + /// + bool makeAll(const FilePath& _filePath, Error* _err = NULL); + + /// Removes file or directory. + /// + bool remove(const FilePath& _filePath, Error* _err = NULL); + + /// Removes file or directory recursivelly. + /// + bool removeAll(const FilePath& _filePath, Error* _err = NULL); + } // namespace bx #endif // BX_FILE_H_HEADER_GUARD diff --git a/include/bx/filepath.h b/include/bx/filepath.h index a2e4dfe..920c0b4 100644 --- a/include/bx/filepath.h +++ b/include/bx/filepath.h @@ -115,22 +115,6 @@ namespace bx char m_filePath[kMaxFilePath]; }; - /// Creates a directory named `_filePath`. - /// - bool make(const FilePath& _filePath, Error* _err = NULL); - - /// Creates a directory named `_filePath` along with all necessary parents. - /// - bool makeAll(const FilePath& _filePath, Error* _err = NULL); - - /// Removes file or directory. - /// - bool remove(const FilePath& _filePath, Error* _err = NULL); - - /// Removes file or directory recursivelly. - /// - bool removeAll(const FilePath& _filePath, Error* _err = NULL); - } // namespace bx #endif // BX_FILEPATH_H_HEADER_GUARD diff --git a/src/file.cpp b/src/file.cpp index 56311fa..c809d71 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -20,8 +20,13 @@ # if BX_CONFIG_CRT_DIRECTORY_READER # include # endif // BX_CONFIG_CRT_DIRECTORY_READER -# include -# include +# include // remove +# if BX_CRT_MSVC +# include // _getcwd +# else +# include // stat, mkdir +# include // getcwd +# endif // BX_CRT_MSVC #endif // !BX_CRT_NONE namespace bx @@ -783,4 +788,171 @@ namespace bx #endif // BX_CRT_NONE } + bool make(const FilePath& _filePath, Error* _err) + { + BX_ERROR_SCOPE(_err); + + if (!_err->isOk() ) + { + return false; + } + +#if BX_CRT_MSVC + int32_t result = ::_mkdir(_filePath.getCPtr() ); +#elif BX_CRT_MINGW + int32_t result = ::mkdir(_filePath.getCPtr()); +#elif BX_CRT_NONE + BX_UNUSED(_filePath); + int32_t result = -1; +#else + int32_t result = ::mkdir(_filePath.getCPtr(), 0700); +#endif // BX_CRT_MSVC + + if (0 != result) + { + BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); + return false; + } + + return true; + } + + bool makeAll(const FilePath& _filePath, Error* _err) + { + BX_ERROR_SCOPE(_err); + + if (!_err->isOk() ) + { + return false; + } + + FileInfo fi; + + if (stat(fi, _filePath) ) + { + if (FileType::Dir == fi.type) + { + return true; + } + + BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); + return false; + } + + const StringView dir = strRTrim(_filePath, "/"); + const StringView slash = strRFind(dir, '/'); + + if (!slash.isEmpty() + && slash.getPtr() - dir.getPtr() > 1) + { + if (!makeAll(StringView(dir.getPtr(), slash.getPtr() ), _err) ) + { + return false; + } + } + + FilePath path(dir); + return make(path, _err); + } + + bool remove(const FilePath& _filePath, Error* _err) + { + BX_ERROR_SCOPE(_err); + + if (!_err->isOk() ) + { + return false; + } + +#if BX_CRT_MSVC + int32_t result = -1; + FileInfo fi; + if (stat(fi, _filePath) ) + { + if (FileType::Dir == fi.type) + { + result = ::_rmdir(_filePath.getCPtr() ); + } + else + { + result = ::remove(_filePath.getCPtr() ); + } + } +#elif BX_CRT_NONE + BX_UNUSED(_filePath); + int32_t result = -1; +#else + int32_t result = ::remove(_filePath.getCPtr() ); +#endif // BX_CRT_MSVC + + if (0 != result) + { + BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); + return false; + } + + return true; + } + + bool removeAll(const FilePath& _filePath, Error* _err) + { + BX_ERROR_SCOPE(_err); + + if (remove(_filePath, _err) ) + { + return true; + } + + _err->reset(); + + FileInfo fi; + + if (!stat(fi, _filePath) ) + { + BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); + return false; + } + + if (FileType::Dir != fi.type) + { + BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); + return false; + } + + Error err; + DirectoryReader dr; + + if (!bx::open(&dr, _filePath) ) + { + BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); + return false; + } + + while (err.isOk() ) + { + bx::read(&dr, fi, &err); + + if (err.isOk() ) + { + if (0 == strCmp(fi.filePath, ".") + || 0 == strCmp(fi.filePath, "..") ) + { + continue; + } + + FilePath path(_filePath); + path.join(fi.filePath); + if (!removeAll(path, _err) ) + { + _err->reset(); + break; + } + } + } + + bx::close(&dr); + + return remove(_filePath, _err); + } + } // namespace bx diff --git a/src/filepath.cpp b/src/filepath.cpp index 1622fec..2814940 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -9,15 +9,12 @@ #include #if !BX_CRT_NONE -# include // remove - # if BX_CRT_MSVC # include // _getcwd # else -# include // mkdir # include // getcwd # endif // BX_CRT_MSVC -#endif // 0 +#endif // !BX_CRT_NONE #if BX_PLATFORM_WINDOWS extern "C" __declspec(dllimport) unsigned long __stdcall GetTempPathA(unsigned long _max, char* _ptr); @@ -408,171 +405,4 @@ namespace bx return 0 == strCmp(m_filePath, "."); } - bool make(const FilePath& _filePath, Error* _err) - { - BX_ERROR_SCOPE(_err); - - if (!_err->isOk() ) - { - return false; - } - -#if BX_CRT_MSVC - int32_t result = ::_mkdir(_filePath.getCPtr() ); -#elif BX_CRT_MINGW - int32_t result = ::mkdir(_filePath.getCPtr()); -#elif BX_CRT_NONE - BX_UNUSED(_filePath); - int32_t result = -1; -#else - int32_t result = ::mkdir(_filePath.getCPtr(), 0700); -#endif // BX_CRT_MSVC - - if (0 != result) - { - BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); - return false; - } - - return true; - } - - bool makeAll(const FilePath& _filePath, Error* _err) - { - BX_ERROR_SCOPE(_err); - - if (!_err->isOk() ) - { - return false; - } - - FileInfo fi; - - if (stat(fi, _filePath) ) - { - if (FileType::Dir == fi.type) - { - return true; - } - - BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); - return false; - } - - const StringView dir = strRTrim(_filePath, "/"); - const StringView slash = strRFind(dir, '/'); - - if (!slash.isEmpty() - && slash.getPtr() - dir.getPtr() > 1) - { - if (!makeAll(StringView(dir.getPtr(), slash.getPtr() ), _err) ) - { - return false; - } - } - - FilePath path(dir); - return make(path, _err); - } - - bool remove(const FilePath& _filePath, Error* _err) - { - BX_ERROR_SCOPE(_err); - - if (!_err->isOk() ) - { - return false; - } - -#if BX_CRT_MSVC - int32_t result = -1; - FileInfo fi; - if (stat(fi, _filePath) ) - { - if (FileType::Dir == fi.type) - { - result = ::_rmdir(_filePath.getCPtr() ); - } - else - { - result = ::remove(_filePath.getCPtr() ); - } - } -#elif BX_CRT_NONE - BX_UNUSED(_filePath); - int32_t result = -1; -#else - int32_t result = ::remove(_filePath.getCPtr() ); -#endif // BX_CRT_MSVC - - if (0 != result) - { - BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); - return false; - } - - return true; - } - - bool removeAll(const FilePath& _filePath, Error* _err) - { - BX_ERROR_SCOPE(_err); - - if (remove(_filePath, _err) ) - { - return true; - } - - _err->reset(); - - FileInfo fi; - - if (!stat(fi, _filePath) ) - { - BX_ERROR_SET(_err, BX_ERROR_ACCESS, "The parent directory does not allow write permission to the process."); - return false; - } - - if (FileType::Dir != fi.type) - { - BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); - return false; - } - - Error err; - DirectoryReader dr; - - if (!bx::open(&dr, _filePath) ) - { - BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory."); - return false; - } - - while (err.isOk() ) - { - bx::read(&dr, fi, &err); - - if (err.isOk() ) - { - if (0 == strCmp(fi.filePath, ".") - || 0 == strCmp(fi.filePath, "..") ) - { - continue; - } - - FilePath path(_filePath); - path.join(fi.filePath); - if (!removeAll(path, _err) ) - { - _err->reset(); - break; - } - } - } - - bx::close(&dr); - - return remove(_filePath, _err); - } - } // namespace bx diff --git a/tests/filepath_test.cpp b/tests/filepath_test.cpp index 1ad64e7..fa91210 100644 --- a/tests/filepath_test.cpp +++ b/tests/filepath_test.cpp @@ -4,7 +4,7 @@ */ #include "test.h" -#include +#include struct FilePathTest {