Repository navigation
Expand file tree
/
Copy pathbookmark_persistence.cpp
More file actions
513 lines (411 loc) · 12.2 KB
/
Copy pathbookmark_persistence.cpp
File metadata and controls
513 lines (411 loc) · 12.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "stdafx.h"
#include <string>
#include <fcntl.h>
#include <share.h>
#include <io.h>
#include "jansson.h"
#include "bookmark_persistence.h"
#include "bookmark_preferences.h"
bookmark_persistence::bookmark_persistence() {
//..
}
bookmark_persistence::~bookmark_persistence()
{
//..
}
//replaces the contents of masterList with the contents of the persistent file
void bookmark_persistence::replaceMasterList(std::vector<bookmark_t>& newContent, std::vector<bookmark_t>& masterList, bool ordered, std::function<void()> p_callback) {
FB2K_console_print_v("Checking pre-load items");
fb2k::splitTask([&masterList, newContent, ordered, p_callback]() {
size_t c = 0;
while (c < 10) {
try {
{
pfc::string8 owner;
std::lock_guard<std::mutex> guard(bookmark_store::get_lock("ReplaceMasterList", owner));
//remove loading...
if (masterList.size()) {
if (masterList.at(0).get_desc().equals(kLoading)) {
masterList.erase(masterList.begin());
}
}
if (!ordered) {
masterList.insert(masterList.end(), newContent.begin(), newContent.end());
}
else {
masterList.insert(masterList.begin(), newContent.begin(), newContent.end());
}
}
p_callback();
break;
}
catch (...) {
Sleep(1000);
c++;
}
}
});
}
std::filesystem::path bookmark_persistence::genFilePath() {
pfc::string8 n8_path = core_api::pathInProfile("configuration");
extract_native_path(n8_path, n8_path);
std::filesystem::path os_dst = std::filesystem::u8path(n8_path.c_str());
pfc::string8 filename = core_api::get_my_file_name();
filename << ".dll.dat";
os_dst.append(filename.c_str());
return os_dst;
}
void add_rec(std::vector<json_t*> &vjson, const std::vector<pfc::string8>& vlbl, const bookmark_t & rec) {
const int nfields = static_cast<int>(vlbl.size());
//bookmark_t to vrec
std::vector<pfc::string8> vrec = {
pfc::print_guid(rec.guid_bm).get_ptr(), //guid_bm
std::to_string(rec.get_time()).c_str(), //time
rec.get_desc().get_ptr(), //desc
rec.get_name(false).get_ptr(), //name
rec.playlist.get_ptr(), //playlist
pfc::print_guid(rec.guid_playlist).get_ptr(), //guid
rec.path.get_ptr(), //path
std::to_string(rec.subsong).c_str(), //subsong
rec.get_comment().get_ptr(), //comment
rec.date.get_ptr() //date
};
vjson.emplace_back(json_object());
auto nobj = vjson.size() - 1;
for (auto wfield = 0; wfield < nfields; wfield++) {
if (vlbl[wfield].equals("name")) {
if (!vrec[wfield].get_length()) {
continue;
}
}
int res = json_object_set_new_nocheck(vjson[nobj], vlbl[wfield], /*vjs_val[i]*/json_string_nocheck(vrec[wfield].get_ptr()));
}
}
void bookmark_persistence::writeDataFile(const std::vector<bookmark_t>& masterList,
std::function<void()> sf_write_callback) {
cmdThFile.add([this, &masterList, sf_write_callback] {
bool res = writeDataFileJSON(masterList);
if (res) sf_write_callback();
});
}
//save masterList to persistent storage
bool bookmark_persistence::writeDataFileJSON(const std::vector<bookmark_t>& masterList) {
bool bres = false;
if (core_api::is_quiet_mode_enabled()) {
FB2K_console_print_e("Quiet mode, will not write bookmarks to file");
return false;
}
if (!masterList.size()) {
std::error_code ec;
std::filesystem::path os_file = genFilePath();
if (std::filesystem::exists(os_file)) {
std::filesystem::remove(os_file, ec);
}
return ec.value() == 0;
}
std::filesystem::path os_file = genFilePath();
std::error_code ec;
std::filesystem::path os_file_tmp = (PFC_string_formatter() << os_file.c_str() << "_tmp").c_str();
if (std::filesystem::exists(os_file)) {
std::filesystem::copy_file(os_file, os_file_tmp,std::filesystem::copy_options::overwrite_existing, ec);
}
int jf = -1;
int resdump = 0;
try {
FB2K_console_print_v("Write data file...");
size_t n_entries = masterList.size();
std::vector<json_t*> vjson;
std::vector<pfc::string8> vlbl = { "guid_bm", "time", "desc", "name", "playlist", "guid", "path", "subsong", "comment", "date" };
//first pass
for (size_t i = 0; i < n_entries; i++) {
//add boolmark_t to vjson as json object
add_rec(vjson, vlbl, masterList[i]);
}
//second pass, could have be done in first pass
json_t* arr_top = json_array();
for (auto wobj : vjson) {
auto res = json_array_append(arr_top, wobj);
}
jf = _wopen(os_file.wstring().c_str(), _O_CREAT | _O_TRUNC | _O_WRONLY| _O_TEXT/*_O_U8TEXT*/, _S_IWRITE);
if (jf == -1) {
foobar2000_io::exception_io e("Open failed on output file");
throw e;
}
resdump = json_dumpfd(arr_top, jf, JSON_INDENT(5));
_close(jf);
bres = true;
for (auto w : vjson) {
free(w);
}
if (resdump == -1) {
bres = false;
exception_io e;
throw e;
}
FB2K_console_print_v("Wrote ", std::to_string(n_entries).c_str(), " bookmarks to file");
if (std::filesystem::exists(os_file_tmp)) {
std::filesystem::remove(os_file_tmp);
}
}
catch (foobar2000_io::exception_io e) {
if (jf != -1) {
_close(jf);
}
if (resdump == -1) {
FB2K_console_print_e("JSON Exception: could not write bookmarks to file", e);
}
else {
FB2K_console_print_e("Could not write bookmarks to file", e);
}
if (std::filesystem::exists(os_file_tmp)) {
if (std::filesystem::exists(os_file)) {
std::filesystem::remove(os_file);
}
std::filesystem::rename(os_file_tmp, os_file);
}
}
catch (...) {
if (jf != -1) {
_close(jf);
}
FB2K_console_print_e("Could not write bookmarks to file", "Unhandled Exception");
}
return bres;
}
//parse cancellation v1.8.2
//todo: update json data format
#define PARSE_CANCEL
bool bookmark_persistence::readDataFileJSON(std::vector<bookmark_t>& masterList, bool ordered, std::function<void()> p_callback, abort_callback& p_abort) {
bool bres = true;
FB2K_console_print_v("Reading bookmarks from file");
size_t clines = 0;
std::vector<bookmark_t> temp_data;
int jf = -1;
try {
//not thread safe
setlocale(LC_ALL, ".UTF8");
//
std::filesystem::path os_file = genFilePath();
jf = _wopen(os_file.wstring().c_str(), _O_RDONLY);
if (jf == -1) {
foobar2000_io::exception_io e("Open failed on input file");
throw e;
}
json_error_t error;
#ifdef PARSE_CANCEL
uint8_t c;
int cr = 0;
do {
cr = _read(jf, &c, 1);
} while (c == '\n');
if (cr != 1 && c != '[') {
throw foobar2000_io::exception_io();
}
auto json = json_loadfd(jf, JSON_DECODE_ANY | JSON_DISABLE_EOF_CHECK, &error);
#else
auto json = json_loadfd(jf, JSON_DECODE_ANY, &error);
_close(jf);
#endif
if (strlen(error.text) && error.line != -1) {
FB2K_console_print_v("JSON error: ",error.text,
" in line: ", error.line,
", column: ", error.column,
", position: ", error.position,
", src: ",error.source);
try {
if (std::filesystem::file_size(os_file.c_str())) {
FB2K_console_print_v("JSON error: Creating backup file...");
pfc::string8 base_bak;
pfc::string8 date_bak;
currentFmtDate(date_bak, "%y-%m-%d_%Hh_%Mm_%Ss");
base_bak << os_file.c_str() << date_bak << ".bak";
size_t posfix = 0;
pfc::string8 buffer_bak = base_bak;
while (std::filesystem::exists(buffer_bak.c_str())) {
++posfix;
buffer_bak = base_bak << posfix;
}
std::filesystem::copy(os_file, buffer_bak.c_str(), std::filesystem::copy_options::none);
FB2K_console_print_v(pfc::string_formatter() << "JSON error: Backup file " << buffer_bak <<" created.");
}
}
catch (std::filesystem::filesystem_error const& ex) {
FB2K_console_print_v("Error: Creating backup file. ", ex.what());
}
//free(error);
}
json_t* js_wobj;
bookmark_t elem = bookmark_t();
#ifdef PARSE_CANCEL
while (true) {
js_wobj = json;
if (p_abort.is_aborting()) {
_close(jf);
return false;
}
#else
size_t index;
clines = json_array_size(json);
json_array_foreach(json, index, js_wobj) {
#endif
if (!json_is_object(js_wobj)) {
break;
}
json_t* js_fld;
{
elem.guid_playlist = pfc::guid_null;
js_fld = json_object_get(js_wobj, "guid_bm");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.guid_bm = pfc::GUID_from_text(dmp_str);
}
else {
elem.guid_bm = pfc::createGUID();
}
}
{
elem.set_time(0);
js_fld = json_object_get(js_wobj, "time");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.set_time(atof(dmp_str));
}
}
{
elem.guid_playlist = pfc::guid_null;
js_fld = json_object_get(js_wobj, "guid");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.guid_playlist = pfc::GUID_from_text(dmp_str);
}
}
{
elem.set_desc("");
js_fld = json_object_get(js_wobj, "desc");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.set_desc(pfc::string8(dmp_str));
}
}
{
elem.set_name("");
js_fld = json_object_get(js_wobj, "name");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.set_name(dmp_str);
}
}
{
elem.path = "";
js_fld = json_object_get(js_wobj, "path");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.path = pfc::string8(dmp_str);
}
}
{
elem.subsong = 0;
js_fld = json_object_get(js_wobj, "subsong");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.subsong = static_cast<t_uint32>(atoi(dmp_str));
}
}
{
elem.set_comment("");
js_fld = json_object_get(js_wobj, "comment");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.set_comment(pfc::string8(dmp_str));
}
}
{
elem.date = "";
js_fld = json_object_get(js_wobj, "date");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str && strlen(dmp_str) > 20) {
time_t rawtime;
struct tm timeinfo = { 0 };
unix_str_date_to_time(dmp_str, rawtime, timeinfo);
elem.date = pfc::string8(dmp_str);
char buffer[DATE_BUFFER_SIZE];
// Format: Mo, 15.06.2009 20:20:00
std::tm ptm;
localtime_s(&ptm, &rawtime);
std::strftime(buffer, DATE_BUFFER_SIZE, cfg_date_format.get_value(), &ptm);
elem.runtime_date = buffer;
}
}
{
elem.playlist = "";
js_fld = json_object_get(js_wobj, "playlist");
const char* dmp_str = json_string_value(js_fld);
if (dmp_str) {
elem.playlist = pfc::string8(dmp_str);
}
}
// check playlist name
pfc::string8 playlist_name_from_guid;
size_t ndx = playlist_manager_v5::get()->find_playlist_by_guid(elem.guid_playlist);
if (ndx != pfc_infinite) {
playlist_manager_v5::get()->playlist_get_name(ndx, playlist_name_from_guid);
}
if (playlist_name_from_guid.get_length()) {
//replace old playlist name
if (!elem.playlist.equals(playlist_name_from_guid)) {
elem.playlist = playlist_name_from_guid;
}
}
//recover non-subsong tracks from swap subsong bug (v1.3.1)
std::filesystem::path p(elem.path.c_str());
auto ext = p.extension().string();
if (p.extension().string().compare(".cue")) {
//not cue
if (elem.subsong) {
elem.subsong = 0;
}
}
else {
//cue
if (!elem.subsong) {
//not recoverable, default to 1
elem.subsong = 1;
}
}
temp_data.push_back(elem); //save to vector
#ifdef PARSE_CANCEL
do {
cr = _read(jf, &c, 1);
} while (c == '\n');
if (cr != 1) {
throw foobar2000_io::exception_io();
}
else if (c == ',') {
//..
}
else if (c == ']') {
_close(jf);
break;
}
json = json_loadfd(jf, JSON_DECODE_ANY | JSON_DISABLE_EOF_CHECK, &error);
#endif
}
}
catch (foobar2000_io::exception_io e) {
if (jf != -1) {
_close(jf);
}
FB2K_console_print_e("Reading data from file failed: ", e);
bres = false;
}
catch (...) {
if (jf != -1) {
_close(jf);
}
FB2K_console_print_e("Reading data from file failed: ", "Unhandled Exception");
bres = false;
}
FB2K_console_print_v("Restored ", std::to_string(clines).c_str(), " bookmarks from file");
replaceMasterList(temp_data, masterList, ordered, p_callback);
return bres;
}