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 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
|
/*
| Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
| Copyright (C) 2009 Christophe Fergeau <cfergeau@mandriva.com>
| Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
|
| The code contained in this file is free software; you can redistribute
| it and/or modify it under the terms of the GNU Lesser General Public
| License as published by the Free Software Foundation; either version
| 2.1 of the License, or (at your option) any later version.
|
| This file 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
| Lesser General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public
| License along with this code; if not, write to the Free Software
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
| USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <sqlite3.h>
#include <plist/plist.h>
#ifdef HAVE_LIBIMOBILEDEVICE
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#endif
#include "itdb.h"
#include "itdb_private.h"
#include "itdb_sqlite_queries.h"
/** time zone offset in seconds */
static uint32_t tzoffset = 0;
static uint32_t timeconv(time_t tv)
{
/* quite strange. this is time_t - 01.01.2001 01:00:00 - tzoffset */
return tv - 978307200 - tzoffset;
}
enum sort_order {
ORDER_TITLE,
ORDER_ARTIST,
ORDER_ALBUM,
ORDER_GENRE,
ORDER_COMPOSER,
ORDER_ALBUM_ARTIST,
ORDER_ALBUM_BY_ARTIST,
ORDER_SERIES_NAME,
ORDER_END
};
static glong sort_offsets[][4] = {
{ G_STRUCT_OFFSET(Itdb_Track, sort_title), G_STRUCT_OFFSET(Itdb_Track, title) },
{ G_STRUCT_OFFSET(Itdb_Track, sort_artist), G_STRUCT_OFFSET(Itdb_Track, artist) },
{ G_STRUCT_OFFSET(Itdb_Track, sort_album), G_STRUCT_OFFSET(Itdb_Track, album) },
{ G_STRUCT_OFFSET(Itdb_Track, genre), 0 },
{ G_STRUCT_OFFSET(Itdb_Track, sort_composer), G_STRUCT_OFFSET(Itdb_Track, composer) },
{ G_STRUCT_OFFSET(Itdb_Track, sort_artist), G_STRUCT_OFFSET(Itdb_Track, artist) },
/* TODO: iTunes sorts first by album_artist, then by artist. */
{ G_STRUCT_OFFSET(Itdb_Track, sort_albumartist), G_STRUCT_OFFSET(Itdb_Track, albumartist),
G_STRUCT_OFFSET(Itdb_Track, sort_artist), G_STRUCT_OFFSET(Itdb_Track, artist) },
{ 0, 0 } /* TODO: Series Name: ignored for now. */
};
static gchar *sort_field(Itdb_Track *track, guint member)
{
gchar *field;
int offset;
int i;
for (i = 0; i < 4; i++) {
offset = sort_offsets[member][i];
field = G_STRUCT_MEMBER(gchar *, track, offset);
if (offset != 0 && field != NULL && *field) {
return field;
}
}
return NULL;
}
static int lookup_order(GHashTable **order_hashes, guint member, Itdb_Track *track)
{
const char *key;
g_assert (member < ORDER_END);
key = sort_field(track, member);
if (key == NULL) {
return 100;
}
return GPOINTER_TO_UINT(g_hash_table_lookup(order_hashes[member], key));
}
static gint compare_track_fields(gconstpointer lhs, gconstpointer rhs)
{
const gchar *field_lhs = (const gchar *)lhs;
const gchar *field_rhs = (const gchar *)rhs;
if (field_lhs == NULL && field_rhs == NULL) {
return 0;
}
if (field_lhs == NULL) {
return -1;
}
if (field_rhs == NULL) {
return 1;
}
/* FIXME: should probably generate collation keys first */
return g_utf8_collate(field_lhs, field_rhs);
}
static gboolean traverse_tracks(gpointer key, gpointer value, gpointer data)
{
GHashTable *hash = (GHashTable *)data;
if (key != NULL) {
g_hash_table_insert(hash, key, GUINT_TO_POINTER((g_hash_table_size(hash) + 1) * 100));
}
return FALSE;
}
static GHashTable **compute_key_orders(GList *tracks)
{
GHashTable **result;
int i;
result = g_new0(GHashTable *, ORDER_END);
g_assert (result != NULL);
for (i = 0; i < ORDER_END; i++) {
GList *li;
GTree *tree;
tree = g_tree_new(compare_track_fields);
for (li = tracks; li != NULL; li = li->next) {
Itdb_Track *track = (Itdb_Track *)li->data;
g_tree_insert(tree, sort_field(track, i), 0);
}
result[i] = g_hash_table_new(g_str_hash, g_str_equal);
g_assert (result[i] != NULL);
g_tree_foreach(tree, traverse_tracks, (gpointer)result[i]);
g_tree_destroy(tree);
}
return result;
}
static void destroy_key_orders(GHashTable **orders)
{
int i;
for (i = 0; i < ORDER_END; i++) {
g_hash_table_destroy(orders[i]);
}
g_free(orders);
}
static int mk_Dynamic(Itdb_iTunesDB *itdb, const char *outpath)
{
int res = -1;
gchar *dbf = NULL;
sqlite3 *db = NULL;
sqlite3_stmt *stmt = NULL;
char *errmsg = NULL;
struct stat fst;
int idx = 0;
GList *gl = NULL;
dbf = g_build_filename(outpath, "Dynamic.itdb", NULL);
printf("[%s] Processing '%s'\n", __func__, dbf);
if (stat(dbf, &fst) != 0) {
if (errno == ENOENT) {
/* file is not present. so we'll create it */
} else {
fprintf(stderr, "[%s] Error: stat: %s\n", __func__, strerror(errno));
goto leave;
}
} else {
/* file is present. delete it, we'll re-create it. */
if (unlink(dbf) != 0) {
fprintf(stderr, "[%s] could not delete '%s': %s\n", __func__, dbf, strerror(errno));
goto leave;
}
}
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
goto leave;
}
sqlite3_exec(db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
fprintf(stderr, "[%s] creating table structure\n", __func__);
/* db structure needs to be created. */
if (SQLITE_OK != sqlite3_exec(db, Dynamic_create, NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
if (itdb->tracks) {
printf("[%s] - processing %d tracks\n", __func__, g_list_length(itdb->tracks));
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"item_stats\" (item_pid,has_been_played,date_played,play_count_user,play_count_recent,date_skipped,skip_count_user,skip_count_recent,bookmark_time_ms,bookmark_time_ms_common,user_rating,user_rating_common) VALUES(?,?,?,?,?,?,?,?,?,?,?,?);", -1, &stmt, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
for (gl=itdb->tracks; gl; gl=gl->next) {
Itdb_Track *track = gl->data;
if (!track->ipod_path) {
continue;
}
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
idx = 0;
/* item_pid */
sqlite3_bind_int64(stmt, ++idx, track->dbid);
/* has_been_played */
sqlite3_bind_int(stmt, ++idx, (track->playcount > 0) ? 1 : 0);
/* date_played */
sqlite3_bind_int(stmt, ++idx, timeconv(track->time_played));
/* play_count_user */
sqlite3_bind_int(stmt, ++idx, track->playcount);
/* play_count_recent */
sqlite3_bind_int(stmt, ++idx, track->recent_playcount);
/* date_skipped */
sqlite3_bind_int(stmt, ++idx, timeconv(track->last_skipped));
/* skip_count_user */
sqlite3_bind_int(stmt, ++idx, track->skipcount);
/* skip_count_recent */
sqlite3_bind_int(stmt, ++idx, track->recent_skipcount);
/* bookmark_time_ms */
sqlite3_bind_double(stmt, ++idx, track->bookmark_time);
/* bookmark_time_ms_common */
/* FIXME: is this the same as bookmark_time_ms ??! */
sqlite3_bind_double(stmt, ++idx, track->bookmark_time);
/* user_rating */
sqlite3_bind_int(stmt, ++idx, track->rating);
/* user_rating_common */
/* FIXME: don't know if this is the right value */
sqlite3_bind_int(stmt, ++idx, track->app_rating);
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 1 sqlite3_step returned %d\n", __func__, res);
}
}
if (stmt) {
sqlite3_finalize(stmt);
stmt = NULL;
}
} else {
printf("[%s] - No tracks available, none written.\n", __func__);
}
/* add playlist info to container_ui */
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"container_ui\" VALUES(?,?,?,?,?,?,?);", -1, &stmt, NULL)) {
fprintf(stderr, "[%s] 2 sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
printf("[%s] - processing %d playlists\n", __func__, g_list_length(itdb->playlists));
for (gl = itdb->playlists; gl; gl = gl->next) {
Itdb_Playlist *pl = (Itdb_Playlist*)gl->data;
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 2 sqlite3_reset returned %d\n", __func__, res);
}
idx = 0;
/* container_pid */
sqlite3_bind_int64(stmt, ++idx, pl->id);
/* play_order TODO: where does this value come from? */
/* FIXME: 5 --> 7, 24 --> 39 ??!? */
switch (pl->sortorder) {
case 5:
sqlite3_bind_int(stmt, ++idx, 7);
break;
case 24:
sqlite3_bind_int(stmt, ++idx, 39);
break;
default:
sqlite3_bind_int(stmt, ++idx, 7);
break;
}
/* is_reversed TODO where does this value come from? */
sqlite3_bind_int(stmt, ++idx, 0);
/* album_field_order TODO where does this value come from? */
sqlite3_bind_int(stmt, ++idx, 1);
/* repeat_mode TODO where does this value come from? */
sqlite3_bind_int(stmt, ++idx, 0);
/* shuffle_items TODO where does this value come from? */
sqlite3_bind_int(stmt, ++idx, 0);
/* has_been_shuffled TODO where does this value come from? */
sqlite3_bind_int(stmt, ++idx, 0);
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 2 sqlite3_step returned %d\n", __func__, res);
}
}
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
if (stmt) {
sqlite3_finalize(stmt);
stmt = NULL;
}
res = 0;
printf("[%s] done.\n", __func__);
leave:
if (db) {
sqlite3_close(db);
}
if (dbf) {
g_free(dbf);
}
return res;
}
static int mk_Extras(Itdb_iTunesDB *itdb, const char *outpath)
{
int res = -1;
int rebuild = 0;
gchar *dbf = NULL;
sqlite3 *db = NULL;
sqlite3_stmt *stmt_chapter = NULL;
char *errmsg = NULL;
struct stat fst;
GList *gl = NULL;
dbf = g_build_filename(outpath, "Extras.itdb", NULL);
printf("[%s] Processing '%s'\n", __func__, dbf);
if (stat(dbf, &fst) != 0) {
if (errno == ENOENT) {
/* file is not present. so we need to create the tables in it ;) */
rebuild = 1;
} else {
fprintf(stderr, "[%s] Error: stat: %s\n", __func__, strerror(errno));
goto leave;
}
}
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
goto leave;
}
sqlite3_exec(db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
if (rebuild) {
fprintf(stderr, "[%s] re-building table structure\n", __func__);
/* db structure needs to be created. */
if (SQLITE_OK != sqlite3_exec(db, Extras_create, NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
}
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"chapter\" VALUES(?,?);", -1, &stmt_chapter, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
/* kill all entries in 'chapter' as they will be re-inserted */
if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM chapter;", NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
/* kill all entries in 'lyrics' as they will be re-inserted */
/* TODO: we do not support this in the moment, so don't touch this */
/*if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM lyrics;", NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}*/
for (gl = itdb->tracks; gl; gl = gl->next) {
Itdb_Track *track = gl->data;
if (track->chapterdata) {
int idx = 0;
GByteArray *chapter_blob = itdb_chapterdata_build_chapter_blob(track->chapterdata, FALSE);
/* printf("[%s] -- inserting into \"chapter\"\n", __func__); */
res = sqlite3_reset(stmt_chapter);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
/* item_pid INTEGER NOT NULL */
sqlite3_bind_int64(stmt_chapter, ++idx, track->dbid);
/* data BLOB */
sqlite3_bind_blob(stmt_chapter, ++idx, chapter_blob->data, chapter_blob->len, SQLITE_TRANSIENT);
res = sqlite3_step(stmt_chapter);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
g_byte_array_free(chapter_blob, TRUE);
}
}
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
res = 0;
printf("[%s] done.\n", __func__);
leave:
if (stmt_chapter) {
sqlite3_finalize(stmt_chapter);
}
if (db) {
sqlite3_close(db);
}
if (dbf) {
g_free(dbf);
}
return res;
}
static int mk_Genius(Itdb_iTunesDB *itdb, const char *outpath)
{
int res = -1;
int rebuild = 0;
gchar *dbf = NULL;
sqlite3 *db = NULL;
char *errmsg = NULL;
struct stat fst;
dbf = g_build_filename(outpath, "Genius.itdb", NULL);
printf("[%s] Processing '%s'\n", __func__, dbf);
if (stat(dbf, &fst) != 0) {
if (errno == ENOENT) {
/* file is not present. so we need to create the tables in it ;) */
rebuild = 1;
} else {
fprintf(stderr, "[%s] Error: stat: %s\n", __func__, strerror(errno));
goto leave;
}
}
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
goto leave;
}
sqlite3_exec(db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
if (rebuild) {
fprintf(stderr, "[%s] re-building table structure\n", __func__);
/* db structure needs to be created. */
if (SQLITE_OK != sqlite3_exec(db, Genius_create, NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
}
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
/* kill all entries in 'genius_config' as they will be re-inserted */
/* TODO: we do not support this in the moment, so don't touch this */
/*if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM genius_config;", NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}*/
/* kill all entries in 'genius_metadata' as they will be re-inserted */
/* TODO: we do not support this in the moment, so don't touch this */
/*if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM genius_metadata;", NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}*/
/* kill all entries in 'genius_similarities' as they will be re-inserted */
/* TODO: we do not support this in the moment, so don't touch this */
/*if (SQLITE_OK != sqlite3_exec(db, "DELETE FROM genius_similarities;", NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}*/
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
res = 0;
printf("[%s] done.\n", __func__);
leave:
if (db) {
sqlite3_close(db);
}
if (dbf) {
g_free(dbf);
}
return res;
}
static void free_key_val_strings(gpointer key, gpointer value, gpointer user_data)
{
g_free(key);
g_free(value);
}
static void bind_first_text(sqlite3_stmt *stmt, int idx, int n, ...)
{
va_list ap;
int i;
va_start(ap, n);
for (i = 0; i < n; i++) {
char *str = va_arg(ap, char*);
if (str && *str) {
sqlite3_bind_text(stmt, idx, str, -1, SQLITE_STATIC);
goto done;
}
}
sqlite3_bind_null(stmt, idx);
done:
va_end(ap);
}
static int mk_Library(Itdb_iTunesDB *itdb,
GHashTable *album_ids, GHashTable *artist_ids,
GHashTable *composer_ids, const char *outpath)
{
int res = -1;
gchar *dbf = NULL;
sqlite3 *db = NULL;
sqlite3_stmt *stmt_version_info = NULL;
sqlite3_stmt *stmt_db_info = NULL;
sqlite3_stmt *stmt_container = NULL;
sqlite3_stmt *stmt_genre_map = NULL;
sqlite3_stmt *stmt_item = NULL;
sqlite3_stmt *stmt_location_kind_map = NULL;
sqlite3_stmt *stmt_avformat_info = NULL;
sqlite3_stmt *stmt_item_to_container = NULL;
sqlite3_stmt *stmt_album = NULL;
sqlite3_stmt *stmt_artist = NULL;
sqlite3_stmt *stmt_composer = NULL;
sqlite3_stmt *stmt_video_info = NULL;
char *errmsg = NULL;
struct stat fst;
GList *gl = NULL;
Itdb_Playlist *dev_playlist = NULL;
int idx = 0;
int pos = 0;
GHashTable *genre_map = NULL;
GHashTable **orders = NULL;
guint32 genre_index;
printf("library_persistent_id = 0x%016"G_GINT64_MODIFIER"x\n", itdb->priv->pid);
dbf = g_build_filename(outpath, "Library.itdb", NULL);
printf("[%s] Processing '%s'\n", __func__, dbf);
if (stat(dbf, &fst) != 0) {
if (errno == ENOENT) {
/* file is not present. so we will create it */
} else {
fprintf(stderr, "[%s] Error: stat: %s\n", __func__, strerror(errno));
goto leave;
}
} else {
/* file is present. delete it, we'll re-create it. */
if (unlink(dbf) != 0) {
fprintf(stderr, "[%s] could not delete '%s': %s\n", __func__, dbf, strerror(errno));
goto leave;
}
}
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
goto leave;
}
sqlite3_exec(db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
fprintf(stderr, "[%s] building table structure\n", __func__);
/* db structure needs to be created. */
if (SQLITE_OK != sqlite3_exec(db, Library_create, NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
fprintf(stderr, "[%s] compiling SQL statements\n", __func__);
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"version_info\" VALUES(?,?,?,?,?,?,?);", -1, &stmt_version_info, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"db_info\" VALUES(?,?,?,?,?,?,?,?);", -1, &stmt_db_info, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"container\" "
"VALUES(?,?,?,?,:name,?,?,?,?,?,?,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);", -1, &stmt_container, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"genre_map\" (id,genre,genre_order) VALUES(?,?,0);", -1, &stmt_genre_map, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"item\" "
"(pid,media_kind,date_modified,year,is_compilation,remember_bookmark,exclude_from_shuffle,artwork_status,artwork_cache_id,"
"start_time_ms,stop_time_ms,total_time_ms,total_burn_time_ms,track_number,track_count,disc_number,disc_count,"
"bpm,relative_volume,eq_preset,radio_stream_status,genre_id,album_pid,artist_pid,composer_pid,title,artist,album,"
"album_artist,composer,sort_title,sort_artist,sort_album,sort_album_artist,sort_composer,title_order,artist_order,"
"album_order,genre_order,composer_order,album_artist_order,album_by_artist_order,series_name_order,comment,grouping,"
"description,description_long) "
"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", -1, &stmt_item, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT OR IGNORE INTO \"location_kind_map\" VALUES(?,:kind);", -1, &stmt_location_kind_map, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"avformat_info\" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);", -1, &stmt_avformat_info, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"item_to_container\" VALUES(?,?,?,?);", -1, &stmt_item_to_container, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT OR IGNORE INTO \"album\" VALUES(?,?,?,?,?,?,?,?,?,?,?);", -1, &stmt_album, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT OR IGNORE INTO \"artist\" VALUES(?,?,?,?,?,?,?);", -1, &stmt_artist, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT OR IGNORE INTO \"composer\" (pid,name,name_order,sort_name) VALUES(?,?,?,?);", -1, &stmt_composer, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"video_info\" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", -1, &stmt_video_info, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
printf("[%s] - inserting into \"version_info\"\n", __func__);
/* INSERT INTO "version_info" VALUES(1,2,40,0,0,0,2); */
idx = 0;
/* id */
sqlite3_bind_int(stmt_version_info, ++idx, 1);
/* major */
/* FIXME: the versioning scheme needs to be updated in libgpod using */
/* major+minor version, this is a workaround */
sqlite3_bind_int(stmt_version_info, ++idx, (itdb->version >= 0x28) ? 2 : 1);
/* minor */
sqlite3_bind_int(stmt_version_info, ++idx, itdb->version);
/* compatibility */
/* This has an unknown meaning. use 0. */
sqlite3_bind_int(stmt_version_info, ++idx, 0);
/* update_level */
/* This has an unknown meaning. use 0. */
sqlite3_bind_int(stmt_version_info, ++idx, 0);
/* device_update_level, default is 0 */
sqlite3_bind_int(stmt_version_info, ++idx, 0);
/* platform, 1 = MacOS, 2 = Windows */
sqlite3_bind_int(stmt_version_info, ++idx, itdb->priv->platform);
res = sqlite3_step(stmt_version_info);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 2 sqlite3_step returned %d\n", __func__, res);
}
printf("[%s] - inserting into \"genre_map\"\n", __func__);
genre_map = g_hash_table_new(g_str_hash, g_str_equal);
/* build genre_map */
genre_index = 1;
for (gl = itdb->tracks; gl; gl = gl->next) {
Itdb_Track *track = gl->data;
if ((track->genre == NULL) || (g_hash_table_lookup(genre_map, track->genre) != NULL))
continue;
g_hash_table_insert(genre_map, track->genre, GUINT_TO_POINTER(genre_index));
res = sqlite3_reset(stmt_genre_map);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
idx = 0;
/* id */
sqlite3_bind_int(stmt_genre_map, ++idx, genre_index++);
/* genre */
sqlite3_bind_text(stmt_genre_map, ++idx, track->genre, -1, SQLITE_STATIC);
res = sqlite3_step(stmt_genre_map);
if (res != SQLITE_DONE) {
fprintf(stderr, "[%s] sqlite3_step returned %d\n", __func__, res);
goto leave;
}
}
pos = 0;
for (gl = itdb->playlists; gl; gl = gl->next) {
int tpos = 0;
GList *glt = NULL;
guint32 types = 0;
Itdb_Playlist *pl = (Itdb_Playlist*)gl->data;
if (pl->type == 1) {
dev_playlist = pl;
}
printf("[%s] - inserting songs into \"item_to_container\"\n", __func__);
for (glt = pl->members; glt; glt = glt->next) {
Itdb_Track *track = glt->data;
/* printf("[%s] -- inserting into \"item_to_container\"\n", __func__); */
res = sqlite3_reset(stmt_item_to_container);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
} /* INSERT INTO "item_to_container" VALUES(-6197982141081478573,959107999841118509,0,NULL); */
idx = 0;
/* item_pid */
sqlite3_bind_int64(stmt_item_to_container, ++idx, track->dbid);
/* container_pid */
sqlite3_bind_int64(stmt_item_to_container, ++idx, pl->id);
/* physical_order */
sqlite3_bind_int(stmt_item_to_container, ++idx, tpos++);
/* shuffle_order */
/* TODO what's this? set to NULL as iTunes does */
sqlite3_bind_null(stmt_item_to_container, ++idx);
res = sqlite3_step(stmt_item_to_container);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
types |= track->mediatype;
}
printf("[%s] - inserting playlist '%s' into \"container\"\n", __func__, pl->name);
res = sqlite3_reset(stmt_container);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
/* INSERT INTO "container" VALUES(959107999841118509,0,267295295,'Hamouda',400,0,1,0,1,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL); */
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_container, ++idx, pl->id);
/* distinguished_kind */
/* TODO: more values? */
if (pl->podcastflag) {
sqlite3_bind_int(stmt_container, ++idx, 11);
} else {
sqlite3_bind_int(stmt_container, ++idx, 0);
}
/* date_created */
sqlite3_bind_int(stmt_container, ++idx, timeconv(pl->timestamp));
/* date_modified */
sqlite3_bind_int(stmt_container, ++idx, timeconv(pl->timestamp));
/* name */
sqlite3_bind_text(stmt_container, ++idx, pl->name, -1, SQLITE_STATIC);
/* name order */
sqlite3_bind_int(stmt_container, ++idx, pos++);
/* parent_pid */
/* TODO: unkown meaning, always 0? */
sqlite3_bind_int(stmt_container, ++idx, 0);
/* media_kinds */
sqlite3_bind_int(stmt_container, ++idx, types);
/* workout_template_id */
/* TODO: seems to be always 0 */
sqlite3_bind_int(stmt_container, ++idx, 0);
/* is_hidden */
/* 1 for dev and podcasts, 0 elsewhere */
if (pl->podcastflag || pl->type == 1) {
sqlite3_bind_int(stmt_container, ++idx, 1);
} else {
sqlite3_bind_int(stmt_container, ++idx, 0);
}
/* smart_is_folder */
/* TODO: smart playlist stuff? */
sqlite3_bind_int(stmt_container, ++idx, 0);
/* iTunes leaves everything else NULL for normal playlists */
res = sqlite3_step(stmt_container);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 4 sqlite3_step returned %d\n", __func__, res);
}
}
if (!dev_playlist) {
fprintf(stderr, "Could not find special device playlist!\n");
goto leave;
}
printf("library_persistent_id = 0x%016"G_GINT64_MODIFIER"x\n", itdb->priv->pid);
if (!dev_playlist->name) {
fprintf(stderr, "Could not fetch device name from itdb!\n");
goto leave;
}
printf("device name = %s\n", dev_playlist->name);
printf("[%s] - inserting into \"db_info\"\n", __func__);
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_db_info, ++idx, itdb->priv->pid);
/* primary_container_pid */
/* ... stored in the playlists where the device name is stored too. */
sqlite3_bind_int64(stmt_db_info, ++idx, dev_playlist->id);
/* media_folder_url */
sqlite3_bind_null(stmt_db_info, ++idx);
/* audio_language */
/* this is +0xA0 */
sqlite3_bind_int(stmt_db_info, ++idx, itdb->priv->audio_language);
/* subtitle_language */
/* this is +0xA2 */
sqlite3_bind_int(stmt_db_info, ++idx, itdb->priv->subtitle_language);
/* genius cuid */
if (itdb->priv->genius_cuid) {
sqlite3_bind_text(stmt_db_info, ++idx, itdb->priv->genius_cuid, -1, SQLITE_STATIC);
} else {
sqlite3_bind_null(stmt_db_info, ++idx);
}
/* bib */
/* TODO: unkown meaning, set to NULL */
sqlite3_bind_null(stmt_db_info, ++idx);
/* rib */
/* TODO: unkown meaning, set to NULL */
sqlite3_bind_null(stmt_db_info, ++idx);
res = sqlite3_step(stmt_db_info);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 3 sqlite3_step returned %d\n", __func__, res);
}
/* for each track: */
printf("[%s] - processing %d tracks\n", __func__, g_list_length(itdb->tracks));
orders = compute_key_orders(itdb->tracks);
for (gl = itdb->tracks; gl; gl = gl->next) {
Itdb_Track *track = gl->data;
Itdb_Item_Id *this_album = NULL;
Itdb_Item_Id *this_artist = NULL;
Itdb_Item_Id *this_composer = NULL;
uint64_t album_pid = 0;
uint64_t artist_pid = 0;
uint64_t composer_pid = 0;
gpointer genre_id = NULL;
int audio_format;
int aw_id;
/* printf("[%s] -- inserting into \"item\"\n", __func__); */
res = sqlite3_reset(stmt_item);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
/* INSERT INTO "item" VALUES(-6197982141081478573,NULL,1,1,0,0,0,0,0,0,0,0,0,0,2004,0,0,0,0,0,0,2,0,0.0,0.0,213812.0,NULL,1,0,0,0,0,0,NULL,NULL,0,0,0,-7670716306765259718,-7576753259813584028,0,'Enjoy the silence 2004 (Reinterpreted by Mike Shinoda)','Depeche Mode','Enjoy The Silence 04 CDS','Depeche Mode',NULL,'Enjoy the silence 2004 (Reinterpreted by Mike Shinoda)','Depeche Mode','Enjoy The Silence 04 CDS','Depeche Mode',NULL,100,100,100,100,100,100,NULL,100,'Lbl MUTE C:MUTE RCDBONG034',NULL,NULL,NULL,1,0,0,0,0,1,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL); */
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_item, ++idx, track->dbid);
/* media_kind */
/* NOTE: this is one of */
/* song = 1 */
/* audio_book = 8 */
/* music_video = 32 */
/* movie = 2 */
/* tv_show = 64 */
/* ringtone = 16384 */
/* podcast = 4 */
/* rental = 32768 */
sqlite3_bind_int(stmt_item, ++idx, track->mediatype);
/* date_modified */
sqlite3_bind_int(stmt_item, ++idx, timeconv(track->time_modified));
/* year */
sqlite3_bind_int(stmt_item, ++idx, track->year);
/* is_compilation */
sqlite3_bind_int(stmt_item, ++idx, track->compilation);
/* remember_bookmark */
sqlite3_bind_int(stmt_item, ++idx, track->remember_playback_position);
/* exclude_from_shuffle */
sqlite3_bind_int(stmt_item, ++idx, track->skip_when_shuffling);
/* artwork_status 1 = has artwork, 2 = doesn't */
sqlite3_bind_int(stmt_item, ++idx, track->has_artwork);
/* artwork_cache_id */
/* TODO check if this value is correct */
aw_id = 0;
if (track->artwork) {
aw_id = track->artwork->id;
}
sqlite3_bind_int(stmt_item, ++idx, aw_id);
/* start_time_ms */
sqlite3_bind_double(stmt_item, ++idx, track->starttime);
/* stop_time_ms */
sqlite3_bind_double(stmt_item, ++idx, track->stoptime);
/* total_time_ms */
sqlite3_bind_double(stmt_item, ++idx, track->tracklen);
/* total_burn_time_ms --> set to NULL */
/* TODO where is this stored? */
sqlite3_bind_null(stmt_item, ++idx);
/* track_number */
sqlite3_bind_int(stmt_item, ++idx, track->track_nr);
/* track_count */
sqlite3_bind_int(stmt_item, ++idx, track->tracks);
/* disc_number */
sqlite3_bind_int(stmt_item, ++idx, track->cd_nr);
/* disc_count */
sqlite3_bind_int(stmt_item, ++idx, track->cds);
/* bpm */
sqlite3_bind_int(stmt_item, ++idx, track->BPM);
/* relative_volume */
sqlite3_bind_int(stmt_item, ++idx, track->volume);
/* eq_preset --> set to NULL */
sqlite3_bind_null(stmt_item, ++idx);
/* radio_stream_status --> set to NULL */
sqlite3_bind_null(stmt_item, ++idx);
/* genre_id */
genre_id = NULL;
if (track->genre && *track->genre && (genre_id = g_hash_table_lookup(genre_map, track->genre)) != NULL) {
sqlite3_bind_int(stmt_item, ++idx, GPOINTER_TO_UINT(genre_id));
}
else
sqlite3_bind_int(stmt_item, ++idx, 0);
/* album_pid -7670716306765259718 */
this_album = g_hash_table_lookup(album_ids, track);
if (this_album) {
album_pid = this_album->sql_id;
}
sqlite3_bind_int64(stmt_item, ++idx, album_pid);
/* artist_pid -7576753259813584028 */
this_artist = g_hash_table_lookup(artist_ids, track);
if (this_artist) {
artist_pid = this_artist->sql_id;
}
sqlite3_bind_int64(stmt_item, ++idx, artist_pid);
/* composer_pid */
this_composer = g_hash_table_lookup(composer_ids, track);
if (this_composer) {
composer_pid = this_composer->sql_id;
}
sqlite3_bind_int64(stmt_item, ++idx, composer_pid);
/* title */
bind_first_text(stmt_item, ++idx, 1, track->title);
/* artist */
bind_first_text(stmt_item, ++idx, 1, track->artist);
/* album */
bind_first_text(stmt_item, ++idx, 1, track->album);
/* album_artist */
bind_first_text(stmt_item, ++idx, 1, track->albumartist);
/* composer */
bind_first_text(stmt_item, ++idx, 1, track->composer);
/* sort_title */
bind_first_text(stmt_item, ++idx, 2, track->sort_title, track->title);
/* sort_artist */
bind_first_text(stmt_item, ++idx, 2, track->sort_artist, track->artist);
/* sort_album */
bind_first_text(stmt_item, ++idx, 2, track->sort_album, track->album);
/* sort_album_artist */
bind_first_text(stmt_item, ++idx, 2, track->sort_albumartist, track->albumartist);
/* sort_composer */
bind_first_text(stmt_item, ++idx, 2, track->sort_composer, track->composer);
/* TODO figure out where these values are stored */
/* title_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_TITLE, track));
/* artist_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_ARTIST, track));
/* album_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_ALBUM, track));
/* genre_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_GENRE, track));
/* composer_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_COMPOSER, track));
/* album_artist_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_ALBUM_ARTIST, track));
/* album_by_artist_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_ALBUM_BY_ARTIST, track));
/* series_name_order */
sqlite3_bind_int(stmt_item, ++idx, lookup_order(orders, ORDER_SERIES_NAME, track));
/* comment */
sqlite3_bind_text(stmt_item, ++idx, track->comment, -1, SQLITE_STATIC);
/* grouping */
bind_first_text(stmt_item, ++idx, 1, track->grouping);
/* description */
bind_first_text(stmt_item, ++idx, 1, track->description);
/* description_long */
/* TODO libgpod doesn't know about it */
sqlite3_bind_null(stmt_item, ++idx);
res = sqlite3_step(stmt_item);
if (res != SQLITE_DONE) {
fprintf(stderr, "[%s] 6 sqlite3_step returned %d\n", __func__, res);
goto leave;
}
/* printf("[%s] -- inserting into \"location_kind_map\" (if required)\n", __func__); */
res = sqlite3_reset(stmt_location_kind_map);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
} /* INSERT INTO "location_kind_map" VALUES(1,'MPEG-Audiodatei'); */
idx = 0;
/* id */
sqlite3_bind_int(stmt_location_kind_map, ++idx, track->mediatype);
/* kind */
sqlite3_bind_text(stmt_location_kind_map, ++idx, track->filetype, -1, SQLITE_STATIC);
res = sqlite3_step(stmt_location_kind_map);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 5 sqlite3_step returned %d: %s\n", __func__, res, sqlite3_errmsg(db));
}
/* printf("[%s] -- inserting into \"avformat_info\"\n", __func__); */
res = sqlite3_reset(stmt_avformat_info);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
} /* INSERT INTO "avformat_info" VALUES(-6197982141081478573,0,301,232,44100.0,9425664,1,576,2880,6224207,0,0,0); */
idx = 0;
/* item_pid */
sqlite3_bind_int64(stmt_avformat_info, ++idx, track->dbid);
/* sub_id */
/* TODO what is this? set to 0 */
sqlite3_bind_int64(stmt_avformat_info, ++idx, 0);
/* audio_format, TODO what's this? */
switch (track->filetype_marker) {
case 0x4d503320:
audio_format = 301;
break;
case 0x4d344120:
case 0x4d345220:
case 0x4d503420:
audio_format = 502;
break;
default:
audio_format = 0;
}
sqlite3_bind_int(stmt_avformat_info, ++idx, audio_format);
/* bit_rate */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->bitrate);
/* sample_rate */
sqlite3_bind_double(stmt_avformat_info, ++idx, track->samplerate);
/* duration (in samples) (track->tracklen is in ms) */
/* iTunes sometimes set it to 0, do that for now since it's easier */
sqlite3_bind_int(stmt_avformat_info, ++idx, 0);
/* gapless_heuristic_info */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->gapless_track_flag);
/* gapless_encoding_delay */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->pregap);
/* gapless_encoding_drain */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->postgap);
/* gapless_last_frame_resynch */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->gapless_data);
/* analysis_inhibit_flags */
/* TODO don't know where this belongs to */
sqlite3_bind_int(stmt_avformat_info, ++idx, 0);
/* audio_fingerprint */
/* TODO this either */
sqlite3_bind_int(stmt_avformat_info, ++idx, 0);
/* volume_normalization_energy */
sqlite3_bind_int(stmt_avformat_info, ++idx, track->soundcheck);
res = sqlite3_step(stmt_avformat_info);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 7 sqlite3_step returned %d\n", __func__, res);
}
/* this is done by a trigger, so we don't need to do this :-D */
/* INSERT INTO "ext_item_view_membership" VALUES(-6197982141081478573,0,0); */
if (this_album) {
/* printf("[%s] -- inserting into \"album\" (if required)\n", __func__); */
res = sqlite3_reset(stmt_album);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
/* INSERT INTO "album" VALUES(-7670716306765259718,2,0,0,-7576753259813584028,0,'Enjoy The Silence 04 CDS',100,0,NULL,0); */
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_album, ++idx, album_pid);
/* kind */
/* TODO use field from mhia */
sqlite3_bind_int(stmt_album, ++idx, 2);
/* artwork_status */
/* TODO */
sqlite3_bind_int(stmt_album, ++idx, 0);
/* artwork_item_pid */
/* TODO */
sqlite3_bind_int(stmt_album, ++idx, 0);
/* artist_pid */
sqlite3_bind_int64(stmt_album, ++idx, artist_pid);
/* user_rating */
sqlite3_bind_int(stmt_album, ++idx, 0);
/* name */
sqlite3_bind_text(stmt_album, ++idx, track->album, -1, SQLITE_STATIC);
/* name_order */
sqlite3_bind_int(stmt_album, ++idx, lookup_order(orders, ORDER_ALBUM_ARTIST, track));
/* all_compilations */
/* TODO */
sqlite3_bind_int(stmt_album, ++idx, 0);
/* feed_url */
/* TODO this is for podcasts? */
sqlite3_bind_null(stmt_album, ++idx);
/* season_number */
/* TODO this is for tv shows?! */
sqlite3_bind_int(stmt_album, ++idx, 0);
res = sqlite3_step(stmt_album);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
}
if (this_artist) {
/* printf("[%s] -- inserting into \"artist\" (if required)\n", __func__); */
res = sqlite3_reset(stmt_artist);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
/* INSERT INTO "artist" VALUES(-7576753259813584028,2,0,0,'Depeche Mode',100,'Depeche Mode'); */
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_artist, ++idx, artist_pid);
/* kind */
/* TODO use field from mhii */
sqlite3_bind_int(stmt_artist, ++idx, 2);
/* artwork_status */
/* TODO */
sqlite3_bind_int(stmt_artist, ++idx, 0);
/* artwork_album_pid */
/* TODO */
sqlite3_bind_int(stmt_artist, ++idx, 0);
/* name */
sqlite3_bind_text(stmt_artist, ++idx, track->artist, -1, SQLITE_STATIC);
/* name_order */
sqlite3_bind_int(stmt_artist, ++idx, lookup_order(orders, ORDER_ARTIST, track));
/* sort_name */
/* TODO always same as 'name'? */
sqlite3_bind_text(stmt_artist, ++idx, track->artist, -1, SQLITE_STATIC);
res = sqlite3_step(stmt_artist);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
}
if (this_composer) {
/* printf("[%s] -- inserting into \"composer\" (if required)\n", __func__); */
res = sqlite3_reset(stmt_composer);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
idx = 0;
/* pid */
sqlite3_bind_int64(stmt_composer, ++idx, composer_pid);
/* name */
sqlite3_bind_text(stmt_composer, ++idx, track->composer, -1, SQLITE_STATIC);
/* name_order */
sqlite3_bind_int(stmt_composer, ++idx, lookup_order(orders, ORDER_COMPOSER, track));
/* sort_name */
/* TODO always same as 'name'? */
sqlite3_bind_text(stmt_composer, ++idx, track->composer, -1, SQLITE_STATIC);
res = sqlite3_step(stmt_composer);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
}
/* if it's a movie, music video or tv show */
if ((track->mediatype & ITDB_MEDIATYPE_MOVIE)
|| (track->mediatype & ITDB_MEDIATYPE_MUSICVIDEO)
|| (track->mediatype & ITDB_MEDIATYPE_TVSHOW)) {
/* printf("[%s] -- inserting into \"video_info\"\n", __func__); */
res = sqlite3_reset(stmt_video_info);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] 1 sqlite3_reset returned %d\n", __func__, res);
}
idx = 0;
/* item_pid INTEGER NOT NULL */
sqlite3_bind_int64(stmt_video_info, ++idx, track->dbid);
/* has_alternate_audio INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* has_subtitles INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* characteristics_valid INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* has_closed_captions INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* is_self_contained INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* is_compressed INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* is_anamorphic INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* season_number INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, track->season_nr);
/* audio_language INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* audio_track_index INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* audio_track_id INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* subtitle_language INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* subtitle_track_index INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* subtitle_track_id INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, 0);
/* series_name TEXT */
sqlite3_bind_text(stmt_video_info, ++idx, track->tvshow, -1, SQLITE_STATIC);
/* sort_series_name TEXT */
bind_first_text(stmt_item, ++idx, 2, track->sort_tvshow, track->tvshow);
/* episode_id TEXT */
sqlite3_bind_text(stmt_video_info, ++idx, track->tvepisode, -1, SQLITE_STATIC);
/* episode_sort_id INTEGER */
sqlite3_bind_int(stmt_video_info, ++idx, track->season_nr << 16 | track->episode_nr);
/* network_name TEXT */
sqlite3_bind_text(stmt_video_info, ++idx, track->tvnetwork, -1, SQLITE_STATIC);
/* extended_content_rating TEXT */
sqlite3_bind_null(stmt_video_info, ++idx);
/* movie_info TEXT */
sqlite3_bind_null(stmt_video_info, ++idx);
res = sqlite3_step(stmt_video_info);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 8 sqlite3_step returned %d\n", __func__, res);
}
}
}
sqlite3_exec(db, "UPDATE album SET artwork_item_pid = (SELECT item.pid FROM item WHERE item.artwork_cache_id != 0 AND item.album_pid = album.pid LIMIT 1);", NULL, NULL, NULL);
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
res = 0;
printf("[%s] done.\n", __func__);
leave:
if (orders != NULL) {
destroy_key_orders(orders);
}
if (stmt_version_info) {
sqlite3_finalize(stmt_version_info);
}
if (stmt_db_info) {
sqlite3_finalize(stmt_db_info);
}
if (stmt_container) {
sqlite3_finalize(stmt_container);
}
if (stmt_genre_map) {
sqlite3_finalize(stmt_genre_map);
}
if (stmt_item) {
sqlite3_finalize(stmt_item);
}
if (stmt_location_kind_map) {
sqlite3_finalize(stmt_location_kind_map);
}
if (stmt_avformat_info) {
sqlite3_finalize(stmt_avformat_info);
}
if (stmt_item_to_container) {
sqlite3_finalize(stmt_item_to_container);
}
if (stmt_album) {
sqlite3_finalize(stmt_album);
}
if (stmt_artist) {
sqlite3_finalize(stmt_artist);
}
if (stmt_composer) {
sqlite3_finalize(stmt_composer);
}
if (stmt_video_info) {
sqlite3_finalize(stmt_video_info);
}
if (genre_map) {
g_hash_table_destroy(genre_map);
}
if (db) {
sqlite3_close(db);
}
if (dbf) {
g_free(dbf);
}
return res;
}
static int mk_Locations(Itdb_iTunesDB *itdb, const char *outpath, const char *uuid)
{
int res = -1;
gchar *dbf = NULL;
sqlite3 *db = NULL;
sqlite3_stmt *stmt = NULL;
char *errmsg = NULL;
int idx = 0;
dbf = g_build_filename(outpath, "Locations.itdb", NULL);
printf("[%s] Processing '%s'\n", __func__, dbf);
/* file is present. delete it, we'll re-create it. */
if (g_unlink(dbf) != 0) {
if (errno != ENOENT) {
fprintf(stderr, "[%s] could not delete '%s': %s\n", __func__, dbf, strerror(errno));
goto leave;
}
}
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
goto leave;
}
sqlite3_exec(db, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
fprintf(stderr, "[%s] re-building table structure\n", __func__);
/* db structure needs to be created. */
if (SQLITE_OK != sqlite3_exec(db, Locations_create, NULL, NULL, &errmsg)) {
fprintf(stderr, "[%s] sqlite3_exec error: %s\n", __func__, sqlite3_errmsg(db));
if (errmsg) {
fprintf(stderr, "[%s] additional error information: %s\n", __func__, errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
}
goto leave;
}
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"base_location\" (id, path) VALUES (?,?);", -1, &stmt, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
idx = 0;
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] sqlite3_reset returned %d\n", __func__, res);
}
sqlite3_bind_int(stmt, ++idx, 1);
if (itdb_device_is_iphone_family(itdb->device)) {
sqlite3_bind_text(stmt, ++idx, "iTunes_Control/Music", -1, SQLITE_STATIC);
} else {
sqlite3_bind_text(stmt, ++idx, "iPod_Control/Music", -1, SQLITE_STATIC);
}
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] sqlite3_step returned %d\n", __func__, res);
}
if (itdb_device_is_iphone_family(itdb->device)) {
idx = 0;
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] sqlite3_reset returned %d\n", __func__, res);
}
sqlite3_bind_int(stmt, ++idx, 4);
sqlite3_bind_text(stmt, ++idx, "Podcasts", -1, SQLITE_STATIC);
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] sqlite3_step returned %d\n", __func__, res);
}
idx = 0;
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] sqlite3_reset returned %d\n", __func__, res);
}
sqlite3_bind_int(stmt, ++idx, 6);
sqlite3_bind_text(stmt, ++idx, "iTunes_Control/Ringtones", -1, SQLITE_STATIC);
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] sqlite3_step returned %d\n", __func__, res);
}
}
if (stmt) {
sqlite3_finalize(stmt);
}
if (SQLITE_OK != sqlite3_prepare_v2(db, "INSERT INTO \"location\" (item_pid, sub_id, base_location_id, location_type, location, extension, kind_id, date_created, file_size) VALUES(?,?,?,?,?,?,?,?,?);", -1, &stmt, NULL)) {
fprintf(stderr, "[%s] sqlite3_prepare error: %s\n", __func__, sqlite3_errmsg(db));
goto leave;
}
if (itdb->tracks) {
GList *gl = NULL;
printf("[%s] Processing %d tracks...\n", __func__, g_list_length(itdb->tracks));
for (gl=itdb->tracks; gl; gl=gl->next) {
Itdb_Track *track = gl->data;
char *ipod_path;
int i = 0;
int cnt = 0;
int pos = 0;
int res;
if (!track->ipod_path) {
continue;
}
ipod_path = strdup(track->ipod_path);
idx = 0;
res = sqlite3_reset(stmt);
if (res != SQLITE_OK) {
fprintf(stderr, "[%s] sqlite3_reset returned %d\n", __func__, res);
}
/* item_pid */
sqlite3_bind_int64(stmt, ++idx, track->dbid);
/* sub_id */
/* TODO subitle id? set to 0. */
sqlite3_bind_int64(stmt, ++idx, 0);
/* base_location_id */
/* use 1 here as this is 'iTunes_Control/Music' */
sqlite3_bind_int(stmt, ++idx, 1);
/* location_type */
/* TODO this should always be 0x46494C45 = "FILE" for now, */
/* perhaps later libgpod will support more. */
sqlite3_bind_int(stmt, ++idx, 0x46494C45);
/* location */
for (i = 0; i < strlen(ipod_path); i++) {
/* replace all ':' with '/' so that the path is valid */
if (ipod_path[i] == ':') {
ipod_path[i] = '/';
cnt++;
if (cnt == 3) {
pos = i+1;
}
}
}
sqlite3_bind_text(stmt, ++idx, ipod_path + pos, -1, SQLITE_STATIC);
/* extension */
sqlite3_bind_int(stmt, ++idx, track->filetype_marker);
/* kind_id, should match track->mediatype */
sqlite3_bind_int(stmt, ++idx, track->mediatype);
/* date_created */
sqlite3_bind_int(stmt, ++idx, timeconv(track->time_modified));
/* file_size */
sqlite3_bind_int(stmt, ++idx, track->size);
#if 0
/* file_creator */
/* TODO unknown, set to NULL */
sqlite3_bind_null(stmt, ++idx);
/* file_type */
/* TODO unknown, set to NULL */
sqlite3_bind_null(stmt, ++idx);
/* num_dir_levels_file */
/* TODO unknown, set to NULL */
sqlite3_bind_null(stmt, ++idx);
/* num_dir_levels_lib */
/* TODO unknown, set to NULL */
sqlite3_bind_null(stmt, ++idx);
#endif
res = sqlite3_step(stmt);
if (res == SQLITE_DONE) {
/* expected result */
} else {
fprintf(stderr, "[%s] 10 sqlite3_step returned %d\n", __func__, res);
}
if (ipod_path) {
free(ipod_path);
ipod_path = NULL;
}
}
} else {
printf("[%s] No tracks available, none written.\n", __func__);
}
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
res = 0;
printf("[%s] done.\n", __func__);
leave:
if (stmt) {
sqlite3_finalize(stmt);
}
if (db) {
sqlite3_close(db);
}
if (dbf) {
g_free(dbf);
}
return res;
}
static void sort_key_get_buffer_boundaries(const char* sval, int *length, int *word_offset)
{
int word_count = 0;
int i = 0;
int l = 0;
int o = 0;
char *sval_uppercase = NULL;
if (sval && strlen(sval)) {
sval_uppercase = g_ascii_strup(sval, strlen(sval));
while (sval_uppercase[i]) {
if (g_ascii_isalnum(sval_uppercase[i])) {
l++;
} else {
switch (sval_uppercase[i]) {
case ' ':
word_count++;
l++;
break;
case ':':
case '-':
case ',':
case '.':
case '\'':
default:
l += 2;
break;
}
}
i++;
}
free(sval_uppercase);
word_count++;
/* magic + transformed string + length + word weights + null */
o = 1 + l + 3;
l = o + (word_count*2) + 1;
} else {
l = 4;
}
*length = l;
*word_offset = o;
}
static void sqlite_func_iphone_sort_key(sqlite3_context *context, int argc, sqlite3_value **argv)
{
const char *sval;
char *sval_uppercase = NULL;
char *buffer = NULL;
int word_count = 0;
int word_offset = 0;
int word_length = 0;
int i = 0;
int buffer_index = 0;
int buffer_size = 0;
if (argc != 1)
fprintf(stderr, "[%s] Error: Unexpected number of arguments: %d\n", __func__, argc);
switch (sqlite3_value_type(argv[0])) {
case SQLITE_TEXT:
sval = (const char*)sqlite3_value_text(argv[0]);
sort_key_get_buffer_boundaries(sval, &buffer_size, &word_offset);
buffer = (char*)malloc(buffer_size);
memset(buffer, '\0', buffer_size);
buffer[buffer_index] = 0x31;
if (sval) {
if (buffer_size > 4) {
buffer[buffer_index++] = 0x30;
/* transform text value */
/* uppercase the text */
sval_uppercase = g_ascii_strup(sval, strlen(sval));
while (sval_uppercase[i]) {
word_length++;
if (g_ascii_isalnum(sval_uppercase[i])) {
/* transform regular character */
buffer[buffer_index++] = sval_uppercase[i] - (0x55 - sval_uppercase[i]);
} else {
/* transform special chars (punctuation,special) */
switch (sval_uppercase[i]) {
case ' ':
buffer[buffer_index++] = 0x06;
word_length--;
/* since we reached word end, calculate word weight */
buffer[word_offset + word_count*2] = 0x8f;
buffer[word_offset + word_count*2 + 1] = (char)(0x86 - word_length);
word_count++;
word_length = 0;
break;
case ':':
buffer[buffer_index++] = 0x07;
buffer[buffer_index++] = 0xd8;
break;
case '-':
buffer[buffer_index++] = 0x07;
buffer[buffer_index++] = 0x90;
break;
case ',':
buffer[buffer_index++] = 0x07;
buffer[buffer_index++] = 0xb2;
break;
case '.':
buffer[buffer_index++] = 0x08;
buffer[buffer_index++] = 0x51;
break;
case '\'':
buffer[buffer_index++] = 0x07;
buffer[buffer_index++] = 0x31;
break;
default:
/* FIXME: We just simulate "-" for any other char, needs proper conversion */
buffer[buffer_index++] = 0x07;
buffer[buffer_index++] = 0x90;
break;
}
}
i++;
}
g_free(sval_uppercase);
/* calculate word weight for last word */
buffer[word_offset + word_count*2] = 0x8f;
buffer[word_offset + word_count*2 + 1] = 3 + word_length;
word_count++;
word_length = 0;
/* write length of input string */
buffer[word_offset - 3] = 0x01;
buffer[word_offset - 2] = i + 4; /* length of input string + 4 */
buffer[word_offset - 1] = 0x01;
} else {
buffer[0] = 0x31;
buffer[1] = 0x01;
buffer[2] = 0x01;
}
}
sqlite3_result_blob(context, buffer, buffer_size, free);
break;
case SQLITE_NULL:
buffer = (char*)malloc(4);
memcpy(buffer, "\x31\x01\x01\x00", 4);
sqlite3_result_blob(context, buffer, 4, free);
break;
default:
sqlite3_result_null(context);
break;
}
}
static void sqlite_func_iphone_sort_section(sqlite3_context *context, int argc, sqlite3_value **argv)
{
const unsigned char *sval;
int res = 26;
if (argc != 1)
fprintf(stderr, "[%s] Error: Unexpected number of arguments: %d\n", __func__, argc);
switch (sqlite3_value_type(argv[0])) {
case SQLITE_BLOB:
case SQLITE_TEXT:
sval = sqlite3_value_text(argv[0]);
if (sval && (sval[0] == 0x30) && (sval[1] >= 0x2D) && (sval[1] <= 0x5F)) {
res = (sval[1] - 0x2D) / 2;
}
break;
default:
break;
}
sqlite3_result_int(context, res);
}
static void run_post_process_commands(Itdb_iTunesDB *itdb, const char *outpath, const char *uuid)
{
plist_t plist_node = NULL;
plist_t ppc_dict = NULL;
const gchar *basedb = "Library.itdb";
const gchar *otherdbs[] = {"Dynamic.itdb", "Extras.itdb", "Genius.itdb", "Locations.itdb", NULL};
int res;
sqlite3 *db = NULL;
#ifdef HAVE_LIBIMOBILEDEVICE
if (itdb_device_is_iphone_family(itdb->device)) {
/* get SQL post process commands via lockdown (iPhone/iPod Touch) */
lockdownd_client_t client = NULL;
idevice_t phone = NULL;
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
lockdownd_error_t lockdownerr = LOCKDOWN_E_UNKNOWN_ERROR;
ret = idevice_new(&phone, uuid);
if (ret != IDEVICE_E_SUCCESS) {
printf("[%s] ERROR: Could not find device with uuid %s, is it plugged in?\n", __func__, uuid);
goto leave;
}
if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "libgpod")) {
printf("[%s] ERROR: Could not connect to device's lockdownd!\n", __func__);
idevice_free(phone);
goto leave;
}
lockdownerr = lockdownd_get_value(client, "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands", NULL, &plist_node);
lockdownd_client_free(client);
idevice_free(phone);
if (lockdownerr == LOCKDOWN_E_SUCCESS) {
ppc_dict = plist_node;
} else {
if (plist_node) {
plist_free(plist_node);
plist_node = NULL;
}
}
} else
#endif
if (itdb->device->sysinfo_extended != NULL) {
/* try to get SQL post process commands via sysinfo_extended */
gchar *dev_path = itdb_get_device_dir(itdb->device->mountpoint);
if (dev_path) {
const gchar *p_sysinfo_ex[] = {"SysInfoExtended", NULL};
gchar *sysinfo_ex_path = itdb_resolve_path(dev_path, p_sysinfo_ex);
g_free(dev_path);
if (sysinfo_ex_path) {
/* open plist file */
char *xml_contents = NULL;
gsize xml_length = 0;
if (g_file_get_contents(sysinfo_ex_path, &xml_contents, &xml_length, NULL)) {
plist_from_xml(xml_contents, xml_length, &plist_node);
if (plist_node) {
/* locate specific key */
ppc_dict = plist_dict_get_item(plist_node, "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands");
}
}
if (xml_contents) {
g_free(xml_contents);
}
g_free(sysinfo_ex_path);
}
}
}
if (ppc_dict) {
plist_dict_iter iter = NULL;
plist_t sql_cmds = NULL;
plist_t user_ver_cmds = NULL;
printf("[%s] Getting SQL post process commands\n", __func__);
sql_cmds = plist_dict_get_item(ppc_dict, "SQLCommands");
user_ver_cmds = plist_dict_get_item(ppc_dict, "UserVersionCommandSets");
if (sql_cmds && user_ver_cmds) {
/* we found the SQLCommands and the UserVersionCommandSets keys */
char *key = NULL;
unsigned long int maxver = 0;
plist_t curnode = user_ver_cmds;
plist_t subnode = NULL;
user_ver_cmds = NULL;
/* now look for numbered subkey in the UserVersionCommandsSets */
plist_dict_new_iter(curnode, &iter);
if (iter) {
plist_dict_next_item(curnode, iter, &key, &subnode);
while (subnode) {
unsigned long int intval = strtoul(key, NULL, 0);
if ((intval > 0) && (intval > maxver)) {
user_ver_cmds = subnode;
maxver = intval;
}
subnode = NULL;
free(key);
key = NULL;
plist_dict_next_item(curnode, iter, &key, &subnode);
}
free(iter);
iter = NULL;
}
if (user_ver_cmds) {
/* found numbered key (usually '8', for Nano 5G it is '9') */
curnode = user_ver_cmds;
/* now get the commands array */
user_ver_cmds = plist_dict_get_item(curnode, "Commands");
if (user_ver_cmds && (plist_get_node_type(user_ver_cmds) == PLIST_ARRAY)) {
/* We found our array with the commands to execute, now
* make a hashmap for the SQLCommands to find them faster
* when we actually execute them in correct order. */
GHashTable *sqlcmd_map = g_hash_table_new(g_str_hash, g_str_equal);
if (sqlcmd_map) {
char *val = NULL;
gchar *dbf = NULL;
gchar *attach_str = NULL;
char *errmsg = NULL;
guint32 i = 0, cnt = 0, ok_cnt = 0;
plist_dict_new_iter(sql_cmds, &iter);
if (iter) {
plist_dict_next_item(sql_cmds, iter, &key, &subnode);
while (subnode) {
if (plist_get_node_type(subnode) == PLIST_STRING) {
plist_get_string_val(subnode, &val);
g_hash_table_insert(sqlcmd_map, key, val);
val = NULL;
} else {
printf("[%s] WARNING: ignoring non-string value for key '%s'\n", __func__, key);
free(key);
}
subnode = NULL;
key = NULL;
plist_dict_next_item(sql_cmds, iter, &key, &subnode);
}
free(iter);
iter = NULL;
}
/* open Library.itdb first */
dbf = g_build_filename(outpath, basedb, NULL);
if (SQLITE_OK != sqlite3_open((const char*)dbf, &db)) {
fprintf(stderr, "Error opening database '%s': %s\n", dbf, sqlite3_errmsg(db));
g_free(dbf);
goto leave;
}
g_free(dbf);
/* now attach other required database files */
i = 0;
while (otherdbs[i]) {
errmsg = NULL;
dbf = g_build_filename(outpath, otherdbs[i], NULL);
attach_str = g_strdup_printf("ATTACH DATABASE '%s' AS '%s';", dbf, otherdbs[i]);
g_free(dbf);
res = sqlite3_exec(db, attach_str, NULL, NULL, &errmsg);
g_free(attach_str);
if (res != SQLITE_OK) {
printf("[%s] WARNING: Could not attach database '%s': %s\n", __func__, otherdbs[i], errmsg);
}
if (errmsg) {
free(errmsg);
}
i++;
}
printf("[%s] binding functions\n", __func__);
sqlite3_create_function(db, "iPhoneSortKey", 1, SQLITE_ANY, NULL, &sqlite_func_iphone_sort_key, NULL, NULL);
sqlite3_create_function(db, "iPhoneSortSection", 1, SQLITE_ANY, NULL, &sqlite_func_iphone_sort_section, NULL, NULL);
cnt = plist_array_get_size(user_ver_cmds);
printf("[%s] Running %d post process commands now\n", __func__, cnt);
sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
for (i=0; i<cnt; i++) {
subnode = plist_array_get_item(user_ver_cmds, i);
plist_get_string_val(subnode, &key);
if (key) {
val = g_hash_table_lookup(sqlcmd_map, key);
if (val) {
char *errmsg = NULL;
if (SQLITE_OK == sqlite3_exec(db, val, NULL, NULL, &errmsg)) {
/*printf("[%s] executing '%s': OK", __func__, key);*/
ok_cnt++;
} else {
printf("[%s] ERROR when executing '%s': %s\n", __func__, key, errmsg);
}
if (errmsg) {
sqlite3_free(errmsg);
}
} else {
printf("[%s] value for '%s' not found in hashmap!\n", __func__, key);
}
free(key);
key = NULL;
}
}
g_hash_table_foreach(sqlcmd_map, free_key_val_strings, NULL);
g_hash_table_destroy(sqlcmd_map);
printf("[%s] %d out of %d post process commands successfully executed\n", __func__, ok_cnt, cnt);
/* TODO perhaps we want to roll back when an error has occured ? */
sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
} else {
printf("[%s]: Error: could not create hash table!\n", __func__);
}
} else {
printf("[%s]: Error fetching commands array\n", __func__);
}
} else {
printf("[%s]: Error fetching user version command set\n", __func__);
}
} else {
printf("[%s]: Error fetching post process commands from device!\n", __func__);
}
}
printf("[%s] done.\n", __func__);
leave:
if (db) {
sqlite3_close(db);
}
if (plist_node) {
plist_free(plist_node);
}
}
static int cbk_calc_sha1_one_block(FILE *f, unsigned char sha1[20])
{
const guint BLOCK_SIZE = 1024;
unsigned char block[BLOCK_SIZE];
size_t read_count;
GChecksum *checksum;
gsize sha1_len;
read_count = fread(block, BLOCK_SIZE, 1, f);
if ((read_count != 1)) {
if (feof(f)) {
return 1;
} else {
return -1;
}
}
sha1_len = g_checksum_type_get_length(G_CHECKSUM_SHA1);
g_assert (sha1_len == 20);
checksum = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum, block, BLOCK_SIZE);
g_checksum_get_digest(checksum, sha1, &sha1_len);
g_checksum_free(checksum);
return 0;
}
static gboolean cbk_calc_sha1s(const char *filename, GArray *sha1s)
{
FILE *f;
int calc_ok;
f = fopen(filename, "rb");
if (f == NULL) {
return FALSE;
}
do {
unsigned char sha1[20];
calc_ok = cbk_calc_sha1_one_block(f, sha1);
if (calc_ok != 0) {
break;
}
g_array_append_vals(sha1s, sha1, sizeof(sha1));
} while (calc_ok == 0);
if (calc_ok < 0) {
goto error;
}
fclose(f);
return TRUE;
error:
fclose(f);
return FALSE;
}
static void cbk_calc_sha1_of_sha1s(GArray *cbk, guint cbk_header_size)
{
GChecksum *checksum;
unsigned char* final_sha1;
unsigned char* sha1s;
gsize final_sha1_len;
g_assert (cbk->len > cbk_header_size + 20);
final_sha1 = &g_array_index(cbk, guchar, cbk_header_size);
sha1s = &g_array_index(cbk, guchar, cbk_header_size + 20);
final_sha1_len = g_checksum_type_get_length(G_CHECKSUM_SHA1);
g_assert (final_sha1_len == 20);
checksum = g_checksum_new(G_CHECKSUM_SHA1);
g_checksum_update(checksum, sha1s, cbk->len - (cbk_header_size + 20));
g_checksum_get_digest(checksum, final_sha1, &final_sha1_len);
g_checksum_free(checksum);
}
static gboolean mk_Locations_cbk(Itdb_iTunesDB *itdb, const char *dirname)
{
char *locations_filename;
char *cbk_filename;
GArray *cbk;
gboolean success;
guchar *cbk_hash;
guchar *final_sha1;
guint cbk_header_size = 0;
guint checksum_type;
checksum_type = itdb_device_get_checksum_type(itdb->device);
switch (checksum_type) {
case ITDB_CHECKSUM_HASHAB:
cbk_header_size = 57;
break;
case ITDB_CHECKSUM_HASH58:
/* the nano 5g advertises DBVersion 3 but expects an hash72 on
* its cbk file.
*/
case ITDB_CHECKSUM_HASH72:
cbk_header_size = 46;
break;
default:
break;
}
if (cbk_header_size == 0) {
fprintf(stderr, "ERROR: Unsupported checksum type '%d' in cbk file generation!\n", checksum_type);
return FALSE;
}
cbk = g_array_sized_new(FALSE, TRUE, 1,
cbk_header_size + 20);
g_array_set_size(cbk, cbk_header_size + 20);
locations_filename = g_build_filename(dirname, "Locations.itdb", NULL);
success = cbk_calc_sha1s(locations_filename, cbk);
g_free(locations_filename);
if (!success) {
g_array_free(cbk, TRUE);
return FALSE;
}
cbk_calc_sha1_of_sha1s(cbk, cbk_header_size);
final_sha1 = &g_array_index(cbk, guchar, cbk_header_size);
cbk_hash = &g_array_index(cbk, guchar, 0);
switch (checksum_type) {
case ITDB_CHECKSUM_HASHAB:
success = itdb_hashAB_compute_hash_for_sha1 (itdb->device, final_sha1, cbk_hash, NULL);
break;
case ITDB_CHECKSUM_HASH58:
/* the nano 5g advertises DBVersion 3 but expects an hash72 on
* its cbk file.
*/
case ITDB_CHECKSUM_HASH72:
success = itdb_hash72_compute_hash_for_sha1 (itdb->device, final_sha1, cbk_hash, NULL);
break;
default:
break;
}
if (!success) {
g_array_free(cbk, TRUE);
return FALSE;
}
cbk_filename = g_build_filename(dirname, "Locations.itdb.cbk", NULL);
success = g_file_set_contents(cbk_filename, cbk->data, cbk->len, NULL);
g_free(cbk_filename);
g_array_free(cbk, TRUE);
return success;
}
static int build_itdb_files(Itdb_iTunesDB *itdb,
GHashTable *album_ids, GHashTable *artist_ids,
GHashTable *composer_ids,
const char *outpath, const char *uuid,
GError **error)
{
if (mk_Dynamic(itdb, outpath) != 0) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Dynamic.itdb generation");
return -1;
}
if (mk_Extras(itdb, outpath) != 0) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Extras.itdb generation");
return -1;
}
if (mk_Genius(itdb, outpath) != 0) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Genius.itdb generation");
return -1;
}
if (mk_Library(itdb, album_ids, artist_ids, composer_ids, outpath) != 0) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Library.itdb generation");
return -1;
}
if (mk_Locations(itdb, outpath, uuid) != 0) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Locations.itdb generation");
return -1;
}
run_post_process_commands(itdb, outpath, uuid);
if (!mk_Locations_cbk(itdb, outpath)) {
g_set_error (error, ITDB_ERROR, ITDB_ERROR_SQLITE,
"an error occurred during Locations.itdb.cbk generation");
return -1;
}
return 0;
}
static int ensure_itlp_dir_exists(const char *itlpdir, GError **error)
{
/* check if directory exists */
if (!g_file_test(itlpdir, G_FILE_TEST_EXISTS)) {
if (g_mkdir(itlpdir, 0755) != 0) {
g_set_error (error, G_FILE_ERROR,
g_file_error_from_errno(errno),
"Could not create directory '%s': %s",
itlpdir, strerror(errno));
return FALSE;
}
} else if (!g_file_test(itlpdir, G_FILE_TEST_IS_DIR)) {
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR,
"'%s' is not a directory as it should be",
itlpdir);
return FALSE;
}
return TRUE;
}
static int copy_itdb_file(const gchar *from_dir, const gchar *to_dir,
const gchar *fname, GError **error)
{
int res = 0;
gchar *srcname = g_build_filename(from_dir, fname, NULL);
gchar *dstname = g_build_filename(to_dir, fname, NULL);
if (itdb_cp(srcname, dstname, error)) {
fprintf(stderr, "itdbprep: copying '%s'\n", fname);
res++;
}
if (error && *error) {
fprintf(stderr, "Error copying '%s' to '%s': %s\n", srcname, dstname, (*error)->message);
}
if (srcname) {
g_free(srcname);
}
if (dstname) {
g_free(dstname);
}
return res;
}
static void rmdir_recursive(gchar *path)
{
GDir *cur_dir;
const gchar *dir_file;
cur_dir = g_dir_open(path, 0, NULL);
if (cur_dir) while ((dir_file = g_dir_read_name(cur_dir)))
{
gchar *fpath = g_build_filename(path, dir_file, NULL);
if (fpath) {
if (g_file_test(fpath, G_FILE_TEST_IS_DIR)) {
rmdir_recursive(fpath);
} else {
g_unlink(fpath);
}
g_free(fpath);
}
}
if (cur_dir) {
g_dir_close(cur_dir);
}
g_rmdir(path);
}
int itdb_sqlite_generate_itdbs(FExport *fexp)
{
int res = 0;
gchar *itlpdir;
gchar *dirname;
gchar *tmpdir = NULL;
printf("libitdbprep: %s called with file %s and uuid %s\n", __func__,
fexp->itdb->filename, itdb_device_get_uuid(fexp->itdb->device));
dirname = itdb_get_itunes_dir(itdb_get_mountpoint(fexp->itdb));
itlpdir = g_build_filename(dirname, "iTunes Library.itlp", NULL);
g_free(dirname);
printf("itlp directory='%s'\n", itlpdir);
if (!ensure_itlp_dir_exists(itlpdir, &fexp->error)) {
res = -1;
goto leave;
}
printf("*.itdb files will be stored in '%s'\n", itlpdir);
g_assert(fexp->itdb != NULL);
g_assert(fexp->itdb->playlists != NULL);
tzoffset = fexp->itdb->tzoffset;
tmpdir = g_build_path(g_get_tmp_dir(), tmpnam(NULL), NULL);
if (g_mkdir(tmpdir, 0755) != 0) {
g_set_error (&fexp->error, G_FILE_ERROR, g_file_error_from_errno(errno),
"Could not create temporary directory '%s': %s",
tmpdir, strerror(errno));
res = -1;
goto leave;
}
/* generate itdb files in temporary directory */
if (build_itdb_files(fexp->itdb, fexp->albums, fexp->artists, fexp->composers, tmpdir,
itdb_device_get_uuid(fexp->itdb->device), &fexp->error) != 0) {
g_prefix_error (&fexp->error, "Failed to generate sqlite database: ");
res = -1;
goto leave;
} else {
/* copy files */
const char *itdb_files[] = { "Dynamic.itdb", "Extras.itdb",
"Genius.itdb", "Library.itdb",
"Locations.itdb", "Locations.itdb.cbk",
NULL };
const char **file;
GError *error = NULL;
g_assert (fexp->error == NULL);
for (file = itdb_files; *file != NULL; file++) {
copy_itdb_file(tmpdir, itlpdir, *file, &error);
if (error) {
res = -1;
/* only the last error will be reported, but this way we
* copy as many files as possible even when errors occur
*/
g_clear_error (&fexp->error);
g_propagate_error (&fexp->error, error);
}
}
if (fexp->error) {
goto leave;
}
}
res = 0;
leave:
if (itlpdir) {
g_free(itlpdir);
}
if (tmpdir) {
/* cleanup tmpdir, this will also delete tmpdir itself */
rmdir_recursive(tmpdir);
g_free(tmpdir);
}
return res;
}
|