pmt: Improve libraries
- Tests have been improved. - Some functions and classes in libhelper have been improved for better results.
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <dirent.h>
|
||||
|
||||
#ifndef ONLY_HELPER_MACROS
|
||||
|
||||
@@ -80,6 +81,7 @@ private:
|
||||
std::vector<char *> _ptrs_c;
|
||||
std::vector<uint8_t *> _ptrs_u;
|
||||
std::vector<FILE *> _fps;
|
||||
std::vector<DIR *> _dps;
|
||||
std::vector<int> _fds;
|
||||
std::vector<std::string> _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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include <iostream>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <unistd.h>
|
||||
#include <pstl/glue_execution_defs.h>
|
||||
|
||||
int main() {
|
||||
if (getuid() != 0) return 2;
|
||||
|
||||
Reference in New Issue
Block a user