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>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
#ifndef ONLY_HELPER_MACROS
|
#ifndef ONLY_HELPER_MACROS
|
||||||
|
|
||||||
@@ -80,6 +81,7 @@ private:
|
|||||||
std::vector<char *> _ptrs_c;
|
std::vector<char *> _ptrs_c;
|
||||||
std::vector<uint8_t *> _ptrs_u;
|
std::vector<uint8_t *> _ptrs_u;
|
||||||
std::vector<FILE *> _fps;
|
std::vector<FILE *> _fps;
|
||||||
|
std::vector<DIR *> _dps;
|
||||||
std::vector<int> _fds;
|
std::vector<int> _fds;
|
||||||
std::vector<std::string> _files;
|
std::vector<std::string> _files;
|
||||||
|
|
||||||
@@ -90,6 +92,7 @@ public:
|
|||||||
void delAfterProgress(uint8_t *&_ptr);
|
void delAfterProgress(uint8_t *&_ptr);
|
||||||
void delFileAfterProgress(const std::string &path);
|
void delFileAfterProgress(const std::string &path);
|
||||||
void closeAfterProgress(FILE *&_fp);
|
void closeAfterProgress(FILE *&_fp);
|
||||||
|
void closeAfterProgress(DIR *&_dp);
|
||||||
void closeAfterProgress(int _fd);
|
void closeAfterProgress(int _fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -106,7 +109,7 @@ void setLoggingState(int state); // Disable/enable logging
|
|||||||
void reset();
|
void reset();
|
||||||
} // namespace LoggingProperties
|
} // namespace LoggingProperties
|
||||||
|
|
||||||
// Checkers
|
// Checkers - don't throw Helper::Error
|
||||||
bool hasSuperUser();
|
bool hasSuperUser();
|
||||||
bool hasAdbPermissions();
|
bool hasAdbPermissions();
|
||||||
bool isExists(std::string_view entry);
|
bool isExists(std::string_view entry);
|
||||||
@@ -133,7 +136,7 @@ bool eraseEntry(std::string_view entry);
|
|||||||
bool eraseDirectoryRecursive(std::string_view directory);
|
bool eraseDirectoryRecursive(std::string_view directory);
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
size_t fileSize(std::string_view file);
|
int64_t fileSize(std::string_view file);
|
||||||
std::string readSymlink(std::string_view entry);
|
std::string readSymlink(std::string_view entry);
|
||||||
|
|
||||||
// SHA-256
|
// SHA-256
|
||||||
@@ -155,9 +158,11 @@ std::string pathBasename(std::string_view entry);
|
|||||||
std::string pathDirname(std::string_view entry);
|
std::string pathDirname(std::string_view entry);
|
||||||
uint64_t getRandomOffset(uint64_t size, uint64_t bufferSize);
|
uint64_t getRandomOffset(uint64_t size, uint64_t bufferSize);
|
||||||
|
|
||||||
|
#ifdef __ANDROID__
|
||||||
// Android
|
// Android
|
||||||
std::string getProperty(std::string_view prop);
|
std::string getProperty(std::string_view prop);
|
||||||
bool reboot(std::string_view arg);
|
bool reboot(std::string_view arg);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Library-specif
|
// Library-specif
|
||||||
std::string getLibVersion();
|
std::string getLibVersion();
|
||||||
@@ -170,6 +175,9 @@ std::string getLibVersion();
|
|||||||
[[nodiscard]] FILE *openAndAddToCloseList(const std::string_view &path,
|
[[nodiscard]] FILE *openAndAddToCloseList(const std::string_view &path,
|
||||||
garbageCollector &collector,
|
garbageCollector &collector,
|
||||||
const char *mode);
|
const char *mode);
|
||||||
|
[[nodiscard]] DIR *openAndAddToCloseList(const std::string_view &path,
|
||||||
|
garbageCollector &collector);
|
||||||
|
|
||||||
} // namespace Helper
|
} // namespace Helper
|
||||||
|
|
||||||
#endif // #ifndef ONLY_HELPER_MACROS
|
#endif // #ifndef ONLY_HELPER_MACROS
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
#include <dirent.h>
|
||||||
#include <libhelper/lib.hpp>
|
#include <libhelper/lib.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -103,21 +104,16 @@ garbageCollector::~garbageCollector() {
|
|||||||
close(fd);
|
close(fd);
|
||||||
for (const auto &fp : _fps)
|
for (const auto &fp : _fps)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
for (const auto &dp : _dps)
|
||||||
|
closedir(dp);
|
||||||
for (const auto &file : _files)
|
for (const auto &file : _files)
|
||||||
eraseEntry(file);
|
eraseEntry(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void garbageCollector::delAfterProgress(char *&_ptr) {
|
void garbageCollector::delAfterProgress(char *&_ptr) { _ptrs_c.push_back(_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::delAfterProgress(uint8_t *&_ptr) {
|
void garbageCollector::closeAfterProgress(const int _fd) { _fds.push_back(_fd); }
|
||||||
_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(FILE *&_fp) { _fps.push_back(_fp); }
|
||||||
|
void garbageCollector::closeAfterProgress(DIR *&_dp) { _dps.push_back(_dp); }
|
||||||
} // namespace Helper
|
} // namespace Helper
|
||||||
|
|||||||
@@ -151,8 +151,9 @@ bool eraseDirectoryRecursive(const std::string_view directory) {
|
|||||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||||
struct stat buf{};
|
struct stat buf{};
|
||||||
dirent *entry;
|
dirent *entry;
|
||||||
|
garbageCollector collector;
|
||||||
|
|
||||||
DIR *dir = opendir(directory.data());
|
DIR *dir = openAndAddToCloseList(directory.data(), collector);
|
||||||
if (dir == nullptr) return false;
|
if (dir == nullptr) return false;
|
||||||
|
|
||||||
while ((entry = readdir(dir)) != nullptr) {
|
while ((entry = readdir(dir)) != nullptr) {
|
||||||
@@ -164,25 +165,18 @@ bool eraseDirectoryRecursive(const std::string_view directory) {
|
|||||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(),
|
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(),
|
||||||
entry->d_name);
|
entry->d_name);
|
||||||
|
|
||||||
if (lstat(fullpath, &buf) == -1) {
|
if (lstat(fullpath, &buf) == -1)
|
||||||
closedir(dir);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if (S_ISDIR(buf.st_mode)) {
|
if (S_ISDIR(buf.st_mode)) {
|
||||||
if (!eraseDirectoryRecursive(fullpath)) {
|
if (!eraseDirectoryRecursive(fullpath)) return false;
|
||||||
closedir(dir);
|
} else if (S_ISREG(buf.st_mode)) {
|
||||||
return false;
|
if (!eraseEntry(fullpath)) return false;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (unlink(fullpath) == -1) {
|
if (unlink(fullpath) == -1) return false;
|
||||||
closedir(dir);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dir);
|
|
||||||
if (rmdir(directory.data()) == -1) return false;
|
if (rmdir(directory.data()) == -1) return false;
|
||||||
|
|
||||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased."
|
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased."
|
||||||
@@ -203,10 +197,10 @@ std::string readSymlink(const std::string_view entry) {
|
|||||||
return target;
|
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;
|
LOGN(HELPER, INFO) << "get file size request: " << file << std::endl;
|
||||||
struct stat st{};
|
struct stat st{};
|
||||||
if (stat(file.data(), &st) != 0) return false;
|
if (stat(file.data(), &st) != 0) return -1;
|
||||||
return static_cast<size_t>(st.st_size);
|
return st.st_size;
|
||||||
}
|
}
|
||||||
} // namespace Helper
|
} // namespace Helper
|
||||||
|
|||||||
@@ -35,6 +35,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifdef __ANDROID__
|
||||||
// From system/core/libcutils/android_reboot.cpp android16-s2-release
|
// From system/core/libcutils/android_reboot.cpp android16-s2-release
|
||||||
int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) {
|
int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) {
|
||||||
int ret;
|
int ret;
|
||||||
@@ -63,6 +64,7 @@ int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) {
|
|||||||
free(prop_value);
|
free(prop_value);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Helper {
|
namespace Helper {
|
||||||
namespace LoggingProperties {
|
namespace LoggingProperties {
|
||||||
@@ -203,6 +205,13 @@ FILE *openAndAddToCloseList(const std::string_view &path,
|
|||||||
return fp;
|
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) {
|
std::string getProperty(const std::string_view prop) {
|
||||||
char val[PROP_VALUE_MAX];
|
char val[PROP_VALUE_MAX];
|
||||||
const int x = __system_property_get(prop.data(), val);
|
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;
|
return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
uint64_t getRandomOffset(const uint64_t size, const uint64_t bufferSize) {
|
uint64_t getRandomOffset(const uint64_t size, const uint64_t bufferSize) {
|
||||||
if (size <= bufferSize) return 0;
|
if (size <= bufferSize) return 0;
|
||||||
|
|||||||
@@ -50,11 +50,10 @@ int main(int argc, char **argv) {
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
if (!Helper::writeFile("file.txt", "hello world"))
|
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;
|
else std::cout << "file.txt writed." << std::endl;
|
||||||
|
|
||||||
auto content = Helper::readFile("file.txt");
|
if (const auto content = Helper::readFile("file.txt");!content) throw Helper::Error("Cannot read 'file.txt'");
|
||||||
if (!content) throw Helper::Error("Cannot read 'file.txt'");
|
|
||||||
else std::cout << "'file.txt': " << *content << std::endl;
|
else std::cout << "'file.txt': " << *content << std::endl;
|
||||||
|
|
||||||
std::cout << "Making directory 'dir2': " << std::boolalpha
|
std::cout << "Making directory 'dir2': " << std::boolalpha
|
||||||
@@ -78,8 +77,7 @@ int main(int argc, char **argv) {
|
|||||||
std::cout << "Read link of 'file2lnk.txt': "
|
std::cout << "Read link of 'file2lnk.txt': "
|
||||||
<< Helper::readSymlink(test_path("file2lnk.txt")) << std::endl;
|
<< Helper::readSymlink(test_path("file2lnk.txt")) << std::endl;
|
||||||
|
|
||||||
auto sha256 = Helper::sha256Of(test_path("file2.txt"));
|
if (const auto sha256 = Helper::sha256Of(test_path("file2.txt"));!sha256) throw Helper::Error("Cannot get sha256 of 'file2.txt'");
|
||||||
if (!sha256) throw Helper::Error("Cannot get sha256 of 'file2.txt'");
|
|
||||||
else std::cout << "SHA256 of 'file2.txt': " << *sha256 << std::endl;
|
else std::cout << "SHA256 of 'file2.txt': " << *sha256 << std::endl;
|
||||||
|
|
||||||
std::cout << "'file2.txt' and 'file2lnk.txt' same? (SHA256): "
|
std::cout << "'file2.txt' and 'file2lnk.txt' same? (SHA256): "
|
||||||
@@ -95,7 +93,7 @@ int main(int argc, char **argv) {
|
|||||||
<< Helper::runCommand("ls") << std::endl;
|
<< Helper::runCommand("ls") << std::endl;
|
||||||
std::cout << "Spawn confirm propt..." << 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::cout << "Result of confirm propt: " << std::boolalpha << p
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <libpartition_map/lib.hpp>
|
#include <libpartition_map/lib.hpp>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pstl/glue_execution_defs.h>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (getuid() != 0) return 2;
|
if (getuid() != 0) return 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user