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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
|
/* Copyright (C) 2010-2019 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (core_updater_list.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <file/file_path.h>
#include <string/stdstring.h>
#include <lists/string_list.h>
#include <net/net_http.h>
#include <array/rbuf.h>
#include <retro_miscellaneous.h>
#include "file_path_special.h"
#include "core_info.h"
#include "core_updater_list.h"
/* Holds all entries in a core updater list */
struct core_updater_list
{
core_updater_list_entry_t *entries;
enum core_updater_list_type type;
};
/* Cached ('global') core updater list */
static core_updater_list_t *core_list_cached = NULL;
/**************************************/
/* Initialisation / De-Initialisation */
/**************************************/
/* Frees contents of specified core updater
* list entry */
static void core_updater_list_free_entry(core_updater_list_entry_t *entry)
{
if (!entry)
return;
if (entry->remote_filename)
{
free(entry->remote_filename);
entry->remote_filename = NULL;
}
if (entry->remote_core_path)
{
free(entry->remote_core_path);
entry->remote_core_path = NULL;
}
if (entry->local_core_path)
{
free(entry->local_core_path);
entry->local_core_path = NULL;
}
if (entry->local_info_path)
{
free(entry->local_info_path);
entry->local_info_path = NULL;
}
if (entry->display_name)
{
free(entry->display_name);
entry->display_name = NULL;
}
if (entry->description)
{
free(entry->description);
entry->description = NULL;
}
if (entry->licenses_list)
{
string_list_free(entry->licenses_list);
entry->licenses_list = NULL;
}
}
/* Creates a new, empty core updater list.
* Returns a handle to a new core_updater_list_t object
* on success, otherwise returns NULL. */
core_updater_list_t *core_updater_list_init(void)
{
/* Create core updater list */
core_updater_list_t *core_list = (core_updater_list_t*)
malloc(sizeof(*core_list));
if (!core_list)
return NULL;
/* Initialise members */
core_list->entries = NULL;
core_list->type = CORE_UPDATER_LIST_TYPE_UNKNOWN;
return core_list;
}
/* Resets (removes all entries of) specified core
* updater list */
void core_updater_list_reset(core_updater_list_t *core_list)
{
if (!core_list)
return;
if (core_list->entries)
{
size_t i;
for (i = 0; i < RBUF_LEN(core_list->entries); i++)
core_updater_list_free_entry(&core_list->entries[i]);
RBUF_FREE(core_list->entries);
}
core_list->type = CORE_UPDATER_LIST_TYPE_UNKNOWN;
}
/* Frees specified core updater list */
void core_updater_list_free(core_updater_list_t *core_list)
{
if (!core_list)
return;
core_updater_list_reset(core_list);
free(core_list);
}
/***************/
/* Cached List */
/***************/
/* Creates a new, empty cached core updater list
* (i.e. 'global' list).
* Returns false in the event of an error. */
bool core_updater_list_init_cached(void)
{
/* Free any existing cached core updater list */
if (core_list_cached)
{
core_updater_list_free(core_list_cached);
core_list_cached = NULL;
}
core_list_cached = core_updater_list_init();
if (!core_list_cached)
return false;
return true;
}
/* Fetches cached core updater list */
core_updater_list_t *core_updater_list_get_cached(void)
{
if (core_list_cached)
return core_list_cached;
return NULL;
}
/* Frees cached core updater list */
void core_updater_list_free_cached(void)
{
core_updater_list_free(core_list_cached);
core_list_cached = NULL;
}
/***********/
/* Getters */
/***********/
/* Returns number of entries in core updater list */
size_t core_updater_list_size(core_updater_list_t *core_list)
{
if (!core_list)
return 0;
return RBUF_LEN(core_list->entries);
}
/* Returns 'type' (core delivery method) of
* specified core updater list */
enum core_updater_list_type core_updater_list_get_type(
core_updater_list_t *core_list)
{
if (!core_list)
return CORE_UPDATER_LIST_TYPE_UNKNOWN;
return core_list->type;
}
/* Fetches core updater list entry corresponding
* to the specified entry index.
* Returns false if index is invalid. */
bool core_updater_list_get_index(
core_updater_list_t *core_list,
size_t idx,
const core_updater_list_entry_t **entry)
{
if (!core_list || !entry)
return false;
if (idx >= RBUF_LEN(core_list->entries))
return false;
*entry = &core_list->entries[idx];
return true;
}
/* Fetches core updater list entry corresponding
* to the specified remote core filename.
* Returns false if core is not found. */
bool core_updater_list_get_filename(
core_updater_list_t *core_list,
const char *remote_filename,
const core_updater_list_entry_t **entry)
{
size_t num_entries;
size_t i;
if (!core_list || !entry || string_is_empty(remote_filename))
return false;
num_entries = RBUF_LEN(core_list->entries);
if (num_entries < 1)
return false;
/* Search for specified filename */
for (i = 0; i < num_entries; i++)
{
core_updater_list_entry_t *current_entry = &core_list->entries[i];
if (string_is_empty(current_entry->remote_filename))
continue;
if (string_is_equal(remote_filename, current_entry->remote_filename))
{
*entry = current_entry;
return true;
}
}
return false;
}
/* Fetches core updater list entry corresponding
* to the specified core.
* Returns false if core is not found. */
bool core_updater_list_get_core(
core_updater_list_t *core_list,
const char *local_core_path,
const core_updater_list_entry_t **entry)
{
bool resolve_symlinks;
size_t num_entries;
size_t i;
char real_core_path[PATH_MAX_LENGTH];
if (!core_list || !entry || string_is_empty(local_core_path))
return false;
if ((num_entries = RBUF_LEN(core_list->entries)) < 1)
return false;
/* Resolve absolute pathname of local_core_path */
strlcpy(real_core_path, local_core_path, sizeof(real_core_path));
/* Can't resolve symlinks when dealing with cores
* installed via play feature delivery, because the
* source files have non-standard file names (which
* will not be recognised by regular core handling
* routines) */
resolve_symlinks = (core_list->type != CORE_UPDATER_LIST_TYPE_PFD);
path_resolve_realpath(real_core_path, sizeof(real_core_path),
resolve_symlinks);
if (string_is_empty(real_core_path))
return false;
/* Search for specified core */
for (i = 0; i < num_entries; i++)
{
core_updater_list_entry_t *current_entry = &core_list->entries[i];
if (string_is_empty(current_entry->local_core_path))
continue;
#ifdef _WIN32
/* Handle case-insensitive operating systems*/
if (string_is_equal_noncase(real_core_path, current_entry->local_core_path))
{
#else
if (string_is_equal(real_core_path, current_entry->local_core_path))
{
#endif
*entry = current_entry;
return true;
}
}
return false;
}
/***********/
/* Setters */
/***********/
/* Parses date string and adds contents to
* specified core updater list entry */
static bool core_updater_list_set_date(
core_updater_list_entry_t *entry, const char *date_str)
{
char *tok, *save;
char *elem0 = NULL;
char *elem1 = NULL;
char *elem2 = NULL;
unsigned list_size = 0;
char *date_str_cpy = NULL;
if (!entry || string_is_empty(date_str))
return false;
date_str_cpy = strdup(date_str);
/* Split date string into component values */
if ((tok = strtok_r(date_str_cpy, "-", &save)))
{
elem0 = strdup(tok);
list_size++;
}
if ((tok = strtok_r(NULL, "-", &save)))
{
elem1 = strdup(tok);
list_size++;
}
if ((tok = strtok_r(NULL, "-", &save)))
{
elem2 = strdup(tok);
list_size++;
}
free(date_str_cpy);
/* Date string must have 3 values:
* [year] [month] [day] */
if (list_size < 3)
{
if (elem0)
free(elem0);
if (elem1)
free(elem1);
if (elem2)
free(elem2);
return false;
}
/* Convert date string values */
entry->date.year = string_to_unsigned(elem0);
entry->date.month = string_to_unsigned(elem1);
entry->date.day = string_to_unsigned(elem2);
/* Clean up */
free(elem0);
free(elem1);
free(elem2);
return true;
}
/* Parses crc string and adds value to
* specified core updater list entry */
static bool core_updater_list_set_crc(
core_updater_list_entry_t *entry, const char *crc_str)
{
uint32_t crc;
if (!entry || string_is_empty(crc_str))
return false;
if ((crc = (uint32_t)string_hex_to_unsigned(crc_str)) == 0)
return false;
entry->crc = crc;
return true;
}
/* Parses core filename string and adds all
* associated paths to the specified core
* updater list entry */
static bool core_updater_list_set_paths(
core_updater_list_entry_t *entry,
const char *path_dir_libretro,
const char *path_libretro_info,
const char *network_buildbot_url,
const char *filename_str,
enum core_updater_list_type list_type)
{
char *last_underscore = NULL;
char *tmp_url = NULL;
bool is_archive = true;
/* Can't resolve symlinks when dealing with cores
* installed via play feature delivery, because the
* source files have non-standard file names (which
* will not be recognised by regular core handling
* routines) */
char remote_core_path[PATH_MAX_LENGTH];
char local_core_path[PATH_MAX_LENGTH];
char local_info_path[PATH_MAX_LENGTH];
bool resolve_symlinks = (list_type != CORE_UPDATER_LIST_TYPE_PFD);
if ( !entry
|| string_is_empty(filename_str)
|| string_is_empty(path_dir_libretro)
|| string_is_empty(path_libretro_info))
return false;
/* Only buildbot cores require the buildbot URL */
if ((list_type == CORE_UPDATER_LIST_TYPE_BUILDBOT) &&
string_is_empty(network_buildbot_url))
return false;
/* Check whether remote file is an archive */
is_archive = path_is_compressed_file(filename_str);
/* remote_filename */
if (entry->remote_filename)
{
free(entry->remote_filename);
entry->remote_filename = NULL;
}
entry->remote_filename = strdup(filename_str);
/* remote_core_path
* > Leave blank if this is not a buildbot core */
if (list_type == CORE_UPDATER_LIST_TYPE_BUILDBOT)
{
fill_pathname_join_special(
remote_core_path,
network_buildbot_url,
filename_str,
sizeof(remote_core_path));
/* > Apply proper URL encoding (messy...) */
tmp_url = strdup(remote_core_path);
remote_core_path[0] = '\0';
net_http_urlencode_full(
remote_core_path, tmp_url, sizeof(remote_core_path));
if (tmp_url)
free(tmp_url);
}
if (entry->remote_core_path)
{
free(entry->remote_core_path);
entry->remote_core_path = NULL;
}
entry->remote_core_path = strdup(remote_core_path);
fill_pathname_join_special(
local_core_path,
path_dir_libretro,
filename_str,
sizeof(local_core_path));
if (is_archive)
path_remove_extension(local_core_path);
path_resolve_realpath(local_core_path, sizeof(local_core_path),
resolve_symlinks);
if (entry->local_core_path)
{
free(entry->local_core_path);
entry->local_core_path = NULL;
}
entry->local_core_path = strdup(local_core_path);
fill_pathname_join_special(
local_info_path,
path_libretro_info,
filename_str,
sizeof(local_info_path));
path_remove_extension(local_info_path);
if (is_archive)
path_remove_extension(local_info_path);
/* > Remove any non-standard core filename
* additions (i.e. info files end with
* '_libretro' but core files may have
* a platform specific addendum,
* e.g. '_android')*/
last_underscore = (char*)strrchr(local_info_path, '_');
if (!string_is_empty(last_underscore))
if (!string_is_equal(last_underscore, "_libretro"))
*last_underscore = '\0';
/* > Add proper file extension */
strlcat(
local_info_path,
FILE_PATH_CORE_INFO_EXTENSION,
sizeof(local_info_path));
if (entry->local_info_path)
{
free(entry->local_info_path);
entry->local_info_path = NULL;
}
entry->local_info_path = strdup(local_info_path);
return true;
}
/* Reads info file associated with core and
* adds relevant information to updater list
* entry */
static bool core_updater_list_set_core_info(
core_updater_list_entry_t *entry,
const char *local_info_path,
const char *filename_str)
{
core_updater_info_t *core_info = NULL;
if ( !entry
|| string_is_empty(local_info_path)
|| string_is_empty(filename_str))
return false;
/* Clear any existing core info */
if (entry->display_name)
{
free(entry->display_name);
entry->display_name = NULL;
}
if (entry->description)
{
free(entry->description);
entry->description = NULL;
}
if (entry->licenses_list)
{
/* Note: We can safely leave this as NULL if
* the core info file is invalid */
string_list_free(entry->licenses_list);
entry->licenses_list = NULL;
}
entry->is_experimental = false;
/* Read core info file
* > Note: It's a bit rubbish that we have to
* read the actual core info files here...
* Would be better to cache this globally
* (at present, we only cache info for
* *installed* cores...) */
if ((core_info = core_info_get_core_updater_info(local_info_path)))
{
/* display_name + is_experimental */
if (!string_is_empty(core_info->display_name))
{
entry->display_name = strdup(core_info->display_name);
entry->is_experimental = core_info->is_experimental;
}
else
{
/* If display name is blank, use core filename and
* assume core is experimental (i.e. all 'fit for consumption'
* cores must have a valid/complete core info file) */
entry->display_name = strdup(filename_str);
entry->is_experimental = true;
}
/* description */
if (!string_is_empty(core_info->description))
entry->description = strdup(core_info->description);
else
entry->description = strldup("", sizeof(""));
/* licenses_list */
if (!string_is_empty(core_info->licenses))
entry->licenses_list = string_split(core_info->licenses, "|");
/* Clean up */
core_info_free_core_updater_info(core_info);
}
else
{
/* If info file is missing, use core filename and
* assume core is experimental (i.e. all 'fit for consumption'
* cores must have a valid/complete core info file) */
entry->display_name = strdup(filename_str);
entry->is_experimental = true;
entry->description = strldup("", sizeof(""));
}
return true;
}
/* Adds entry to the end of the specified core
* updater list
* NOTE: Entry string values are passed by
* reference - *do not free the entry passed
* to this function* */
static bool core_updater_list_push_entry(
core_updater_list_t *core_list, core_updater_list_entry_t *entry)
{
core_updater_list_entry_t *list_entry = NULL;
size_t num_entries;
if (!core_list || !entry)
return false;
/* Get current number of list entries */
num_entries = RBUF_LEN(core_list->entries);
/* Attempt to allocate memory for new entry */
if (!RBUF_TRYFIT(core_list->entries, num_entries + 1))
return false;
/* Allocation successful - increment array size */
RBUF_RESIZE(core_list->entries, num_entries + 1);
/* Get handle of new entry at end of list, and
* zero-initialise members */
list_entry = &core_list->entries[num_entries];
memset(list_entry, 0, sizeof(*list_entry));
/* Assign paths */
list_entry->remote_filename = entry->remote_filename;
list_entry->remote_core_path = entry->remote_core_path;
list_entry->local_core_path = entry->local_core_path;
list_entry->local_info_path = entry->local_info_path;
/* Assign core info */
list_entry->display_name = entry->display_name;
list_entry->description = entry->description;
list_entry->licenses_list = entry->licenses_list;
list_entry->is_experimental = entry->is_experimental;
/* Copy crc */
list_entry->crc = entry->crc;
/* Copy date */
memcpy(&list_entry->date, &entry->date, sizeof(core_updater_list_date_t));
return true;
}
/* Parses the contents of a single buildbot
* core listing and adds it to the specified
* core updater list */
static void core_updater_list_add_entry(
core_updater_list_t *core_list,
const char *path_dir_libretro,
const char *path_libretro_info,
const char *network_buildbot_url,
const char *date_str,
const char *crc_str,
const char *filename_str)
{
const core_updater_list_entry_t *search_entry = NULL;
core_updater_list_entry_t entry = {0};
/* Check whether core file is already included
* in the list (this is *not* an error condition,
* it just means we can skip the current listing) */
if (core_updater_list_get_filename(core_list,
filename_str, &search_entry))
goto error;
/* Parse individual listing strings */
if (!core_updater_list_set_date(&entry, date_str))
goto error;
if (!core_updater_list_set_crc(&entry, crc_str))
goto error;
if (!core_updater_list_set_paths(
&entry,
path_dir_libretro,
path_libretro_info,
network_buildbot_url,
filename_str,
CORE_UPDATER_LIST_TYPE_BUILDBOT))
goto error;
if (!core_updater_list_set_core_info(
&entry,
entry.local_info_path,
filename_str))
goto error;
/* Add entry to list */
if (!core_updater_list_push_entry(core_list, &entry))
goto error;
return;
error:
/* This is not a *fatal* error - it just
* means one of the following:
* - The current line of entry text received
* from the buildbot is broken somehow
* (could be the case that the network buffer
* wasn't large enough to cache the entire
* string, so the last line was truncated)
* - We had insufficient memory to allocate a new
* entry in the core updater list
* In either case, the current entry is discarded
* and we move on to the next one
* (network transfers are fishy business, so we
* choose to ignore this sort of error - don't
* want the whole fetch to fail because of a
* trivial glitch...) */
core_updater_list_free_entry(&entry);
}
/* Core updater list qsort helper function */
static int core_updater_list_qsort_func(
const core_updater_list_entry_t *a, const core_updater_list_entry_t *b)
{
if (!a || !b)
return 0;
if (string_is_empty(a->display_name) || string_is_empty(b->display_name))
return 0;
return strcasecmp(a->display_name, b->display_name);
}
/* Sorts core updater list into alphabetical order */
static void core_updater_list_qsort(core_updater_list_t *core_list)
{
size_t num_entries;
if (!core_list)
return;
if ((num_entries = RBUF_LEN(core_list->entries)) < 2)
return;
if (core_list->entries)
qsort(
core_list->entries, num_entries,
sizeof(core_updater_list_entry_t),
(int (*)(const void *, const void *))
core_updater_list_qsort_func);
}
/* Reads the contents of a buildbot core list
* network request into the specified
* core_updater_list_t object.
* Returns false in the event of an error. */
bool core_updater_list_parse_network_data(
core_updater_list_t *core_list,
const char *path_dir_libretro,
const char *path_libretro_info,
const char *network_buildbot_url,
const char *data, size_t len)
{
char *tok, *save;
unsigned list_size = 0;
char *data_buf = NULL;
/* Sanity check */
if (!core_list || string_is_empty(data) || (len < 1))
return false;
/* We're populating a list 'from scratch' - remove
* any existing entries */
core_updater_list_reset(core_list);
/* Input data string is not terminated - have
* to copy it to a temporary buffer... */
if (!(data_buf = (char*)malloc((len + 1) * sizeof(char))))
return false;
memcpy(data_buf, data, len * sizeof(char));
data_buf[len] = '\0';
list_size = string_count_occurrences_single_character(data_buf, '\n');
if (list_size < 1)
{
free(data_buf);
return false;
}
/* Split network listing request into lines */
/* Loop over lines */
for (tok = strtok_r(data_buf, "\n", &save); tok;
tok = strtok_r(NULL, "\n", &save))
{
char *tok2, *save2;
char *elem0 = NULL;
char *elem1 = NULL;
char *elem2 = NULL;
char *line_cpy = NULL;
const char *line = tok;
if (string_is_empty(line))
continue;
line_cpy = strdup(line);
/* Split line into listings info components */
if ((tok2 = strtok_r(line_cpy, " ", &save2)))
elem0 = strdup(tok2); /* date */
if ((tok2 = strtok_r(NULL, " ", &save2)))
elem1 = strdup(tok2); /* crc */
if ((tok2 = strtok_r(NULL, " ", &save2)))
elem2 = strdup(tok2); /* filename */
free(line_cpy);
/* Parse listings info and add to core updater
* list */
/* > Listings must have 3 entries:
* [date] [crc] [filename] */
if ( !string_is_empty(elem0)
&& !string_is_empty(elem1)
&& !string_is_empty(elem2))
core_updater_list_add_entry(
core_list,
path_dir_libretro,
path_libretro_info,
network_buildbot_url,
elem0, elem1, elem2);
/* Clean up */
free(elem0);
free(elem1);
free(elem2);
}
/* Temporary data buffer is no longer required */
free(data_buf);
data_buf = NULL;
/* Sanity check */
if (RBUF_LEN(core_list->entries) < 1)
return false;
/* Sort completed list */
core_updater_list_qsort(core_list);
/* Set list type */
core_list->type = CORE_UPDATER_LIST_TYPE_BUILDBOT;
return true;
}
/* Parses a single play feature delivery core
* listing and adds it to the specified core
* updater list */
static void core_updater_list_add_pfd_entry(
core_updater_list_t *core_list,
const char *path_dir_libretro,
const char *path_libretro_info,
const char *filename_str)
{
const core_updater_list_entry_t *search_entry = NULL;
core_updater_list_entry_t entry = {0};
if (!core_list || string_is_empty(filename_str))
goto error;
/* Check whether core file is already included
* in the list (this is *not* an error condition,
* it just means we can skip the current listing) */
if (core_updater_list_get_filename(core_list,
filename_str, &search_entry))
goto error;
/* Note: Play feature delivery cores have no
* timestamp or CRC info - leave these fields
* zero initialised */
/* Populate entry fields */
if (!core_updater_list_set_paths(
&entry,
path_dir_libretro,
path_libretro_info,
NULL,
filename_str,
CORE_UPDATER_LIST_TYPE_PFD))
goto error;
if (!core_updater_list_set_core_info(
&entry,
entry.local_info_path,
filename_str))
goto error;
/* Add entry to list */
if (!core_updater_list_push_entry(core_list, &entry))
goto error;
return;
error:
/* This is not a *fatal* error - it just
* means one of the following:
* - The core listing entry obtained from the
* play feature delivery interface is broken
* somehow
* - We had insufficient memory to allocate a new
* entry in the core updater list
* In either case, the current entry is discarded
* and we move on to the next one */
core_updater_list_free_entry(&entry);
}
/* Reads the list of cores currently available
* via play feature delivery (PFD) into the
* specified core_updater_list_t object.
* Returns false in the event of an error. */
bool core_updater_list_parse_pfd_data(
core_updater_list_t *core_list,
const char *path_dir_libretro,
const char *path_libretro_info,
const struct string_list *pfd_cores)
{
size_t i;
/* Sanity check */
if (!core_list || !pfd_cores || (pfd_cores->size < 1))
return false;
/* We're populating a list 'from scratch' - remove
* any existing entries */
core_updater_list_reset(core_list);
/* Loop over play feature delivery core list */
for (i = 0; i < pfd_cores->size; i++)
{
const char *filename_str = pfd_cores->elems[i].data;
if (string_is_empty(filename_str))
continue;
/* Parse core file name and add to core
* updater list */
core_updater_list_add_pfd_entry(
core_list,
path_dir_libretro,
path_libretro_info,
filename_str);
}
/* Sanity check */
if (RBUF_LEN(core_list->entries) < 1)
return false;
/* Sort completed list */
core_updater_list_qsort(core_list);
/* Set list type */
core_list->type = CORE_UPDATER_LIST_TYPE_PFD;
return true;
}
|