pmt: Improve libraries
- Tests have been improved. - Some functions and classes in libhelper have been improved for better results.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include <exception>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <dirent.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
@@ -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
|
||||
|
||||
@@ -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<size_t>(st.st_size);
|
||||
if (stat(file.data(), &st) != 0) return -1;
|
||||
return st.st_size;
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
|
||||
Reference in New Issue
Block a user