pmt: reformat code
This commit is contained in:
78
srclib/libhelper/src/Checkers.cpp
Executable file → Normal file
78
srclib/libhelper/src/Checkers.cpp
Executable file → Normal file
@@ -16,57 +16,55 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <private/android_filesystem_config.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
bool hasSuperUser() {
|
||||
return (getuid() == AID_ROOT);
|
||||
}
|
||||
bool hasSuperUser() { return (getuid() == AID_ROOT); }
|
||||
|
||||
bool isExists(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
return (stat(entry.data(), &st) == 0);
|
||||
}
|
||||
bool isExists(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
return (stat(entry.data(), &st) == 0);
|
||||
}
|
||||
|
||||
bool fileIsExists(const std::string_view file) {
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return S_ISREG(st.st_mode);
|
||||
}
|
||||
bool fileIsExists(const std::string_view file) {
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return S_ISREG(st.st_mode);
|
||||
}
|
||||
|
||||
bool directoryIsExists(const std::string_view directory) {
|
||||
struct stat st{};
|
||||
if (stat(directory.data(), &st) != 0) return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
bool directoryIsExists(const std::string_view directory) {
|
||||
struct stat st{};
|
||||
if (stat(directory.data(), &st) != 0) return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
bool linkIsExists(const std::string_view entry) {
|
||||
return (isLink(entry) || isHardLink(entry));
|
||||
}
|
||||
bool linkIsExists(const std::string_view entry) {
|
||||
return (isLink(entry) || isHardLink(entry));
|
||||
}
|
||||
|
||||
bool isLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
bool isLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
|
||||
bool isSymbolicLink(const std::string_view entry) {
|
||||
return isLink(entry);
|
||||
}
|
||||
bool isSymbolicLink(const std::string_view entry) { return isLink(entry); }
|
||||
|
||||
bool isHardLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return (st.st_nlink >= 2);
|
||||
}
|
||||
bool isHardLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return (st.st_nlink >= 2);
|
||||
}
|
||||
|
||||
bool areLinked(const std::string_view entry1, const std::string_view entry2) {
|
||||
const std::string st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1) : std::string(entry1.data());
|
||||
const std::string st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2) : std::string(entry2.data());
|
||||
bool areLinked(const std::string_view entry1, const std::string_view entry2) {
|
||||
const std::string st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1)
|
||||
: std::string(entry1.data());
|
||||
const std::string st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2)
|
||||
: std::string(entry2.data());
|
||||
|
||||
return (st1 == st2);
|
||||
}
|
||||
return (st1 == st2);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
143
srclib/libhelper/src/Classes.cpp
Executable file → Normal file
143
srclib/libhelper/src/Classes.cpp
Executable file → Normal file
@@ -14,86 +14,97 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <cerrno>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <cstdarg>
|
||||
#include <cerrno>
|
||||
#include <exception>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
Error::Error(const char *format, ...) {
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
Error::Error(const char *format, ...) {
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
|
||||
const char *Error::what() const noexcept {
|
||||
return _message.data();
|
||||
}
|
||||
const char *Error::what() const noexcept { return _message.data(); }
|
||||
|
||||
Logger::Logger(const LogLevels level, const char *func, const char *file, const char *name, const char *sfile,
|
||||
const int line) : _level(level), _funcname(func), _logFile(file), _program_name(name), _file(sfile),
|
||||
_line(line) {
|
||||
}
|
||||
Logger::Logger(const LogLevels level, const char *func, const char *file,
|
||||
const char *name, const char *sfile, const int line)
|
||||
: _level(level), _funcname(func), _logFile(file), _program_name(name),
|
||||
_file(sfile), _line(line) {}
|
||||
|
||||
Logger::~Logger() {
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level),
|
||||
_program_name,
|
||||
basename(const_cast<char *>(_file)),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
_funcname,
|
||||
_oss.str().data());
|
||||
Logger::~Logger() {
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level), _program_name,
|
||||
basename(const_cast<char *>(_file)), _line, currentDate().data(),
|
||||
currentTime().data(), _funcname, _oss.str().data());
|
||||
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd = open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS); fd != -1) close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" New logging file: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
}
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd =
|
||||
open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS);
|
||||
fd != -1)
|
||||
close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": "
|
||||
<< strerror(errno)
|
||||
<< " New logging file: last_logs.log (this file)."
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (FILE *fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot write logs to log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" Logging file setting up as: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
if (FILE *fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO)
|
||||
<< "Cannot write logs to log file: " << _logFile << ": "
|
||||
<< strerror(errno)
|
||||
<< " Logging file setting up as: last_logs.log (this file)."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
|
||||
Logger &Logger::operator<<(std::ostream & (*msg)(std::ostream &)) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
Logger &Logger::operator<<(std::ostream &(*msg)(std::ostream &)) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
garbageCollector::~garbageCollector() {
|
||||
for (const auto& ptr : _ptrs_c) delete[] ptr;
|
||||
for (const auto& ptr : _ptrs_u) delete[] ptr;
|
||||
for (const auto& fd : _fds) close(fd);
|
||||
for (const auto& fp : _fps) fclose(fp);
|
||||
}
|
||||
garbageCollector::~garbageCollector() {
|
||||
for (const auto &ptr : _ptrs_c)
|
||||
delete[] ptr;
|
||||
for (const auto &ptr : _ptrs_u)
|
||||
delete[] ptr;
|
||||
for (const auto &fd : _fds)
|
||||
close(fd);
|
||||
for (const auto &fp : _fps)
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void garbageCollector::delAfterProgress(char* &_ptr) { _ptrs_c.push_back(_ptr); }
|
||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) { _ptrs_u.push_back(_ptr); }
|
||||
void garbageCollector::closeAfterProgress(const int _fd) { _fds.push_back(_fd); }
|
||||
void garbageCollector::delAfterProgress(FILE* &_fp) { _fps.push_back(_fp); }
|
||||
void garbageCollector::delAfterProgress(char *&_ptr) {
|
||||
_ptrs_c.push_back(_ptr);
|
||||
}
|
||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) {
|
||||
_ptrs_u.push_back(_ptr);
|
||||
}
|
||||
void garbageCollector::closeAfterProgress(const int _fd) {
|
||||
_fds.push_back(_fd);
|
||||
}
|
||||
void garbageCollector::delAfterProgress(FILE *&_fp) { _fps.push_back(_fp); }
|
||||
} // namespace Helper
|
||||
|
||||
286
srclib/libhelper/src/FileUtil.cpp
Executable file → Normal file
286
srclib/libhelper/src/FileUtil.cpp
Executable file → Normal file
@@ -14,187 +14,199 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
bool writeFile(const std::string_view file, const std::string_view text) {
|
||||
LOGN(HELPER, INFO) << "write \"" << text << "\" to " << file << "requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
bool writeFile(const std::string_view file, const std::string_view text) {
|
||||
LOGN(HELPER, INFO) << "write \"" << text << "\" to " << file << "requested."
|
||||
<< std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "a");
|
||||
if (fp == nullptr) return false;
|
||||
fprintf(fp, "%s", text.data());
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "a");
|
||||
if (fp == nullptr) return false;
|
||||
fprintf(fp, "%s", text.data());
|
||||
|
||||
LOGN(HELPER, INFO) << "write " << file << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "write " << file << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<std::string> readFile(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "read " << file << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
std::optional<std::string> readFile(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "read " << file << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "r");
|
||||
if (fp == nullptr) return std::nullopt;
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "r");
|
||||
if (fp == nullptr) return std::nullopt;
|
||||
|
||||
char buffer[1024];
|
||||
std::string str;
|
||||
while (fgets(buffer, sizeof(buffer), fp)) str += buffer;
|
||||
char buffer[1024];
|
||||
std::string str;
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
str += buffer;
|
||||
|
||||
LOGN(HELPER, INFO) << "read " << file << " successfully, read text: \"" << str << "\"" << std::endl;
|
||||
return str;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "read " << file << " successfully, read text: \"" << str
|
||||
<< "\"" << std::endl;
|
||||
return str;
|
||||
}
|
||||
|
||||
bool copyFile(const std::string_view file, const std::string_view dest) {
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
bool copyFile(const std::string_view file, const std::string_view dest) {
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested."
|
||||
<< std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
const int src_fd = openAndAddToCloseList(file.data(), collector, O_RDONLY);
|
||||
if (src_fd == -1) return false;
|
||||
const int src_fd = openAndAddToCloseList(file.data(), collector, O_RDONLY);
|
||||
if (src_fd == -1) return false;
|
||||
|
||||
const int dst_fd = openAndAddToCloseList(dest.data(), collector, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_FILE_PERMS);
|
||||
if (dst_fd == -1) return false;
|
||||
const int dst_fd = openAndAddToCloseList(
|
||||
dest.data(), collector, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_FILE_PERMS);
|
||||
if (dst_fd == -1) return false;
|
||||
|
||||
char buffer[512];
|
||||
ssize_t br;
|
||||
while ((br = read(src_fd, buffer, 512)) > 0) {
|
||||
if (const ssize_t bw = write(dst_fd, buffer, br); bw != br) return false;
|
||||
}
|
||||
char buffer[512];
|
||||
ssize_t br;
|
||||
while ((br = read(src_fd, buffer, 512)) > 0) {
|
||||
if (const ssize_t bw = write(dst_fd, buffer, br); bw != br) return false;
|
||||
}
|
||||
|
||||
if (br == -1) return false;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
if (br == -1) return false;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfully."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool makeDirectory(const std::string_view path) {
|
||||
if (isExists(path)) return false;
|
||||
LOGN(HELPER, INFO) << "trying making directory: " << path << std::endl;
|
||||
return (mkdir(path.data(), DEFAULT_DIR_PERMS) == 0);
|
||||
}
|
||||
bool makeDirectory(const std::string_view path) {
|
||||
if (isExists(path)) return false;
|
||||
LOGN(HELPER, INFO) << "trying making directory: " << path << std::endl;
|
||||
return (mkdir(path.data(), DEFAULT_DIR_PERMS) == 0);
|
||||
}
|
||||
|
||||
bool makeRecursiveDirectory(const std::string_view paths) {
|
||||
LOGN(HELPER, INFO) << "make recursive directory requested: " << paths << std::endl;
|
||||
bool makeRecursiveDirectory(const std::string_view paths) {
|
||||
LOGN(HELPER, INFO) << "make recursive directory requested: " << paths
|
||||
<< std::endl;
|
||||
|
||||
char tmp[PATH_MAX];
|
||||
char tmp[PATH_MAX];
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s", paths.data());
|
||||
if (const size_t len = strlen(tmp); tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
snprintf(tmp, sizeof(tmp), "%s", paths.data());
|
||||
if (const size_t len = strlen(tmp); tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0
|
||||
&& errno != EEXIST)
|
||||
return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool createFile(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
bool createFile(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
|
||||
if (isExists(path)) return false;
|
||||
if (isExists(path)) return false;
|
||||
|
||||
const int fd = open(path.data(), O_RDONLY | O_CREAT, DEFAULT_FILE_PERMS);
|
||||
if (fd == -1) return false;
|
||||
const int fd = open(path.data(), O_RDONLY | O_CREAT, DEFAULT_FILE_PERMS);
|
||||
if (fd == -1) return false;
|
||||
|
||||
close(fd);
|
||||
LOGN(HELPER, INFO) << "create file \"" << path << "\" successfull." << std::endl;
|
||||
return true;
|
||||
}
|
||||
close(fd);
|
||||
LOGN(HELPER, INFO) << "create file \"" << path << "\" successfull."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool createSymlink(const std::string_view entry1, const std::string_view entry2) {
|
||||
LOGN(HELPER, INFO) << "symlink \"" << entry1 << "\" to \"" << entry2 << "\" requested." << std::endl;
|
||||
if (const int ret = symlink(entry1.data(), entry2.data()); ret != 0) return false;
|
||||
bool createSymlink(const std::string_view entry1,
|
||||
const std::string_view entry2) {
|
||||
LOGN(HELPER, INFO) << "symlink \"" << entry1 << "\" to \"" << entry2
|
||||
<< "\" requested." << std::endl;
|
||||
if (const int ret = symlink(entry1.data(), entry2.data()); ret != 0)
|
||||
return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << entry1 << "\" symlinked to \"" << entry2 << "\" successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << entry1 << "\" symlinked to \"" << entry2
|
||||
<< "\" successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eraseEntry(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "erase \"" << entry << "\" requested." << std::endl;
|
||||
if (const int ret = remove(entry.data()); ret != 0) return false;
|
||||
bool eraseEntry(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "erase \"" << entry << "\" requested." << std::endl;
|
||||
if (const int ret = remove(entry.data()); ret != 0) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eraseDirectoryRecursive(const std::string_view directory) {
|
||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||
struct stat buf{};
|
||||
dirent *entry;
|
||||
bool eraseDirectoryRecursive(const std::string_view directory) {
|
||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||
struct stat buf{};
|
||||
dirent *entry;
|
||||
|
||||
DIR *dir = opendir(directory.data());
|
||||
if (dir == nullptr) return false;
|
||||
DIR *dir = opendir(directory.data());
|
||||
if (dir == nullptr) return false;
|
||||
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
char fullpath[PATH_MAX];
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
char fullpath[PATH_MAX];
|
||||
|
||||
if (strcmp(entry->d_name, ".") == 0
|
||||
|| strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(), entry->d_name);
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(),
|
||||
entry->d_name);
|
||||
|
||||
if (lstat(fullpath, &buf) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
if (lstat(fullpath, &buf) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
if (!eraseDirectoryRecursive(fullpath)) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (unlink(fullpath) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
if (!eraseDirectoryRecursive(fullpath)) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (unlink(fullpath) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
if (rmdir(directory.data()) == -1) return false;
|
||||
closedir(dir);
|
||||
if (rmdir(directory.data()) == -1) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string readSymlink(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
std::string readSymlink(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
|
||||
char target[PATH_MAX];
|
||||
const ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
if (len == -1) return entry.data();
|
||||
char target[PATH_MAX];
|
||||
const ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
if (len == -1) return entry.data();
|
||||
|
||||
target[len] = '\0';
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" symlink to \"" << target << "\"" << std::endl;
|
||||
return target;
|
||||
}
|
||||
target[len] = '\0';
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" symlink to \"" << target << "\""
|
||||
<< std::endl;
|
||||
return target;
|
||||
}
|
||||
|
||||
size_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);
|
||||
}
|
||||
size_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);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
@@ -15,36 +15,43 @@
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <optional>
|
||||
#include <picosha2.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Helper {
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path <<
|
||||
"\" request. Getting full path (if input is link and exists)." << std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO)
|
||||
<< "get sha256 of \"" << path
|
||||
<< "\" request. Getting full path (if input is link and exists)."
|
||||
<< std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
|
||||
if (!fileIsExists(fp)) throw Error("Is not exists or not file: %s", fp.data());
|
||||
if (!fileIsExists(fp))
|
||||
throw Error("Is not exists or not file: %s", fp.data());
|
||||
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file) throw Error("Cannot open file: %s", fp.data());
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file)
|
||||
throw Error("Cannot open file: %s", fp.data());
|
||||
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully." << std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully."
|
||||
<< std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2) << "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files."
|
||||
<< std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2)
|
||||
<< "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
308
srclib/libhelper/src/Utilities.cpp
Executable file → Normal file
308
srclib/libhelper/src/Utilities.cpp
Executable file → Normal file
@@ -14,201 +14,207 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <fcntl.h>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <iostream>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <ctime>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/_system_properties.h>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// From system/core/libcutils/android_reboot.cpp android16-s2-release
|
||||
int android_reboot(const unsigned cmd, int /*flags*/, const char* arg) {
|
||||
int ret;
|
||||
const char* restart_cmd = nullptr;
|
||||
char* prop_value;
|
||||
int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) {
|
||||
int ret;
|
||||
const char *restart_cmd = nullptr;
|
||||
char *prop_value;
|
||||
|
||||
switch (cmd) {
|
||||
case ANDROID_RB_RESTART: // deprecated
|
||||
case ANDROID_RB_RESTART2:
|
||||
restart_cmd = "reboot";
|
||||
break;
|
||||
case ANDROID_RB_POWEROFF:
|
||||
restart_cmd = "shutdown";
|
||||
break;
|
||||
case ANDROID_RB_THERMOFF:
|
||||
restart_cmd = "shutdown,thermal";
|
||||
break;
|
||||
}
|
||||
switch (cmd) {
|
||||
case ANDROID_RB_RESTART: // deprecated
|
||||
case ANDROID_RB_RESTART2:
|
||||
restart_cmd = "reboot";
|
||||
break;
|
||||
case ANDROID_RB_POWEROFF:
|
||||
restart_cmd = "shutdown";
|
||||
break;
|
||||
case ANDROID_RB_THERMOFF:
|
||||
restart_cmd = "shutdown,thermal";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!restart_cmd) return -1;
|
||||
if (arg && arg[0]) ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
|
||||
else ret = asprintf(&prop_value, "%s", restart_cmd);
|
||||
if (!restart_cmd) return -1;
|
||||
if (arg && arg[0]) ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
|
||||
else ret = asprintf(&prop_value, "%s", restart_cmd);
|
||||
|
||||
if (ret < 0) return -1;
|
||||
ret = __system_property_set(ANDROID_RB_PROPERTY, prop_value);
|
||||
free(prop_value);
|
||||
return ret;
|
||||
if (ret < 0) return -1;
|
||||
ret = __system_property_set(ANDROID_RB_PROPERTY, prop_value);
|
||||
free(prop_value);
|
||||
return ret;
|
||||
}
|
||||
|
||||
namespace Helper {
|
||||
namespace LoggingProperties {
|
||||
std::string_view FILE = "last_logs.log", NAME = "main";
|
||||
bool PRINT = NO, DISABLE = NO;
|
||||
namespace LoggingProperties {
|
||||
std::string_view FILE = "last_logs.log", NAME = "main";
|
||||
bool PRINT = NO, DISABLE = NO;
|
||||
|
||||
void reset() {
|
||||
FILE = "last_logs.log";
|
||||
NAME = "main";
|
||||
PRINT = NO;
|
||||
}
|
||||
void reset() {
|
||||
FILE = "last_logs.log";
|
||||
NAME = "main";
|
||||
PRINT = NO;
|
||||
}
|
||||
|
||||
void set(std::string_view file, std::string_view name) {
|
||||
if (file.data() != nullptr) FILE = file;
|
||||
if (name.data() != nullptr) NAME = name;
|
||||
}
|
||||
void set(std::string_view file, std::string_view name) {
|
||||
if (file.data() != nullptr) FILE = file;
|
||||
if (name.data() != nullptr) NAME = name;
|
||||
}
|
||||
|
||||
void setProgramName(const std::string_view name) { NAME = name; }
|
||||
void setLogFile(const std::string_view file) { FILE = file; }
|
||||
void setProgramName(const std::string_view name) { NAME = name; }
|
||||
void setLogFile(const std::string_view file) { FILE = file; }
|
||||
|
||||
void setPrinting(const int state) {
|
||||
if (state == 1 || state == 0) PRINT = state;
|
||||
else PRINT = NO;
|
||||
}
|
||||
void setPrinting(const int state) {
|
||||
if (state == 1 || state == 0) PRINT = state;
|
||||
else PRINT = NO;
|
||||
}
|
||||
|
||||
void setLoggingState(const int state) {
|
||||
if (state == 1 || state == 0) DISABLE = state;
|
||||
else DISABLE = NO;
|
||||
}
|
||||
} // namespace LoggingProperties
|
||||
void setLoggingState(const int state) {
|
||||
if (state == 1 || state == 0) DISABLE = state;
|
||||
else DISABLE = NO;
|
||||
}
|
||||
} // namespace LoggingProperties
|
||||
|
||||
bool runCommand(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command request: " << cmd << std::endl;
|
||||
return (system(cmd.data()) == 0) ? true : false;
|
||||
}
|
||||
bool runCommand(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command request: " << cmd << std::endl;
|
||||
return (system(cmd.data()) == 0) ? true : false;
|
||||
}
|
||||
|
||||
bool confirmPropt(const std::string_view message) {
|
||||
LOGN(HELPER, INFO) << "create confirm propt request. Creating." << std::endl;
|
||||
char p;
|
||||
bool confirmPropt(const std::string_view message) {
|
||||
LOGN(HELPER, INFO) << "create confirm propt request. Creating." << std::endl;
|
||||
char p;
|
||||
|
||||
printf("%s [ y / n ]: ", message.data());
|
||||
std::cin >> p;
|
||||
printf("%s [ y / n ]: ", message.data());
|
||||
std::cin >> p;
|
||||
|
||||
if (p == 'y' || p == 'Y') return true;
|
||||
if (p == 'n' || p == 'N') return false;
|
||||
if (p == 'y' || p == 'Y') return true;
|
||||
if (p == 'n' || p == 'N') return false;
|
||||
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
|
||||
std::string currentWorkingDirectory() {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return {};
|
||||
return cwd;
|
||||
}
|
||||
std::string currentWorkingDirectory() {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return {};
|
||||
return cwd;
|
||||
}
|
||||
|
||||
std::string currentDate() {
|
||||
const time_t t = time(nullptr);
|
||||
std::string currentDate() {
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_mday) + "/" +
|
||||
std::to_string(date->tm_mon + 1) + "/" +
|
||||
std::to_string(date->tm_year + 1900));
|
||||
return "--/--/----";
|
||||
}
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(std::to_string(date->tm_mday) + "/" +
|
||||
std::to_string(date->tm_mon + 1) + "/" +
|
||||
std::to_string(date->tm_year + 1900));
|
||||
return "--/--/----";
|
||||
}
|
||||
|
||||
std::string currentTime() {
|
||||
const time_t t = time(nullptr);
|
||||
std::string currentTime() {
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_hour) + ":" +
|
||||
std::to_string(date->tm_min) + ":" +
|
||||
std::to_string(date->tm_sec));
|
||||
return "--:--:--";
|
||||
}
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(std::to_string(date->tm_hour) + ":" +
|
||||
std::to_string(date->tm_min) + ":" +
|
||||
std::to_string(date->tm_sec));
|
||||
return "--:--:--";
|
||||
}
|
||||
|
||||
std::string runCommandWithOutput(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command and catch out request: " << cmd << std::endl;
|
||||
std::string runCommandWithOutput(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command and catch out request: " << cmd
|
||||
<< std::endl;
|
||||
|
||||
FILE *pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) return {};
|
||||
FILE *pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) return {};
|
||||
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||
|
||||
std::string output;
|
||||
char buffer[1024];
|
||||
std::string output;
|
||||
char buffer[1024];
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr) output += buffer;
|
||||
while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr)
|
||||
output += buffer;
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string pathJoin(std::string base, std::string relative) {
|
||||
if (base.back() != '/') base += '/';
|
||||
if (relative[0] == '/') relative.erase(0, 1);
|
||||
base += relative;
|
||||
return base;
|
||||
}
|
||||
std::string pathJoin(std::string base, std::string relative) {
|
||||
if (base.back() != '/') base += '/';
|
||||
if (relative[0] == '/') relative.erase(0, 1);
|
||||
base += relative;
|
||||
return base;
|
||||
}
|
||||
|
||||
std::string pathBasename(const std::string_view entry) {
|
||||
char *base = basename(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
std::string pathBasename(const std::string_view entry) {
|
||||
char *base = basename(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
|
||||
std::string pathDirname(const std::string_view entry) {
|
||||
char *base = dirname(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
std::string pathDirname(const std::string_view entry) {
|
||||
char *base = dirname(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
|
||||
bool changeMode(const std::string_view file, const mode_t mode) {
|
||||
LOGN(HELPER, INFO) << "change mode request: " << file << ". As mode: " << mode << std::endl;
|
||||
return chmod(file.data(), mode) == 0;
|
||||
}
|
||||
bool changeMode(const std::string_view file, const mode_t mode) {
|
||||
LOGN(HELPER, INFO) << "change mode request: " << file << ". As mode: " << mode
|
||||
<< std::endl;
|
||||
return chmod(file.data(), mode) == 0;
|
||||
}
|
||||
|
||||
bool changeOwner(const std::string_view file, const uid_t uid, const gid_t gid) {
|
||||
LOGN(HELPER, INFO) << "change owner request: " << file << ". As owner:group: " << uid << ":" << gid << std::endl;
|
||||
return chown(file.data(), uid, gid) == 0;
|
||||
}
|
||||
bool changeOwner(const std::string_view file, const uid_t uid,
|
||||
const gid_t gid) {
|
||||
LOGN(HELPER, INFO) << "change owner request: " << file
|
||||
<< ". As owner:group: " << uid << ":" << gid << std::endl;
|
||||
return chown(file.data(), uid, gid) == 0;
|
||||
}
|
||||
|
||||
int openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, const int flags, const mode_t mode) {
|
||||
const int fd = mode == 0 ? open(path.data(), flags) : open(path.data(), flags, mode);
|
||||
collector.closeAfterProgress(fd);
|
||||
return fd;
|
||||
}
|
||||
int openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector, const int flags,
|
||||
const mode_t mode) {
|
||||
const int fd =
|
||||
mode == 0 ? open(path.data(), flags) : open(path.data(), flags, mode);
|
||||
collector.closeAfterProgress(fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
FILE* openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, const char* mode) {
|
||||
FILE *fp = fopen(path.data(), mode);
|
||||
collector.delAfterProgress(fp);
|
||||
return fp;
|
||||
}
|
||||
FILE *openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector, const char *mode) {
|
||||
FILE *fp = fopen(path.data(), mode);
|
||||
collector.delAfterProgress(fp);
|
||||
return fp;
|
||||
}
|
||||
|
||||
std::string getProperty(const std::string_view prop) {
|
||||
char val[PROP_VALUE_MAX];
|
||||
const int x = __system_property_get(prop.data(), val);
|
||||
return x > 0 ? val : "ERROR";
|
||||
}
|
||||
std::string getProperty(const std::string_view prop) {
|
||||
char val[PROP_VALUE_MAX];
|
||||
const int x = __system_property_get(prop.data(), val);
|
||||
return x > 0 ? val : "ERROR";
|
||||
}
|
||||
|
||||
bool reboot(const std::string_view arg) {
|
||||
LOGN(HELPER, INFO) << "reboot request sent!!!" << std::endl;
|
||||
bool reboot(const std::string_view arg) {
|
||||
LOGN(HELPER, INFO) << "reboot request sent!!!" << std::endl;
|
||||
|
||||
unsigned cmd = ANDROID_RB_RESTART2;
|
||||
if (const std::string prop = getProperty("ro.build.version.sdk"); prop != "ERROR") {
|
||||
if (std::stoi(prop) < 26) cmd = ANDROID_RB_RESTART;
|
||||
}
|
||||
unsigned cmd = ANDROID_RB_RESTART2;
|
||||
if (const std::string prop = getProperty("ro.build.version.sdk");
|
||||
prop != "ERROR") {
|
||||
if (std::stoi(prop) < 26) cmd = ANDROID_RB_RESTART;
|
||||
}
|
||||
|
||||
return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1;
|
||||
}
|
||||
return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1;
|
||||
}
|
||||
|
||||
std::string getLibVersion() {
|
||||
MKVERSION("libhelper");
|
||||
}
|
||||
std::string getLibVersion() { MKVERSION("libhelper"); }
|
||||
} // namespace Helper
|
||||
|
||||
Reference in New Issue
Block a user