From 360959381b7817ef1fac94a1e9a12b20ece993ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1z=20Zengin?= Date: Tue, 2 Sep 2025 12:41:09 +0300 Subject: [PATCH] pmt: Improve libraries - Tests have been improved. - Some functions and classes in libhelper have been improved for better results. --- srclib/libhelper/include/libhelper/lib.hpp | 12 +++++++-- .../private/android_filesystem_config.h | 0 srclib/libhelper/src/Classes.cpp | 20 ++++++-------- srclib/libhelper/src/FileUtil.cpp | 26 +++++++------------ srclib/libhelper/src/Utilities.cpp | 10 +++++++ srclib/libhelper/tests/test.cpp | 10 +++---- srclib/libpartition_map/tests/test.cpp | 1 - 7 files changed, 42 insertions(+), 37 deletions(-) rename {include => srclib/libhelper/include}/private/android_filesystem_config.h (100%) diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index a3896c1..602855e 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #ifndef ONLY_HELPER_MACROS @@ -80,6 +81,7 @@ private: std::vector _ptrs_c; std::vector _ptrs_u; std::vector _fps; + std::vector _dps; std::vector _fds; std::vector _files; @@ -90,6 +92,7 @@ public: void delAfterProgress(uint8_t *&_ptr); void delFileAfterProgress(const std::string &path); void closeAfterProgress(FILE *&_fp); + void closeAfterProgress(DIR *&_dp); void closeAfterProgress(int _fd); }; @@ -106,7 +109,7 @@ void setLoggingState(int state); // Disable/enable logging void reset(); } // namespace LoggingProperties -// Checkers +// Checkers - don't throw Helper::Error bool hasSuperUser(); bool hasAdbPermissions(); bool isExists(std::string_view entry); @@ -133,7 +136,7 @@ bool eraseEntry(std::string_view entry); bool eraseDirectoryRecursive(std::string_view directory); // Getters -size_t fileSize(std::string_view file); +int64_t fileSize(std::string_view file); std::string readSymlink(std::string_view entry); // SHA-256 @@ -155,9 +158,11 @@ std::string pathBasename(std::string_view entry); std::string pathDirname(std::string_view entry); uint64_t getRandomOffset(uint64_t size, uint64_t bufferSize); +#ifdef __ANDROID__ // Android std::string getProperty(std::string_view prop); bool reboot(std::string_view arg); +#endif // Library-specif std::string getLibVersion(); @@ -170,6 +175,9 @@ std::string getLibVersion(); [[nodiscard]] FILE *openAndAddToCloseList(const std::string_view &path, garbageCollector &collector, const char *mode); +[[nodiscard]] DIR *openAndAddToCloseList(const std::string_view &path, + garbageCollector &collector); + } // namespace Helper #endif // #ifndef ONLY_HELPER_MACROS diff --git a/include/private/android_filesystem_config.h b/srclib/libhelper/include/private/android_filesystem_config.h similarity index 100% rename from include/private/android_filesystem_config.h rename to srclib/libhelper/include/private/android_filesystem_config.h diff --git a/srclib/libhelper/src/Classes.cpp b/srclib/libhelper/src/Classes.cpp index 8511e1b..4063eb8 100644 --- a/srclib/libhelper/src/Classes.cpp +++ b/srclib/libhelper/src/Classes.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -103,21 +104,16 @@ garbageCollector::~garbageCollector() { close(fd); for (const auto &fp : _fps) fclose(fp); + for (const auto &dp : _dps) + closedir(dp); for (const auto &file : _files) eraseEntry(file); } -void garbageCollector::delAfterProgress(char *&_ptr) { - _ptrs_c.push_back(_ptr); -} -void garbageCollector::delAfterProgress(uint8_t *&_ptr) { - _ptrs_u.push_back(_ptr); -} -void garbageCollector::delFileAfterProgress(const std::string &path) { - _files.push_back(path); -} -void garbageCollector::closeAfterProgress(const int _fd) { - _fds.push_back(_fd); -} +void garbageCollector::delAfterProgress(char *&_ptr) { _ptrs_c.push_back(_ptr); } +void garbageCollector::delAfterProgress(uint8_t *&_ptr) { _ptrs_u.push_back(_ptr); } +void garbageCollector::delFileAfterProgress(const std::string &path) { _files.push_back(path); } +void garbageCollector::closeAfterProgress(const int _fd) { _fds.push_back(_fd); } void garbageCollector::closeAfterProgress(FILE *&_fp) { _fps.push_back(_fp); } +void garbageCollector::closeAfterProgress(DIR *&_dp) { _dps.push_back(_dp); } } // namespace Helper diff --git a/srclib/libhelper/src/FileUtil.cpp b/srclib/libhelper/src/FileUtil.cpp index fad55ee..9995c58 100644 --- a/srclib/libhelper/src/FileUtil.cpp +++ b/srclib/libhelper/src/FileUtil.cpp @@ -151,8 +151,9 @@ bool eraseDirectoryRecursive(const std::string_view directory) { LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl; struct stat buf{}; dirent *entry; + garbageCollector collector; - DIR *dir = opendir(directory.data()); + DIR *dir = openAndAddToCloseList(directory.data(), collector); if (dir == nullptr) return false; while ((entry = readdir(dir)) != nullptr) { @@ -164,25 +165,18 @@ bool eraseDirectoryRecursive(const std::string_view directory) { snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(), entry->d_name); - if (lstat(fullpath, &buf) == -1) { - closedir(dir); + if (lstat(fullpath, &buf) == -1) return false; - } if (S_ISDIR(buf.st_mode)) { - if (!eraseDirectoryRecursive(fullpath)) { - closedir(dir); - return false; - } + if (!eraseDirectoryRecursive(fullpath)) return false; + } else if (S_ISREG(buf.st_mode)) { + if (!eraseEntry(fullpath)) return false; } else { - if (unlink(fullpath) == -1) { - closedir(dir); - return false; - } + if (unlink(fullpath) == -1) return false; } } - closedir(dir); if (rmdir(directory.data()) == -1) return false; LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased." @@ -203,10 +197,10 @@ std::string readSymlink(const std::string_view entry) { return target; } -size_t fileSize(const std::string_view file) { +int64_t fileSize(const std::string_view file) { LOGN(HELPER, INFO) << "get file size request: " << file << std::endl; struct stat st{}; - if (stat(file.data(), &st) != 0) return false; - return static_cast(st.st_size); + if (stat(file.data(), &st) != 0) return -1; + return st.st_size; } } // namespace Helper diff --git a/srclib/libhelper/src/Utilities.cpp b/srclib/libhelper/src/Utilities.cpp index 3a83765..f3d5d6f 100644 --- a/srclib/libhelper/src/Utilities.cpp +++ b/srclib/libhelper/src/Utilities.cpp @@ -35,6 +35,7 @@ #include #include +#ifdef __ANDROID__ // From system/core/libcutils/android_reboot.cpp android16-s2-release int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) { int ret; @@ -63,6 +64,7 @@ int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) { free(prop_value); return ret; } +#endif namespace Helper { namespace LoggingProperties { @@ -203,6 +205,13 @@ FILE *openAndAddToCloseList(const std::string_view &path, return fp; } +DIR *openAndAddToCloseList(const std::string_view &path, garbageCollector &collector) { + DIR *dp = opendir(path.data()); + collector.closeAfterProgress(dp); + return dp; +} + +#ifdef __ANDROID__ std::string getProperty(const std::string_view prop) { char val[PROP_VALUE_MAX]; const int x = __system_property_get(prop.data(), val); @@ -220,6 +229,7 @@ bool reboot(const std::string_view arg) { return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1; } +#endif uint64_t getRandomOffset(const uint64_t size, const uint64_t bufferSize) { if (size <= bufferSize) return 0; diff --git a/srclib/libhelper/tests/test.cpp b/srclib/libhelper/tests/test.cpp index 83bceb5..c50c8ca 100644 --- a/srclib/libhelper/tests/test.cpp +++ b/srclib/libhelper/tests/test.cpp @@ -50,11 +50,10 @@ int main(int argc, char **argv) { << std::endl; if (!Helper::writeFile("file.txt", "hello world")) - throw Helper::Error("Cannor write \"hello world\" in 'file.txt'"); + throw Helper::Error("Cannot write \"hello world\" in 'file.txt'"); else std::cout << "file.txt writed." << std::endl; - auto content = Helper::readFile("file.txt"); - if (!content) throw Helper::Error("Cannot read 'file.txt'"); + if (const auto content = Helper::readFile("file.txt");!content) throw Helper::Error("Cannot read 'file.txt'"); else std::cout << "'file.txt': " << *content << std::endl; std::cout << "Making directory 'dir2': " << std::boolalpha @@ -78,8 +77,7 @@ int main(int argc, char **argv) { std::cout << "Read link of 'file2lnk.txt': " << Helper::readSymlink(test_path("file2lnk.txt")) << std::endl; - auto sha256 = Helper::sha256Of(test_path("file2.txt")); - if (!sha256) throw Helper::Error("Cannot get sha256 of 'file2.txt'"); + if (const auto sha256 = Helper::sha256Of(test_path("file2.txt"));!sha256) throw Helper::Error("Cannot get sha256 of 'file2.txt'"); else std::cout << "SHA256 of 'file2.txt': " << *sha256 << std::endl; std::cout << "'file2.txt' and 'file2lnk.txt' same? (SHA256): " @@ -95,7 +93,7 @@ int main(int argc, char **argv) { << Helper::runCommand("ls") << std::endl; std::cout << "Spawn confirm propt..." << std::endl; - bool p = Helper::confirmPropt("Please answer"); + const bool p = Helper::confirmPropt("Please answer"); std::cout << "Result of confirm propt: " << std::boolalpha << p << std::endl; diff --git a/srclib/libpartition_map/tests/test.cpp b/srclib/libpartition_map/tests/test.cpp index 8e83d25..6cef785 100644 --- a/srclib/libpartition_map/tests/test.cpp +++ b/srclib/libpartition_map/tests/test.cpp @@ -18,7 +18,6 @@ #include #include #include -#include int main() { if (getuid() != 0) return 2;