/
cap.cpp
365 lines (306 loc) · 11 KB
/
cap.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Windows Tools
#include "stdafx.h"
#include "cap.h"
#include <cstdint>
#include <filesystem>
#include <string>
#include <unordered_set>
#include "cfg.h"
#include "cvt.h"
#include "logger.h"
#include "tools/_raii.h"
#include "tools/_xlog.h"
#include "upgrade.h"
namespace cma::cfg::cap {
// calculate valid path and create folder
// returns path
std::wstring ProcessPluginPath(const std::string &File) {
namespace fs = std::filesystem;
// Extract basename and dirname from path
fs::path fpath = File;
fs::path plugin_folder = cma::cfg::GetUserDir();
plugin_folder /= fpath;
return plugin_folder.lexically_normal().wstring();
}
// -1 means FAILURE
// 0 means end of file
// all other name should be read
uint32_t ReadFileNameLength(std::ifstream &CapFile) {
uint8_t length = 0;
CapFile.read(reinterpret_cast<char *>(&length), sizeof(length));
if (CapFile.good()) return length;
if (CapFile.eof()) {
XLOG::l.t("End of CAP-file. OK!");
return 0;
}
XLOG::l("Unexpected problems with CAP-file name header");
return -1;
}
// File format
// [BYTE][variable][INT32][variable]
std::string ReadFileName(std::ifstream &CapFile, uint32_t Length) {
size_t buffer_length = Length + 1;
std::vector<char> dataBuffer(buffer_length, 0);
CapFile.read(dataBuffer.data(), Length);
if (!CapFile.good()) {
XLOG::l("Unexpected problems with CAP-file name body");
return {};
}
dataBuffer[Length] = '\0';
XLOG::d.t("Processing file '{}'", dataBuffer.data());
return std::string(dataBuffer.data());
}
// must be successful!
std::vector<char> ReadFileData(std::ifstream &CapFile) {
// read 32-bit length
int32_t length = 0;
CapFile.read(reinterpret_cast<char *>(&length), sizeof(length));
if (!CapFile.good()) {
XLOG::l("Unexpected problems with CAP-file data header");
return {};
}
XLOG::d.t("Processing {} bytes of data", length);
if (length > 20 * 1024 * 1024) {
XLOG::l.crit("Size of data is too big {} ", length);
return {};
}
// read content
size_t buffer_length = length;
std::vector<char> dataBuffer(buffer_length, 0);
CapFile.read(dataBuffer.data(), length);
if (!CapFile.good()) {
XLOG::l("Unexpected problems with CAP-file adat body");
return {};
}
return dataBuffer;
}
// reads name and data
// writes file
// if problems or end return false
FileInfo ExtractFile(std::ifstream &CapFile) {
// Read Filename
auto l = ReadFileNameLength(CapFile);
if (l == 0) {
XLOG::l.t("File CAP end!");
return {{}, {}, true};
}
if (l > 256) return {{}, {}, false};
const auto name = ReadFileName(CapFile, l);
if (name.empty() || !CapFile.good()) {
if (CapFile.eof()) return {{}, {}, false};
XLOG::l.crit("Invalid cap file, [name]");
return {{}, {}, false};
}
const auto content = ReadFileData(CapFile);
if (content.empty() || !CapFile.good()) {
XLOG::l.crit("Invalid cap file, [name] {}", name);
return {{}, {}, false};
}
return {name, content, false};
}
// may create dirs too
// may create empty file
bool StoreFile(const std::wstring &Name, const std::vector<char> &Data) {
namespace fs = std::filesystem;
fs::path fpath = Name;
std::error_code ec;
if (!fs::create_directories(fpath.parent_path(), ec) && ec.value() != 0) {
XLOG::l.crit("Cannot create path to '{}', status = {}",
fpath.parent_path().u8string(), ec.value());
return false;
}
// Write plugin
try {
std::ofstream ofs(Name, std::ios::binary | std::ios::trunc);
if (ofs.good()) {
ofs.write(Data.data(), Data.size());
return true;
}
} catch (const std::exception &e) {
XLOG::l("Exception on create/write file '{}', '{}'", fpath.u8string(),
e.what());
}
XLOG::l.crit("Cannot create file to '{}', status = {}", fpath.u8string(),
GetLastError());
return false;
}
bool CheckAllFilesWritable(const std::string &Directory) {
namespace fs = std::filesystem;
bool all_writable = true;
for (auto &p : fs::recursive_directory_iterator(Directory)) {
std::error_code ec;
auto path = p.path();
if (fs::is_directory(path, ec)) continue;
if (!fs::is_regular_file(path, ec)) continue;
auto path_string = path.wstring();
if (path_string.empty()) continue;
auto handle = ::CreateFile(path_string.c_str(), // file to open
GENERIC_WRITE, // open for write
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, // default security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, // normal file
nullptr);
if (handle && handle != INVALID_HANDLE_VALUE) {
::CloseHandle(handle);
} else {
XLOG::d("file '{}' is not writable, error {}", path.u8string(),
GetLastError());
all_writable = false;
break;
}
}
return all_writable;
}
bool Process(const std::string CapFileName, ProcMode Mode,
std::vector<std::wstring> &FilesLeftOnDisk) {
namespace fs = std::filesystem;
std::ifstream ifs(CapFileName, std::ifstream::in | std::ifstream::binary);
if (!ifs) {
XLOG::l.crit("Unable to open Check_MK-Agent package {} ", CapFileName);
return false;
}
while (!ifs.eof()) {
auto [name, data, eof] = ExtractFile(ifs);
if (eof) return true;
if (name.empty()) {
XLOG::l("CAP file {} looks as bad", CapFileName);
return false;
}
if (data.empty()) {
XLOG::l("CAP file {} looks as bad for file {}", CapFileName, name);
return false;
}
const auto full_path = ProcessPluginPath(name);
if (Mode == ProcMode::install) {
StoreFile(full_path, data);
std::error_code ec;
if (fs::exists(full_path, ec)) FilesLeftOnDisk.push_back(full_path);
} else if ((Mode == ProcMode::remove)) {
std::error_code ec;
if (fs::remove(full_path, ec))
FilesLeftOnDisk.push_back(full_path);
else {
XLOG::l("Cannot remove '{}' error {}",
wtools::ConvertToUTF8(full_path), ec.value());
}
} else if ((Mode == ProcMode::list)) {
FilesLeftOnDisk.push_back(full_path);
}
}
// CheckAllFilesWritable(wtools::ConvertToUTF8(cma::cfg::GetUserPluginsDir()));
// CheckAllFilesWritable(wtools::ConvertToUTF8(cma::cfg::GetLocalDir()));
XLOG::l("CAP file {} looks as bad with unexpected eof", CapFileName);
return false;
}
bool NeedReinstall(const std::filesystem::path Target,
const std::filesystem::path Src) {
namespace fs = std::filesystem;
std::error_code ec;
if (!fs::exists(Src, ec)) {
XLOG::d.w("Source File '{}' is absent, reinstall not possible",
Src.u8string());
return false;
}
if (!fs::exists(Target, ec)) {
XLOG::d.i("Target File '{}' is absent, reinstall is mandatory",
Target.u8string());
return true;
}
// now both file are present
auto target_time = fs::last_write_time(Target, ec);
auto src_time = fs::last_write_time(Src, ec);
return src_time > target_time;
}
// returns true when changes had been done
bool ReinstallCaps(const std::filesystem::path TargetCap,
const std::filesystem::path SourceCap) {
bool changed = false;
namespace fs = std::filesystem;
std::error_code ec;
std::vector<std::wstring> files_left;
if (fs::exists(TargetCap, ec)) {
if (true ==
Process(TargetCap.u8string(), ProcMode::remove, files_left)) {
XLOG::l.t("File '{}' uninstall-ed", TargetCap.u8string());
fs::remove(TargetCap, ec);
for (auto &name : files_left)
XLOG::l.i("\tRemoved '{}'", wtools::ConvertToUTF8(name));
changed = true;
}
} else
XLOG::l.t("File '{}' is absent, skipping uninstall",
TargetCap.u8string());
files_left.clear();
if (fs::exists(SourceCap, ec)) {
if (true ==
Process(SourceCap.u8string(), ProcMode::install, files_left)) {
XLOG::l.t("File {} installed", SourceCap.u8string());
fs::copy_file(SourceCap, TargetCap, ec);
for (auto &name : files_left)
XLOG::l.i("\tAdded '{}'", wtools::ConvertToUTF8(name));
changed = true;
}
} else
XLOG::l.t("File {} is absent, skipping install", SourceCap.u8string());
return changed;
}
bool ReinstallIni(const std::filesystem::path TargetIni,
const std::filesystem::path SourceIni) {
namespace fs = std::filesystem;
std::error_code ec;
// remove old files
auto bakery_yml = cma::cfg::GetBakeryFile();
fs::remove(bakery_yml, ec);
fs::remove(TargetIni, ec);
// generate new
if (fs::exists(SourceIni)) {
cma::cfg::cvt::Parser p;
p.prepare();
p.readIni(SourceIni.u8string(), false);
auto yaml = p.emitYaml();
std::ofstream ofs(bakery_yml, std::ios::binary);
if (ofs) {
ofs << cma::cfg::upgrade::MakeComments(SourceIni, true);
ofs << yaml;
}
ofs.close();
fs::copy_file(SourceIni, TargetIni, ec);
}
return true;
}
void Install() {
using namespace cma::cfg;
using namespace cma::cfg::cap;
namespace fs = std::filesystem;
fs::path target_cap = cma::cfg::GetUserDir();
target_cap /= dirs::kCapInstallDir;
target_cap /= files::kCapFile;
fs::path source_cap = cma::cfg::GetRootDir();
source_cap /= dirs::kCapInstallDir;
source_cap /= files::kCapFile;
XLOG::l.t("Installing cap file '{}'", source_cap.u8string());
if (NeedReinstall(target_cap, source_cap)) {
XLOG::l.i("Reinstalling '{}' with '{}'", target_cap.u8string(),
source_cap.u8string());
ReinstallCaps(target_cap, source_cap);
} else
XLOG::l.t(
"Installing of CAP file is not required, the file is already installed");
fs::path target_ini = cma::cfg::GetUserDir();
target_ini /= dirs::kCapInstallDir;
target_ini /= files::kIniFile;
fs::path source_ini = cma::cfg::GetRootDir();
source_ini /= dirs::kCapInstallDir;
source_ini /= files::kIniFile;
std::error_code ec;
XLOG::l.t("Installing ini file '{}'", source_ini.u8string());
if (NeedReinstall(target_ini, source_ini)) {
XLOG::l.i("Reinstalling '{}' with '{}'", target_ini.u8string(),
source_ini.u8string());
ReinstallIni(target_ini, source_ini);
} else
XLOG::l.t(
"Installing of INI file is not required, the file is already installed");
}
} // namespace cma::cfg::cap