-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcore_backup.c
685 lines (546 loc) · 18.5 KB
/
core_backup.c
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2014-2017 - Jean-André Santoni
* Copyright (C) 2016-2019 - Brad Parker
* Copyright (C) 2019-2020 - James Leaver
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string/stdstring.h>
#include <lists/string_list.h>
#include <file/file_path.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <lists/dir_list.h>
#include <time/rtime.h>
#include <retro_miscellaneous.h>
#include "frontend/frontend_driver.h"
#include "file_path_special.h"
#include "verbosity.h"
#include "core_backup.h"
/* Holds all entries in a core backup list */
struct core_backup_list
{
core_backup_list_entry_t *entries;
size_t size;
size_t capacity;
};
/*********************/
/* Utility Functions */
/*********************/
/* Generates backup directory path for specified core.
* Returns false if 'core_path' and/or 'dir_core_assets'
* are invalid, or a filesystem error occurs */
static bool core_backup_get_backup_dir(
const char *dir_libretro, const char *dir_core_assets,
const char *core_filename,
char *backup_dir, size_t len)
{
char tmp[PATH_MAX_LENGTH];
char core_file_id[NAME_MAX_LENGTH];
char *last_underscore = NULL;
/* Extract core file 'ID' (name without extension + suffix)
* from core path */
if ( string_is_empty(dir_libretro)
|| string_is_empty(core_filename)
|| (len < 1))
return false;
fill_pathname(core_file_id, core_filename, "",
sizeof(core_file_id));
if (string_is_empty(core_file_id))
return false;
/* > Remove platform-specific file name suffix,
* if required */
last_underscore = strrchr(core_file_id, '_');
if (!string_is_empty(last_underscore))
if (!string_is_equal(last_underscore, "_libretro"))
*last_underscore = '\0';
if (string_is_empty(core_file_id))
return false;
/* Get core backup directory
* > If no assets directory is defined, use
* core directory as a base */
fill_pathname_join_special(tmp,
string_is_empty(dir_core_assets)
? dir_libretro
: dir_core_assets,
"core_backups", sizeof(tmp));
fill_pathname_join_special(backup_dir, tmp,
core_file_id, len);
if (string_is_empty(backup_dir))
return false;
/* > Create directory, if required */
if (!path_is_directory(backup_dir))
{
if (!path_mkdir(backup_dir))
{
RARCH_ERR("[core backup] Failed to create backup directory: %s.\n", backup_dir);
return false;
}
}
return true;
}
/* Generates a timestamped core backup file path from
* the specified core path. Returns true if successful */
bool core_backup_get_backup_path(
const char *core_path, uint32_t crc,
enum core_backup_mode backup_mode,
const char *dir_core_assets,
char *backup_path, size_t len)
{
time_t current_time;
struct tm time_info;
const char *core_filename = NULL;
char core_dir[DIR_MAX_LENGTH];
char backup_dir[DIR_MAX_LENGTH];
char backup_filename[PATH_MAX_LENGTH];
backup_dir[0] = '\0';
backup_filename[0] = '\0';
/* Get core filename and parent directory */
if (string_is_empty(core_path))
return false;
core_filename = path_basename(core_path);
if (string_is_empty(core_filename))
return false;
fill_pathname_parent_dir(core_dir, core_path, sizeof(core_dir));
if (string_is_empty(core_dir))
return false;
/* Get backup directory */
if (!core_backup_get_backup_dir(core_dir, dir_core_assets, core_filename,
backup_dir, sizeof(backup_dir)))
return false;
/* Get current time */
time(¤t_time);
rtime_localtime(¤t_time, &time_info);
/* Generate backup filename */
snprintf(backup_filename, sizeof(backup_filename),
"%s.%04u%02u%02uT%02u%02u%02u.%08lx.%u%s",
core_filename,
(unsigned)time_info.tm_year + 1900,
(unsigned)time_info.tm_mon + 1,
(unsigned)time_info.tm_mday,
(unsigned)time_info.tm_hour,
(unsigned)time_info.tm_min,
(unsigned)time_info.tm_sec,
(unsigned long)crc,
(unsigned)backup_mode,
FILE_PATH_CORE_BACKUP_EXTENSION);
/* Build final path */
fill_pathname_join_special(backup_path, backup_dir,
backup_filename, len);
return true;
}
/* Returns detected type of specified core backup file */
enum core_backup_type core_backup_get_backup_type(const char *backup_path)
{
char core_ext[16];
const char *backup_ext = NULL;
struct string_list *metadata_list = NULL;
if (string_is_empty(backup_path) || !path_is_valid(backup_path))
goto error;
/* Get backup file extension */
backup_ext = path_get_extension(backup_path);
if (string_is_empty(backup_ext))
goto error;
/* Get platform-specific dynamic library extension */
if (!frontend_driver_get_core_extension(core_ext, sizeof(core_ext)))
goto error;
/* Check if this is an archived backup */
if (string_is_equal_noncase(backup_ext,
FILE_PATH_CORE_BACKUP_EXTENSION_NO_DOT))
{
const char *backup_filename = NULL;
const char *src_ext = NULL;
/* Split the backup filename into its various
* metadata components */
backup_filename = path_basename(backup_path);
if (string_is_empty(backup_filename))
goto error;
metadata_list = string_split(backup_filename, ".");
if (!metadata_list || (metadata_list->size != 6))
goto error;
/* Get extension of source core file */
src_ext = metadata_list->elems[1].data;
if (string_is_empty(src_ext))
goto error;
/* Check whether extension is valid */
if (!string_is_equal_noncase(src_ext, core_ext))
goto error;
string_list_free(metadata_list);
metadata_list = NULL;
return CORE_BACKUP_TYPE_ARCHIVE;
}
/* Check if this is a plain dynamic library file */
if (string_is_equal_noncase(backup_ext, core_ext))
return CORE_BACKUP_TYPE_LIB;
error:
if (metadata_list)
{
string_list_free(metadata_list);
metadata_list = NULL;
}
return CORE_BACKUP_TYPE_INVALID;
}
/* Fetches crc value of specified core backup file.
* Returns true if successful */
bool core_backup_get_backup_crc(char *backup_path, uint32_t *crc)
{
enum core_backup_type backup_type;
struct string_list *metadata_list = NULL;
if (string_is_empty(backup_path) || !crc)
return false;
/* Get backup type */
backup_type = core_backup_get_backup_type(backup_path);
switch (backup_type)
{
case CORE_BACKUP_TYPE_ARCHIVE:
{
const char *backup_filename = NULL;
const char *crc_str = NULL;
/* Split the backup filename into its various
* metadata components */
backup_filename = path_basename(backup_path);
if (string_is_empty(backup_filename))
goto error;
metadata_list = string_split(backup_filename, ".");
if (!metadata_list || (metadata_list->size != 6))
goto error;
/* Get crc string */
crc_str = metadata_list->elems[3].data;
if (string_is_empty(crc_str))
goto error;
/* Convert to an integer */
if ((*crc = (uint32_t)string_hex_to_unsigned(crc_str)) == 0)
goto error;
string_list_free(metadata_list);
metadata_list = NULL;
}
return true;
case CORE_BACKUP_TYPE_LIB:
{
intfstream_t *backup_file = NULL;
/* This is a plain dynamic library file,
* have to read file data to determine crc */
/* Open backup file */
backup_file = intfstream_open_file(
backup_path, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (backup_file)
{
bool success;
/* Get crc value */
success = intfstream_get_crc(backup_file, crc);
/* Close backup file */
intfstream_close(backup_file);
free(backup_file);
backup_file = NULL;
return success;
}
}
break;
default:
/* Backup is invalid */
break;
}
error:
if (metadata_list)
{
string_list_free(metadata_list);
metadata_list = NULL;
}
return false;
}
/* Fetches core path associated with specified core
* backup file. Returns detected type of backup
* file - CORE_BACKUP_TYPE_INVALID indicates that
* backup file cannot be restored/installed, or
* arguments are otherwise invalid */
enum core_backup_type core_backup_get_core_path(
const char *backup_path, const char *dir_libretro,
char *core_path, size_t len)
{
const char *backup_filename = NULL;
if (string_is_empty(backup_path) || string_is_empty(dir_libretro))
return CORE_BACKUP_TYPE_INVALID;
backup_filename = path_basename(backup_path);
if (!string_is_empty(backup_filename))
{
/* Check backup type */
switch (core_backup_get_backup_type(backup_path))
{
case CORE_BACKUP_TYPE_ARCHIVE:
{
/* This is an archived backup with timestamp/crc
* metadata in the filename */
char *core_filename = strdup(backup_filename);
/* Find the location of the second period */
char *period = strchr(core_filename, '.');
if (!period || (*(++period) == '\0'))
{
free(core_filename);
break;
}
if (!(period = strchr(period, '.')))
{
free(core_filename);
break;
}
/* Trim everything after (and including) the
* second period */
*period = '\0';
if (string_is_empty(core_filename))
{
free(core_filename);
break;
}
/* All good - build core path */
fill_pathname_join_special(core_path, dir_libretro,
core_filename, len);
free(core_filename);
}
return CORE_BACKUP_TYPE_ARCHIVE;
case CORE_BACKUP_TYPE_LIB:
/* This is a plain dynamic library file */
fill_pathname_join_special(core_path, dir_libretro,
backup_filename, len);
return CORE_BACKUP_TYPE_LIB;
default:
/* Backup is invalid */
break;
}
}
return CORE_BACKUP_TYPE_INVALID;
}
/*************************/
/* Backup List Functions */
/*************************/
/**************************************/
/* Initialisation / De-Initialisation */
/**************************************/
/* Parses backup file name and adds to backup list, if valid */
static bool core_backup_add_entry(core_backup_list_t *backup_list,
const char *core_filename, const char *backup_path)
{
char *backup_filename = NULL;
core_backup_list_entry_t *entry = NULL;
unsigned long crc = 0;
unsigned backup_mode = 0;
if ( !backup_list
|| string_is_empty(core_filename)
|| string_is_empty(backup_path)
|| (backup_list->size >= backup_list->capacity))
return false;
backup_filename = strdup(path_basename(backup_path));
if (string_is_empty(backup_filename))
goto error;
/* Ensure base backup filename matches core */
if (!string_starts_with(backup_filename, core_filename))
goto error;
/* Remove backup file extension */
path_remove_extension(backup_filename);
/* Parse backup filename metadata
* - <core_filename>.<timestamp>.<crc>.<backup_mode>
* - timestamp: YYYYMMDDTHHMMSS */
entry = &backup_list->entries[backup_list->size];
if (sscanf(backup_filename + strlen(core_filename),
".%04u%02u%02uT%02u%02u%02u.%08lx.%u",
&entry->date.year, &entry->date.month, &entry->date.day,
&entry->date.hour, &entry->date.minute, &entry->date.second,
&crc, &backup_mode) != 8)
goto error;
entry->crc = (uint32_t)crc;
entry->backup_mode = (enum core_backup_mode)backup_mode;
/* Cache backup path */
entry->backup_path = strdup(backup_path);
backup_list->size++;
free(backup_filename);
return true;
error:
if (backup_filename)
free(backup_filename);
return false;
}
/* Creates a new core backup list containing entries
* for all existing backup files.
* Returns a handle to a new core_backup_list_t object
* on success, otherwise returns NULL. */
core_backup_list_t *core_backup_list_init(
const char *core_path, const char *dir_core_assets)
{
size_t i;
const char *core_filename = NULL;
struct string_list *dir_list = NULL;
core_backup_list_t *backup_list = NULL;
core_backup_list_entry_t *entries = NULL;
char core_dir[DIR_MAX_LENGTH];
char backup_dir[DIR_MAX_LENGTH];
core_dir[0] = '\0';
backup_dir[0] = '\0';
/* Get core filename and parent directory */
if (string_is_empty(core_path))
goto error;
core_filename = path_basename(core_path);
if (string_is_empty(core_filename))
goto error;
fill_pathname_parent_dir(core_dir, core_path, sizeof(core_dir));
if (string_is_empty(core_dir))
goto error;
/* Get backup directory */
if (!core_backup_get_backup_dir(core_dir, dir_core_assets, core_filename,
backup_dir, sizeof(backup_dir)))
goto error;
/* Get backup file list */
dir_list = dir_list_new(
backup_dir,
FILE_PATH_CORE_BACKUP_EXTENSION,
false, /* include_dirs */
false, /* include_hidden */
false, /* include_compressed */
false /* recursive */
);
/* Sanity check */
if (!dir_list)
goto error;
if (dir_list->size < 1)
goto error;
/* Ensure list is sorted in alphabetical order
* > This corresponds to 'timestamp' order */
dir_list_sort(dir_list, true);
/* Create core backup list */
if (!(backup_list = (core_backup_list_t*)malloc(sizeof(*backup_list))))
goto error;
backup_list->entries = NULL;
backup_list->capacity = 0;
backup_list->size = 0;
/* Create entries array
* (Note: Set this to the full size of the directory
* list - this may be larger than we need, but saves
* many inefficiencies later) */
if (!(entries = (core_backup_list_entry_t*)
calloc(dir_list->size, sizeof(*entries))))
goto error;
backup_list->entries = entries;
backup_list->capacity = dir_list->size;
/* Loop over backup files and parse file names */
for (i = 0; i < dir_list->size; i++)
{
const char *backup_path = dir_list->elems[i].data;
core_backup_add_entry(backup_list, core_filename, backup_path);
}
if (backup_list->size == 0)
goto error;
string_list_free(dir_list);
return backup_list;
error:
if (dir_list)
string_list_free(dir_list);
if (backup_list)
core_backup_list_free(backup_list);
return NULL;
}
/* Frees specified core backup list */
void core_backup_list_free(core_backup_list_t *backup_list)
{
size_t i;
if (!backup_list)
return;
if (backup_list->entries)
{
for (i = 0; i < backup_list->size; i++)
{
core_backup_list_entry_t *entry = &backup_list->entries[i];
if (!entry)
continue;
if (entry->backup_path)
{
free(entry->backup_path);
entry->backup_path = NULL;
}
}
free(backup_list->entries);
backup_list->entries = NULL;
}
free(backup_list);
}
/***********/
/* Getters */
/***********/
/* Returns number of entries in core backup list */
size_t core_backup_list_size(core_backup_list_t *backup_list)
{
if (!backup_list)
return 0;
return backup_list->size;
}
/* Returns number of entries of specified 'backup mode'
* (manual or automatic) in core backup list */
size_t core_backup_list_get_num_backups(
core_backup_list_t *backup_list,
enum core_backup_mode backup_mode)
{
size_t i;
size_t num_backups = 0;
if (!backup_list || !backup_list->entries)
return 0;
for (i = 0; i < backup_list->size; i++)
{
core_backup_list_entry_t *current_entry = &backup_list->entries[i];
if ( current_entry
&& (current_entry->backup_mode == backup_mode))
num_backups++;
}
return num_backups;
}
/* Fetches core backup list entry corresponding
* to the specified entry index.
* Returns false if index is invalid. */
bool core_backup_list_get_index(
core_backup_list_t *backup_list,
size_t idx,
const core_backup_list_entry_t **entry)
{
if (!backup_list || !backup_list->entries || !entry)
return false;
if (idx >= backup_list->size)
return false;
*entry = &backup_list->entries[idx];
if (*entry)
return true;
return false;
}
/* Fetches core backup list entry corresponding
* to the specified core crc checksum value.
* Note that 'manual' and 'auto' backups are
* considered independent - we only compare
* crc values for the specified backup_mode.
* Returns false if entry is not found. */
bool core_backup_list_get_crc(
core_backup_list_t *backup_list,
uint32_t crc, enum core_backup_mode backup_mode,
const core_backup_list_entry_t **entry)
{
size_t i;
if (!backup_list || !backup_list->entries || !entry)
return false;
for (i = 0; i < backup_list->size; i++)
{
core_backup_list_entry_t *current_entry = &backup_list->entries[i];
if ( (current_entry)
&& (current_entry->crc == crc)
&& (current_entry->backup_mode == backup_mode))
{
*entry = current_entry;
return true;
}
}
return false;
}