pmt: ready for release

- Fix memory leaks
 - Writed functions and apply
 - Test pmt and verify stability
This commit is contained in:
2025-08-06 21:58:05 +03:00
parent 670f2bfad5
commit 6294482b39
29 changed files with 1033 additions and 502 deletions

View File

@@ -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);
}