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
|
||||
|
||||
Reference in New Issue
Block a user