pmt: ready for release
- Fix memory leaks - Writed functions and apply - Test pmt and verify stability
This commit is contained in:
@@ -25,13 +25,17 @@ set(LIBHELPER_SOURCES
|
||||
# Add targets
|
||||
add_library(helper_shared SHARED ${LIBHELPER_SOURCES})
|
||||
add_library(helper_static STATIC ${LIBHELPER_SOURCES})
|
||||
add_executable(libhelper_test tests/test.cpp)
|
||||
|
||||
# Set linker flags
|
||||
target_link_libraries(libhelper_test PRIVATE helper_shared)
|
||||
target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local")
|
||||
target_link_options(helper_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
|
||||
|
||||
# Set appropriate output names
|
||||
set_target_properties(helper_shared PROPERTIES OUTPUT_NAME "helper")
|
||||
set_target_properties(helper_static PROPERTIES OUTPUT_NAME "helper")
|
||||
|
||||
# Build libhelper_test if CMAKE_BUILD_TYPE is not release
|
||||
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
|
||||
add_executable(libhelper_test ${CMAKE_CURRENT_SOURCE_DIR}/tests/test.cpp)
|
||||
target_link_libraries(libhelper_test PRIVATE helper_shared)
|
||||
target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
|
||||
endif()
|
||||
|
||||
@@ -26,10 +26,10 @@
|
||||
#ifndef ONLY_HELPER_MACROS
|
||||
|
||||
enum LogLevels {
|
||||
INFO = (int)'I',
|
||||
WARNING = (int)'W',
|
||||
ERROR = (int)'E',
|
||||
ABORT = (int)'A'
|
||||
INFO = static_cast<int>('I'),
|
||||
WARNING = static_cast<int>('W'),
|
||||
ERROR = static_cast<int>('E'),
|
||||
ABORT = static_cast<int>('A')
|
||||
};
|
||||
|
||||
constexpr mode_t DEFAULT_FILE_PERMS = 0644;
|
||||
@@ -41,7 +41,7 @@ constexpr int NO = 0;
|
||||
namespace Helper {
|
||||
|
||||
// Logging
|
||||
class Logger {
|
||||
class Logger final {
|
||||
private:
|
||||
LogLevels _level;
|
||||
std::ostringstream _oss;
|
||||
@@ -62,14 +62,15 @@ public:
|
||||
};
|
||||
|
||||
// Throwable error class
|
||||
class Error : public std::exception {
|
||||
class Error final : public std::exception {
|
||||
private:
|
||||
std::string _message;
|
||||
|
||||
public:
|
||||
Error(const char* format, ...);
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
explicit Error(const char* format, ...);
|
||||
|
||||
const char* what() const noexcept override;
|
||||
[[nodiscard]] const char* what() const noexcept override;
|
||||
};
|
||||
|
||||
namespace LoggingProperties {
|
||||
@@ -88,48 +89,50 @@ void reset();
|
||||
|
||||
// Checkers
|
||||
bool hasSuperUser();
|
||||
bool isExists(const std::string_view entry);
|
||||
bool fileIsExists(const std::string_view file);
|
||||
bool directoryIsExists(const std::string_view directory);
|
||||
bool linkIsExists(const std::string_view entry);
|
||||
bool isLink(const std::string_view entry);
|
||||
bool isSymbolicLink(const std::string_view entry);
|
||||
bool isHardLink(const std::string_view entry);
|
||||
bool areLinked(const std::string_view entry1, const std::string_view entry2);
|
||||
bool isExists(std::string_view entry);
|
||||
bool fileIsExists(std::string_view file);
|
||||
bool directoryIsExists(std::string_view directory);
|
||||
bool linkIsExists(std::string_view entry);
|
||||
bool isLink(std::string_view entry);
|
||||
bool isSymbolicLink(std::string_view entry);
|
||||
bool isHardLink(std::string_view entry);
|
||||
bool areLinked(std::string_view entry1, std::string_view entry2);
|
||||
|
||||
// File I/O
|
||||
bool writeFile(const std::string_view file, const std::string_view text);
|
||||
std::optional<std::string> readFile(const std::string_view file);
|
||||
bool writeFile(std::string_view file, std::string_view text);
|
||||
std::optional<std::string> readFile(std::string_view file);
|
||||
|
||||
// Creators
|
||||
bool makeDirectory(const std::string_view path);
|
||||
bool makeRecursiveDirectory(const std::string_view paths);
|
||||
bool createFile(const std::string_view path);
|
||||
bool createSymlink(const std::string_view entry1, const std::string_view entry2);
|
||||
bool makeDirectory(std::string_view path);
|
||||
bool makeRecursiveDirectory(std::string_view paths);
|
||||
bool createFile(std::string_view path);
|
||||
bool createSymlink(std::string_view entry1, std::string_view entry2);
|
||||
|
||||
// Removers
|
||||
bool eraseEntry(const std::string_view entry);
|
||||
bool eraseDirectoryRecursive(const std::string_view directory);
|
||||
bool eraseEntry(std::string_view entry);
|
||||
bool eraseDirectoryRecursive(std::string_view directory);
|
||||
|
||||
// Getters
|
||||
size_t fileSize(const std::string_view file);
|
||||
std::string readSymlink(const std::string_view entry);
|
||||
size_t fileSize(std::string_view file);
|
||||
std::string readSymlink(std::string_view entry);
|
||||
|
||||
// SHA-256
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2);
|
||||
std::optional<std::string> sha256Of(const std::string_view path);
|
||||
bool sha256Compare(std::string_view file1, std::string_view file2);
|
||||
std::optional<std::string> sha256Of(std::string_view path);
|
||||
|
||||
// Utilities
|
||||
bool copyFile(const std::string_view file, const std::string_view dest);
|
||||
bool runCommand(const std::string_view cmd);
|
||||
bool confirmPropt(const std::string_view message);
|
||||
bool copyFile(std::string_view file, std::string_view dest);
|
||||
bool runCommand(std::string_view cmd);
|
||||
bool confirmPropt(std::string_view message);
|
||||
bool changeMode(std::string_view file, mode_t mode);
|
||||
bool changeOwner(std::string_view file, uid_t uid, gid_t gid);
|
||||
std::string currentWorkingDirectory();
|
||||
std::string currentDate();
|
||||
std::string currentTime();
|
||||
std::string runCommandWithOutput(const std::string_view cmd);
|
||||
std::string runCommandWithOutput(std::string_view cmd);
|
||||
std::string pathJoin(std::string base, std::string relative);
|
||||
std::string pathBasename(const std::string_view entry);
|
||||
std::string pathDirname(const std::string_view entry);
|
||||
std::string pathBasename(std::string_view entry);
|
||||
std::string pathDirname(std::string_view entry);
|
||||
|
||||
// Library-specif
|
||||
std::string getLibVersion();
|
||||
@@ -144,8 +147,9 @@ std::string getLibVersion();
|
||||
#define MB(x) (KB(x) * 1024) // MB(4) = 4194304 (KB(4) * 1024)
|
||||
#define GB(x) (MB(x) * 1024) // GB(1) = 1073741824 (MB(1) * 1024)
|
||||
|
||||
#define TO_MB(x) (x / 1024) // TO_MB(2048) (2048 / 1024)
|
||||
#define TO_GB(x) (TO_GB(x) / 1024) // TO_GB(1048576) (TO_MB(1048576) / 1024)
|
||||
#define TO_KB(x) (x / 1024) // TO_KB(1024) = 1
|
||||
#define TO_MB(x) (TO_KB(x) / 1024) // TO_MB(2048) (2048 / 1024)
|
||||
#define TO_GB(x) (TO_MB(x) / 1024) // TO_GB(1048576) (TO_MB(1048576) / 1024)
|
||||
|
||||
#define STYLE_RESET "\033[0m"
|
||||
#define BOLD "\033[1m"
|
||||
@@ -197,6 +201,8 @@ std::string getLibVersion();
|
||||
if (condition) Helper::Logger(level, __func__, file, name, __FILE__, __LINE__)
|
||||
|
||||
#define MKVERSION(name) \
|
||||
"%s %s [%s %s]\nBuildType: %s\nCMakeVersion: %s\nCompilerVersion: %s\nBuildFlags: %s\n", name, BUILD_VERSION, BUILD_DATE, BUILD_TIME, BUILD_TYPE, BUILD_CMAKE_VERSION, BUILD_COMPILER_VERSION, BUILD_FLAGS
|
||||
char vinfo[512]; \
|
||||
sprintf(vinfo, "%s %s [%s %s]\nBuildType: %s\nCMakeVersion: %s\nCompilerVersion: %s\nBuildFlags: %s", name, BUILD_VERSION, BUILD_DATE, BUILD_TIME, BUILD_TYPE, BUILD_CMAKE_VERSION, BUILD_COMPILER_VERSION, BUILD_FLAGS); \
|
||||
return std::string(vinfo)
|
||||
|
||||
#endif // #ifndef LIBHELPER_LIB_HPP
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
@@ -30,20 +29,20 @@ bool hasSuperUser()
|
||||
|
||||
bool isExists(const std::string_view entry)
|
||||
{
|
||||
struct stat st;
|
||||
struct stat st{};
|
||||
return (stat(entry.data(), &st) == 0);
|
||||
}
|
||||
|
||||
bool fileIsExists(const std::string_view file)
|
||||
{
|
||||
struct stat st;
|
||||
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;
|
||||
struct stat st{};
|
||||
if (stat(directory.data(), &st) != 0) return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
@@ -55,7 +54,7 @@ bool linkIsExists(const std::string_view entry)
|
||||
|
||||
bool isLink(const std::string_view entry)
|
||||
{
|
||||
struct stat st;
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
@@ -67,7 +66,7 @@ bool isSymbolicLink(const std::string_view entry)
|
||||
|
||||
bool isHardLink(const std::string_view entry)
|
||||
{
|
||||
struct stat st;
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return (st.st_nlink >= 2);
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <stdarg.h>
|
||||
#include <cstdarg>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
|
||||
@@ -34,7 +36,7 @@ Error::Error(const char* format, ...)
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << "Error::Error(): " << _message << std::endl;
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
|
||||
const char* Error::what() const noexcept
|
||||
@@ -42,16 +44,16 @@ const char* Error::what() const noexcept
|
||||
return _message.data();
|
||||
}
|
||||
|
||||
Logger::Logger(LogLevels level, const char* func, const char* file, const char* name, const char* sfile, 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",
|
||||
(char)_level,
|
||||
static_cast<char>(_level),
|
||||
_program_name,
|
||||
basename((char*)_file),
|
||||
basename(const_cast<char *>(_file)),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
@@ -59,18 +61,22 @@ Logger::~Logger()
|
||||
_oss.str().data());
|
||||
|
||||
if (!isExists(_logFile)) {
|
||||
remove(_logFile);
|
||||
int fd = open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS);
|
||||
if (fd != -1) close(fd);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
FILE* fp = fopen(_logFile, "a");
|
||||
if (fp != NULL) {
|
||||
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\n", str);
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
|
||||
Logger& Logger::operator<<(std::ostream& (*msg)(std::ostream&))
|
||||
|
||||
@@ -15,13 +15,12 @@
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
@@ -29,10 +28,7 @@
|
||||
static FILE* open_file(const std::string_view file, const char* mode)
|
||||
{
|
||||
FILE* fp = fopen(file.data(), mode);
|
||||
if (fp == nullptr) {
|
||||
throw Helper::Error("Cannot open %s: %s", file.data(), strerror(errno));
|
||||
return fp;
|
||||
}
|
||||
if (fp == nullptr) return nullptr;
|
||||
|
||||
return fp;
|
||||
}
|
||||
@@ -71,37 +67,22 @@ bool copyFile(const std::string_view file, const std::string_view dest)
|
||||
{
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested." << std::endl;
|
||||
|
||||
int src_fd = open(file.data(), O_RDONLY);
|
||||
if (src_fd == - 1) {
|
||||
throw Error("Cannot open %s: %s", file.data(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
const int src_fd = open(file.data(), O_RDONLY);
|
||||
if (src_fd == - 1) return false;
|
||||
|
||||
int dst_fd = open(dest.data(), O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_FILE_PERMS);
|
||||
if (dst_fd == - 1) {
|
||||
throw Error("Cannot create/open %s: %s", dest.data(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
const int dst_fd = open(dest.data(), 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) {
|
||||
ssize_t bw = write(dst_fd, buffer, br);
|
||||
if (bw != br) {
|
||||
throw Error("Cannot write %s: %s", dest.data(), strerror(errno));
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
return false;
|
||||
}
|
||||
if (const ssize_t bw = write(dst_fd, buffer, br); bw != br) return false;
|
||||
}
|
||||
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
if (br == -1) {
|
||||
throw Error("Cannot read %s: %s", file.data(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (br == -1) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfull." << std::endl;
|
||||
return true;
|
||||
@@ -111,39 +92,32 @@ 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) ? true : false;
|
||||
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;
|
||||
|
||||
char tmp[PATH_MAX], *p;
|
||||
size_t len;
|
||||
char tmp[PATH_MAX];
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s", paths.data());
|
||||
len = strlen(tmp);
|
||||
if (tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
if (const size_t len = strlen(tmp); tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
|
||||
for (p = tmp + 1; *p; 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) {
|
||||
throw Error("Cannot create directory: %s: %s", tmp, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
&& errno != EEXIST)
|
||||
return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) {
|
||||
throw Error("Cannot create directory: %s: %s", tmp, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
@@ -154,16 +128,10 @@ bool createFile(const std::string_view path)
|
||||
{
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
|
||||
if (isExists(path)) {
|
||||
throw Error("%s: is exists", path.data());
|
||||
return false;
|
||||
}
|
||||
if (isExists(path)) return false;
|
||||
|
||||
int fd = open(path.data(), O_RDONLY | O_CREAT, DEFAULT_FILE_PERMS);
|
||||
if (fd == -1) {
|
||||
throw Error("Cannot create %s: %s", path.data(), strerror(errno));
|
||||
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;
|
||||
@@ -173,38 +141,31 @@ bool createFile(const std::string_view path)
|
||||
bool createSymlink(const std::string_view entry1, const std::string_view entry2)
|
||||
{
|
||||
LOGN(HELPER, INFO) << "symlink \"" << entry1 << "\" to \"" << entry2 << "\" requested." << std::endl;
|
||||
int ret = symlink(entry1.data(), entry2.data());
|
||||
if (ret != 0)
|
||||
throw Error("Cannot symlink %s: %s", entry2.data(), strerror(errno));
|
||||
if (const int ret = symlink(entry1.data(), entry2.data()); ret != 0) return false;
|
||||
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << "\"" << entry1 << "\" symlinked to \"" << entry2 << "\" successfully." << std::endl;
|
||||
return (ret == 0);
|
||||
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;
|
||||
int ret = remove(entry.data());
|
||||
if (ret != 0)
|
||||
throw Error("Cannot remove %s: %s", entry.data(), strerror(errno));
|
||||
if (int ret = remove(entry.data()); ret != 0) return false;
|
||||
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return (ret == 0);
|
||||
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;
|
||||
struct dirent *entry;
|
||||
struct stat buf{};
|
||||
dirent *entry;
|
||||
|
||||
DIR *dir = opendir(directory.data());
|
||||
if (dir == nullptr) {
|
||||
throw Error("Cannot open directory %s: %s", directory.data(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (dir == nullptr) return false;
|
||||
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
char fullpath[PATH_MAX];
|
||||
|
||||
if (strcmp(entry->d_name, ".") == 0
|
||||
@@ -214,7 +175,6 @@ bool eraseDirectoryRecursive(const std::string_view directory)
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(), entry->d_name);
|
||||
|
||||
if (lstat(fullpath, &buf) == -1) {
|
||||
throw Error("Cannot stat %s: %s", fullpath, strerror(errno));
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
@@ -226,7 +186,6 @@ bool eraseDirectoryRecursive(const std::string_view directory)
|
||||
}
|
||||
} else {
|
||||
if (unlink(fullpath) == -1) {
|
||||
throw Error("Cannot unlink %s: %s", fullpath, strerror(errno));
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
@@ -234,10 +193,7 @@ bool eraseDirectoryRecursive(const std::string_view directory)
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
if (rmdir(directory.data()) == -1) {
|
||||
throw Error("Cannot remove directory %s: %s", directory.data(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
if (rmdir(directory.data()) == -1) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased." << std::endl;
|
||||
return true;
|
||||
@@ -248,11 +204,8 @@ std::string readSymlink(const std::string_view entry)
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
|
||||
char target[PATH_MAX];
|
||||
ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
if (len == -1) {
|
||||
throw Error("Cannot read symlink %s: %s", entry.data(), strerror(errno));
|
||||
return entry.data();
|
||||
}
|
||||
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;
|
||||
@@ -262,7 +215,7 @@ std::string readSymlink(const std::string_view entry)
|
||||
size_t fileSize(const std::string_view file)
|
||||
{
|
||||
LOGN(HELPER, INFO) << "get file size request: " << file << std::endl;
|
||||
struct stat st;
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return static_cast<size_t>(st.st_size);
|
||||
}
|
||||
|
||||
@@ -30,16 +30,9 @@ 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());
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!fileIsExists(fp)) throw Error("Is not exists or not file: %s", fp.data());
|
||||
|
||||
std::ifstream file(fp, std::ios::binary);
|
||||
if (!file) {
|
||||
throw Error("Cannot open file: %s", fp.data());
|
||||
return std::nullopt;
|
||||
}
|
||||
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());
|
||||
@@ -50,8 +43,8 @@ std::optional<std::string> sha256Of(const std::string_view path)
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2)
|
||||
{
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
|
||||
auto f1 = sha256Of(file1);
|
||||
auto f2 = sha256Of(file2);
|
||||
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);
|
||||
|
||||
@@ -18,14 +18,14 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <ctime>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace Helper {
|
||||
namespace LoggingProperties {
|
||||
@@ -78,28 +78,24 @@ bool confirmPropt(const std::string_view message)
|
||||
std::cin >> p;
|
||||
|
||||
if (p == 'y' || p == 'Y') return true;
|
||||
else if (p == 'n' || p == 'N') return false;
|
||||
else {
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
if (p == 'n' || p == 'N') return false;
|
||||
|
||||
return false;
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
|
||||
std::string currentWorkingDirectory()
|
||||
{
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return std::string();
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return {};
|
||||
return cwd;
|
||||
}
|
||||
|
||||
std::string currentDate()
|
||||
{
|
||||
time_t t = time(nullptr);
|
||||
struct tm *date = localtime(&t);
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (date)
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_mday) + "/" +
|
||||
std::to_string(date->tm_mon + 1) + "/" +
|
||||
@@ -109,10 +105,9 @@ std::string currentDate()
|
||||
|
||||
std::string currentTime()
|
||||
{
|
||||
time_t t = time(nullptr);
|
||||
struct tm *date = localtime(&t);
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (date)
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_hour) + ":" +
|
||||
std::to_string(date->tm_min) + ":" +
|
||||
@@ -125,10 +120,7 @@ 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) {
|
||||
throw Error("Cannot run command: %s", cmd.data());
|
||||
return {};
|
||||
}
|
||||
if (!pipe) return {};
|
||||
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||
|
||||
@@ -150,31 +142,31 @@ std::string pathJoin(std::string base, std::string relative)
|
||||
|
||||
std::string pathBasename(const std::string_view entry)
|
||||
{
|
||||
if (!isExists(entry)) {
|
||||
throw Error("No such file or directory: %s", entry.data());
|
||||
return {};
|
||||
}
|
||||
|
||||
char* base = basename((char*)entry.data());
|
||||
char* base = basename(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
|
||||
std::string pathDirname(const std::string_view entry)
|
||||
{
|
||||
if (!isExists(entry)) {
|
||||
throw Error("No such file or directory: %s", entry.data());
|
||||
return {};
|
||||
}
|
||||
|
||||
char* base = dirname((char*)entry.data());
|
||||
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 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;
|
||||
}
|
||||
|
||||
std::string getLibVersion()
|
||||
{
|
||||
char vinfo[512];
|
||||
sprintf(vinfo, MKVERSION("libhelper"));
|
||||
return std::string(vinfo);
|
||||
MKVERSION("libhelper");
|
||||
}
|
||||
|
||||
} // namespace Helper
|
||||
|
||||
@@ -24,14 +24,18 @@ set(LIBPARTITION_MAP_SOURCES
|
||||
# Add targets
|
||||
add_library(partition_map_shared SHARED ${LIBPARTITION_MAP_SOURCES})
|
||||
add_library(partition_map_static STATIC ${LIBPARTITION_MAP_SOURCES})
|
||||
add_executable(libpartition_map_test tests/test.cpp)
|
||||
|
||||
# Set appropriate output names
|
||||
set_target_properties(partition_map_shared PROPERTIES OUTPUT_NAME "partition_map")
|
||||
set_target_properties(partition_map_static PROPERTIES OUTPUT_NAME "partition_map")
|
||||
|
||||
# Set linker flags
|
||||
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local")
|
||||
target_link_options(partition_map_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
|
||||
target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
|
||||
target_link_libraries(partition_map_shared PRIVATE helper_shared)
|
||||
|
||||
# Build libpartition_map_test if CMAKE_BUILD_TYPE is not release
|
||||
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
|
||||
add_executable(libpartition_map_test ${CMAKE_CURRENT_SOURCE_DIR}/tests/test.cpp)
|
||||
target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
|
||||
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
|
||||
endif()
|
||||
|
||||
@@ -39,19 +39,17 @@ struct _entry {
|
||||
};
|
||||
|
||||
/**
|
||||
* basic_partition_map
|
||||
* -------------------
|
||||
* The main type of the library. The Builder class is designed
|
||||
* to be easily manipulated and modified only on this class.
|
||||
*/
|
||||
class basic_partition_map {
|
||||
private:
|
||||
void _resize_map();
|
||||
int _index_of(const std::string_view name) const;
|
||||
[[nodiscard]] int _index_of(std::string_view name) const;
|
||||
|
||||
public:
|
||||
_entry* _data;
|
||||
size_t _count, _capacity;
|
||||
size_t _count{}, _capacity{};
|
||||
|
||||
struct _returnable_entry {
|
||||
uint64_t size;
|
||||
@@ -60,20 +58,20 @@ public:
|
||||
|
||||
using BasicInf = _returnable_entry;
|
||||
|
||||
basic_partition_map(const std::string name, uint64_t size, bool logical);
|
||||
basic_partition_map(const std::string& name, uint64_t size, bool logical);
|
||||
basic_partition_map(const basic_partition_map& other);
|
||||
basic_partition_map();
|
||||
~basic_partition_map();
|
||||
|
||||
bool insert(const std::string name, uint64_t size, bool logical);
|
||||
bool insert(const std::string& name, uint64_t size, bool logical);
|
||||
void merge(const basic_partition_map& map);
|
||||
uint64_t get_size(const std::string_view name) const;
|
||||
bool is_logical(const std::string_view name) const;
|
||||
_returnable_entry get_all(const std::string_view name) const;
|
||||
bool find(const std::string_view name) const;
|
||||
std::string find_(const std::string name) const;
|
||||
size_t size() const;
|
||||
bool empty() const;
|
||||
[[nodiscard]] uint64_t get_size(std::string_view name) const;
|
||||
[[nodiscard]] bool is_logical(std::string_view name) const;
|
||||
[[nodiscard]] _returnable_entry get_all(std::string_view name) const;
|
||||
[[nodiscard]] bool find(std::string_view name) const;
|
||||
[[nodiscard]] std::string find_(const std::string& name) const;
|
||||
[[nodiscard]] size_t size() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
void clear();
|
||||
|
||||
basic_partition_map& operator=(const basic_partition_map& map);
|
||||
@@ -84,10 +82,10 @@ public:
|
||||
public:
|
||||
_entry* ptr;
|
||||
|
||||
iterator(_entry* p);
|
||||
explicit iterator(_entry* p);
|
||||
|
||||
auto operator*() -> std::pair<std::string&, decltype(_entry::props)&>;
|
||||
_entry* operator->();
|
||||
auto operator*() const -> std::pair<std::string&, decltype(_entry::props)&>;
|
||||
_entry* operator->() const;
|
||||
iterator& operator++();
|
||||
iterator operator++(int);
|
||||
bool operator!=(const iterator& other) const;
|
||||
@@ -98,7 +96,7 @@ public:
|
||||
public:
|
||||
const _entry* ptr;
|
||||
|
||||
constant_iterator(const _entry* p);
|
||||
explicit constant_iterator(const _entry* p);
|
||||
|
||||
auto operator*() const -> std::pair<const std::string&, const decltype(_entry::props)&>;
|
||||
const _entry* operator->() const;
|
||||
@@ -109,13 +107,11 @@ public:
|
||||
};
|
||||
|
||||
/* for-each support */
|
||||
iterator begin();
|
||||
iterator end();
|
||||
[[nodiscard]] iterator begin() const;
|
||||
[[nodiscard]] iterator end() const;
|
||||
|
||||
constant_iterator begin() const;
|
||||
constant_iterator cbegin() const;
|
||||
constant_iterator end() const;
|
||||
constant_iterator cend() const;
|
||||
[[nodiscard]] constant_iterator cbegin() const;
|
||||
[[nodiscard]] constant_iterator cend() const;
|
||||
};
|
||||
|
||||
using Map_t = basic_partition_map;
|
||||
@@ -126,16 +122,14 @@ private:
|
||||
std::string _workdir;
|
||||
bool _any_generating_error, _map_builded;
|
||||
|
||||
bool _is_real_block_dir(const std::string_view path) const;
|
||||
[[nodiscard]] static bool _is_real_block_dir(std::string_view path);
|
||||
Map_t _build_map(std::string_view path, bool logical = false);
|
||||
void _insert_logicals(Map_t&& logicals);
|
||||
void _map_build_check() const;
|
||||
uint64_t _get_size(const std::string path);
|
||||
[[nodiscard]] uint64_t _get_size(const std::string& path);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructor
|
||||
* -------------------
|
||||
* By default, it searches the directories in the
|
||||
* defaultEntryList in PartitionMap.cpp in order and
|
||||
* uses the directory it finds.
|
||||
@@ -143,189 +137,145 @@ public:
|
||||
basic_partition_map_builder();
|
||||
|
||||
/**
|
||||
* Secondary constructor
|
||||
* ---------------------
|
||||
* It has one arguments:
|
||||
* - Directory path to search
|
||||
* A constructor with input. Need search path
|
||||
*/
|
||||
basic_partition_map_builder(const std::string_view path);
|
||||
explicit basic_partition_map_builder(std::string_view path);
|
||||
|
||||
/**
|
||||
* getAll()
|
||||
* --------
|
||||
* Returns the current list content in Map_t type.
|
||||
* If no list is created, returns std::nullopt.
|
||||
*/
|
||||
Map_t getAll() const;
|
||||
[[nodiscard]] Map_t getAll() const;
|
||||
|
||||
/**
|
||||
* get(name)
|
||||
* ---------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* Returns information of a specific partition in
|
||||
* Map_temp_t type. If the partition is not in the
|
||||
* currently created list, returns std::nullopt.
|
||||
*/
|
||||
std::optional<std::pair<uint64_t, bool>> get(const std::string_view name) const;
|
||||
[[nodiscard]] std::optional<std::pair<uint64_t, bool>> get(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* getLogicalPartitionList()
|
||||
* -------------------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* If there is a logical partition(s) in the created
|
||||
* list, it returns a list of type std::list (containing
|
||||
* data of type std::string). If there is no logical
|
||||
* partition in the created list, it returns std::nullopt.
|
||||
*/
|
||||
std::optional<std::list<std::string>> getLogicalPartitionList() const;
|
||||
[[nodiscard]] std::optional<std::list<std::string>> getLogicalPartitionList() const;
|
||||
|
||||
/**
|
||||
* getPhysicalPartitionList()
|
||||
* --------------------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* The physical partitions in the created list are
|
||||
* returned as std::list type. If there is no content
|
||||
* due to any problem, returns std::nullopt.
|
||||
*/
|
||||
std::optional<std::list<std::string>> getPhysicalPartitionList() const;
|
||||
[[nodiscard]] std::optional<std::list<std::string>> getPhysicalPartitionList() const;
|
||||
|
||||
/**
|
||||
* The partitions in the created list are returned as std::list
|
||||
* If there is no content due to any problem, returns std::nullopt
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::list<std::string>> getPartitionList() const;
|
||||
|
||||
/**
|
||||
* getRealLinkPathOf(name)
|
||||
* -----------------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* Returns the full link path of the entered partition
|
||||
* name in the current search directory as std::string.
|
||||
* If the partition is not in the list, an empty
|
||||
* std::string is returned.
|
||||
*/
|
||||
std::string getRealLinkPathOf(const std::string_view name) const;
|
||||
[[nodiscard]] std::string getRealLinkPathOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* getRealPathOf(name)
|
||||
* -------------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* Returns the actual path of the partition as
|
||||
* std::string. Like /dev/block/sda5
|
||||
*/
|
||||
std::string getRealPathOf(const std::string_view name) const;
|
||||
[[nodiscard]] std::string getRealPathOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* getCurrentWorkDir()
|
||||
* -------------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* If it exists, the path to the search string is
|
||||
* returned as std::string. If it does not exist,
|
||||
* an empty std::string is returned.
|
||||
*/
|
||||
std::string getCurrentWorkDir() const;
|
||||
[[nodiscard]] std::string getCurrentWorkDir() const;
|
||||
|
||||
/**
|
||||
* hasPartition(name)
|
||||
* ------------------
|
||||
* Returns whether the entered partition name is in the
|
||||
* created partition list as a bool.
|
||||
*/
|
||||
bool hasPartition(const std::string_view name) const;
|
||||
[[nodiscard]] bool hasPartition(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* isLogical(name)
|
||||
* ---------------
|
||||
* Returns the bool type status of whether the
|
||||
* entered section name is marked as logical in the
|
||||
* created list. Alternatively, the current section
|
||||
* information can be retrieved with the Get() function
|
||||
* entered partition name is marked as logical in the
|
||||
* created list. Alternatively, the current partition
|
||||
* information can be retrieved with the get() function
|
||||
* and checked for logicality.
|
||||
*/
|
||||
bool isLogical(const std::string_view name) const;
|
||||
[[nodiscard]] bool isLogical(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* clear()
|
||||
* -------
|
||||
* The created list and the current search index name are cleared.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* readDirectory(path)
|
||||
* -------------------
|
||||
* The entered path is defined as the new search
|
||||
* directory and the search is performed in the entered
|
||||
* directory. If everything goes well, true is returned.
|
||||
*/
|
||||
bool readDirectory(const std::string_view path);
|
||||
bool readDirectory(std::string_view path);
|
||||
|
||||
/**
|
||||
* Reads default /dev entries and builds map.
|
||||
*/
|
||||
bool readDefaultDirectories();
|
||||
|
||||
/**
|
||||
* empty()
|
||||
* -------
|
||||
* Whether the current list is empty or not is returned
|
||||
* as bool type. If there is content in the list, true
|
||||
* is returned, otherwise false is returned.
|
||||
*/
|
||||
bool empty() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
/**
|
||||
* sizeOf(name)
|
||||
* ------------
|
||||
* WARNING: Learn about std::optional before using this function.
|
||||
*
|
||||
* If it exists, the size of the partition with the
|
||||
* entered name is returned as uint64_t type.
|
||||
* If it does not exist, 0 is returned.
|
||||
*/
|
||||
uint64_t sizeOf(const std::string_view name) const;
|
||||
[[nodiscard]] uint64_t sizeOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* == operator
|
||||
* -----------
|
||||
* If the content lists of the two created objects are
|
||||
* the same (checked only according to the partition
|
||||
* names), true is returned, otherwise false is returned
|
||||
*/
|
||||
friend bool operator==(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs);
|
||||
friend bool operator==(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs);
|
||||
|
||||
/**
|
||||
* != operator
|
||||
* -----------
|
||||
* The opposite logic of the == operator.
|
||||
*/
|
||||
friend bool operator!=(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs);
|
||||
friend bool operator!=(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs);
|
||||
|
||||
/**
|
||||
* Boolean operator
|
||||
* ----------------
|
||||
* You can check whether the object was created
|
||||
* successfully. If the problem did not occur, true is
|
||||
* returned, if it did, false is returned.
|
||||
*/
|
||||
operator bool() const;
|
||||
explicit operator bool() const;
|
||||
|
||||
/**
|
||||
* ! operator
|
||||
* ----------
|
||||
* Returns true if the object creation failed (i.e., there's a problem),
|
||||
* and false if the object is correctly created.
|
||||
*/
|
||||
bool operator!() const;
|
||||
|
||||
/**
|
||||
* () operator
|
||||
* -----------
|
||||
* Build map with input path. Implementation of readDirectory().
|
||||
*/
|
||||
bool operator()(const std::string_view path);
|
||||
bool operator()(std::string_view path);
|
||||
};
|
||||
|
||||
using Error = Helper::Error;
|
||||
|
||||
/**
|
||||
* getLibVersion()
|
||||
* ---------------
|
||||
* To get the version information of libpartition_map
|
||||
* library. It is returned as std::string type.
|
||||
*/
|
||||
|
||||
@@ -59,6 +59,16 @@ std::optional<std::list<std::string>> basic_partition_map_builder::getPhysicalPa
|
||||
return physicals;
|
||||
}
|
||||
|
||||
std::optional<std::list<std::string>> basic_partition_map_builder::getPartitionList() const {
|
||||
_map_build_check();
|
||||
|
||||
std::list<std::string> partitions;
|
||||
for (const auto& [name, props] : _current_map) partitions.push_back(name);
|
||||
|
||||
if (partitions.empty()) return std::nullopt;
|
||||
return partitions;
|
||||
}
|
||||
|
||||
std::string basic_partition_map_builder::getRealLinkPathOf(const std::string_view name) const
|
||||
{
|
||||
_map_build_check();
|
||||
|
||||
@@ -18,19 +18,18 @@
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <errno.h>
|
||||
#include <cerrno>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
|
||||
static constexpr std::array<std::string_view, 3> defaultEntryList = {
|
||||
@@ -41,9 +40,12 @@ static constexpr std::array<std::string_view, 3> defaultEntryList = {
|
||||
|
||||
namespace PartitionMap {
|
||||
|
||||
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path) const
|
||||
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path)
|
||||
{
|
||||
if (path.find("/block/") == std::string::npos) return false;
|
||||
if (path.find("/block/") == std::string::npos) {
|
||||
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -68,6 +70,7 @@ Map_t basic_partition_map_builder::_build_map(std::string_view path, bool logica
|
||||
|
||||
void basic_partition_map_builder::_insert_logicals(Map_t&& logicals)
|
||||
{
|
||||
LOGN(MAP, INFO) << "merging created logical partition list to this object's variable." << std::endl;
|
||||
_current_map.merge(logicals);
|
||||
}
|
||||
|
||||
@@ -77,10 +80,10 @@ void basic_partition_map_builder::_map_build_check() const
|
||||
throw Error("Please build partition map before!");
|
||||
}
|
||||
|
||||
uint64_t basic_partition_map_builder::_get_size(const std::string path)
|
||||
uint64_t basic_partition_map_builder::_get_size(const std::string& path)
|
||||
{
|
||||
std::string real = std::filesystem::read_symlink(path);
|
||||
int fd = open(real.data(), O_RDONLY);
|
||||
const std::string real = std::filesystem::read_symlink(path);
|
||||
const int fd = open(real.data(), O_RDONLY);
|
||||
if (fd < 0) {
|
||||
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno) << std::endl;
|
||||
return 0;
|
||||
@@ -106,7 +109,6 @@ basic_partition_map_builder::basic_partition_map_builder()
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) {
|
||||
_any_generating_error = true;
|
||||
continue;
|
||||
} else {
|
||||
_workdir = path;
|
||||
break;
|
||||
@@ -127,7 +129,7 @@ basic_partition_map_builder::basic_partition_map_builder(const std::string_view
|
||||
LOGN(MAP, INFO) << "argument-based constructor called. Starting build." << std::endl;
|
||||
|
||||
if (std::filesystem::exists(path)) {
|
||||
_is_real_block_dir(path);
|
||||
if (!_is_real_block_dir(path)) return;
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) _any_generating_error = true;
|
||||
else _workdir = path;
|
||||
@@ -179,6 +181,33 @@ bool basic_partition_map_builder::readDirectory(const std::string_view path)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::readDefaultDirectories()
|
||||
{
|
||||
_map_builded = false;
|
||||
LOGN(MAP, INFO) << "read default directories request." << std::endl;
|
||||
|
||||
for (const auto& path : defaultEntryList) {
|
||||
if (std::filesystem::exists(path)) {
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) {
|
||||
_any_generating_error = true;
|
||||
return false;
|
||||
} else {
|
||||
_workdir = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_current_map.empty())
|
||||
LOGN(MAP, ERROR) << "Cannot build map by any default search entry." << std::endl;
|
||||
|
||||
LOGN(MAP, INFO) << "read default directories successfull." << std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::empty() const
|
||||
{
|
||||
_map_build_check();
|
||||
@@ -191,12 +220,12 @@ uint64_t basic_partition_map_builder::sizeOf(const std::string_view name) const
|
||||
return _current_map.get_size(name);
|
||||
}
|
||||
|
||||
bool operator==(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
|
||||
bool operator==(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs)
|
||||
{
|
||||
return lhs._current_map == rhs._current_map;
|
||||
}
|
||||
|
||||
bool operator!=(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
|
||||
bool operator!=(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
@@ -219,9 +248,7 @@ bool basic_partition_map_builder::operator()(const std::string_view path)
|
||||
|
||||
std::string getLibVersion()
|
||||
{
|
||||
char vinfo[512];
|
||||
sprintf(vinfo, MKVERSION("libpartition_map"));
|
||||
return std::string(vinfo);
|
||||
MKVERSION("libpartition_map");
|
||||
}
|
||||
|
||||
} // namespace PartitionMap
|
||||
|
||||
@@ -23,12 +23,12 @@ namespace PartitionMap {
|
||||
|
||||
basic_partition_map::iterator::iterator(_entry* p) : ptr(p) {}
|
||||
|
||||
auto basic_partition_map::iterator::operator*() -> std::pair<std::string&, decltype(_entry::props)&>
|
||||
auto basic_partition_map::iterator::operator*() const -> std::pair<std::string&, decltype(_entry::props)&>
|
||||
{
|
||||
return {ptr->name, ptr->props};
|
||||
}
|
||||
|
||||
_entry* basic_partition_map::iterator::operator->()
|
||||
_entry* basic_partition_map::iterator::operator->() const
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
@@ -41,17 +41,17 @@ basic_partition_map::iterator& basic_partition_map::iterator::operator++()
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::iterator::operator++(int)
|
||||
{
|
||||
basic_partition_map::iterator tmp = *this;
|
||||
iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool basic_partition_map::iterator::operator==(const basic_partition_map::iterator& other) const
|
||||
bool basic_partition_map::iterator::operator==(const iterator& other) const
|
||||
{
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
|
||||
bool basic_partition_map::iterator::operator!=(const basic_partition_map::iterator& other) const
|
||||
bool basic_partition_map::iterator::operator!=(const iterator& other) const
|
||||
{
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
@@ -76,25 +76,25 @@ basic_partition_map::constant_iterator& basic_partition_map::constant_iterator::
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::constant_iterator::operator++(int)
|
||||
{
|
||||
basic_partition_map::constant_iterator tmp = *this;
|
||||
constant_iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool basic_partition_map::constant_iterator::operator==(const basic_partition_map::constant_iterator& other) const
|
||||
bool basic_partition_map::constant_iterator::operator==(const constant_iterator& other) const
|
||||
{
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
|
||||
bool basic_partition_map::constant_iterator::operator!=(const basic_partition_map::constant_iterator& other) const
|
||||
bool basic_partition_map::constant_iterator::operator!=(const constant_iterator& other) const
|
||||
{
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
|
||||
void basic_partition_map::_resize_map()
|
||||
{
|
||||
size_t new_capacity = _capacity * 2;
|
||||
_entry* new_data = new _entry[new_capacity];
|
||||
const size_t new_capacity = _capacity * 2;
|
||||
auto* new_data = new _entry[new_capacity];
|
||||
|
||||
for (size_t i = 0; i < _count; i++) new_data[i] = _data[i];
|
||||
|
||||
@@ -106,13 +106,13 @@ void basic_partition_map::_resize_map()
|
||||
int basic_partition_map::_index_of(const std::string_view name) const
|
||||
{
|
||||
for (size_t i = 0; i < _count; i++) {
|
||||
if (name == _data[i].name) return (int)i;
|
||||
if (name == _data[i].name) return static_cast<int>(i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
basic_partition_map::basic_partition_map(const std::string name, uint64_t size, bool logical)
|
||||
basic_partition_map::basic_partition_map(const std::string& name, const uint64_t size, const bool logical)
|
||||
{
|
||||
_data = new _entry[_capacity];
|
||||
insert(name, size, logical);
|
||||
@@ -126,7 +126,7 @@ basic_partition_map::basic_partition_map(const basic_partition_map& other) :
|
||||
std::copy(other._data, other._data + _count, _data);
|
||||
}
|
||||
|
||||
basic_partition_map::basic_partition_map() : _count(0), _capacity(6)
|
||||
basic_partition_map::basic_partition_map() : _capacity(6)
|
||||
{
|
||||
_data = new _entry[_capacity];
|
||||
}
|
||||
@@ -136,7 +136,7 @@ basic_partition_map::~basic_partition_map()
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
bool basic_partition_map::insert(const std::string name, uint64_t size, bool logical)
|
||||
bool basic_partition_map::insert(const std::string& name, const uint64_t size, const bool logical)
|
||||
{
|
||||
if (name == _data[_index_of(name)].name) return false;
|
||||
if (_count == _capacity) _resize_map();
|
||||
@@ -186,7 +186,7 @@ bool basic_partition_map::find(const std::string_view name) const
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string basic_partition_map::find_(const std::string name) const
|
||||
std::string basic_partition_map::find_(const std::string& name) const
|
||||
{
|
||||
if (name == _data[_index_of(name)].name) return name;
|
||||
|
||||
@@ -249,34 +249,24 @@ bool basic_partition_map::operator!=(const basic_partition_map& other) const
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::begin()
|
||||
basic_partition_map::iterator basic_partition_map::begin() const
|
||||
{
|
||||
return basic_partition_map::iterator(_data);
|
||||
return iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::end()
|
||||
basic_partition_map::iterator basic_partition_map::end() const
|
||||
{
|
||||
return basic_partition_map::iterator(_data + _count);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::begin() const
|
||||
{
|
||||
return basic_partition_map::constant_iterator(_data);
|
||||
return iterator(_data + _count);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::cbegin() const
|
||||
{
|
||||
return basic_partition_map::constant_iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::end() const
|
||||
{
|
||||
return basic_partition_map::constant_iterator(_data + _count);
|
||||
return constant_iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::cend() const
|
||||
{
|
||||
return basic_partition_map::constant_iterator(_data + _count);
|
||||
return constant_iterator(_data + _count);
|
||||
}
|
||||
|
||||
} // namespace PartitionMap
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include <unistd.h>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
|
||||
int main(void) {
|
||||
int main() {
|
||||
if (getuid() != 0) return 2;
|
||||
|
||||
try {
|
||||
@@ -28,7 +28,7 @@ int main(void) {
|
||||
if (!MyMap) throw PartitionMap::Error("Cannot generate object!");
|
||||
}
|
||||
|
||||
auto map = MyMap.getAll();
|
||||
const auto map = MyMap.getAll();
|
||||
if (map.empty()) throw PartitionMap::Error("getAll() empty");
|
||||
for (const auto& [name, props] : map) {
|
||||
std::cout << "Partition: " << name << ", size: "
|
||||
@@ -36,19 +36,19 @@ int main(void) {
|
||||
<< props.isLogical << std::endl;
|
||||
}
|
||||
|
||||
auto boot = MyMap.get("boot");
|
||||
const auto boot = MyMap.get("boot");
|
||||
if (!boot) throw PartitionMap::Error("get(\"boot\") returned nullopt");
|
||||
std::cout << "Name: boot" << ", size: "
|
||||
<< boot->first << ", logical: "
|
||||
<< boot->second << std::endl;
|
||||
|
||||
auto logicals = MyMap.getLogicalPartitionList();
|
||||
const auto logicals = MyMap.getLogicalPartitionList();
|
||||
if (!logicals) throw PartitionMap::Error("getLogicalPartitionList() returned nullopt");
|
||||
std::cout << "Logical partitions: " << std::endl;
|
||||
for (const auto& name : *logicals)
|
||||
std::cout << " - " << name << std::endl;
|
||||
|
||||
auto physicals = MyMap.getPhysicalPartitionList();
|
||||
const auto physicals = MyMap.getPhysicalPartitionList();
|
||||
if (!physicals) throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
|
||||
std::cout << "Physical partitions: " << std::endl;
|
||||
for (const auto& name : *physicals)
|
||||
|
||||
Reference in New Issue
Block a user