-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.cpp
165 lines (142 loc) · 5.02 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <chrono>
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>
#include <limits.h>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/sha.h>
#include <iomanip>
#include "ansi_color.h"
#include "util.h"
// 경로 유효성 검사 함수
bool IsDirectory(const std::string& path) {
struct stat info;
if (stat(path.c_str(), &info) != 0) {
return false;
}
return (info.st_mode & S_IFDIR) != 0;
}
void PrintError(const std::string& message) {
std::cerr << "\n" << COLOR_RED << message << COLOR_RESET << "\n";
}
void PrintInputError(std::string& error){
std::cerr << "Invalid Input : " << COLOR_RED << "\""<< error << "\"" << COLOR_RESET << std::endl;
}
void PrintErrorMessage(int code, const std::string& context) {
if (code != SUCCESS_CODE) {
std::cerr << "\n" << COLOR_RED << "[Error] " << GetErrorMessage(code) << COLOR_RESET;
if (!context.empty()) {
std::cerr << "\n" << COLOR_RED << ": " << context << COLOR_RESET;
}
std::cerr << "\n";
}
}
// 에러 처리를 담당하는 함수
void HandleError(int code, const std::string& context) {
PrintErrorMessage(code, context);
exit(code);
}
// 특정 확장자 파일 필터 함수
bool IsExtension(const std::string& filePath, const std::string& extension) {
if (filePath.length() >= extension.length() &&
filePath.substr(filePath.length() - extension.length()) == extension) {
return true;
}
return false;
}
// ELF 파일 필터 함수
bool IsELFFile(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (file) {
char magic[4];
file.read(magic, 4);
return (magic[0] == 0x7f && magic[1] == 0x45 && magic[2] == 0x4c && magic[3] == 0x46);
}
return false;
}
// SHA256 해시알고리즘을 사용해서 파일의 해시값을 계산
int ComputeSHA256(const std::string& fileName, std::string& fileHash) {
std::ifstream file(fileName, std::ifstream::binary);
if (!file) {
return ERROR_CANNOT_OPEN_FILE;
}
SHA256_CTX sha256;
SHA256_Init(&sha256);
char buffer[BUFFER_SIZE];
while (file.read(buffer, sizeof(buffer))) {
SHA256_Update(&sha256, buffer, file.gcount());
}
SHA256_Update(&sha256, buffer, file.gcount());
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_Final(hash, &sha256);
std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
fileHash = ss.str();
return SUCCESS_CODE;
}
std::time_t GetCurrentTime() {
auto now = std::chrono::system_clock::now();
auto currentTime = std::chrono::system_clock::to_time_t(now);
return currentTime;
}
std::string GetCurrentTimeWithMilliseconds() {
auto currentTime = GetCurrentTime();
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch()) % 1000;
std::stringstream timeStream;
timeStream << std::put_time(std::localtime(¤tTime), "%Y-%m-%d %H:%M:%S");
timeStream << '.' << std::setfill('0') << std::setw(3) << milliseconds.count();
return timeStream.str();
}
// 문자열의 앞뒤 공백 제거 함수
std::string Trim(const std::string& str) {
size_t start = str.find_first_not_of(" \t\r\n");
size_t end = str.find_last_not_of(" \t\r\n");
return (start == std::string::npos) ? "" : str.substr(start, end - start + 1);
}
std::string GetAbsolutePath(std::string path) {
char absolutePath[PATH_MAX];
if (realpath(path.c_str(), absolutePath) != nullptr) {
return absolutePath;
} else {
return path;
}
}
void SaveLogInJson(Json::Value logEntry, std::string logFilePath) {
Json::StreamWriterBuilder writer;
// 수정 및 추가: 기존 로그 파일을 읽고 JSON 배열로 변환하는 부분
std::ifstream logFileIn(logFilePath);
std::vector<Json::Value> logEntries;
if (logFileIn.is_open()) {
Json::CharReaderBuilder reader;
Json::Value existingLog;
std::string errs;
if (Json::parseFromStream(reader, logFileIn, &existingLog, &errs)) {
if (existingLog.isArray()) {
for (const auto &entry : existingLog) {
logEntries.push_back(entry);
}
}
}
logFileIn.close();
}
logEntries.push_back(logEntry);
// 수정 및 추가: JSON 배열 형식으로 로그 파일에 저장하는 부분
std::ofstream logFileOut(logFilePath, std::ios::out);
if (!logFileOut.is_open()) {
HandleError(ERROR_CANNOT_OPEN_FILE, logFilePath);
}
logFileOut << "[\n";
for (size_t i = 0; i < logEntries.size(); ++i) {
logFileOut << Json::writeString(writer, logEntries[i]);
if (i != logEntries.size() - 1) {
logFileOut << ","; // 수정: 각 JSON 객체 사이에 쉼표 추가
}
logFileOut << "\n";
}
logFileOut << "]";
logFileOut.close();
}