pmt: reformat code
This commit is contained in:
312
srclib/libhelper/include/libhelper/lib.hpp
Executable file → Normal file
312
srclib/libhelper/include/libhelper/lib.hpp
Executable file → Normal file
@@ -18,19 +18,19 @@
|
||||
#define LIBHELPER_LIB_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#ifndef ONLY_HELPER_MACROS
|
||||
|
||||
enum LogLevels {
|
||||
INFO = static_cast<int>('I'),
|
||||
WARNING = static_cast<int>('W'),
|
||||
ERROR = static_cast<int>('E'),
|
||||
ABORT = static_cast<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;
|
||||
@@ -40,127 +40,131 @@ constexpr int YES = 1;
|
||||
constexpr int NO = 0;
|
||||
|
||||
namespace Helper {
|
||||
// Logging
|
||||
class Logger final {
|
||||
private:
|
||||
LogLevels _level;
|
||||
std::ostringstream _oss;
|
||||
const char *_funcname, *_logFile, *_program_name, *_file;
|
||||
int _line;
|
||||
// Logging
|
||||
class Logger final {
|
||||
private:
|
||||
LogLevels _level;
|
||||
std::ostringstream _oss;
|
||||
const char *_funcname, *_logFile, *_program_name, *_file;
|
||||
int _line;
|
||||
|
||||
public:
|
||||
Logger(LogLevels level, const char *func, const char *file, const char *name, const char *sfile, int line);
|
||||
public:
|
||||
Logger(LogLevels level, const char *func, const char *file, const char *name,
|
||||
const char *sfile, int line);
|
||||
|
||||
~Logger();
|
||||
~Logger();
|
||||
|
||||
template<typename T>
|
||||
Logger &operator<<(const T &msg) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
template <typename T> Logger &operator<<(const T &msg) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Logger &operator<<(std::ostream & (*msg)(std::ostream &));
|
||||
};
|
||||
Logger &operator<<(std::ostream &(*msg)(std::ostream &));
|
||||
};
|
||||
|
||||
// Throwable error class
|
||||
class Error final : public std::exception {
|
||||
private:
|
||||
std::string _message;
|
||||
// Throwable error class
|
||||
class Error final : public std::exception {
|
||||
private:
|
||||
std::string _message;
|
||||
|
||||
public:
|
||||
__attribute__((format(printf, 2, 3)))
|
||||
explicit Error(const char *format, ...);
|
||||
public:
|
||||
__attribute__((format(printf, 2, 3))) explicit Error(const char *format, ...);
|
||||
|
||||
[[nodiscard]] const char *what() const noexcept override;
|
||||
};
|
||||
[[nodiscard]] const char *what() const noexcept override;
|
||||
};
|
||||
|
||||
// Close file descriptors and delete allocated array memory
|
||||
class garbageCollector {
|
||||
private:
|
||||
std::vector<char*> _ptrs_c;
|
||||
std::vector<uint8_t*> _ptrs_u;
|
||||
std::vector<FILE*> _fps;
|
||||
std::vector<int> _fds;
|
||||
// Close file descriptors and delete allocated array memory
|
||||
class garbageCollector {
|
||||
private:
|
||||
std::vector<char *> _ptrs_c;
|
||||
std::vector<uint8_t *> _ptrs_u;
|
||||
std::vector<FILE *> _fps;
|
||||
std::vector<int> _fds;
|
||||
|
||||
public:
|
||||
~garbageCollector();
|
||||
public:
|
||||
~garbageCollector();
|
||||
|
||||
void delAfterProgress(char* &_ptr);
|
||||
void delAfterProgress(uint8_t* &_ptr);
|
||||
void delAfterProgress(FILE* &_fp);
|
||||
void closeAfterProgress(int _fd);
|
||||
};
|
||||
void delAfterProgress(char *&_ptr);
|
||||
void delAfterProgress(uint8_t *&_ptr);
|
||||
void delAfterProgress(FILE *&_fp);
|
||||
void closeAfterProgress(int _fd);
|
||||
};
|
||||
|
||||
namespace LoggingProperties {
|
||||
extern std::string_view FILE, NAME;
|
||||
extern bool PRINT, DISABLE;
|
||||
namespace LoggingProperties {
|
||||
extern std::string_view FILE, NAME;
|
||||
extern bool PRINT, DISABLE;
|
||||
|
||||
void set(std::string_view name, std::string_view file);
|
||||
void setProgramName(std::string_view name);
|
||||
void setLogFile(std::string_view file);
|
||||
void setPrinting(int state);
|
||||
void setLoggingState(int state); // Disable/enable logging
|
||||
void set(std::string_view name, std::string_view file);
|
||||
void setProgramName(std::string_view name);
|
||||
void setLogFile(std::string_view file);
|
||||
void setPrinting(int state);
|
||||
void setLoggingState(int state); // Disable/enable logging
|
||||
|
||||
void reset();
|
||||
} // namespace LoggingProperties
|
||||
void reset();
|
||||
} // namespace LoggingProperties
|
||||
|
||||
// Checkers
|
||||
bool hasSuperUser();
|
||||
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);
|
||||
// Checkers
|
||||
bool hasSuperUser();
|
||||
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(std::string_view file, std::string_view text);
|
||||
std::optional<std::string> readFile(std::string_view file);
|
||||
// File I/O
|
||||
bool writeFile(std::string_view file, std::string_view text);
|
||||
std::optional<std::string> readFile(std::string_view file);
|
||||
|
||||
// Creators
|
||||
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);
|
||||
// Creators
|
||||
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(std::string_view entry);
|
||||
bool eraseDirectoryRecursive(std::string_view directory);
|
||||
// Removers
|
||||
bool eraseEntry(std::string_view entry);
|
||||
bool eraseDirectoryRecursive(std::string_view directory);
|
||||
|
||||
// Getters
|
||||
size_t fileSize(std::string_view file);
|
||||
std::string readSymlink(std::string_view entry);
|
||||
// Getters
|
||||
size_t fileSize(std::string_view file);
|
||||
std::string readSymlink(std::string_view entry);
|
||||
|
||||
// SHA-256
|
||||
bool sha256Compare(std::string_view file1, std::string_view file2);
|
||||
std::optional<std::string> sha256Of(std::string_view path);
|
||||
// SHA-256
|
||||
bool sha256Compare(std::string_view file1, std::string_view file2);
|
||||
std::optional<std::string> sha256Of(std::string_view path);
|
||||
|
||||
// Utilities
|
||||
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(std::string_view cmd);
|
||||
std::string pathJoin(std::string base, std::string relative);
|
||||
std::string pathBasename(std::string_view entry);
|
||||
std::string pathDirname(std::string_view entry);
|
||||
// Utilities
|
||||
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(std::string_view cmd);
|
||||
std::string pathJoin(std::string base, std::string relative);
|
||||
std::string pathBasename(std::string_view entry);
|
||||
std::string pathDirname(std::string_view entry);
|
||||
|
||||
// Android
|
||||
std::string getProperty(std::string_view prop);
|
||||
bool reboot(std::string_view arg);
|
||||
// Android
|
||||
std::string getProperty(std::string_view prop);
|
||||
bool reboot(std::string_view arg);
|
||||
|
||||
// Library-specif
|
||||
std::string getLibVersion();
|
||||
// Library-specif
|
||||
std::string getLibVersion();
|
||||
|
||||
// Open input path with flags and add to integrity list. And return file descriptor
|
||||
[[nodiscard]] int openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, int flags, mode_t mode = 0000);
|
||||
[[nodiscard]] FILE* openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, const char* mode);
|
||||
// Open input path with flags and add to integrity list. And return file
|
||||
// descriptor
|
||||
[[nodiscard]] int openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector, int flags,
|
||||
mode_t mode = 0000);
|
||||
[[nodiscard]] FILE *openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector,
|
||||
const char *mode);
|
||||
} // namespace Helper
|
||||
|
||||
#endif // #ifndef ONLY_HELPER_MACROS
|
||||
@@ -171,62 +175,76 @@ namespace Helper {
|
||||
#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_KB(x) (x / 1024) // TO_KB(1024) = 1
|
||||
#define TO_MB(x) (TO_KB(x) / 1024) // TO_MB(2048) (2048 / 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"
|
||||
#define FAINT "\033[2m"
|
||||
#define ITALIC "\033[3m"
|
||||
#define UNDERLINE "\033[4m"
|
||||
#define BLINC "\033[5m"
|
||||
#define FAST_BLINC "\033[6m"
|
||||
#define STRIKE_THROUGHT "\033[9m"
|
||||
#define NO_UNDERLINE "\033[24m"
|
||||
#define NO_BLINC "\033[25m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define STYLE_RESET "\033[0m"
|
||||
#define BOLD "\033[1m"
|
||||
#define FAINT "\033[2m"
|
||||
#define ITALIC "\033[3m"
|
||||
#define UNDERLINE "\033[4m"
|
||||
#define BLINC "\033[5m"
|
||||
#define FAST_BLINC "\033[6m"
|
||||
#define STRIKE_THROUGHT "\033[9m"
|
||||
#define NO_UNDERLINE "\033[24m"
|
||||
#define NO_BLINC "\033[25m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
|
||||
#ifndef NO_C_TYPE_HANDLERS
|
||||
// ABORT(message), ex: ABORT("memory error!\n")
|
||||
#define ABORT(msg) \
|
||||
do { \
|
||||
fprintf(stderr, "%s%sCRITICAL ERROR%s: %s\nAborting...\n", BOLD, RED, STYLE_RESET, msg); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
#define ABORT(msg) \
|
||||
do { \
|
||||
fprintf(stderr, "%s%sCRITICAL ERROR%s: %s\nAborting...\n", BOLD, RED, \
|
||||
STYLE_RESET, msg); \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
// ERROR(message, exit), ex: ERROR("an error occured.\n", 1)
|
||||
#define ERROR(msg, code) \
|
||||
do { \
|
||||
fprintf(stderr, "%s%sERROR%s: %s", BOLD, RED, STYLE_RESET, msg); \
|
||||
exit(code); \
|
||||
} while (0)
|
||||
#define ERROR(msg, code) \
|
||||
do { \
|
||||
fprintf(stderr, "%s%sERROR%s: %s", BOLD, RED, STYLE_RESET, msg); \
|
||||
exit(code); \
|
||||
} while (0)
|
||||
|
||||
// WARNING(message), ex: WARNING("using default setting.\n")
|
||||
#define WARNING(msg) \
|
||||
fprintf(stderr, "%s%sWARNING%s: %s", BOLD, YELLOW, STYLE_RESET, msg);
|
||||
#define WARNING(msg) \
|
||||
fprintf(stderr, "%s%sWARNING%s: %s", BOLD, YELLOW, STYLE_RESET, msg);
|
||||
|
||||
// INFO(message), ex: INFO("operation ended.\n")
|
||||
#define INFO(msg) \
|
||||
fprintf(stdout, "%s%sINFO%s: %s", BOLD, GREEN, STYLE_RESET, msg);
|
||||
#define INFO(msg) \
|
||||
fprintf(stdout, "%s%sINFO%s: %s", BOLD, GREEN, STYLE_RESET, msg);
|
||||
#endif // #ifndef NO_C_TYPE_HANDLERS
|
||||
|
||||
#define LOG(level) Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), Helper::LoggingProperties::NAME.data(), __FILE__, __LINE__)
|
||||
#define LOGN(name, level) Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), name, __FILE__, __LINE__)
|
||||
#define LOGNF(name, file, level) Helper::Logger(level, file, name, __FILE__, __LINE__)
|
||||
#define LOG(level) \
|
||||
Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), \
|
||||
Helper::LoggingProperties::NAME.data(), __FILE__, __LINE__)
|
||||
#define LOGN(name, level) \
|
||||
Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), \
|
||||
name, __FILE__, __LINE__)
|
||||
#define LOGNF(name, file, level) \
|
||||
Helper::Logger(level, file, name, __FILE__, __LINE__)
|
||||
|
||||
#define LOG_IF(level, condition) \
|
||||
if (condition) Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), Helper::LoggingProperties::NAME.data(), __FILE__, __LINE__)
|
||||
#define LOGN_IF(name, level, condition) \
|
||||
if (condition) Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), name, __FILE__, __LINE__)
|
||||
#define LOGNF_IF(name, file, level, condition) \
|
||||
if (condition) Helper::Logger(level, __func__, file, name, __FILE__, __LINE__)
|
||||
#define LOG_IF(level, condition) \
|
||||
if (condition) \
|
||||
Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), \
|
||||
Helper::LoggingProperties::NAME.data(), __FILE__, __LINE__)
|
||||
#define LOGN_IF(name, level, condition) \
|
||||
if (condition) \
|
||||
Helper::Logger(level, __func__, Helper::LoggingProperties::FILE.data(), \
|
||||
name, __FILE__, __LINE__)
|
||||
#define LOGNF_IF(name, file, level, condition) \
|
||||
if (condition) Helper::Logger(level, __func__, file, name, __FILE__, __LINE__)
|
||||
|
||||
#define MKVERSION(name) \
|
||||
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)
|
||||
#define MKVERSION(name) \
|
||||
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
|
||||
|
||||
78
srclib/libhelper/src/Checkers.cpp
Executable file → Normal file
78
srclib/libhelper/src/Checkers.cpp
Executable file → Normal file
@@ -16,57 +16,55 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <private/android_filesystem_config.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
bool hasSuperUser() {
|
||||
return (getuid() == AID_ROOT);
|
||||
}
|
||||
bool hasSuperUser() { return (getuid() == AID_ROOT); }
|
||||
|
||||
bool isExists(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
return (stat(entry.data(), &st) == 0);
|
||||
}
|
||||
bool isExists(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
return (stat(entry.data(), &st) == 0);
|
||||
}
|
||||
|
||||
bool fileIsExists(const std::string_view file) {
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return S_ISREG(st.st_mode);
|
||||
}
|
||||
bool fileIsExists(const std::string_view file) {
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return S_ISREG(st.st_mode);
|
||||
}
|
||||
|
||||
bool directoryIsExists(const std::string_view directory) {
|
||||
struct stat st{};
|
||||
if (stat(directory.data(), &st) != 0) return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
bool directoryIsExists(const std::string_view directory) {
|
||||
struct stat st{};
|
||||
if (stat(directory.data(), &st) != 0) return false;
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
bool linkIsExists(const std::string_view entry) {
|
||||
return (isLink(entry) || isHardLink(entry));
|
||||
}
|
||||
bool linkIsExists(const std::string_view entry) {
|
||||
return (isLink(entry) || isHardLink(entry));
|
||||
}
|
||||
|
||||
bool isLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
bool isLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
|
||||
bool isSymbolicLink(const std::string_view entry) {
|
||||
return isLink(entry);
|
||||
}
|
||||
bool isSymbolicLink(const std::string_view entry) { return isLink(entry); }
|
||||
|
||||
bool isHardLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return (st.st_nlink >= 2);
|
||||
}
|
||||
bool isHardLink(const std::string_view entry) {
|
||||
struct stat st{};
|
||||
if (lstat(entry.data(), &st) != 0) return false;
|
||||
return (st.st_nlink >= 2);
|
||||
}
|
||||
|
||||
bool areLinked(const std::string_view entry1, const std::string_view entry2) {
|
||||
const std::string st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1) : std::string(entry1.data());
|
||||
const std::string st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2) : std::string(entry2.data());
|
||||
bool areLinked(const std::string_view entry1, const std::string_view entry2) {
|
||||
const std::string st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1)
|
||||
: std::string(entry1.data());
|
||||
const std::string st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2)
|
||||
: std::string(entry2.data());
|
||||
|
||||
return (st1 == st2);
|
||||
}
|
||||
return (st1 == st2);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
143
srclib/libhelper/src/Classes.cpp
Executable file → Normal file
143
srclib/libhelper/src/Classes.cpp
Executable file → Normal file
@@ -14,86 +14,97 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <cerrno>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <cstdarg>
|
||||
#include <cerrno>
|
||||
#include <exception>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
Error::Error(const char *format, ...) {
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
Error::Error(const char *format, ...) {
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
|
||||
const char *Error::what() const noexcept {
|
||||
return _message.data();
|
||||
}
|
||||
const char *Error::what() const noexcept { return _message.data(); }
|
||||
|
||||
Logger::Logger(const LogLevels level, const char *func, const char *file, const char *name, const char *sfile,
|
||||
const int line) : _level(level), _funcname(func), _logFile(file), _program_name(name), _file(sfile),
|
||||
_line(line) {
|
||||
}
|
||||
Logger::Logger(const LogLevels level, const char *func, const char *file,
|
||||
const char *name, const char *sfile, const int line)
|
||||
: _level(level), _funcname(func), _logFile(file), _program_name(name),
|
||||
_file(sfile), _line(line) {}
|
||||
|
||||
Logger::~Logger() {
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level),
|
||||
_program_name,
|
||||
basename(const_cast<char *>(_file)),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
_funcname,
|
||||
_oss.str().data());
|
||||
Logger::~Logger() {
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level), _program_name,
|
||||
basename(const_cast<char *>(_file)), _line, currentDate().data(),
|
||||
currentTime().data(), _funcname, _oss.str().data());
|
||||
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd = open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS); fd != -1) close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" New logging file: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
}
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd =
|
||||
open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS);
|
||||
fd != -1)
|
||||
close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": "
|
||||
<< strerror(errno)
|
||||
<< " New logging file: last_logs.log (this file)."
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (FILE *fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot write logs to log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" Logging file setting up as: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
if (FILE *fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO)
|
||||
<< "Cannot write logs to log file: " << _logFile << ": "
|
||||
<< strerror(errno)
|
||||
<< " Logging file setting up as: last_logs.log (this file)."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
|
||||
Logger &Logger::operator<<(std::ostream & (*msg)(std::ostream &)) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
Logger &Logger::operator<<(std::ostream &(*msg)(std::ostream &)) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
garbageCollector::~garbageCollector() {
|
||||
for (const auto& ptr : _ptrs_c) delete[] ptr;
|
||||
for (const auto& ptr : _ptrs_u) delete[] ptr;
|
||||
for (const auto& fd : _fds) close(fd);
|
||||
for (const auto& fp : _fps) fclose(fp);
|
||||
}
|
||||
garbageCollector::~garbageCollector() {
|
||||
for (const auto &ptr : _ptrs_c)
|
||||
delete[] ptr;
|
||||
for (const auto &ptr : _ptrs_u)
|
||||
delete[] ptr;
|
||||
for (const auto &fd : _fds)
|
||||
close(fd);
|
||||
for (const auto &fp : _fps)
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void garbageCollector::delAfterProgress(char* &_ptr) { _ptrs_c.push_back(_ptr); }
|
||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) { _ptrs_u.push_back(_ptr); }
|
||||
void garbageCollector::closeAfterProgress(const int _fd) { _fds.push_back(_fd); }
|
||||
void garbageCollector::delAfterProgress(FILE* &_fp) { _fps.push_back(_fp); }
|
||||
void garbageCollector::delAfterProgress(char *&_ptr) {
|
||||
_ptrs_c.push_back(_ptr);
|
||||
}
|
||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) {
|
||||
_ptrs_u.push_back(_ptr);
|
||||
}
|
||||
void garbageCollector::closeAfterProgress(const int _fd) {
|
||||
_fds.push_back(_fd);
|
||||
}
|
||||
void garbageCollector::delAfterProgress(FILE *&_fp) { _fps.push_back(_fp); }
|
||||
} // namespace Helper
|
||||
|
||||
286
srclib/libhelper/src/FileUtil.cpp
Executable file → Normal file
286
srclib/libhelper/src/FileUtil.cpp
Executable file → Normal file
@@ -14,187 +14,199 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Helper {
|
||||
bool writeFile(const std::string_view file, const std::string_view text) {
|
||||
LOGN(HELPER, INFO) << "write \"" << text << "\" to " << file << "requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
bool writeFile(const std::string_view file, const std::string_view text) {
|
||||
LOGN(HELPER, INFO) << "write \"" << text << "\" to " << file << "requested."
|
||||
<< std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "a");
|
||||
if (fp == nullptr) return false;
|
||||
fprintf(fp, "%s", text.data());
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "a");
|
||||
if (fp == nullptr) return false;
|
||||
fprintf(fp, "%s", text.data());
|
||||
|
||||
LOGN(HELPER, INFO) << "write " << file << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "write " << file << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<std::string> readFile(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "read " << file << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
std::optional<std::string> readFile(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "read " << file << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "r");
|
||||
if (fp == nullptr) return std::nullopt;
|
||||
FILE *fp = openAndAddToCloseList(file, collector, "r");
|
||||
if (fp == nullptr) return std::nullopt;
|
||||
|
||||
char buffer[1024];
|
||||
std::string str;
|
||||
while (fgets(buffer, sizeof(buffer), fp)) str += buffer;
|
||||
char buffer[1024];
|
||||
std::string str;
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
str += buffer;
|
||||
|
||||
LOGN(HELPER, INFO) << "read " << file << " successfully, read text: \"" << str << "\"" << std::endl;
|
||||
return str;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "read " << file << " successfully, read text: \"" << str
|
||||
<< "\"" << std::endl;
|
||||
return str;
|
||||
}
|
||||
|
||||
bool copyFile(const std::string_view file, const std::string_view dest) {
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested." << std::endl;
|
||||
garbageCollector collector;
|
||||
bool copyFile(const std::string_view file, const std::string_view dest) {
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " requested."
|
||||
<< std::endl;
|
||||
garbageCollector collector;
|
||||
|
||||
const int src_fd = openAndAddToCloseList(file.data(), collector, O_RDONLY);
|
||||
if (src_fd == -1) return false;
|
||||
const int src_fd = openAndAddToCloseList(file.data(), collector, O_RDONLY);
|
||||
if (src_fd == -1) return false;
|
||||
|
||||
const int dst_fd = openAndAddToCloseList(dest.data(), collector, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_FILE_PERMS);
|
||||
if (dst_fd == -1) return false;
|
||||
const int dst_fd = openAndAddToCloseList(
|
||||
dest.data(), collector, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_FILE_PERMS);
|
||||
if (dst_fd == -1) return false;
|
||||
|
||||
char buffer[512];
|
||||
ssize_t br;
|
||||
while ((br = read(src_fd, buffer, 512)) > 0) {
|
||||
if (const ssize_t bw = write(dst_fd, buffer, br); bw != br) return false;
|
||||
}
|
||||
char buffer[512];
|
||||
ssize_t br;
|
||||
while ((br = read(src_fd, buffer, 512)) > 0) {
|
||||
if (const ssize_t bw = write(dst_fd, buffer, br); bw != br) return false;
|
||||
}
|
||||
|
||||
if (br == -1) return false;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
if (br == -1) return false;
|
||||
LOGN(HELPER, INFO) << "copy " << file << " to " << dest << " successfully."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool makeDirectory(const std::string_view path) {
|
||||
if (isExists(path)) return false;
|
||||
LOGN(HELPER, INFO) << "trying making directory: " << path << std::endl;
|
||||
return (mkdir(path.data(), DEFAULT_DIR_PERMS) == 0);
|
||||
}
|
||||
bool makeDirectory(const std::string_view path) {
|
||||
if (isExists(path)) return false;
|
||||
LOGN(HELPER, INFO) << "trying making directory: " << path << std::endl;
|
||||
return (mkdir(path.data(), DEFAULT_DIR_PERMS) == 0);
|
||||
}
|
||||
|
||||
bool makeRecursiveDirectory(const std::string_view paths) {
|
||||
LOGN(HELPER, INFO) << "make recursive directory requested: " << paths << std::endl;
|
||||
bool makeRecursiveDirectory(const std::string_view paths) {
|
||||
LOGN(HELPER, INFO) << "make recursive directory requested: " << paths
|
||||
<< std::endl;
|
||||
|
||||
char tmp[PATH_MAX];
|
||||
char tmp[PATH_MAX];
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s", paths.data());
|
||||
if (const size_t len = strlen(tmp); tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
snprintf(tmp, sizeof(tmp), "%s", paths.data());
|
||||
if (const size_t len = strlen(tmp); tmp[len - 1] == '/') tmp[len - 1] = '\0';
|
||||
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0
|
||||
&& errno != EEXIST)
|
||||
return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
if (access(tmp, F_OK) != 0) {
|
||||
if (mkdir(tmp, DEFAULT_DIR_PERMS) != 0 && errno != EEXIST) return false;
|
||||
}
|
||||
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "" << paths << " successfully created." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool createFile(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
bool createFile(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "create file request: " << path << std::endl;
|
||||
|
||||
if (isExists(path)) return false;
|
||||
if (isExists(path)) return false;
|
||||
|
||||
const int fd = open(path.data(), O_RDONLY | O_CREAT, DEFAULT_FILE_PERMS);
|
||||
if (fd == -1) return false;
|
||||
const int fd = open(path.data(), O_RDONLY | O_CREAT, DEFAULT_FILE_PERMS);
|
||||
if (fd == -1) return false;
|
||||
|
||||
close(fd);
|
||||
LOGN(HELPER, INFO) << "create file \"" << path << "\" successfull." << std::endl;
|
||||
return true;
|
||||
}
|
||||
close(fd);
|
||||
LOGN(HELPER, INFO) << "create file \"" << path << "\" successfull."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool createSymlink(const std::string_view entry1, const std::string_view entry2) {
|
||||
LOGN(HELPER, INFO) << "symlink \"" << entry1 << "\" to \"" << entry2 << "\" requested." << std::endl;
|
||||
if (const int ret = symlink(entry1.data(), entry2.data()); ret != 0) return false;
|
||||
bool createSymlink(const std::string_view entry1,
|
||||
const std::string_view entry2) {
|
||||
LOGN(HELPER, INFO) << "symlink \"" << entry1 << "\" to \"" << entry2
|
||||
<< "\" requested." << std::endl;
|
||||
if (const int ret = symlink(entry1.data(), entry2.data()); ret != 0)
|
||||
return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << entry1 << "\" symlinked to \"" << entry2 << "\" successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << entry1 << "\" symlinked to \"" << entry2
|
||||
<< "\" successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eraseEntry(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "erase \"" << entry << "\" requested." << std::endl;
|
||||
if (const int ret = remove(entry.data()); ret != 0) return false;
|
||||
bool eraseEntry(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "erase \"" << entry << "\" requested." << std::endl;
|
||||
if (const int ret = remove(entry.data()); ret != 0) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" erased successfully." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eraseDirectoryRecursive(const std::string_view directory) {
|
||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||
struct stat buf{};
|
||||
dirent *entry;
|
||||
bool eraseDirectoryRecursive(const std::string_view directory) {
|
||||
LOGN(HELPER, INFO) << "erase recursive requested: " << directory << std::endl;
|
||||
struct stat buf{};
|
||||
dirent *entry;
|
||||
|
||||
DIR *dir = opendir(directory.data());
|
||||
if (dir == nullptr) return false;
|
||||
DIR *dir = opendir(directory.data());
|
||||
if (dir == nullptr) return false;
|
||||
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
char fullpath[PATH_MAX];
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
char fullpath[PATH_MAX];
|
||||
|
||||
if (strcmp(entry->d_name, ".") == 0
|
||||
|| strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(), entry->d_name);
|
||||
snprintf(fullpath, sizeof(fullpath), "%s/%s", directory.data(),
|
||||
entry->d_name);
|
||||
|
||||
if (lstat(fullpath, &buf) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
if (lstat(fullpath, &buf) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
if (!eraseDirectoryRecursive(fullpath)) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (unlink(fullpath) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
if (!eraseDirectoryRecursive(fullpath)) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (unlink(fullpath) == -1) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
if (rmdir(directory.data()) == -1) return false;
|
||||
closedir(dir);
|
||||
if (rmdir(directory.data()) == -1) return false;
|
||||
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased." << std::endl;
|
||||
return true;
|
||||
}
|
||||
LOGN(HELPER, INFO) << "\"" << directory << "\" successfully erased."
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string readSymlink(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
std::string readSymlink(const std::string_view entry) {
|
||||
LOGN(HELPER, INFO) << "read symlink request: " << entry << std::endl;
|
||||
|
||||
char target[PATH_MAX];
|
||||
const ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
if (len == -1) return entry.data();
|
||||
char target[PATH_MAX];
|
||||
const ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||
if (len == -1) return entry.data();
|
||||
|
||||
target[len] = '\0';
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" symlink to \"" << target << "\"" << std::endl;
|
||||
return target;
|
||||
}
|
||||
target[len] = '\0';
|
||||
LOGN(HELPER, INFO) << "\"" << entry << "\" symlink to \"" << target << "\""
|
||||
<< std::endl;
|
||||
return target;
|
||||
}
|
||||
|
||||
size_t fileSize(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "get file size request: " << file << std::endl;
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return static_cast<size_t>(st.st_size);
|
||||
}
|
||||
size_t fileSize(const std::string_view file) {
|
||||
LOGN(HELPER, INFO) << "get file size request: " << file << std::endl;
|
||||
struct stat st{};
|
||||
if (stat(file.data(), &st) != 0) return false;
|
||||
return static_cast<size_t>(st.st_size);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
51
srclib/libhelper/src/Sha256.cpp
Executable file → Normal file
@@ -15,36 +15,43 @@
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <optional>
|
||||
#include <picosha2.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Helper {
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path <<
|
||||
"\" request. Getting full path (if input is link and exists)." << std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
std::optional<std::string> sha256Of(const std::string_view path) {
|
||||
LOGN(HELPER, INFO)
|
||||
<< "get sha256 of \"" << path
|
||||
<< "\" request. Getting full path (if input is link and exists)."
|
||||
<< std::endl;
|
||||
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||
|
||||
if (!fileIsExists(fp)) throw Error("Is not exists or not file: %s", fp.data());
|
||||
if (!fileIsExists(fp))
|
||||
throw Error("Is not exists or not file: %s", fp.data());
|
||||
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file) throw Error("Cannot open file: %s", fp.data());
|
||||
if (const std::ifstream file(fp, std::ios::binary); !file)
|
||||
throw Error("Cannot open file: %s", fp.data());
|
||||
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully." << std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
||||
picosha2::hash256(fp, hash.begin(), hash.end());
|
||||
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully."
|
||||
<< std::endl;
|
||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||
}
|
||||
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2) << "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
|
||||
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files."
|
||||
<< std::endl;
|
||||
const auto f1 = sha256Of(file1);
|
||||
const auto f2 = sha256Of(file2);
|
||||
if (f1->empty() || f2->empty()) return false;
|
||||
LOGN_IF(HELPER, INFO, *f1 == *f2)
|
||||
<< "(): input files is contains same sha256 signature." << std::endl;
|
||||
return (*f1 == *f2);
|
||||
}
|
||||
} // namespace Helper
|
||||
|
||||
308
srclib/libhelper/src/Utilities.cpp
Executable file → Normal file
308
srclib/libhelper/src/Utilities.cpp
Executable file → Normal file
@@ -14,201 +14,207 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <fcntl.h>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <iostream>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <ctime>
|
||||
#include <fcntl.h>
|
||||
#include <libgen.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/_system_properties.h>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// From system/core/libcutils/android_reboot.cpp android16-s2-release
|
||||
int android_reboot(const unsigned cmd, int /*flags*/, const char* arg) {
|
||||
int ret;
|
||||
const char* restart_cmd = nullptr;
|
||||
char* prop_value;
|
||||
int android_reboot(const unsigned cmd, int /*flags*/, const char *arg) {
|
||||
int ret;
|
||||
const char *restart_cmd = nullptr;
|
||||
char *prop_value;
|
||||
|
||||
switch (cmd) {
|
||||
case ANDROID_RB_RESTART: // deprecated
|
||||
case ANDROID_RB_RESTART2:
|
||||
restart_cmd = "reboot";
|
||||
break;
|
||||
case ANDROID_RB_POWEROFF:
|
||||
restart_cmd = "shutdown";
|
||||
break;
|
||||
case ANDROID_RB_THERMOFF:
|
||||
restart_cmd = "shutdown,thermal";
|
||||
break;
|
||||
}
|
||||
switch (cmd) {
|
||||
case ANDROID_RB_RESTART: // deprecated
|
||||
case ANDROID_RB_RESTART2:
|
||||
restart_cmd = "reboot";
|
||||
break;
|
||||
case ANDROID_RB_POWEROFF:
|
||||
restart_cmd = "shutdown";
|
||||
break;
|
||||
case ANDROID_RB_THERMOFF:
|
||||
restart_cmd = "shutdown,thermal";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!restart_cmd) return -1;
|
||||
if (arg && arg[0]) ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
|
||||
else ret = asprintf(&prop_value, "%s", restart_cmd);
|
||||
if (!restart_cmd) return -1;
|
||||
if (arg && arg[0]) ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
|
||||
else ret = asprintf(&prop_value, "%s", restart_cmd);
|
||||
|
||||
if (ret < 0) return -1;
|
||||
ret = __system_property_set(ANDROID_RB_PROPERTY, prop_value);
|
||||
free(prop_value);
|
||||
return ret;
|
||||
if (ret < 0) return -1;
|
||||
ret = __system_property_set(ANDROID_RB_PROPERTY, prop_value);
|
||||
free(prop_value);
|
||||
return ret;
|
||||
}
|
||||
|
||||
namespace Helper {
|
||||
namespace LoggingProperties {
|
||||
std::string_view FILE = "last_logs.log", NAME = "main";
|
||||
bool PRINT = NO, DISABLE = NO;
|
||||
namespace LoggingProperties {
|
||||
std::string_view FILE = "last_logs.log", NAME = "main";
|
||||
bool PRINT = NO, DISABLE = NO;
|
||||
|
||||
void reset() {
|
||||
FILE = "last_logs.log";
|
||||
NAME = "main";
|
||||
PRINT = NO;
|
||||
}
|
||||
void reset() {
|
||||
FILE = "last_logs.log";
|
||||
NAME = "main";
|
||||
PRINT = NO;
|
||||
}
|
||||
|
||||
void set(std::string_view file, std::string_view name) {
|
||||
if (file.data() != nullptr) FILE = file;
|
||||
if (name.data() != nullptr) NAME = name;
|
||||
}
|
||||
void set(std::string_view file, std::string_view name) {
|
||||
if (file.data() != nullptr) FILE = file;
|
||||
if (name.data() != nullptr) NAME = name;
|
||||
}
|
||||
|
||||
void setProgramName(const std::string_view name) { NAME = name; }
|
||||
void setLogFile(const std::string_view file) { FILE = file; }
|
||||
void setProgramName(const std::string_view name) { NAME = name; }
|
||||
void setLogFile(const std::string_view file) { FILE = file; }
|
||||
|
||||
void setPrinting(const int state) {
|
||||
if (state == 1 || state == 0) PRINT = state;
|
||||
else PRINT = NO;
|
||||
}
|
||||
void setPrinting(const int state) {
|
||||
if (state == 1 || state == 0) PRINT = state;
|
||||
else PRINT = NO;
|
||||
}
|
||||
|
||||
void setLoggingState(const int state) {
|
||||
if (state == 1 || state == 0) DISABLE = state;
|
||||
else DISABLE = NO;
|
||||
}
|
||||
} // namespace LoggingProperties
|
||||
void setLoggingState(const int state) {
|
||||
if (state == 1 || state == 0) DISABLE = state;
|
||||
else DISABLE = NO;
|
||||
}
|
||||
} // namespace LoggingProperties
|
||||
|
||||
bool runCommand(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command request: " << cmd << std::endl;
|
||||
return (system(cmd.data()) == 0) ? true : false;
|
||||
}
|
||||
bool runCommand(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command request: " << cmd << std::endl;
|
||||
return (system(cmd.data()) == 0) ? true : false;
|
||||
}
|
||||
|
||||
bool confirmPropt(const std::string_view message) {
|
||||
LOGN(HELPER, INFO) << "create confirm propt request. Creating." << std::endl;
|
||||
char p;
|
||||
bool confirmPropt(const std::string_view message) {
|
||||
LOGN(HELPER, INFO) << "create confirm propt request. Creating." << std::endl;
|
||||
char p;
|
||||
|
||||
printf("%s [ y / n ]: ", message.data());
|
||||
std::cin >> p;
|
||||
printf("%s [ y / n ]: ", message.data());
|
||||
std::cin >> p;
|
||||
|
||||
if (p == 'y' || p == 'Y') return true;
|
||||
if (p == 'n' || p == 'N') return false;
|
||||
if (p == 'y' || p == 'Y') return true;
|
||||
if (p == 'n' || p == 'N') return false;
|
||||
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
printf("Unexpected answer: '%c'. Try again.\n", p);
|
||||
return confirmPropt(message);
|
||||
}
|
||||
|
||||
std::string currentWorkingDirectory() {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return {};
|
||||
return cwd;
|
||||
}
|
||||
std::string currentWorkingDirectory() {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof(cwd)) == nullptr) return {};
|
||||
return cwd;
|
||||
}
|
||||
|
||||
std::string currentDate() {
|
||||
const time_t t = time(nullptr);
|
||||
std::string currentDate() {
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_mday) + "/" +
|
||||
std::to_string(date->tm_mon + 1) + "/" +
|
||||
std::to_string(date->tm_year + 1900));
|
||||
return "--/--/----";
|
||||
}
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(std::to_string(date->tm_mday) + "/" +
|
||||
std::to_string(date->tm_mon + 1) + "/" +
|
||||
std::to_string(date->tm_year + 1900));
|
||||
return "--/--/----";
|
||||
}
|
||||
|
||||
std::string currentTime() {
|
||||
const time_t t = time(nullptr);
|
||||
std::string currentTime() {
|
||||
const time_t t = time(nullptr);
|
||||
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(
|
||||
std::to_string(date->tm_hour) + ":" +
|
||||
std::to_string(date->tm_min) + ":" +
|
||||
std::to_string(date->tm_sec));
|
||||
return "--:--:--";
|
||||
}
|
||||
if (const tm *date = localtime(&t))
|
||||
return std::string(std::to_string(date->tm_hour) + ":" +
|
||||
std::to_string(date->tm_min) + ":" +
|
||||
std::to_string(date->tm_sec));
|
||||
return "--:--:--";
|
||||
}
|
||||
|
||||
std::string runCommandWithOutput(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command and catch out request: " << cmd << std::endl;
|
||||
std::string runCommandWithOutput(const std::string_view cmd) {
|
||||
LOGN(HELPER, INFO) << "run command and catch out request: " << cmd
|
||||
<< std::endl;
|
||||
|
||||
FILE *pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) return {};
|
||||
FILE *pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) return {};
|
||||
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||
|
||||
std::string output;
|
||||
char buffer[1024];
|
||||
std::string output;
|
||||
char buffer[1024];
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr) output += buffer;
|
||||
while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr)
|
||||
output += buffer;
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string pathJoin(std::string base, std::string relative) {
|
||||
if (base.back() != '/') base += '/';
|
||||
if (relative[0] == '/') relative.erase(0, 1);
|
||||
base += relative;
|
||||
return base;
|
||||
}
|
||||
std::string pathJoin(std::string base, std::string relative) {
|
||||
if (base.back() != '/') base += '/';
|
||||
if (relative[0] == '/') relative.erase(0, 1);
|
||||
base += relative;
|
||||
return base;
|
||||
}
|
||||
|
||||
std::string pathBasename(const std::string_view entry) {
|
||||
char *base = basename(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
std::string pathBasename(const std::string_view entry) {
|
||||
char *base = basename(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
|
||||
std::string pathDirname(const std::string_view entry) {
|
||||
char *base = dirname(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
std::string pathDirname(const std::string_view entry) {
|
||||
char *base = dirname(const_cast<char *>(entry.data()));
|
||||
return (base == nullptr) ? std::string() : std::string(base);
|
||||
}
|
||||
|
||||
bool changeMode(const std::string_view file, const mode_t mode) {
|
||||
LOGN(HELPER, INFO) << "change mode request: " << file << ". As mode: " << mode << std::endl;
|
||||
return chmod(file.data(), mode) == 0;
|
||||
}
|
||||
bool changeMode(const std::string_view file, const mode_t mode) {
|
||||
LOGN(HELPER, INFO) << "change mode request: " << file << ". As mode: " << mode
|
||||
<< std::endl;
|
||||
return chmod(file.data(), mode) == 0;
|
||||
}
|
||||
|
||||
bool changeOwner(const std::string_view file, const uid_t uid, const gid_t gid) {
|
||||
LOGN(HELPER, INFO) << "change owner request: " << file << ". As owner:group: " << uid << ":" << gid << std::endl;
|
||||
return chown(file.data(), uid, gid) == 0;
|
||||
}
|
||||
bool changeOwner(const std::string_view file, const uid_t uid,
|
||||
const gid_t gid) {
|
||||
LOGN(HELPER, INFO) << "change owner request: " << file
|
||||
<< ". As owner:group: " << uid << ":" << gid << std::endl;
|
||||
return chown(file.data(), uid, gid) == 0;
|
||||
}
|
||||
|
||||
int openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, const int flags, const mode_t mode) {
|
||||
const int fd = mode == 0 ? open(path.data(), flags) : open(path.data(), flags, mode);
|
||||
collector.closeAfterProgress(fd);
|
||||
return fd;
|
||||
}
|
||||
int openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector, const int flags,
|
||||
const mode_t mode) {
|
||||
const int fd =
|
||||
mode == 0 ? open(path.data(), flags) : open(path.data(), flags, mode);
|
||||
collector.closeAfterProgress(fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
FILE* openAndAddToCloseList(const std::string_view& path, garbageCollector &collector, const char* mode) {
|
||||
FILE *fp = fopen(path.data(), mode);
|
||||
collector.delAfterProgress(fp);
|
||||
return fp;
|
||||
}
|
||||
FILE *openAndAddToCloseList(const std::string_view &path,
|
||||
garbageCollector &collector, const char *mode) {
|
||||
FILE *fp = fopen(path.data(), mode);
|
||||
collector.delAfterProgress(fp);
|
||||
return fp;
|
||||
}
|
||||
|
||||
std::string getProperty(const std::string_view prop) {
|
||||
char val[PROP_VALUE_MAX];
|
||||
const int x = __system_property_get(prop.data(), val);
|
||||
return x > 0 ? val : "ERROR";
|
||||
}
|
||||
std::string getProperty(const std::string_view prop) {
|
||||
char val[PROP_VALUE_MAX];
|
||||
const int x = __system_property_get(prop.data(), val);
|
||||
return x > 0 ? val : "ERROR";
|
||||
}
|
||||
|
||||
bool reboot(const std::string_view arg) {
|
||||
LOGN(HELPER, INFO) << "reboot request sent!!!" << std::endl;
|
||||
bool reboot(const std::string_view arg) {
|
||||
LOGN(HELPER, INFO) << "reboot request sent!!!" << std::endl;
|
||||
|
||||
unsigned cmd = ANDROID_RB_RESTART2;
|
||||
if (const std::string prop = getProperty("ro.build.version.sdk"); prop != "ERROR") {
|
||||
if (std::stoi(prop) < 26) cmd = ANDROID_RB_RESTART;
|
||||
}
|
||||
unsigned cmd = ANDROID_RB_RESTART2;
|
||||
if (const std::string prop = getProperty("ro.build.version.sdk");
|
||||
prop != "ERROR") {
|
||||
if (std::stoi(prop) < 26) cmd = ANDROID_RB_RESTART;
|
||||
}
|
||||
|
||||
return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1;
|
||||
}
|
||||
return android_reboot(cmd, 0, arg.empty() ? nullptr : arg.data()) != -1;
|
||||
}
|
||||
|
||||
std::string getLibVersion() {
|
||||
MKVERSION("libhelper");
|
||||
}
|
||||
std::string getLibVersion() { MKVERSION("libhelper"); }
|
||||
} // namespace Helper
|
||||
|
||||
163
srclib/libhelper/tests/test.cpp
Executable file → Normal file
163
srclib/libhelper/tests/test.cpp
Executable file → Normal file
@@ -19,85 +19,116 @@
|
||||
#include <iostream>
|
||||
#include <libhelper/lib.hpp>
|
||||
|
||||
char* TEST_DIR = nullptr;
|
||||
char *TEST_DIR = nullptr;
|
||||
|
||||
std::string test_path(const char* file)
|
||||
{
|
||||
std::string end = std::string(TEST_DIR) + "/" + file;
|
||||
return end;
|
||||
std::string test_path(const char *file) {
|
||||
std::string end = std::string(TEST_DIR) + "/" + file;
|
||||
return end;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) return 2;
|
||||
else TEST_DIR = argv[1];
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) return 2;
|
||||
else TEST_DIR = argv[1];
|
||||
|
||||
try {
|
||||
std::cout << "Has super user?; " << std::boolalpha << Helper::hasSuperUser() << std::endl;
|
||||
std::cout << "file.txt is exists?; " << std::boolalpha << Helper::isExists(test_path("file.txt")) << std::endl;
|
||||
std::cout << "'file.txt' file is exists?; " << std::boolalpha << Helper::fileIsExists(test_path("file")) << std::endl;
|
||||
std::cout << "'dir' directory is exists?; " << std::boolalpha << Helper::directoryIsExists(test_path("dir")) << std::endl;
|
||||
std::cout << "'linkdir' is link?; " << std::boolalpha << Helper::isLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is symlink?; " << std::boolalpha << Helper::isSymbolicLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is hardlink?; " << std::boolalpha << Helper::isHardLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is symlink to 'dir'?; " << std::boolalpha << Helper::areLinked(test_path("linkdir"), test_path("dir")) << std::endl;
|
||||
try {
|
||||
std::cout << "Has super user?; " << std::boolalpha << Helper::hasSuperUser()
|
||||
<< std::endl;
|
||||
std::cout << "file.txt is exists?; " << std::boolalpha
|
||||
<< Helper::isExists(test_path("file.txt")) << std::endl;
|
||||
std::cout << "'file.txt' file is exists?; " << std::boolalpha
|
||||
<< Helper::fileIsExists(test_path("file")) << std::endl;
|
||||
std::cout << "'dir' directory is exists?; " << std::boolalpha
|
||||
<< Helper::directoryIsExists(test_path("dir")) << std::endl;
|
||||
std::cout << "'linkdir' is link?; " << std::boolalpha
|
||||
<< Helper::isLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is symlink?; " << std::boolalpha
|
||||
<< Helper::isSymbolicLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is hardlink?; " << std::boolalpha
|
||||
<< Helper::isHardLink(test_path("linkdir")) << std::endl;
|
||||
std::cout << "'linkdir' is symlink to 'dir'?; " << std::boolalpha
|
||||
<< Helper::areLinked(test_path("linkdir"), test_path("dir"))
|
||||
<< std::endl;
|
||||
|
||||
if (!Helper::writeFile("file.txt", "hello world"))
|
||||
throw Helper::Error("Cannor write \"hello world\" in 'file.txt'");
|
||||
else
|
||||
std::cout << "file.txt writed." << std::endl;
|
||||
if (!Helper::writeFile("file.txt", "hello world"))
|
||||
throw Helper::Error("Cannor write \"hello world\" in 'file.txt'");
|
||||
else std::cout << "file.txt writed." << std::endl;
|
||||
|
||||
auto content = Helper::readFile("file.txt");
|
||||
if (!content)
|
||||
throw Helper::Error("Cannot read 'file.txt'");
|
||||
else
|
||||
std::cout << "'file.txt': " << *content << std::endl;
|
||||
auto content = Helper::readFile("file.txt");
|
||||
if (!content) throw Helper::Error("Cannot read 'file.txt'");
|
||||
else std::cout << "'file.txt': " << *content << std::endl;
|
||||
|
||||
std::cout << "Making directory 'dir2': " << std::boolalpha << Helper::makeDirectory(test_path("dir2")) << std::endl;
|
||||
std::cout << "Making recursive directories 'dir3/x/y': " << std::boolalpha << Helper::makeRecursiveDirectory(test_path("dir3/x/y")) << std::endl;
|
||||
std::cout << "Create 'file2.txt': " << std::boolalpha << Helper::createFile(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Create symlink 'file2.txt' to 'file2lnk.txt': " << std::boolalpha << Helper::createSymlink(test_path("file2.txt"), test_path("file2lnk.txt")) << std::endl;
|
||||
std::cout << "Size of 'file2.txt': " << Helper::fileSize(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Erasing 'file.txt': " << std::boolalpha << Helper::eraseEntry(test_path("file.txt")) << std::endl;
|
||||
std::cout << "Erasing 'dir2': " << std::boolalpha << Helper::eraseEntry(test_path("dir2")) << std::endl;
|
||||
std::cout << "Read link of 'file2lnk.txt': " << Helper::readSymlink(test_path("file2lnk.txt")) << std::endl;
|
||||
std::cout << "Making directory 'dir2': " << std::boolalpha
|
||||
<< Helper::makeDirectory(test_path("dir2")) << std::endl;
|
||||
std::cout << "Making recursive directories 'dir3/x/y': " << std::boolalpha
|
||||
<< Helper::makeRecursiveDirectory(test_path("dir3/x/y"))
|
||||
<< std::endl;
|
||||
std::cout << "Create 'file2.txt': " << std::boolalpha
|
||||
<< Helper::createFile(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Create symlink 'file2.txt' to 'file2lnk.txt': "
|
||||
<< std::boolalpha
|
||||
<< Helper::createSymlink(test_path("file2.txt"),
|
||||
test_path("file2lnk.txt"))
|
||||
<< std::endl;
|
||||
std::cout << "Size of 'file2.txt': "
|
||||
<< Helper::fileSize(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Erasing 'file.txt': " << std::boolalpha
|
||||
<< Helper::eraseEntry(test_path("file.txt")) << std::endl;
|
||||
std::cout << "Erasing 'dir2': " << std::boolalpha
|
||||
<< Helper::eraseEntry(test_path("dir2")) << std::endl;
|
||||
std::cout << "Read link of 'file2lnk.txt': "
|
||||
<< Helper::readSymlink(test_path("file2lnk.txt")) << std::endl;
|
||||
|
||||
auto sha256 = Helper::sha256Of(test_path("file2.txt"));
|
||||
if (!sha256)
|
||||
throw Helper::Error("Cannot get sha256 of 'file2.txt'");
|
||||
else
|
||||
std::cout << "SHA256 of 'file2.txt': " << *sha256 << std::endl;
|
||||
auto sha256 = Helper::sha256Of(test_path("file2.txt"));
|
||||
if (!sha256) throw Helper::Error("Cannot get sha256 of 'file2.txt'");
|
||||
else std::cout << "SHA256 of 'file2.txt': " << *sha256 << std::endl;
|
||||
|
||||
std::cout << "'file2.txt' and 'file2lnk.txt' same? (SHA256): " << std::boolalpha << Helper::sha256Compare(test_path("file2.txt"), test_path("file2lnk.txt")) << std::endl;
|
||||
std::cout << "Copy 'file2.txt' as 'file2cpy.txt': " << std::boolalpha << Helper::copyFile(test_path("file2.txt"), test_path("file2cpy.txt")) << std::endl;
|
||||
std::cout << "Run command: 'ls': " << std::boolalpha << Helper::runCommand("ls") << std::endl;
|
||||
std::cout << "Spawn confirm propt..." << std::endl;
|
||||
std::cout << "'file2.txt' and 'file2lnk.txt' same? (SHA256): "
|
||||
<< std::boolalpha
|
||||
<< Helper::sha256Compare(test_path("file2.txt"),
|
||||
test_path("file2lnk.txt"))
|
||||
<< std::endl;
|
||||
std::cout << "Copy 'file2.txt' as 'file2cpy.txt': " << std::boolalpha
|
||||
<< Helper::copyFile(test_path("file2.txt"),
|
||||
test_path("file2cpy.txt"))
|
||||
<< std::endl;
|
||||
std::cout << "Run command: 'ls': " << std::boolalpha
|
||||
<< Helper::runCommand("ls") << std::endl;
|
||||
std::cout << "Spawn confirm propt..." << std::endl;
|
||||
|
||||
bool p = Helper::confirmPropt("Please answer");
|
||||
std::cout << "Result of confirm propt: " << std::boolalpha << p << std::endl;
|
||||
bool p = Helper::confirmPropt("Please answer");
|
||||
std::cout << "Result of confirm propt: " << std::boolalpha << p
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "Working directory: " << Helper::currentWorkingDirectory() << std::endl;
|
||||
std::cout << "Current date: " << Helper::currentDate() << std::endl;
|
||||
std::cout << "Current time: " << Helper::currentTime() << std::endl;
|
||||
std::cout << "Output of 'ls' command: " << Helper::runCommandWithOutput("ls") << std::endl;
|
||||
std::cout << "Basename of " << test_path("file2.txt") << ": " << Helper::pathBasename(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Dirname of " << test_path("file2.txt") << ": " << Helper::pathDirname(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Working directory: " << Helper::currentWorkingDirectory()
|
||||
<< std::endl;
|
||||
std::cout << "Current date: " << Helper::currentDate() << std::endl;
|
||||
std::cout << "Current time: " << Helper::currentTime() << std::endl;
|
||||
std::cout << "Output of 'ls' command: "
|
||||
<< Helper::runCommandWithOutput("ls") << std::endl;
|
||||
std::cout << "Basename of " << test_path("file2.txt") << ": "
|
||||
<< Helper::pathBasename(test_path("file2.txt")) << std::endl;
|
||||
std::cout << "Dirname of " << test_path("file2.txt") << ": "
|
||||
<< Helper::pathDirname(test_path("file2.txt")) << std::endl;
|
||||
|
||||
std::cout << "pathJoin() test 1: " << Helper::pathJoin("mydir", "dir2") << std::endl;
|
||||
std::cout << "pathJoin() test 2: " << Helper::pathJoin("mydir/", "dir2") << std::endl;
|
||||
std::cout << "pathJoin() test 3: " << Helper::pathJoin("mydir/", "/dir2") << std::endl;
|
||||
std::cout << "pathJoin() test 4: " << Helper::pathJoin("mydir", "/dir2") << std::endl;
|
||||
std::cout << "pathJoin() test 1: " << Helper::pathJoin("mydir", "dir2")
|
||||
<< std::endl;
|
||||
std::cout << "pathJoin() test 2: " << Helper::pathJoin("mydir/", "dir2")
|
||||
<< std::endl;
|
||||
std::cout << "pathJoin() test 3: " << Helper::pathJoin("mydir/", "/dir2")
|
||||
<< std::endl;
|
||||
std::cout << "pathJoin() test 4: " << Helper::pathJoin("mydir", "/dir2")
|
||||
<< std::endl;
|
||||
|
||||
std::cout << Helper::getLibVersion() << std::endl;
|
||||
std::cout << Helper::getLibVersion() << std::endl;
|
||||
|
||||
LOG(INFO) << "Info message" << std::endl;
|
||||
LOG(WARNING) << "Warning message" << std::endl;
|
||||
LOG(ERROR) << "Error message" << std::endl;
|
||||
LOG(ABORT) << "Abort message" << std::endl;
|
||||
} catch (Helper::Error& err) {
|
||||
std::cout << err.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
LOG(INFO) << "Info message" << std::endl;
|
||||
LOG(WARNING) << "Warning message" << std::endl;
|
||||
LOG(ERROR) << "Error message" << std::endl;
|
||||
LOG(ABORT) << "Abort message" << std::endl;
|
||||
} catch (Helper::Error &err) {
|
||||
std::cout << err.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
500
srclib/libpartition_map/include/libpartition_map/lib.hpp
Executable file → Normal file
500
srclib/libpartition_map/include/libpartition_map/lib.hpp
Executable file → Normal file
@@ -17,310 +17,318 @@
|
||||
#ifndef LIBPARTITION_MAP_LIB_HPP
|
||||
#define LIBPARTITION_MAP_LIB_HPP
|
||||
|
||||
#include <cstdint> // for uint64_t
|
||||
#include <exception>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <optional>
|
||||
#include <exception>
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <utility> // for std::pair
|
||||
#include <cstdint> // for uint64_t
|
||||
#include <libhelper/lib.hpp>
|
||||
|
||||
namespace PartitionMap {
|
||||
struct _entry {
|
||||
std::string name;
|
||||
struct _entry {
|
||||
std::string name;
|
||||
|
||||
struct {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
} props;
|
||||
};
|
||||
struct {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
} props;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 final {
|
||||
private:
|
||||
void _resize_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 final {
|
||||
private:
|
||||
void _resize_map();
|
||||
|
||||
[[nodiscard]] int _index_of(std::string_view name) const;
|
||||
[[nodiscard]] int _index_of(std::string_view name) const;
|
||||
|
||||
public:
|
||||
_entry *_data;
|
||||
size_t _count{}, _capacity{};
|
||||
public:
|
||||
_entry *_data;
|
||||
size_t _count{}, _capacity{};
|
||||
|
||||
struct _returnable_entry {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
};
|
||||
struct _returnable_entry {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
};
|
||||
|
||||
using BasicInf = _returnable_entry;
|
||||
using BasicInf = _returnable_entry;
|
||||
|
||||
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();
|
||||
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);
|
||||
void merge(const basic_partition_map &map);
|
||||
void clear();
|
||||
bool insert(const std::string &name, uint64_t size, bool logical);
|
||||
void merge(const basic_partition_map &map);
|
||||
void clear();
|
||||
|
||||
[[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;
|
||||
[[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;
|
||||
|
||||
basic_partition_map &operator=(const basic_partition_map &map);
|
||||
basic_partition_map &operator=(const basic_partition_map &map);
|
||||
|
||||
bool operator==(const basic_partition_map &other) const;
|
||||
bool operator!=(const basic_partition_map &other) const;
|
||||
bool operator==(const basic_partition_map &other) const;
|
||||
bool operator!=(const basic_partition_map &other) const;
|
||||
|
||||
class iterator {
|
||||
public:
|
||||
_entry *ptr;
|
||||
class iterator {
|
||||
public:
|
||||
_entry *ptr;
|
||||
|
||||
explicit iterator(_entry *p);
|
||||
explicit iterator(_entry *p);
|
||||
|
||||
auto operator*() const -> std::pair<std::string &, decltype(_entry::props) &>;
|
||||
_entry *operator->() const;
|
||||
iterator &operator++();
|
||||
iterator operator++(int);
|
||||
bool operator!=(const iterator &other) const;
|
||||
bool operator==(const iterator &other) const;
|
||||
};
|
||||
auto operator*() const
|
||||
-> std::pair<std::string &, decltype(_entry::props) &>;
|
||||
_entry *operator->() const;
|
||||
iterator &operator++();
|
||||
iterator operator++(int);
|
||||
bool operator!=(const iterator &other) const;
|
||||
bool operator==(const iterator &other) const;
|
||||
};
|
||||
|
||||
class constant_iterator {
|
||||
public:
|
||||
const _entry *ptr;
|
||||
class constant_iterator {
|
||||
public:
|
||||
const _entry *ptr;
|
||||
|
||||
explicit 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;
|
||||
constant_iterator &operator++();
|
||||
constant_iterator operator++(int);
|
||||
bool operator!=(const constant_iterator &other) const;
|
||||
bool operator==(const constant_iterator &other) const;
|
||||
};
|
||||
auto operator*() const
|
||||
-> std::pair<const std::string &, const decltype(_entry::props) &>;
|
||||
const _entry *operator->() const;
|
||||
constant_iterator &operator++();
|
||||
constant_iterator operator++(int);
|
||||
bool operator!=(const constant_iterator &other) const;
|
||||
bool operator==(const constant_iterator &other) const;
|
||||
};
|
||||
|
||||
/* for-each support */
|
||||
[[nodiscard]] iterator begin() const;
|
||||
[[nodiscard]] iterator end() const;
|
||||
[[nodiscard]] constant_iterator cbegin() const;
|
||||
[[nodiscard]] constant_iterator cend() const;
|
||||
};
|
||||
/* for-each support */
|
||||
[[nodiscard]] iterator begin() const;
|
||||
[[nodiscard]] iterator end() const;
|
||||
[[nodiscard]] constant_iterator cbegin() const;
|
||||
[[nodiscard]] constant_iterator cend() const;
|
||||
};
|
||||
|
||||
using Map_t = basic_partition_map;
|
||||
using Map_t = basic_partition_map;
|
||||
|
||||
class basic_partition_map_builder final {
|
||||
private:
|
||||
Map_t _current_map;
|
||||
std::string _workdir;
|
||||
bool _any_generating_error, _map_builded;
|
||||
class basic_partition_map_builder final {
|
||||
private:
|
||||
Map_t _current_map;
|
||||
std::string _workdir;
|
||||
bool _any_generating_error, _map_builded;
|
||||
|
||||
Map_t _build_map(std::string_view path, bool logical = false);
|
||||
Map_t _build_map(std::string_view path, bool logical = false);
|
||||
|
||||
void _insert_logicals(Map_t &&logicals);
|
||||
void _map_build_check() const;
|
||||
void _insert_logicals(Map_t &&logicals);
|
||||
void _map_build_check() const;
|
||||
|
||||
[[nodiscard]] static bool _is_real_block_dir(std::string_view path);
|
||||
[[nodiscard]] uint64_t _get_size(const std::string &path);
|
||||
[[nodiscard]] static bool _is_real_block_dir(std::string_view path);
|
||||
[[nodiscard]] uint64_t _get_size(const std::string &path);
|
||||
|
||||
public:
|
||||
/**
|
||||
* By default, it searches the directories in the
|
||||
* defaultEntryList in PartitionMap.cpp in order and
|
||||
* uses the directory it finds.
|
||||
*/
|
||||
basic_partition_map_builder();
|
||||
public:
|
||||
/**
|
||||
* By default, it searches the directories in the
|
||||
* defaultEntryList in PartitionMap.cpp in order and
|
||||
* uses the directory it finds.
|
||||
*/
|
||||
basic_partition_map_builder();
|
||||
|
||||
/**
|
||||
* A constructor with input. Need search path
|
||||
*/
|
||||
explicit basic_partition_map_builder(std::string_view path);
|
||||
/**
|
||||
* A constructor with input. Need search path
|
||||
*/
|
||||
explicit basic_partition_map_builder(std::string_view path);
|
||||
|
||||
/**
|
||||
* Returns the current list content in Map_t type.
|
||||
* If no list is created, returns std::nullopt.
|
||||
*/
|
||||
[[nodiscard]] Map_t getAll() const;
|
||||
/**
|
||||
* Returns the current list content in Map_t type.
|
||||
* If no list is created, returns std::nullopt.
|
||||
*/
|
||||
[[nodiscard]] Map_t getAll() const;
|
||||
|
||||
/**
|
||||
* Returns information of a specific partition in
|
||||
* Map_temp_t type. If the partition is not in the
|
||||
* currently created list, returns std::nullopt.
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::pair<uint64_t, bool> > get(std::string_view name) const;
|
||||
/**
|
||||
* Returns information of a specific partition in
|
||||
* Map_temp_t type. If the partition is not in the
|
||||
* currently created list, returns std::nullopt.
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::pair<uint64_t, bool>>
|
||||
get(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::list<std::string> > getLogicalPartitionList() const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::list<std::string>>
|
||||
getLogicalPartitionList() const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::optional<std::list<std::string> > getPhysicalPartitionList() const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[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;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::string getRealLinkPathOf(std::string_view name) const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::string getRealLinkPathOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* Returns the actual path of the partition as
|
||||
* std::string. Like /dev/block/sda5
|
||||
*/
|
||||
[[nodiscard]] std::string getRealPathOf(std::string_view name) const;
|
||||
/**
|
||||
* Returns the actual path of the partition as
|
||||
* std::string. Like /dev/block/sda5
|
||||
*/
|
||||
[[nodiscard]] std::string getRealPathOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::string getCurrentWorkDir() const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] std::string getCurrentWorkDir() const;
|
||||
|
||||
/**
|
||||
* Returns whether the entered partition name is in the
|
||||
* created partition list as a bool.
|
||||
*/
|
||||
[[nodiscard]] bool hasPartition(std::string_view name) const;
|
||||
/**
|
||||
* Returns whether the entered partition name is in the
|
||||
* created partition list as a bool.
|
||||
*/
|
||||
[[nodiscard]] bool hasPartition(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* Returns the bool type status of whether the
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] bool isLogical(std::string_view name) const;
|
||||
/**
|
||||
* Returns the bool type status of whether the
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] bool isLogical(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* The created list and the current search index name are cleared.
|
||||
*/
|
||||
void clear();
|
||||
/**
|
||||
* The created list and the current search index name are cleared.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 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(std::string_view 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(std::string_view path);
|
||||
|
||||
/**
|
||||
* Reads default /dev entries and builds map.
|
||||
*/
|
||||
bool readDefaultDirectories();
|
||||
/**
|
||||
* Reads default /dev entries and builds map.
|
||||
*/
|
||||
bool readDefaultDirectories();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] bool empty() const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] uint64_t sizeOf(std::string_view name) const;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
[[nodiscard]] uint64_t sizeOf(std::string_view name) const;
|
||||
|
||||
/**
|
||||
* 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==(const basic_partition_map_builder &lhs, const basic_partition_map_builder &rhs);
|
||||
/**
|
||||
* 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==(const basic_partition_map_builder &lhs,
|
||||
const basic_partition_map_builder &rhs);
|
||||
|
||||
/**
|
||||
* The opposite logic of the == operator.
|
||||
*/
|
||||
friend bool operator!=(const basic_partition_map_builder &lhs, const basic_partition_map_builder &rhs);
|
||||
/**
|
||||
* The opposite logic of the == operator.
|
||||
*/
|
||||
friend bool operator!=(const basic_partition_map_builder &lhs,
|
||||
const basic_partition_map_builder &rhs);
|
||||
|
||||
/**
|
||||
* You can check whether the object was created
|
||||
* successfully. If the problem did not occur, true is
|
||||
* returned, if it did, false is returned.
|
||||
*/
|
||||
explicit operator bool() const;
|
||||
/**
|
||||
* You can check whether the object was created
|
||||
* successfully. If the problem did not occur, true is
|
||||
* returned, if it did, false is returned.
|
||||
*/
|
||||
explicit operator bool() const;
|
||||
|
||||
/**
|
||||
* Returns true if the object creation failed (i.e., there's a problem),
|
||||
* and false if the object is correctly created.
|
||||
*/
|
||||
bool operator!() const;
|
||||
/**
|
||||
* Returns true if the object creation failed (i.e., there's a problem),
|
||||
* and false if the object is correctly created.
|
||||
*/
|
||||
bool operator!() const;
|
||||
|
||||
/**
|
||||
* Build map with input path. Implementation of readDirectory().
|
||||
*/
|
||||
bool operator()(std::string_view path);
|
||||
};
|
||||
/**
|
||||
* Build map with input path. Implementation of readDirectory().
|
||||
*/
|
||||
bool operator()(std::string_view path);
|
||||
};
|
||||
|
||||
using Error = Helper::Error;
|
||||
using Error = Helper::Error;
|
||||
|
||||
/**
|
||||
* To get the version information of libpartition_map
|
||||
* library. It is returned as std::string type.
|
||||
*/
|
||||
std::string getLibVersion();
|
||||
/**
|
||||
* To get the version information of libpartition_map
|
||||
* library. It is returned as std::string type.
|
||||
*/
|
||||
std::string getLibVersion();
|
||||
|
||||
using BuildMap = basic_partition_map_builder;
|
||||
using Map = basic_partition_map_builder;
|
||||
using BuildMap = basic_partition_map_builder;
|
||||
using Map = basic_partition_map_builder;
|
||||
|
||||
namespace Extras {
|
||||
namespace FileSystemMagic {
|
||||
constexpr uint64_t EXTFS_FS = 0xEF53;
|
||||
constexpr uint64_t F2FS_FS = 0xF2F52010;
|
||||
constexpr uint64_t EROFS_FS = 0xE0F5E1E2;
|
||||
constexpr uint64_t EXFAT_FS = 0x5441465845;
|
||||
constexpr uint64_t FAT12_FS = 0x3231544146;
|
||||
constexpr uint64_t FAT16_FS = 0x3631544146;
|
||||
constexpr uint64_t FAT32_FS = 0x3233544146;
|
||||
constexpr uint64_t NTFS_FS = 0x5346544E;
|
||||
constexpr uint64_t MSDOS_FS = 0x4d44;
|
||||
} // namespace FileSystemMagic
|
||||
namespace Extras {
|
||||
namespace FileSystemMagic {
|
||||
constexpr uint64_t EXTFS_FS = 0xEF53;
|
||||
constexpr uint64_t F2FS_FS = 0xF2F52010;
|
||||
constexpr uint64_t EROFS_FS = 0xE0F5E1E2;
|
||||
constexpr uint64_t EXFAT_FS = 0x5441465845;
|
||||
constexpr uint64_t FAT12_FS = 0x3231544146;
|
||||
constexpr uint64_t FAT16_FS = 0x3631544146;
|
||||
constexpr uint64_t FAT32_FS = 0x3233544146;
|
||||
constexpr uint64_t NTFS_FS = 0x5346544E;
|
||||
constexpr uint64_t MSDOS_FS = 0x4d44;
|
||||
} // namespace FileSystemMagic
|
||||
|
||||
namespace AndroidMagic {
|
||||
constexpr uint64_t BOOT_IMAGE = 0x2144494F52444E41;
|
||||
constexpr uint64_t VBOOT_IMAGE = 0x544F4F4252444E56;
|
||||
constexpr uint64_t LK_IMAGE = 0x00006B6C;
|
||||
constexpr uint64_t DTBO_IMAGE = 0x1EABB7D7;
|
||||
constexpr uint64_t VBMETA_IMAGE = 0x425641;
|
||||
constexpr uint64_t SUPER_IMAGE = 0x7265797573;
|
||||
constexpr uint64_t SPARSE_IMAGE = 0x3AFF26ED;
|
||||
constexpr uint64_t ELF = 0x464C457F; // It makes more sense than between file systems
|
||||
constexpr uint64_t RAW = 0x00000000;
|
||||
} // namespace AndroidMagic
|
||||
namespace AndroidMagic {
|
||||
constexpr uint64_t BOOT_IMAGE = 0x2144494F52444E41;
|
||||
constexpr uint64_t VBOOT_IMAGE = 0x544F4F4252444E56;
|
||||
constexpr uint64_t LK_IMAGE = 0x00006B6C;
|
||||
constexpr uint64_t DTBO_IMAGE = 0x1EABB7D7;
|
||||
constexpr uint64_t VBMETA_IMAGE = 0x425641;
|
||||
constexpr uint64_t SUPER_IMAGE = 0x7265797573;
|
||||
constexpr uint64_t SPARSE_IMAGE = 0x3AFF26ED;
|
||||
constexpr uint64_t ELF =
|
||||
0x464C457F; // It makes more sense than between file systems
|
||||
constexpr uint64_t RAW = 0x00000000;
|
||||
} // namespace AndroidMagic
|
||||
|
||||
extern std::unordered_map<uint64_t, std::string> FileSystemMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> AndroidMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> MagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> FileSystemMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> AndroidMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> MagicMap;
|
||||
|
||||
size_t getMagicLength(uint64_t magic);
|
||||
bool hasMagic(uint64_t magic, ssize_t buf, const std::string &path);
|
||||
std::string formatMagic(uint64_t magic);
|
||||
} // namespace Extras
|
||||
size_t getMagicLength(uint64_t magic);
|
||||
bool hasMagic(uint64_t magic, ssize_t buf, const std::string &path);
|
||||
std::string formatMagic(uint64_t magic);
|
||||
} // namespace Extras
|
||||
} // namespace PartitionMap
|
||||
|
||||
#define MAP "libpartition_map"
|
||||
|
||||
110
srclib/libpartition_map/src/Getters.cpp
Executable file → Normal file
110
srclib/libpartition_map/src/Getters.cpp
Executable file → Normal file
@@ -14,77 +14,83 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <filesystem>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace PartitionMap {
|
||||
Map_t basic_partition_map_builder::getAll() const {
|
||||
_map_build_check();
|
||||
return _current_map;
|
||||
}
|
||||
Map_t basic_partition_map_builder::getAll() const {
|
||||
_map_build_check();
|
||||
return _current_map;
|
||||
}
|
||||
|
||||
std::optional<std::pair<uint64_t, bool> > basic_partition_map_builder::get(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
std::optional<std::pair<uint64_t, bool>>
|
||||
basic_partition_map_builder::get(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
|
||||
if (!_current_map.find(name)) return std::nullopt;
|
||||
return std::make_pair(_current_map.get_size(name), _current_map.is_logical(name));
|
||||
}
|
||||
if (!_current_map.find(name)) return std::nullopt;
|
||||
return std::make_pair(_current_map.get_size(name),
|
||||
_current_map.is_logical(name));
|
||||
}
|
||||
|
||||
std::optional<std::list<std::string> > basic_partition_map_builder::getLogicalPartitionList() const {
|
||||
_map_build_check();
|
||||
std::optional<std::list<std::string>>
|
||||
basic_partition_map_builder::getLogicalPartitionList() const {
|
||||
_map_build_check();
|
||||
|
||||
std::list<std::string> logicals;
|
||||
for (const auto &[name, props]: _current_map)
|
||||
if (props.isLogical) logicals.push_back(name);
|
||||
std::list<std::string> logicals;
|
||||
for (const auto &[name, props] : _current_map)
|
||||
if (props.isLogical) logicals.push_back(name);
|
||||
|
||||
if (logicals.empty()) return std::nullopt;
|
||||
return logicals;
|
||||
}
|
||||
if (logicals.empty()) return std::nullopt;
|
||||
return logicals;
|
||||
}
|
||||
|
||||
std::optional<std::list<std::string> > basic_partition_map_builder::getPhysicalPartitionList() const {
|
||||
_map_build_check();
|
||||
std::optional<std::list<std::string>>
|
||||
basic_partition_map_builder::getPhysicalPartitionList() const {
|
||||
_map_build_check();
|
||||
|
||||
std::list<std::string> physicals;
|
||||
for (const auto &[name, props]: _current_map)
|
||||
if (!props.isLogical) physicals.push_back(name);
|
||||
std::list<std::string> physicals;
|
||||
for (const auto &[name, props] : _current_map)
|
||||
if (!props.isLogical) physicals.push_back(name);
|
||||
|
||||
if (physicals.empty()) return std::nullopt;
|
||||
return physicals;
|
||||
}
|
||||
if (physicals.empty()) return std::nullopt;
|
||||
return physicals;
|
||||
}
|
||||
|
||||
std::optional<std::list<std::string> > basic_partition_map_builder::getPartitionList() const {
|
||||
_map_build_check();
|
||||
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);
|
||||
std::list<std::string> partitions;
|
||||
for (const auto &[name, props] : _current_map)
|
||||
partitions.push_back(name);
|
||||
|
||||
if (partitions.empty()) return std::nullopt;
|
||||
return partitions;
|
||||
}
|
||||
if (partitions.empty()) return std::nullopt;
|
||||
return partitions;
|
||||
}
|
||||
|
||||
std::string basic_partition_map_builder::getRealLinkPathOf(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
std::string basic_partition_map_builder::getRealLinkPathOf(
|
||||
const std::string_view name) const {
|
||||
_map_build_check();
|
||||
|
||||
if (!_current_map.find(name)) return {};
|
||||
return std::string(_workdir + "/" + name.data());
|
||||
}
|
||||
if (!_current_map.find(name)) return {};
|
||||
return std::string(_workdir + "/" + name.data());
|
||||
}
|
||||
|
||||
std::string basic_partition_map_builder::getRealPathOf(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
std::string
|
||||
basic_partition_map_builder::getRealPathOf(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
|
||||
const std::string full = (isLogical(name))
|
||||
? std::string("/dev/block/mapper/") + name.data()
|
||||
: _workdir + "/" + name.data();
|
||||
if (!_current_map.find(name)
|
||||
|| !std::filesystem::is_symlink(full))
|
||||
return {};
|
||||
const std::string full = (isLogical(name))
|
||||
? std::string("/dev/block/mapper/") + name.data()
|
||||
: _workdir + "/" + name.data();
|
||||
if (!_current_map.find(name) || !std::filesystem::is_symlink(full)) return {};
|
||||
|
||||
return std::filesystem::read_symlink(full);
|
||||
}
|
||||
return std::filesystem::read_symlink(full);
|
||||
}
|
||||
|
||||
std::string basic_partition_map_builder::getCurrentWorkDir() const {
|
||||
return _workdir;
|
||||
}
|
||||
std::string basic_partition_map_builder::getCurrentWorkDir() const {
|
||||
return _workdir;
|
||||
}
|
||||
} // namespace PartitionMap
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -15,105 +15,109 @@
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "PartitionManager/PartitionManager.hpp"
|
||||
|
||||
namespace PartitionMap::Extras {
|
||||
std::unordered_map<uint64_t, std::string> FileSystemMagicMap = {
|
||||
{FileSystemMagic::EXTFS_FS, "EXT2/3/4"},
|
||||
{FileSystemMagic::F2FS_FS, "F2FS"},
|
||||
{FileSystemMagic::EROFS_FS, "EROFS"},
|
||||
{FileSystemMagic::EXFAT_FS, "exFAT"},
|
||||
{FileSystemMagic::FAT12_FS, "FAT12"},
|
||||
{FileSystemMagic::FAT16_FS, "FAT16"},
|
||||
{FileSystemMagic::FAT32_FS, "FAT32"},
|
||||
{FileSystemMagic::NTFS_FS, "NTFS"},
|
||||
{FileSystemMagic::MSDOS_FS, "MSDOS"}
|
||||
};
|
||||
std::unordered_map<uint64_t, std::string> FileSystemMagicMap = {
|
||||
{FileSystemMagic::EXTFS_FS, "EXT2/3/4"},
|
||||
{FileSystemMagic::F2FS_FS, "F2FS"},
|
||||
{FileSystemMagic::EROFS_FS, "EROFS"},
|
||||
{FileSystemMagic::EXFAT_FS, "exFAT"},
|
||||
{FileSystemMagic::FAT12_FS, "FAT12"},
|
||||
{FileSystemMagic::FAT16_FS, "FAT16"},
|
||||
{FileSystemMagic::FAT32_FS, "FAT32"},
|
||||
{FileSystemMagic::NTFS_FS, "NTFS"},
|
||||
{FileSystemMagic::MSDOS_FS, "MSDOS"}};
|
||||
|
||||
std::unordered_map<uint64_t, std::string> AndroidMagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
{AndroidMagic::DTBO_IMAGE, "Android DTBO Image"},
|
||||
{AndroidMagic::VBMETA_IMAGE, "Android VBMeta Image"},
|
||||
{AndroidMagic::SUPER_IMAGE, "Android Super Image"},
|
||||
{AndroidMagic::SPARSE_IMAGE, "Android Sparse Image"},
|
||||
{AndroidMagic::ELF, "ELF"},
|
||||
{AndroidMagic::RAW, "Raw Data"}
|
||||
};
|
||||
std::unordered_map<uint64_t, std::string> AndroidMagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
{AndroidMagic::DTBO_IMAGE, "Android DTBO Image"},
|
||||
{AndroidMagic::VBMETA_IMAGE, "Android VBMeta Image"},
|
||||
{AndroidMagic::SUPER_IMAGE, "Android Super Image"},
|
||||
{AndroidMagic::SPARSE_IMAGE, "Android Sparse Image"},
|
||||
{AndroidMagic::ELF, "ELF"},
|
||||
{AndroidMagic::RAW, "Raw Data"}};
|
||||
|
||||
std::unordered_map<uint64_t, std::string> MagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
{AndroidMagic::DTBO_IMAGE, "Android DTBO Image"},
|
||||
{AndroidMagic::VBMETA_IMAGE, "Android VBMeta Image"},
|
||||
{AndroidMagic::SUPER_IMAGE, "Android Super Image"},
|
||||
{AndroidMagic::SPARSE_IMAGE, "Android Sparse Image"},
|
||||
{AndroidMagic::ELF, "ELF"},
|
||||
{AndroidMagic::RAW, "Raw Data"},
|
||||
{FileSystemMagic::EXTFS_FS, "EXT2/3/4"},
|
||||
{FileSystemMagic::F2FS_FS, "F2FS"},
|
||||
{FileSystemMagic::EROFS_FS, "EROFS"},
|
||||
{FileSystemMagic::EXFAT_FS, "exFAT"},
|
||||
{FileSystemMagic::FAT12_FS, "FAT12"},
|
||||
{FileSystemMagic::FAT16_FS, "FAT16"},
|
||||
{FileSystemMagic::FAT32_FS, "FAT32"},
|
||||
{FileSystemMagic::NTFS_FS, "NTFS"},
|
||||
{FileSystemMagic::MSDOS_FS, "MSDOS"}
|
||||
};
|
||||
std::unordered_map<uint64_t, std::string> MagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
{AndroidMagic::DTBO_IMAGE, "Android DTBO Image"},
|
||||
{AndroidMagic::VBMETA_IMAGE, "Android VBMeta Image"},
|
||||
{AndroidMagic::SUPER_IMAGE, "Android Super Image"},
|
||||
{AndroidMagic::SPARSE_IMAGE, "Android Sparse Image"},
|
||||
{AndroidMagic::ELF, "ELF"},
|
||||
{AndroidMagic::RAW, "Raw Data"},
|
||||
{FileSystemMagic::EXTFS_FS, "EXT2/3/4"},
|
||||
{FileSystemMagic::F2FS_FS, "F2FS"},
|
||||
{FileSystemMagic::EROFS_FS, "EROFS"},
|
||||
{FileSystemMagic::EXFAT_FS, "exFAT"},
|
||||
{FileSystemMagic::FAT12_FS, "FAT12"},
|
||||
{FileSystemMagic::FAT16_FS, "FAT16"},
|
||||
{FileSystemMagic::FAT32_FS, "FAT32"},
|
||||
{FileSystemMagic::NTFS_FS, "NTFS"},
|
||||
{FileSystemMagic::MSDOS_FS, "MSDOS"}};
|
||||
|
||||
size_t getMagicLength(const uint64_t magic) {
|
||||
size_t length = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if ((magic >> (8 * i)) & 0xFF) length = i + 1;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
size_t getMagicLength(const uint64_t magic) {
|
||||
size_t length = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if ((magic >> (8 * i)) & 0xFF) length = i + 1;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
bool hasMagic(const uint64_t magic, const ssize_t buf, const std::string &path) {
|
||||
LOGN(MAP, INFO) << "Checking magic of " << path << " with using " << buf << " byte buffer size (has magic 0x" << std::hex << magic << "?)" << std::endl;
|
||||
Helper::garbageCollector collector;
|
||||
bool hasMagic(const uint64_t magic, const ssize_t buf,
|
||||
const std::string &path) {
|
||||
LOGN(MAP, INFO) << "Checking magic of " << path << " with using " << buf
|
||||
<< " byte buffer size (has magic 0x" << std::hex << magic
|
||||
<< "?)" << std::endl;
|
||||
Helper::garbageCollector collector;
|
||||
|
||||
const int fd = Helper::openAndAddToCloseList(path, collector, O_RDONLY);
|
||||
if (fd < 0) return false;
|
||||
if (buf < 1) {
|
||||
LOGN(MAP, ERROR) << "Buffer size is older than 1" << std::endl;
|
||||
return false;
|
||||
}
|
||||
const int fd = Helper::openAndAddToCloseList(path, collector, O_RDONLY);
|
||||
if (fd < 0) return false;
|
||||
if (buf < 1) {
|
||||
LOGN(MAP, ERROR) << "Buffer size is older than 1" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto *buffer = new uint8_t[buf];
|
||||
collector.delAfterProgress(buffer);
|
||||
auto *buffer = new uint8_t[buf];
|
||||
collector.delAfterProgress(buffer);
|
||||
|
||||
const ssize_t bytesRead = read(fd, buffer, buf);
|
||||
if (bytesRead < 0) return false;
|
||||
const ssize_t bytesRead = read(fd, buffer, buf);
|
||||
if (bytesRead < 0) return false;
|
||||
|
||||
const size_t magicLength = getMagicLength(magic);
|
||||
if (magicLength == 0) return false;
|
||||
const size_t magicLength = getMagicLength(magic);
|
||||
if (magicLength == 0) return false;
|
||||
|
||||
for (size_t i = 0; i <= bytesRead - magicLength; i++) {
|
||||
uint64_t value = 0;
|
||||
for (size_t j = 0; j < magicLength; ++j) value |= static_cast<uint64_t>(buffer[i + j]) << (8 * j);
|
||||
if (value == magic) {
|
||||
LOGN(MAP, INFO) << path << " contains 0x" << std::hex << magic << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i <= bytesRead - magicLength; i++) {
|
||||
uint64_t value = 0;
|
||||
for (size_t j = 0; j < magicLength; ++j)
|
||||
value |= static_cast<uint64_t>(buffer[i + j]) << (8 * j);
|
||||
if (value == magic) {
|
||||
LOGN(MAP, INFO) << path << " contains 0x" << std::hex << magic
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
LOGN(MAP, INFO) << path << " is not contains 0x" << std::hex << magic << std::endl;
|
||||
return false;
|
||||
}
|
||||
LOGN(MAP, INFO) << path << " is not contains 0x" << std::hex << magic
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string formatMagic(const uint64_t magic) {
|
||||
std::stringstream ss;
|
||||
ss << "0x" << std::uppercase << std::hex << std::setw(16) << std::setfill('0') << magic;
|
||||
return ss.str();
|
||||
}
|
||||
std::string formatMagic(const uint64_t magic) {
|
||||
std::stringstream ss;
|
||||
ss << "0x" << std::uppercase << std::hex << std::setw(16) << std::setfill('0')
|
||||
<< magic;
|
||||
return ss.str();
|
||||
}
|
||||
} // namespace PartitionMap::Extras
|
||||
|
||||
368
srclib/libpartition_map/src/PartitionMap.cpp
Executable file → Normal file
368
srclib/libpartition_map/src/PartitionMap.cpp
Executable file → Normal file
@@ -14,222 +14,242 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <generated/buildInfo.hpp>
|
||||
#include <iostream>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <linux/fs.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#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 <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
static constexpr std::array<std::string_view, 3> defaultEntryList = {
|
||||
"/dev/block/by-name",
|
||||
"/dev/block/bootdevice/by-name",
|
||||
"/dev/block/platform/bootdevice/by-name"
|
||||
};
|
||||
"/dev/block/by-name", "/dev/block/bootdevice/by-name",
|
||||
"/dev/block/platform/bootdevice/by-name"};
|
||||
|
||||
namespace PartitionMap {
|
||||
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path) {
|
||||
if (path.find("/block/") == std::string::npos) {
|
||||
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool basic_partition_map_builder::_is_real_block_dir(
|
||||
const std::string_view path) {
|
||||
if (path.find("/block/") == std::string::npos) {
|
||||
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Map_t basic_partition_map_builder::_build_map(std::string_view path, bool logical) {
|
||||
Map_t map;
|
||||
std::vector<std::filesystem::directory_entry> entries{
|
||||
std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()
|
||||
};
|
||||
std::sort(entries.begin(), entries.end(), [](const auto &a, const auto &b) {
|
||||
return a.path().filename() < b.path().filename();
|
||||
});
|
||||
Map_t basic_partition_map_builder::_build_map(std::string_view path,
|
||||
bool logical) {
|
||||
Map_t map;
|
||||
std::vector<std::filesystem::directory_entry> entries{
|
||||
std::filesystem::directory_iterator(path),
|
||||
std::filesystem::directory_iterator()};
|
||||
std::sort(entries.begin(), entries.end(), [](const auto &a, const auto &b) {
|
||||
return a.path().filename() < b.path().filename();
|
||||
});
|
||||
|
||||
LOGN_IF(MAP, WARNING, entries.empty()) << "" << path <<
|
||||
"is exists but generated vector is empty (std::vector<std::filesystem::directory_entry>)." << std::endl;
|
||||
for (const auto &entry: entries) {
|
||||
if (entry.path().filename() != "by-uuid"
|
||||
&& std::string(entry.path()).find("com.") == std::string::npos)
|
||||
map.insert(entry.path().filename().string(), _get_size(entry.path()), logical);
|
||||
}
|
||||
LOGN_IF(MAP, WARNING, entries.empty())
|
||||
<< "" << path
|
||||
<< "is exists but generated vector is empty "
|
||||
"(std::vector<std::filesystem::directory_entry>)."
|
||||
<< std::endl;
|
||||
for (const auto &entry : entries) {
|
||||
if (entry.path().filename() != "by-uuid" &&
|
||||
std::string(entry.path()).find("com.") == std::string::npos)
|
||||
map.insert(entry.path().filename().string(), _get_size(entry.path()),
|
||||
logical);
|
||||
}
|
||||
|
||||
LOGN(MAP, INFO) << std::boolalpha << "Map generated successfully. is_logical_map=" << logical << std::endl;
|
||||
return map;
|
||||
}
|
||||
LOGN(MAP, INFO) << std::boolalpha
|
||||
<< "Map generated successfully. is_logical_map=" << logical
|
||||
<< std::endl;
|
||||
return map;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void basic_partition_map_builder::_map_build_check() const {
|
||||
if (!_map_builded)
|
||||
throw Error("Please build partition map before!");
|
||||
}
|
||||
void basic_partition_map_builder::_map_build_check() const {
|
||||
if (!_map_builded) throw Error("Please build partition map before!");
|
||||
}
|
||||
|
||||
uint64_t basic_partition_map_builder::_get_size(const std::string &path) {
|
||||
const std::string real = std::filesystem::read_symlink(path);
|
||||
Helper::garbageCollector collector;
|
||||
uint64_t basic_partition_map_builder::_get_size(const std::string &path) {
|
||||
const std::string real = std::filesystem::read_symlink(path);
|
||||
Helper::garbageCollector collector;
|
||||
|
||||
const int fd = Helper::openAndAddToCloseList(real, collector, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
const int fd = Helper::openAndAddToCloseList(real, collector, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno)
|
||||
<< std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t size = 0;
|
||||
if (ioctl(fd, BLKGETSIZE64, &size) != 0) {
|
||||
LOGN(MAP, ERROR) << "ioctl() process failed for " << real << ": " << strerror(errno) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
uint64_t size = 0;
|
||||
if (ioctl(fd, BLKGETSIZE64, &size) != 0) {
|
||||
LOGN(MAP, ERROR) << "ioctl() process failed for " << real << ": "
|
||||
<< strerror(errno) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
basic_partition_map_builder::basic_partition_map_builder() {
|
||||
LOGN(MAP, INFO) << "default constructor called. Starting build." << std::endl;
|
||||
basic_partition_map_builder::basic_partition_map_builder() {
|
||||
LOGN(MAP, INFO) << "default constructor called. Starting build." << 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;
|
||||
} else {
|
||||
_workdir = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto &path : defaultEntryList) {
|
||||
if (std::filesystem::exists(path)) {
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) {
|
||||
_any_generating_error = true;
|
||||
} else {
|
||||
_workdir = path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_current_map.empty())
|
||||
LOGN(MAP, ERROR) << "Cannot build map by any default search entry." << std::endl;
|
||||
if (_current_map.empty())
|
||||
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
|
||||
<< std::endl;
|
||||
|
||||
LOGN(MAP, INFO) << "default constructor successfully ended work." << std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
}
|
||||
LOGN(MAP, INFO) << "default constructor successfully ended work."
|
||||
<< std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
}
|
||||
|
||||
basic_partition_map_builder::basic_partition_map_builder(const std::string_view path) {
|
||||
LOGN(MAP, INFO) << "argument-based constructor called. Starting build." << std::endl;
|
||||
basic_partition_map_builder::basic_partition_map_builder(
|
||||
const std::string_view path) {
|
||||
LOGN(MAP, INFO) << "argument-based constructor called. Starting build."
|
||||
<< std::endl;
|
||||
|
||||
if (std::filesystem::exists(path)) {
|
||||
if (!_is_real_block_dir(path)) return;
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) _any_generating_error = true;
|
||||
else _workdir = path;
|
||||
} else
|
||||
throw Error("Cannot find directory: %s. Cannot build partition map!", path.data());
|
||||
if (std::filesystem::exists(path)) {
|
||||
if (!_is_real_block_dir(path)) return;
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) _any_generating_error = true;
|
||||
else _workdir = path;
|
||||
} else
|
||||
throw Error("Cannot find directory: %s. Cannot build partition map!",
|
||||
path.data());
|
||||
|
||||
LOGN(MAP, INFO) << "argument-based constructor successfully ended work." << std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
}
|
||||
LOGN(MAP, INFO) << "argument-based constructor successfully ended work."
|
||||
<< std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::hasPartition(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.find(name);
|
||||
}
|
||||
bool basic_partition_map_builder::hasPartition(
|
||||
const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.find(name);
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::isLogical(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.is_logical(name);
|
||||
}
|
||||
bool basic_partition_map_builder::isLogical(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.is_logical(name);
|
||||
}
|
||||
|
||||
void basic_partition_map_builder::clear() {
|
||||
_current_map.clear();
|
||||
_workdir.clear();
|
||||
_any_generating_error = false;
|
||||
}
|
||||
void basic_partition_map_builder::clear() {
|
||||
_current_map.clear();
|
||||
_workdir.clear();
|
||||
_any_generating_error = false;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::readDirectory(const std::string_view path) {
|
||||
_map_builded = false;
|
||||
LOGN(MAP, INFO) << "read " << path << " directory request." << std::endl;
|
||||
bool basic_partition_map_builder::readDirectory(const std::string_view path) {
|
||||
_map_builded = false;
|
||||
LOGN(MAP, INFO) << "read " << path << " directory request." << std::endl;
|
||||
|
||||
if (std::filesystem::exists(path)) {
|
||||
if (!_is_real_block_dir(path)) return false;
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) {
|
||||
_any_generating_error = true;
|
||||
return false;
|
||||
} else _workdir = path;
|
||||
} else
|
||||
throw Error("Cannot find directory: %s. Cannot build partition map!", path.data());
|
||||
if (std::filesystem::exists(path)) {
|
||||
if (!_is_real_block_dir(path)) return false;
|
||||
_current_map = _build_map(path);
|
||||
if (_current_map.empty()) {
|
||||
_any_generating_error = true;
|
||||
return false;
|
||||
} else _workdir = path;
|
||||
} else
|
||||
throw Error("Cannot find directory: %s. Cannot build partition map!",
|
||||
path.data());
|
||||
|
||||
LOGN(MAP, INFO) << "read " << path << " successfull." << std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
return true;
|
||||
}
|
||||
LOGN(MAP, INFO) << "read " << path << " successfull." << std::endl;
|
||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||
_map_builded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::readDefaultDirectories() {
|
||||
_map_builded = false;
|
||||
LOGN(MAP, INFO) << "read default directories request." << std::endl;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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();
|
||||
return _current_map.empty();
|
||||
}
|
||||
bool basic_partition_map_builder::empty() const {
|
||||
_map_build_check();
|
||||
return _current_map.empty();
|
||||
}
|
||||
|
||||
uint64_t basic_partition_map_builder::sizeOf(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.get_size(name);
|
||||
}
|
||||
uint64_t
|
||||
basic_partition_map_builder::sizeOf(const std::string_view name) const {
|
||||
_map_build_check();
|
||||
return _current_map.get_size(name);
|
||||
}
|
||||
|
||||
bool operator==(const basic_partition_map_builder &lhs, const basic_partition_map_builder &rhs) {
|
||||
return lhs._current_map == rhs._current_map;
|
||||
}
|
||||
bool operator==(const basic_partition_map_builder &lhs,
|
||||
const basic_partition_map_builder &rhs) {
|
||||
return lhs._current_map == rhs._current_map;
|
||||
}
|
||||
|
||||
bool operator!=(const basic_partition_map_builder &lhs, const basic_partition_map_builder &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
bool operator!=(const basic_partition_map_builder &lhs,
|
||||
const basic_partition_map_builder &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
basic_partition_map_builder::operator bool() const {
|
||||
return !this->_any_generating_error;
|
||||
}
|
||||
basic_partition_map_builder::operator bool() const {
|
||||
return !this->_any_generating_error;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::operator!() const {
|
||||
return this->_any_generating_error;
|
||||
}
|
||||
bool basic_partition_map_builder::operator!() const {
|
||||
return this->_any_generating_error;
|
||||
}
|
||||
|
||||
bool basic_partition_map_builder::operator()(const std::string_view path) {
|
||||
LOGN(MAP, INFO) << "calling readDirectory() for building map with " << path << std::endl;
|
||||
return readDirectory(path);
|
||||
}
|
||||
bool basic_partition_map_builder::operator()(const std::string_view path) {
|
||||
LOGN(MAP, INFO) << "calling readDirectory() for building map with " << path
|
||||
<< std::endl;
|
||||
return readDirectory(path);
|
||||
}
|
||||
|
||||
std::string getLibVersion() {
|
||||
MKVERSION("libpartition_map");
|
||||
}
|
||||
std::string getLibVersion() { MKVERSION("libpartition_map"); }
|
||||
} // namespace PartitionMap
|
||||
|
||||
336
srclib/libpartition_map/src/Type.cpp
Executable file → Normal file
336
srclib/libpartition_map/src/Type.cpp
Executable file → Normal file
@@ -14,223 +14,227 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace PartitionMap {
|
||||
basic_partition_map::iterator::iterator(_entry *p) : ptr(p) {
|
||||
}
|
||||
basic_partition_map::iterator::iterator(_entry *p) : ptr(p) {}
|
||||
|
||||
auto basic_partition_map::iterator::operator*() const -> std::pair<std::string &, decltype(_entry::props) &> {
|
||||
return {ptr->name, ptr->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->() const {
|
||||
return ptr;
|
||||
}
|
||||
_entry *basic_partition_map::iterator::operator->() const { return ptr; }
|
||||
|
||||
basic_partition_map::iterator &basic_partition_map::iterator::operator++() {
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
basic_partition_map::iterator &basic_partition_map::iterator::operator++() {
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::iterator::operator++(int) {
|
||||
iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
basic_partition_map::iterator basic_partition_map::iterator::operator++(int) {
|
||||
iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool basic_partition_map::iterator::operator==(const iterator &other) const {
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
bool basic_partition_map::iterator::operator==(const iterator &other) const {
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
|
||||
bool basic_partition_map::iterator::operator!=(const iterator &other) const {
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
bool basic_partition_map::iterator::operator!=(const iterator &other) const {
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator::constant_iterator(const _entry *p) : ptr(p) {
|
||||
}
|
||||
basic_partition_map::constant_iterator::constant_iterator(const _entry *p)
|
||||
: ptr(p) {}
|
||||
|
||||
auto basic_partition_map::constant_iterator::operator
|
||||
*() const -> std::pair<const std::string &, const decltype(_entry::props) &> {
|
||||
return {ptr->name, ptr->props};
|
||||
}
|
||||
auto basic_partition_map::constant_iterator::operator*() const
|
||||
-> std::pair<const std::string &, const decltype(_entry::props) &> {
|
||||
return {ptr->name, ptr->props};
|
||||
}
|
||||
|
||||
const _entry *basic_partition_map::constant_iterator::operator->() const {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
const _entry *basic_partition_map::constant_iterator::operator->() const {
|
||||
return ptr;
|
||||
}
|
||||
basic_partition_map::constant_iterator &
|
||||
basic_partition_map::constant_iterator::operator++() {
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator &basic_partition_map::constant_iterator::operator++() {
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
basic_partition_map::constant_iterator
|
||||
basic_partition_map::constant_iterator::operator++(int) {
|
||||
constant_iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::constant_iterator::operator++(int) {
|
||||
constant_iterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
bool basic_partition_map::constant_iterator::operator==(
|
||||
const constant_iterator &other) const {
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
|
||||
bool basic_partition_map::constant_iterator::operator==(const constant_iterator &other) const {
|
||||
return ptr == other.ptr;
|
||||
}
|
||||
bool basic_partition_map::constant_iterator::operator!=(
|
||||
const constant_iterator &other) const {
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
|
||||
bool basic_partition_map::constant_iterator::operator!=(const constant_iterator &other) const {
|
||||
return ptr != other.ptr;
|
||||
}
|
||||
void basic_partition_map::_resize_map() {
|
||||
const size_t new_capacity = _capacity * 2;
|
||||
auto *new_data = new _entry[new_capacity];
|
||||
|
||||
void basic_partition_map::_resize_map() {
|
||||
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];
|
||||
|
||||
for (size_t i = 0; i < _count; i++) new_data[i] = _data[i];
|
||||
delete[] _data;
|
||||
_data = new_data;
|
||||
_capacity = new_capacity;
|
||||
}
|
||||
|
||||
delete[] _data;
|
||||
_data = new_data;
|
||||
_capacity = new_capacity;
|
||||
}
|
||||
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 static_cast<int>(i);
|
||||
}
|
||||
|
||||
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 static_cast<int>(i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
basic_partition_map::basic_partition_map(const basic_partition_map &other)
|
||||
: _data(new _entry[other._capacity]), _count(other._count),
|
||||
_capacity(other._capacity) {
|
||||
std::copy(other._data, other._data + _count, _data);
|
||||
}
|
||||
|
||||
basic_partition_map::basic_partition_map(const basic_partition_map &other) : _data(new _entry[other._capacity]),
|
||||
_count(other._count),
|
||||
_capacity(other._capacity) {
|
||||
std::copy(other._data, other._data + _count, _data);
|
||||
}
|
||||
basic_partition_map::basic_partition_map() : _capacity(6) {
|
||||
_data = new _entry[_capacity];
|
||||
}
|
||||
|
||||
basic_partition_map::basic_partition_map() : _capacity(6) {
|
||||
_data = new _entry[_capacity];
|
||||
}
|
||||
basic_partition_map::~basic_partition_map() { delete[] _data; }
|
||||
|
||||
basic_partition_map::~basic_partition_map() {
|
||||
delete[] _data;
|
||||
}
|
||||
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();
|
||||
|
||||
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();
|
||||
_data[_count++] = {name, {size, logical}};
|
||||
LOGN(MAP, INFO) << std::boolalpha << "partition " << name
|
||||
<< " inserted (size=" << size << ", is_logical=" << logical
|
||||
<< ")." << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
_data[_count++] = {name, {size, logical}};
|
||||
LOGN(MAP, INFO) << std::boolalpha << "partition " << name << " inserted (size=" << size << ", is_logical=" <<
|
||||
logical << ")." << std::endl;
|
||||
return true;
|
||||
}
|
||||
void basic_partition_map::merge(const basic_partition_map &map) {
|
||||
LOGN(MAP, INFO) << "map merge request." << std::endl;
|
||||
for (const auto &[name, props] : map)
|
||||
insert(name, props.size, props.isLogical);
|
||||
LOGN(MAP, INFO) << "map merged successfully." << std::endl;
|
||||
}
|
||||
|
||||
void basic_partition_map::merge(const basic_partition_map &map) {
|
||||
LOGN(MAP, INFO) << "map merge request." << std::endl;
|
||||
for (const auto &[name, props]: map)
|
||||
insert(name, props.size, props.isLogical);
|
||||
LOGN(MAP, INFO) << "map merged successfully." << std::endl;
|
||||
}
|
||||
uint64_t basic_partition_map::get_size(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name)
|
||||
return _data[pos].props.size;
|
||||
|
||||
uint64_t basic_partition_map::get_size(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name) return _data[pos].props.size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
bool basic_partition_map::is_logical(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name)
|
||||
return _data[pos].props.isLogical;
|
||||
|
||||
bool basic_partition_map::is_logical(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name) return _data[pos].props.isLogical;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
basic_partition_map::_returnable_entry
|
||||
basic_partition_map::get_all(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name)
|
||||
return _returnable_entry{_data[pos].props.size, _data[pos].props.isLogical};
|
||||
|
||||
basic_partition_map::_returnable_entry basic_partition_map::get_all(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name)
|
||||
return _returnable_entry{_data[pos].props.size, _data[pos].props.isLogical};
|
||||
return _returnable_entry{};
|
||||
}
|
||||
|
||||
return _returnable_entry{};
|
||||
}
|
||||
bool basic_partition_map::find(const std::string_view name) const {
|
||||
if (name == _data[_index_of(name)].name) return true;
|
||||
|
||||
bool basic_partition_map::find(const std::string_view name) const {
|
||||
if (name == _data[_index_of(name)].name) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
std::string basic_partition_map::find_(const std::string &name) const {
|
||||
if (name == _data[_index_of(name)].name) return name;
|
||||
|
||||
std::string basic_partition_map::find_(const std::string &name) const {
|
||||
if (name == _data[_index_of(name)].name) return name;
|
||||
return {};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
size_t basic_partition_map::size() const { return _count; }
|
||||
|
||||
size_t basic_partition_map::size() const {
|
||||
return _count;
|
||||
}
|
||||
bool basic_partition_map::empty() const {
|
||||
if (_count > 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool basic_partition_map::empty() const {
|
||||
if (_count > 0) return false;
|
||||
return true;
|
||||
}
|
||||
void basic_partition_map::clear() {
|
||||
LOGN(MAP, INFO) << "map clean requested. Map is empty now." << std::endl;
|
||||
delete[] _data;
|
||||
_count = 0;
|
||||
_capacity = 6;
|
||||
_data = new _entry[_capacity];
|
||||
}
|
||||
|
||||
void basic_partition_map::clear() {
|
||||
LOGN(MAP, INFO) << "map clean requested. Map is empty now." << std::endl;
|
||||
delete[] _data;
|
||||
_count = 0;
|
||||
_capacity = 6;
|
||||
_data = new _entry[_capacity];
|
||||
}
|
||||
basic_partition_map &
|
||||
basic_partition_map::operator=(const basic_partition_map &map) {
|
||||
if (this != &map) {
|
||||
delete[] _data;
|
||||
|
||||
basic_partition_map &basic_partition_map::operator=(const basic_partition_map &map) {
|
||||
if (this != &map) {
|
||||
delete[] _data;
|
||||
_capacity = map._capacity;
|
||||
_count = map._count;
|
||||
_data = new _entry[_capacity];
|
||||
std::copy(map._data, map._data + _count, _data);
|
||||
}
|
||||
|
||||
_capacity = map._capacity;
|
||||
_count = map._count;
|
||||
_data = new _entry[_capacity];
|
||||
std::copy(map._data, map._data + _count, _data);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
bool basic_partition_map::operator==(const basic_partition_map &other) const {
|
||||
if (this->_capacity != other._capacity || this->_count != other._count)
|
||||
return false;
|
||||
|
||||
bool basic_partition_map::operator==(const basic_partition_map &other) const {
|
||||
if (this->_capacity != other._capacity
|
||||
|| this->_count != other._count)
|
||||
return false;
|
||||
for (size_t i = 0; i < _count; i++)
|
||||
if (_data[i].name == other._data[i].name &&
|
||||
_data[i].props.size == other._data[i].props.size &&
|
||||
_data[i].props.isLogical == other._data[i].props.isLogical)
|
||||
continue;
|
||||
else return false;
|
||||
|
||||
for (size_t i = 0; i < _count; i++)
|
||||
if (_data[i].name == other._data[i].name
|
||||
&& _data[i].props.size == other._data[i].props.size
|
||||
&& _data[i].props.isLogical == other._data[i].props.isLogical)
|
||||
continue;
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool basic_partition_map::operator!=(const basic_partition_map &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool basic_partition_map::operator!=(const basic_partition_map &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
basic_partition_map::iterator basic_partition_map::begin() const {
|
||||
return iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::begin() const {
|
||||
return iterator(_data);
|
||||
}
|
||||
basic_partition_map::iterator basic_partition_map::end() const {
|
||||
return iterator(_data + _count);
|
||||
}
|
||||
|
||||
basic_partition_map::iterator basic_partition_map::end() const {
|
||||
return iterator(_data + _count);
|
||||
}
|
||||
basic_partition_map::constant_iterator basic_partition_map::cbegin() const {
|
||||
return constant_iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::cbegin() const {
|
||||
return constant_iterator(_data);
|
||||
}
|
||||
|
||||
basic_partition_map::constant_iterator basic_partition_map::cend() const {
|
||||
return constant_iterator(_data + _count);
|
||||
}
|
||||
basic_partition_map::constant_iterator basic_partition_map::cend() const {
|
||||
return constant_iterator(_data + _count);
|
||||
}
|
||||
} // namespace PartitionMap
|
||||
|
||||
100
srclib/libpartition_map/tests/test.cpp
Executable file → Normal file
100
srclib/libpartition_map/tests/test.cpp
Executable file → Normal file
@@ -15,66 +15,70 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
if (getuid() != 0) return 2;
|
||||
if (getuid() != 0) return 2;
|
||||
|
||||
try {
|
||||
PartitionMap::BuildMap MyMap;
|
||||
if (!MyMap) {
|
||||
MyMap.readDirectory("/dev/block/by-name");
|
||||
if (!MyMap) throw PartitionMap::Error("Cannot generate object!");
|
||||
}
|
||||
try {
|
||||
PartitionMap::BuildMap MyMap;
|
||||
if (!MyMap) {
|
||||
MyMap.readDirectory("/dev/block/by-name");
|
||||
if (!MyMap) throw PartitionMap::Error("Cannot generate object!");
|
||||
}
|
||||
|
||||
const auto map = MyMap.getAll();
|
||||
if (map.empty()) throw PartitionMap::Error("getAll() empty");
|
||||
for (const auto& [name, props] : map) {
|
||||
std::cout << "Partition: " << name << ", size: "
|
||||
<< props.size << ", logical: "
|
||||
<< props.isLogical << std::endl;
|
||||
}
|
||||
const auto map = MyMap.getAll();
|
||||
if (map.empty()) throw PartitionMap::Error("getAll() empty");
|
||||
for (const auto &[name, props] : map) {
|
||||
std::cout << "Partition: " << name << ", size: " << props.size
|
||||
<< ", logical: " << props.isLogical << std::endl;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
const auto physicals = MyMap.getPhysicalPartitionList();
|
||||
if (!physicals) throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
|
||||
std::cout << "Physical partitions: " << std::endl;
|
||||
for (const auto& name : *physicals)
|
||||
std::cout << " - " << name << std::endl;
|
||||
const auto physicals = MyMap.getPhysicalPartitionList();
|
||||
if (!physicals)
|
||||
throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
|
||||
std::cout << "Physical partitions: " << std::endl;
|
||||
for (const auto &name : *physicals)
|
||||
std::cout << " - " << name << std::endl;
|
||||
|
||||
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
|
||||
std::cout << "Boot (realpath): " << MyMap.getRealPathOf("boot") << std::endl;
|
||||
std::cout << "Search dir: " << MyMap.getCurrentWorkDir() << std::endl;
|
||||
std::cout << "Has partition cache? = " << MyMap.hasPartition("cache") << std::endl;
|
||||
std::cout << "system partition is logical? = " << MyMap.isLogical("system") << std::endl;
|
||||
std::cout << "Size of system partition: " << MyMap.sizeOf("system") << std::endl;
|
||||
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
|
||||
std::cout << "Boot (realpath): " << MyMap.getRealPathOf("boot")
|
||||
<< std::endl;
|
||||
std::cout << "Search dir: " << MyMap.getCurrentWorkDir() << std::endl;
|
||||
std::cout << "Has partition cache? = " << MyMap.hasPartition("cache")
|
||||
<< std::endl;
|
||||
std::cout << "system partition is logical? = " << MyMap.isLogical("system")
|
||||
<< std::endl;
|
||||
std::cout << "Size of system partition: " << MyMap.sizeOf("system")
|
||||
<< std::endl;
|
||||
|
||||
MyMap.clear();
|
||||
if (!MyMap.empty()) throw PartitionMap::Error("map cleaned but check fail");
|
||||
MyMap.clear();
|
||||
if (!MyMap.empty()) throw PartitionMap::Error("map cleaned but check fail");
|
||||
|
||||
MyMap.readDirectory("/dev/block/by-name");
|
||||
PartitionMap::BuildMap MyMap2;
|
||||
MyMap.readDirectory("/dev/block/by-name");
|
||||
PartitionMap::BuildMap MyMap2;
|
||||
|
||||
if (MyMap == MyMap2) std::cout << "map1 = map2" << std::endl;
|
||||
if (MyMap != MyMap2) std::cout << "map1 != map2" << std::endl;
|
||||
if (MyMap == MyMap2) std::cout << "map1 = map2" << std::endl;
|
||||
if (MyMap != MyMap2) std::cout << "map1 != map2" << std::endl;
|
||||
|
||||
std::cout << PartitionMap::getLibVersion() << std::endl;
|
||||
} catch (PartitionMap::Error& error) {
|
||||
std::cerr << error.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << PartitionMap::getLibVersion() << std::endl;
|
||||
} catch (PartitionMap::Error &error) {
|
||||
std::cerr << error.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user