pmt: Write the function base
- Write the function manager. - Make minor changes to the function structure. - Add CLI11 license. - Added main function to libpmt.
This commit is contained in:
@@ -25,11 +25,9 @@
|
||||
|
||||
static void __create_log_file(const char* file)
|
||||
{
|
||||
remove(file);
|
||||
int fd = open(file, O_WRONLY | O_TRUNC, DEFAULT_EXTENDED_FILE_PERMS);
|
||||
if (fd == -1) {
|
||||
fd = open(file, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS);
|
||||
if (fd != -1) close(fd);
|
||||
} else if (fd != -1) close(fd);
|
||||
if (fd != -1) close(fd);
|
||||
}
|
||||
|
||||
namespace Helper {
|
||||
@@ -50,19 +48,20 @@ const char* Error::what() const noexcept
|
||||
return _message.data();
|
||||
}
|
||||
|
||||
Logger::Logger(LogLevels level, const char* file, const char* name, const char* sfile, int line) : _level(level), _logFile(file), _program_name(name), _file(sfile), _line(line) {}
|
||||
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()
|
||||
{
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s",
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
(char)_level,
|
||||
_program_name,
|
||||
basename((char*)_file),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
_funcname,
|
||||
_oss.str().data());
|
||||
|
||||
if (!isExists(_logFile)) __create_log_file(_logFile);
|
||||
|
||||
@@ -41,20 +41,20 @@ namespace Helper {
|
||||
|
||||
bool writeFile(const std::string_view file, const std::string_view text)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): write \"" << text << "\" to " << file << "requested." << std::endl;
|
||||
LOGN(HELPER, INFO) << "write \"" << text << "\" to " << file << "requested." << std::endl;
|
||||
FILE* fp = open_file(file, "a");
|
||||
if (fp == nullptr) return false;
|
||||
|
||||
fprintf(fp, "%s", text.data());
|
||||
fclose(fp);
|
||||
|
||||
LOGN(HELPER, INFO) << __func__ << "(): write " << file << " successfull." << std::endl;
|
||||
LOGN(HELPER, INFO) << "write " << file << " successfull." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<std::string> readFile(const std::string_view file)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): read " << file << " requested." << std::endl;
|
||||
LOGN(HELPER, INFO) << "read " << file << " requested." << std::endl;
|
||||
FILE* fp = open_file(file, "r");
|
||||
if (fp == nullptr) return std::nullopt;
|
||||
|
||||
@@ -63,13 +63,13 @@ std::optional<std::string> readFile(const std::string_view file)
|
||||
while (fgets(buffer, sizeof(buffer), fp)) str += buffer;
|
||||
|
||||
fclose(fp);
|
||||
LOGN(HELPER, INFO) << __func__ << "(): read " << file << " successfull, readed text: \"" << str << "\"" << std::endl;
|
||||
LOGN(HELPER, INFO) << "read " << file << " successfull, readed text: \"" << str << "\"" << std::endl;
|
||||
return str;
|
||||
}
|
||||
|
||||
bool copyFile(const std::string_view file, const std::string_view dest)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): copy " << file << " to " << dest << " requested." << std::endl;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested." << std::endl;
|
||||
|
||||
int src_fd = open(file.data(), O_RDONLY);
|
||||
if (src_fd == - 1) {
|
||||
@@ -103,20 +103,20 @@ bool copyFile(const std::string_view file, const std::string_view dest)
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << __func__ << "(); copy " << file << " to " << dest << " successfull." << std::endl;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfull." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool makeDirectory(const std::string_view path)
|
||||
{
|
||||
if (isExists(path)) return false;
|
||||
LOGN(HELPER, INFO) << __func__ << "(): trying making directory: " << path << std::endl;
|
||||
LOGN(HELPER, INFO) << "trying making directory: " << path << std::endl;
|
||||
return (mkdir(path.data(), DEFAULT_DIR_PERMS) == 0) ? true : false;
|
||||
}
|
||||
|
||||
bool makeRecursiveDirectory(const std::string_view paths)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): make recursive directory requested: " << paths << std::endl;
|
||||
LOGN(HELPER, INFO) << "make recursive directory requested: " << paths << std::endl;
|
||||
|
||||
char tmp[PATH_MAX], *p;
|
||||
size_t len;
|
||||
@@ -146,13 +146,13 @@ bool makeRecursiveDirectory(const std::string_view paths)
|
||||
}
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << __func__ << "(): " << paths << " successfully created." << std::endl;
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool createFile(const std::string_view path)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): create file request: " << path << std::endl;
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
|
||||
if (isExists(path)) {
|
||||
throw Error("%s: is exists", path.data());
|
||||
@@ -166,35 +166,35 @@ bool createFile(const std::string_view path)
|
||||
}
|
||||
|
||||
close(fd);
|
||||
LOGN(HELPER, INFO) << __func__ << "(): create file \"" << path << "\" successfull." << std::endl;
|
||||
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) << __func__ << "(): symlink \"" << entry1 << "\" to \"" << entry2 << "\" requested." << std::endl;
|
||||
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));
|
||||
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << __func__ << "(): \"" << entry1 << "\" symlinked to \"" << entry2 << "\" successfully." << std::endl;
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << "\"" << entry1 << "\" symlinked to \"" << entry2 << "\" successfully." << std::endl;
|
||||
return (ret == 0);
|
||||
}
|
||||
|
||||
bool eraseEntry(const std::string_view entry)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): erase \"" << entry << "\" requested." << std::endl;
|
||||
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));
|
||||
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << __func__ << "(): \"" << entry << "\" erased successfully." << std::endl;
|
||||
LOGN_IF(HELPER, INFO, ret == 0) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return (ret == 0);
|
||||
}
|
||||
|
||||
bool eraseDirectoryRecursive(const std::string_view directory)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): erase recursive requested: " << directory << std::endl;
|
||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||
struct stat buf;
|
||||
struct dirent *entry;
|
||||
|
||||
@@ -239,13 +239,13 @@ bool eraseDirectoryRecursive(const std::string_view directory)
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << __func__ << "(): \"" << directory << "\" successfully erased." << std::endl;
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string readSymlink(const std::string_view entry)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): read symlink request: " << entry << std::endl;
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
|
||||
char target[PATH_MAX];
|
||||
ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
@@ -255,13 +255,13 @@ std::string readSymlink(const std::string_view entry)
|
||||
}
|
||||
|
||||
target[len] = '\0';
|
||||
LOGN(HELPER, INFO) << __func__ << "(): \"" << entry << "\" symlink to \"" << target << "\"" << std::endl;
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" symlink to \"" << target << "\"" << std::endl;
|
||||
return target;
|
||||
}
|
||||
|
||||
size_t fileSize(const std::string_view file)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): get file size request: " << file << std::endl;
|
||||
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);
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Helper {
|
||||
|
||||
std::optional<std::string> sha256Of(const std::string_view path)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request. Getting full path (if input is link and exists)." << std::endl;
|
||||
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)) {
|
||||
@@ -43,13 +43,13 @@ std::optional<std::string> sha256Of(const std::string_view path)
|
||||
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" successfull." << std::endl;
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfull." << 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) << __func__ << "(): comparing sha256 signatures of input files." << std::endl;
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
|
||||
auto f1 = sha256Of(file1);
|
||||
auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
|
||||
@@ -65,13 +65,13 @@ void setLoggingState(int state)
|
||||
|
||||
bool runCommand(const std::string_view cmd)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): run command request: " << cmd << std::endl;
|
||||
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) << __func__ << "(): create confirm propt request. Creating." << std::endl;
|
||||
LOGN(HELPER, INFO) << "create confirm propt request. Creating." << std::endl;
|
||||
char p;
|
||||
|
||||
printf("%s [ y / n ]: ", message.data());
|
||||
@@ -122,7 +122,7 @@ std::string currentTime()
|
||||
|
||||
std::string runCommandWithOutput(const std::string_view cmd)
|
||||
{
|
||||
LOGN(HELPER, INFO) << __func__ << "(): run command and catch out request: " << cmd << std::endl;
|
||||
LOGN(HELPER, INFO) << "run command and catch out request: " << cmd << std::endl;
|
||||
|
||||
FILE* pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) {
|
||||
|
||||
Reference in New Issue
Block a user