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 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
|
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/
/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
*
******************************************************************************/
/******************************************************************************
* Functions in this file write to volInfo.imageForWriting and are probably
* unsutable for anything else.
******************************************************************************/
#include <strings.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "bk.h"
#include "bkInternal.h"
#include "bkWrite7x.h"
#include "bkTime.h"
#include "bkWrite.h"
#include "bkMangle.h"
#include "bkError.h"
#include "bkSort.h"
#include "bkPath.h"
#include "bkCache.h"
#include "bkRead7x.h"
#include "bkLink.h"
#include "bkIoWrappers.h"
/******************************************************************************
* bk_write_image()
* Writes everything from first to last byte of the iso.
* Public function.
* */
int bk_write_image(const char* newImagePathAndName, VolInfo* volInfo,
time_t creationTime, int filenameTypes,
void(*progressFunction)(VolInfo*, double))
{
int rc;
DirToWrite newTree;
bk_off_t svdOffset;
bk_off_t pRealRootDrOffset;
int pRootDirSize;
bk_off_t sRealRootDrOffset;
int sRootDirSize;
bk_off_t lPathTable9660Loc;
bk_off_t mPathTable9660Loc;
int pathTable9660Size;
bk_off_t lPathTableJolietLoc;
bk_off_t mPathTableJolietLoc;
int pathTableJolietSize;
bk_off_t bootCatalogSectorNumberOffset;
bk_off_t currPos;
volInfo->writeProgressFunction = progressFunction;
volInfo->stopOperation = false;
volInfo->estimatedIsoSize = bk_estimate_iso_size(volInfo, filenameTypes);
progressFunction(volInfo, 0);
BkStatStruct statStruct;
rc = bkStat(newImagePathAndName, &statStruct);
if(rc == 0 && statStruct.st_ino == volInfo->imageForReadingInode)
return BKERROR_SAVE_OVERWRITE;
/* because mangleDir works on dir's children i need to
* copy the root manually */
memset(&newTree, 0, sizeof(DirToWrite));
newTree.base.posixFileMode = volInfo->dirTree.base.posixFileMode;
printf("mangling\n");fflush(NULL);
/* create tree to write */
rc = mangleDir(&(volInfo->dirTree), &newTree, filenameTypes);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
return rc;
}
printf("opening '%s' for writing\n", newImagePathAndName);fflush(NULL);
volInfo->imageForWriting = open(newImagePathAndName,
O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR);
if(volInfo->imageForWriting == -1)
{
freeDirToWriteContents(&newTree);
return BKERROR_OPEN_WRITE_FAILED;
}
printf("writing blank at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
/* system area, always zeroes */
rc = writeByteBlock(volInfo, 0, NBYTES_LOGICAL_BLOCK * NLS_SYSTEM_AREA);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
/* skip pvd (1 block), write it after files */
wcSeekForward(volInfo, NBYTES_LOGICAL_BLOCK);
if(volInfo->bootMediaType != BOOT_MEDIA_NONE)
{
/* el torito volume descriptor */
rc = writeElToritoVd(volInfo, &bootCatalogSectorNumberOffset);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
}
if(filenameTypes & FNTYPE_JOLIET)
/* skip svd (1 block), write it after pvd */
{
svdOffset = wcSeekTell(volInfo);
wcSeekForward(volInfo, NBYTES_LOGICAL_BLOCK);
}
printf("writing terminator at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
/* volume descriptor set terminator */
rc = writeVdsetTerminator(volInfo);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
if(volInfo->bootMediaType != BOOT_MEDIA_NONE)
{
/* write boot catalog sector number */
currPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, bootCatalogSectorNumberOffset);
rc = write731(volInfo, currPos / NBYTES_LOGICAL_BLOCK);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
wcSeekSet(volInfo, currPos);
/* write el torito booting catalog */
rc = writeElToritoBootCatalog(volInfo, &(volInfo->bootRecordSectorNumberOffset));
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
}
/* MAYBE write boot record file now */
if(volInfo->bootMediaType != BOOT_MEDIA_NONE &&
!volInfo->bootRecordIsVisible)
{
int blankSize;
int srcFile; /* either the old image or the boot record file on
* the regular filesystem */
bool srcFileOpened;
/* set up source file pointer */
if(volInfo->bootRecordIsOnImage)
{
srcFile = volInfo->imageForReading;
bkSeekSet(volInfo->imageForReading, volInfo->bootRecordOffset, SEEK_SET);
srcFileOpened = false;
}
else
{
srcFile = open(volInfo->bootRecordPathAndName, O_RDONLY, 0);
if(srcFile == -1)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return BKERROR_OPEN_READ_FAILED;
}
srcFileOpened = true;
}
/* write boot record sector number */
currPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, volInfo->bootRecordSectorNumberOffset);
rc = write731(volInfo, currPos / NBYTES_LOGICAL_BLOCK);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
if(srcFileOpened)
bkClose(srcFile);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
wcSeekSet(volInfo, currPos);
/* file contents */
rc = writeByteBlockFromFile(srcFile, volInfo, volInfo->bootRecordSize);
if(rc < 0)
{
freeDirToWriteContents(&newTree);
if(srcFileOpened)
bkClose(srcFile);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
blankSize = NBYTES_LOGICAL_BLOCK -
volInfo->bootRecordSize % NBYTES_LOGICAL_BLOCK;
/* fill the last sector with 0s */
rc = writeByteBlock(volInfo, 0x00, blankSize);
if(rc < 0)
{
freeDirToWriteContents(&newTree);
if(srcFileOpened)
bkClose(srcFile);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
if(srcFileOpened)
bkClose(srcFile);
}
/* END MAYBE write boot record file now */
printf("sorting 9660\n");
sortDir(&newTree, FNTYPE_9660);
pRealRootDrOffset = wcSeekTell(volInfo);
printf("writing primary directory tree at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
/* 9660 and maybe rockridge dir tree */
rc = writeDir(volInfo, &newTree, 0, 0, 0, creationTime,
filenameTypes & (FNTYPE_9660 | FNTYPE_ROCKRIDGE), true);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
pRootDirSize = rc;
/* joliet dir tree */
if(filenameTypes & FNTYPE_JOLIET)
{
printf("sorting joliet\n");
sortDir(&newTree, FNTYPE_JOLIET);
printf("writing supplementary directory tree at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
sRealRootDrOffset = wcSeekTell(volInfo);
rc = writeDir(volInfo, &newTree, 0, 0, 0, creationTime,
FNTYPE_JOLIET, true);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
sRootDirSize = rc;
}
printf("writing 9660 path tables at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
lPathTable9660Loc = wcSeekTell(volInfo);
rc = writePathTable(volInfo, &newTree, true, FNTYPE_9660);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
pathTable9660Size = rc;
mPathTable9660Loc = wcSeekTell(volInfo);
rc = writePathTable(volInfo, &newTree, false, FNTYPE_9660);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
if(filenameTypes & FNTYPE_JOLIET)
{
printf("writing joliet path tables at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
lPathTableJolietLoc = wcSeekTell(volInfo);
rc = writePathTable(volInfo, &newTree, true, FNTYPE_JOLIET);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
pathTableJolietSize = rc;
mPathTableJolietLoc = wcSeekTell(volInfo);
rc = writePathTable(volInfo, &newTree, false, FNTYPE_JOLIET);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
}
printf("writing files at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
resetWriteStatus(volInfo->fileLocations);
/* all files and offsets/sizes */
rc = writeFileContents(volInfo, &newTree, filenameTypes);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
if(filenameTypes & FNTYPE_ROCKRIDGE)
{
printf("writing long NMs at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
rc = writeLongNMsInDir(volInfo, &newTree);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
}
wcSeekSet(volInfo, NBYTES_LOGICAL_BLOCK * NLS_SYSTEM_AREA);
printf("writing pvd at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
rc = writeVolDescriptor(volInfo, pRealRootDrOffset,
pRootDirSize, lPathTable9660Loc, mPathTable9660Loc,
pathTable9660Size, creationTime, true);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
if(filenameTypes & FNTYPE_JOLIET)
{
wcSeekSet(volInfo, svdOffset);
printf("writing svd at %X\n", (int)wcSeekTell(volInfo));fflush(NULL);
rc = writeVolDescriptor(volInfo, sRealRootDrOffset,
sRootDirSize, lPathTableJolietLoc, mPathTableJolietLoc,
pathTableJolietSize, creationTime, false);
if(rc <= 0)
{
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
unlink(newImagePathAndName);
return rc;
}
}
printf("freeing memory\n");fflush(NULL);
freeDirToWriteContents(&newTree);
bkClose(volInfo->imageForWriting);
return 1;
}
/******************************************************************************
* bootInfoTableChecksum()
* Calculate the checksum to be written into the boot info table.
* */
int bootInfoTableChecksum(int oldImage, FileToWrite* file, unsigned* checksum)
{
int numTrailingBytes; /* to make sure the file size is divisible by 4 */
ssize_t rc;
int srcFile;
unsigned char* contents;
unsigned count;
numTrailingBytes = file->size % 4;
contents = malloc(file->size + numTrailingBytes);
if(contents == NULL)
return BKERROR_OUT_OF_MEMORY;
/* make sure the extra bytes i added are 0s */
memset(contents + file->size, 0, numTrailingBytes);
if(file->onImage)
/* read file from original image */
{
bkSeekSet(oldImage, file->offset, SEEK_SET);
rc = bkRead(oldImage, contents, file->size);
if(rc == -1 || rc != (int)(file->size))
{
free(contents);
return BKERROR_READ_GENERIC;
}
}
else
/* read file from fs */
{
srcFile = open(file->pathAndName, O_RDONLY, 0);
if(srcFile == -1)
{
free(contents);
return BKERROR_OPEN_READ_FAILED;
}
rc = bkRead(srcFile, contents, file->size);
bkClose(srcFile);
if(rc == -1 || rc != (int)(file->size))
{
free(contents);
return BKERROR_READ_GENERIC;
}
}
*checksum = 0;
/* do 32 bit checksum starting from byte 64
* because i check above that the file is divisible by 4 i will not be
* reading wrong memory */
for(count = 64; count < file->size; count += 4)
{
unsigned toAdd;
toAdd = *(contents + count) | (*(contents + count + 1) << 8) |
(*(contents + count + 2) << 16) | (*(contents + count + 3) << 24);
*checksum += toAdd;
}
free(contents);
return 1;
}
/******************************************************************************
* countDirsOnLevel()
* a 'level' is described in ecma119 6.8.2
* it's needed for path tables, don't remember exactly what for
* */
int countDirsOnLevel(const DirToWrite* dir, int targetLevel, int thisLevel)
{
BaseToWrite* child;
int sum;
if(targetLevel == thisLevel)
{
return 1;
}
else
{
sum = 0;
child = dir->children;
while(child != NULL)
{
if( IS_DIR(child->posixFileMode) )
sum += countDirsOnLevel(DIRTW_PTR(child), targetLevel, thisLevel + 1);
child = child->next;
}
return sum;
}
}
/******************************************************************************
* countTreeHeight()
* caller should set heightSoFar to 1
* */
int countTreeHeight(const DirToWrite* dir, int heightSoFar)
{
BaseToWrite* child;
int maxHeight;
int thisHeight;
maxHeight = heightSoFar;
child = dir->children;
while(child != NULL)
{
if( IS_DIR(child->posixFileMode) )
{
thisHeight = countTreeHeight(DIRTW_PTR(child), heightSoFar + 1);
if(thisHeight > maxHeight)
maxHeight = thisHeight;
}
child = child->next;
}
return maxHeight;
}
/******************************************************************************
* elToritoChecksum()
* Algorithm: the sum of all words, including the checksum must trunkate to
* a 16-bit 0x0000
* */
unsigned short elToritoChecksum(const unsigned char* record)
{
short sum;
int i;
sum = 0;
for(i = 0; i < 32; i += 2)
{
sum += *(record + i) | (*(record + i + 1) << 8);
}
return 0xFFFF - sum + 1;
}
/******************************************************************************
* writeByteBlock()
* Fills numBytes with byteToWrite.
* */
int writeByteBlock(VolInfo* volInfo, unsigned char byteToWrite, int numBytes)
{
int rc;
int count;
int numBlocks;
int sizeLastBlock;
memset(volInfo->readWriteBuffer, byteToWrite, READ_WRITE_BUFFER_SIZE);
numBlocks = numBytes / READ_WRITE_BUFFER_SIZE;
sizeLastBlock = numBytes % READ_WRITE_BUFFER_SIZE;
for(count = 0; count < numBlocks; count++)
{
rc = wcWrite(volInfo, volInfo->readWriteBuffer, READ_WRITE_BUFFER_SIZE);
if(rc <= 0)
return rc;
}
if(sizeLastBlock > 0)
{
rc = wcWrite(volInfo, volInfo->readWriteBuffer, sizeLastBlock);
if(rc <= 0)
return rc;
}
return 1;
}
/******************************************************************************
* writeByteBlockFromFile()
* copies numBytes from src into the image to write in blocks of 10K
* */
int writeByteBlockFromFile(int src, VolInfo* volInfo, unsigned numBytes)
{
int rc;
int count;
int numBlocks;
int sizeLastBlock;
numBlocks = numBytes / READ_WRITE_BUFFER_SIZE;
sizeLastBlock = numBytes % READ_WRITE_BUFFER_SIZE;
for(count = 0; count < numBlocks; count++)
{
if(volInfo->stopOperation)
return BKERROR_OPER_CANCELED_BY_USER;
rc = bkRead(src, volInfo->readWriteBuffer, READ_WRITE_BUFFER_SIZE);
if(rc != READ_WRITE_BUFFER_SIZE)
return BKERROR_READ_GENERIC;
rc = wcWrite(volInfo, volInfo->readWriteBuffer, READ_WRITE_BUFFER_SIZE);
if(rc <= 0)
return rc;
}
if(sizeLastBlock > 0)
{
rc = bkRead(src, volInfo->readWriteBuffer, sizeLastBlock);
if(rc != sizeLastBlock)
return BKERROR_READ_GENERIC;
rc = wcWrite(volInfo, volInfo->readWriteBuffer, sizeLastBlock);
if(rc <= 0)
return rc;
}
return 1;
}
/******************************************************************************
* writeDir()
* Writes the contents of a directory. Also writes locations and sizes of
* directory records for directories but not for files.
* Returns data length of the dir written.
* */
int writeDir(VolInfo* volInfo, DirToWrite* dir, int parentLbNum,
int parentNumBytes, int parentPosix, time_t recordingTime,
int filenameTypes, bool isRoot)
{
int rc;
bk_off_t startPos;
int numUnusedBytes;
bk_off_t endPos;
DirToWrite selfDir; /* will have a different filename */
DirToWrite parentDir;
BaseToWrite* child;
if(wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK != 0)
return BKERROR_SANITY;
/* names other then 9660 are not used for self and parent */
selfDir.base.name9660[0] = 0x00;
selfDir.base.posixFileMode = dir->base.posixFileMode;
parentDir.base.name9660[0] = 0x01;
parentDir.base.name9660[1] = '\0';
if(isRoot)
parentDir.base.posixFileMode = selfDir.base.posixFileMode;
else
parentDir.base.posixFileMode = parentPosix;
startPos = wcSeekTell(volInfo);
if( startPos % NBYTES_LOGICAL_BLOCK != 0 )
/* this should never happen */
return BKERROR_SANITY;
if(filenameTypes & FNTYPE_JOLIET)
dir->extentNumber2 = startPos / NBYTES_LOGICAL_BLOCK;
else
dir->base.extentNumber = startPos / NBYTES_LOGICAL_BLOCK;
/* write self */
if(isRoot)
{
rc = writeDr(volInfo, BASETW_PTR(&selfDir), recordingTime, true, true, true, filenameTypes);
if(rc < 0)
return rc;
if(filenameTypes & FNTYPE_JOLIET)
dir->base.extentLocationOffset2 = selfDir.base.extentLocationOffset2;
else
dir->base.extentLocationOffset = selfDir.base.extentLocationOffset;
}
else
{
rc = writeDr(volInfo, BASETW_PTR(&selfDir), recordingTime, true, true, false, filenameTypes);
if(rc < 0)
return rc;
}
if(rc < 0)
return rc;
/* write parent */
rc = writeDr(volInfo, BASETW_PTR(&parentDir), recordingTime, true, true, false, filenameTypes);
if(rc < 0)
return rc;
child = dir->children;
/* WRITE children drs */
while(child != NULL)
{
if(IS_DIR(child->posixFileMode))
{
rc = writeDr(volInfo, child, recordingTime,
true, false, false, filenameTypes);
}
else
{
rc = writeDr(volInfo, child, recordingTime,
false, false, false, filenameTypes);
}
if(rc < 0)
return rc;
child = child->next;
}
/* END WRITE children drs */
/* write blank to conclude extent */
numUnusedBytes = NBYTES_LOGICAL_BLOCK -
wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK;
rc = writeByteBlock(volInfo, 0x00, numUnusedBytes);
if(rc < 0)
return rc;
if(filenameTypes & FNTYPE_JOLIET)
dir->dataLength2 = wcSeekTell(volInfo) - startPos;
else
dir->dataLength = wcSeekTell(volInfo) - startPos;
/* write subdirectories */
child = dir->children;
while(child != NULL)
{
if(IS_DIR(child->posixFileMode))
{
if(filenameTypes & FNTYPE_JOLIET)
{
rc = writeDir(volInfo, DIRTW_PTR(child), dir->extentNumber2,
dir->dataLength2, BASETW_PTR(dir)->posixFileMode, recordingTime,
filenameTypes, false);
}
else
{
rc = writeDir(volInfo, DIRTW_PTR(child), BASETW_PTR(dir)->extentNumber,
dir->dataLength, BASETW_PTR(dir)->posixFileMode, recordingTime,
filenameTypes, false);
}
if(rc < 0)
return rc;
}
child = child->next;
}
endPos = wcSeekTell(volInfo);
/* SELF extent location and size */
if(filenameTypes & FNTYPE_JOLIET)
wcSeekSet(volInfo, selfDir.base.extentLocationOffset2);
else
wcSeekSet(volInfo, selfDir.base.extentLocationOffset);
if(filenameTypes & FNTYPE_JOLIET)
{
rc = write733(volInfo, dir->extentNumber2);
if(rc <= 0)
return rc;
rc = write733(volInfo, dir->dataLength2);
if(rc <= 0)
return rc;
}
else
{
rc = write733(volInfo, BASETW_PTR(dir)->extentNumber);
if(rc <= 0)
return rc;
rc = write733(volInfo, dir->dataLength);
if(rc <= 0)
return rc;
}
/* END SELF extent location and size */
/* PARENT extent location and size */
if(filenameTypes & FNTYPE_JOLIET)
wcSeekSet(volInfo, parentDir.base.extentLocationOffset2);
else
wcSeekSet(volInfo, parentDir.base.extentLocationOffset);
if(parentLbNum == 0)
/* root, parent is same as self */
{
if(filenameTypes & FNTYPE_JOLIET)
{
rc = write733(volInfo, dir->extentNumber2);
if(rc <= 0)
return rc;
rc = write733(volInfo, dir->dataLength2);
if(rc <= 0)
return rc;
}
else
{
rc = write733(volInfo, BASETW_PTR(dir)->extentNumber);
if(rc <= 0)
return rc;
rc = write733(volInfo, dir->dataLength);
if(rc <= 0)
return rc;
}
}
else
/* normal parent */
{
rc = write733(volInfo, parentLbNum);
if(rc <= 0)
return rc;
rc = write733(volInfo, parentNumBytes);
if(rc <= 0)
return rc;
}
/* END PARENT extent location and size */
/* ALL subdir extent locations and sizes */
child = dir->children;
while(child != NULL)
{
if(IS_DIR(child->posixFileMode))
{
if(filenameTypes & FNTYPE_JOLIET)
{
wcSeekSet(volInfo, child->extentLocationOffset2);
rc = write733(volInfo, DIRTW_PTR(child)->extentNumber2);
if(rc <= 0)
return rc;
rc = write733(volInfo, DIRTW_PTR(child)->dataLength2);
if(rc <= 0)
return rc;
}
else
{
wcSeekSet(volInfo, child->extentLocationOffset);
rc = write733(volInfo, child->extentNumber);
if(rc <= 0)
return rc;
rc = write733(volInfo, DIRTW_PTR(child)->dataLength);
if(rc <= 0)
return rc;
}
}
child = child->next;
}
/* END ALL subdir extent locations and sizes */
wcSeekSet(volInfo, endPos);
if(filenameTypes & FNTYPE_JOLIET)
return dir->dataLength2;
else
return dir->dataLength;
}
/******************************************************************************
* writeDr()
* Writes a directory record.
* Note that it uses only the members of DirToWrite and FileToWrite that are
* the same.
* */
int writeDr(VolInfo* volInfo, BaseToWrite* node, time_t recordingTime, bool isADir,
bool isSelfOrParent, bool isFirstRecord, int filenameTypes)
{
int rc;
unsigned char byte;
char aString[256];
unsigned short aShort;
bk_off_t startPos;
bk_off_t endPos;
unsigned char lenFileId;
unsigned char recordLen;
/* look at the end of the function for an explanation */
writeDrStartLabel:
startPos = wcSeekTell(volInfo);
/* record length is recorded in the end */
wcSeekForward(volInfo, 1);
/* extended attribute record length */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
if(filenameTypes & FNTYPE_JOLIET)
node->extentLocationOffset2 = wcSeekTell(volInfo);
else
node->extentLocationOffset = wcSeekTell(volInfo);
/* location of extent not recorded in this function */
wcSeekForward(volInfo, 8);
/* data length not recorded in this function */
wcSeekForward(volInfo, 8);
/* RECORDING time and date */
epochToShortString(recordingTime, aString);
rc = write711(volInfo, aString[0]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[1]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[2]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[3]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[4]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[5]);
if(rc <= 0)
return rc;
rc = write711(volInfo, aString[6]);
if(rc <= 0)
return rc;
/* END RECORDING time and date */
/* FILE flags */
if(isADir)
/* (only directory bit on) */
byte = 0x02;
else
/* nothing on */
byte = 0x00;
rc = wcWrite(volInfo, (char*)&byte, 1);
if(rc <= 0)
return rc;
/* END FILE flags */
/* file unit size (always 0, non-interleaved mode) */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* interleave gap size (also always 0, non-interleaved mode) */
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* volume sequence number (always 1) */
aShort = 1;
rc = write723(volInfo, aShort);
if(rc <= 0)
return rc;
/* LENGTH of file identifier */
if(isSelfOrParent)
lenFileId = 1;
else
{
if(filenameTypes & FNTYPE_JOLIET)
lenFileId = 2 * strlen(node->nameJoliet);
else
/*if(isADir) see microsoft comment below */
lenFileId = strlen(node->name9660);
/*else
lenFileId = strlen(node->name9660) + 2; */
}
rc = write711(volInfo, lenFileId);
if(rc <= 0)
return rc;
/* END LENGTH of file identifier */
/* FILE identifier */
if(isSelfOrParent)
{
/* that byte has 0x00 or 0x01 */
rc = write711(volInfo, node->name9660[0]);
if(rc <= 0)
return rc;
}
else
{
if(filenameTypes & FNTYPE_JOLIET)
{
rc = writeJolietStringField(volInfo, node->nameJoliet,
2 * strlen(node->nameJoliet));
if(rc < 0)
return rc;
}
else
{
/* ISO9660 requires ";1" after the filename (not directory name)
* but the windows NT/2K boot loaders cannot find NTLDR inside
* the I386 directory because they are looking for "NTLDR" not
* "NTLDR;1". i guess if microsoft can do it, i can do it. filenames
* on images written by me do not end with ";1"
if(isADir)
{*/
/* the name */
rc = wcWrite(volInfo, node->name9660, lenFileId);
if(rc <= 0)
return rc;
/*}
else
{
rc = writeWrapper(image, node->name9660, lenFileId - 2);
if(rc <= 0)
return rc;
rc = writeWrapper(image, ";1", 2);
if(rc <= 0)
return rc;
}*/
}
}
/* END FILE identifier */
/* padding field */
if(lenFileId % 2 == 0)
{
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
}
if(filenameTypes & FNTYPE_ROCKRIDGE)
{
if(isFirstRecord)
{
rc = writeRockSP(volInfo);
if(rc < 0)
return rc;
rc = writeRockER(volInfo);
if(rc < 0)
return rc;
}
rc = writeRockPX(volInfo, node->posixFileMode, isADir);
if(rc < 0)
return rc;
if(!isSelfOrParent)
{
if(wcSeekTell(volInfo) - startPos < (int)strlen(node->nameRock) + 5)
/* have no room for the NM entry in this directory record */
{
node->offsetForCE = wcSeekTell(volInfo);
/* leave room for CE entry */
wcSeekForward(volInfo, 28);
}
else
{
rc = writeRockNM(volInfo, node->nameRock, strlen(node->nameRock), false);
if(rc < 0)
return rc;
}
if(IS_SYMLINK(node->posixFileMode))
{
rc = writeRockSL(volInfo, SYMLINKTW_PTR(node), true);
if(rc < 0)
return rc;
}
}
}
/* RECORD length */
endPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, startPos);
recordLen = endPos - startPos;
rc = write711(volInfo, recordLen);
if(rc <= 0)
return rc;
wcSeekSet(volInfo, endPos);
/* END RECORD length */
/* the goto is good! really!
* if, after writing the record we see that the record is in two logical
* sectors (that's not allowed by iso9660) we erase the record just
* written, write zeroes to the end of the first logical sector
* (as required by iso9660) and restart the function, which will write
* the same record again but at the beginning of the next logical sector
* yeah, so don't complain :) */
if(endPos / NBYTES_LOGICAL_BLOCK > startPos / NBYTES_LOGICAL_BLOCK)
/* crossed a logical sector boundary while writing the record */
{
wcSeekSet(volInfo, startPos);
/* overwrite a piece of the record written in this function
* (the piece that's in the first sector) with zeroes */
rc = writeByteBlock(volInfo, 0x00, recordLen - endPos % NBYTES_LOGICAL_BLOCK);
if(rc < 0)
return rc;
goto writeDrStartLabel;
}
return 1;
}
/******************************************************************************
* writeElToritoBootCatalog()
* Write the el torito boot catalog (validation entry and inital/default entry).
* Returns the offset where the boot record sector number should
* be written (7.3.1).
* */
int writeElToritoBootCatalog(VolInfo* volInfo,
bk_off_t* bootRecordSectorNumberOffset)
{
unsigned char buffer[NBYTES_LOGICAL_BLOCK];
int rc;
memset(buffer, 0, NBYTES_LOGICAL_BLOCK);
if(wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK != 0)
/* file pointer not at sector boundary */
return BKERROR_SANITY;
/* SETUP VALIDATION entry (first 20 bytes of boot catalog) */
/* header, must be 1 */
buffer[0] = 1;
/* platform id, 0 for x86 (bzero at start took care of this) */
/* 2 bytes reserved, must be 0 (bzero at start took care of this) */
/* 24 bytes id string for manufacturer/developer of cdrom */
strncpy((char*)&(buffer[4]), "Edited with ISO Master", 22);
/* key byte 0x55 */
buffer[30] = 0x55;
/* key byte 0xAA */
buffer[31] = 0xAA;
/* checksum */
write721ToByteArray(&(buffer[28]), elToritoChecksum(buffer));
/* END SETUP VALIDATION validation entry (first 20 bytes of boot catalog) */
/* SETUP INITIAL entry (next 20 bytes of boot catalog) */
/* boot indicator. 0x88 = bootable */
buffer[32] = 0x88;
/* boot media type */
if(volInfo->bootMediaType == BOOT_MEDIA_NO_EMULATION)
buffer[33] = 0;
else if(volInfo->bootMediaType == BOOT_MEDIA_1_2_FLOPPY)
buffer[33] = 1;
else if(volInfo->bootMediaType == BOOT_MEDIA_1_44_FLOPPY)
buffer[33] = 2;
else if(volInfo->bootMediaType == BOOT_MEDIA_2_88_FLOPPY)
buffer[33] = 3;
else if(volInfo->bootMediaType == BOOT_MEDIA_HARD_DISK)
buffer[33] = 4;
/* load segment leave it at 0 */
/* system type, leave it at 0 */
/* 1 byte unused, leave it at 0 */
/* sector count. i have yet to see a boot record with a sector count
* that's not 4 */
write721ToByteArray(&(buffer[38]), 4);
/* logical block number of boot record file. this is not known until
* after that file is written */
*bootRecordSectorNumberOffset = wcSeekTell(volInfo) + 40;
/* the rest is unused, leave it at 0 */
/* END SETUP INITIAL entry (next 20 bytes of boot catalog) */
rc = wcWrite(volInfo, (char*)buffer, NBYTES_LOGICAL_BLOCK);
if(rc <= 0)
return rc;
return 1;
}
/******************************************************************************
* writeElToritoVd()
* Write the el torito volume descriptor.
* Returns the offset where the boot catalog sector number should
* be written (7.3.1).
* */
int writeElToritoVd(VolInfo* volInfo, bk_off_t* bootCatalogSectorNumberOffset)
{
char buffer[NBYTES_LOGICAL_BLOCK];
int rc;
memset(buffer, 0, NBYTES_LOGICAL_BLOCK);
if(wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK != 0)
/* file pointer not at sector boundary */
return BKERROR_SANITY;
/* SETUP BOOT record volume descriptor sector */
/* boot record indicator, must be 0 (bzero at start took care of this) */
/* iso9660 identifier, must be "CD001" */
strncpy((char*)buffer + 1, "CD001", 5);
/* version, must be 1 */
buffer[6] = 1;
/* boot system identifier, must be 32 bytes "EL TORITO SPECIFICATION"
* padded with 0x00 (bzero at start took care of this) */
strncpy(&(buffer[7]), "EL TORITO SPECIFICATION", 23);
/* unused 32 bytes, must be 0 (bzero at start took care of this) */
/* boot catalog location, 4 byte intel format. written later. */
*bootCatalogSectorNumberOffset = wcSeekTell(volInfo) + 71;
/*write731ToByteArray(&(buffer[71]), bootCatalogSectorNumber);*/
/* the rest of this sector is unused, must be set to 0 */
/* END SETUP BOOT record volume descriptor sector */
rc = wcWrite(volInfo, buffer, NBYTES_LOGICAL_BLOCK);
if(rc <= 0)
return rc;
return 1;
}
/******************************************************************************
* writeFileContents()
* Write file contents into an extent and also write the file's location and
* size into the directory records back in the tree.
* Also write location and size for symbolic links.
* */
int writeFileContents(VolInfo* volInfo, DirToWrite* dir, int filenameTypes)
{
int rc;
BaseToWrite* child;
int numUnusedBytes;
int srcFile;
bk_off_t endPos;
child = dir->children;
while(child != NULL)
/* each file in current directory */
{
if(volInfo->stopOperation)
return BKERROR_OPER_CANCELED_BY_USER;
if(wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK != 0)
return BKERROR_SANITY;
if( IS_REG_FILE(child->posixFileMode) )
{
bool needToCopy = true;
child->extentNumber = wcSeekTell(volInfo) / NBYTES_LOGICAL_BLOCK;
if(volInfo->scanForDuplicateFiles)
{
if(FILETW_PTR(child)->location->extentNumberWrittenTo == 0)
/* file not yet written */
{
FILETW_PTR(child)->location->extentNumberWrittenTo = child->extentNumber;
}
else
{
child->extentNumber = FILETW_PTR(child)->location->extentNumberWrittenTo;
needToCopy = false;
}
}
if(volInfo->bootMediaType != BOOT_MEDIA_NONE &&
volInfo->bootRecordIsVisible &&
FILETW_PTR(child)->origFile == volInfo->bootRecordOnImage)
/* this file is the boot record. write its sector number in
* the boot catalog */
{
bk_off_t currPos;
currPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, volInfo->bootRecordSectorNumberOffset);
rc = write731(volInfo, child->extentNumber);
if(rc <= 0)
return rc;
wcSeekSet(volInfo, currPos);
}
if(needToCopy)
{
if(FILETW_PTR(child)->onImage)
/* copy file from original image to new one */
{
readSeekSet(volInfo, FILETW_PTR(child)->offset,
SEEK_SET);
rc = writeByteBlockFromFile(volInfo->imageForReading,
volInfo, FILETW_PTR(child)->size);
if(rc < 0)
return rc;
}
else
/* copy file from fs to new image */
{
/* UPDATE the file's size, in case it's changed since we added it */
BkStatStruct statStruct;
rc = bkStat(FILETW_PTR(child)->pathAndName, &statStruct);
if(rc != 0)
return BKERROR_STAT_FAILED;
if(statStruct.st_size > 0xFFFFFFFF)
/* size won't fit in a 32bit variable on the iso */
return BKERROR_EDITED_WRITE_TOO_BIG;
FILETW_PTR(child)->size = statStruct.st_size;
/* UPDATE the file's size, in case it's changed since we added it */
srcFile = open(FILETW_PTR(child)->pathAndName, O_RDONLY, 0);
if(srcFile == -1)
return BKERROR_OPEN_READ_FAILED;
rc = writeByteBlockFromFile(srcFile,
volInfo, FILETW_PTR(child)->size);
bkClose(srcFile);
if(rc < 0)
return rc;
}
/* fill extent with zeroes */
numUnusedBytes = NBYTES_LOGICAL_BLOCK -
wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK;
rc = writeByteBlock(volInfo, 0x00, numUnusedBytes);
if(rc < 0)
return rc;
}
endPos = wcSeekTell(volInfo);
bool isIsolinux;
rc = wroteIsolinuxBootRecord(volInfo, FILETW_PTR(child), &isIsolinux);
if(rc < 0)
return rc;
if(isIsolinux)
/* write the boot info table for the isolinux boot record */
{
unsigned char bootInfoTable[56];
unsigned checksum;
memset(bootInfoTable, 0, 56);
/* go to the offset in the file where the boot info table is */
wcSeekSet(volInfo, child->extentNumber *
NBYTES_LOGICAL_BLOCK + 8);
/* sector number of pvd */
write731ToByteArray(bootInfoTable, 16);
/* sector number of boot file (this one) */
write731ToByteArray(bootInfoTable + 4, child->extentNumber);
/* boot file length in bytes */
write731ToByteArray(bootInfoTable + 8, FILETW_PTR(child)->size);
/* 32 bit checksum (the sum of all the 32-bit words in the boot
* file starting at byte offset 64 */
rc = bootInfoTableChecksum(volInfo->imageForReading, FILETW_PTR(child), &checksum);
if(rc <= 0)
return rc;
write731ToByteArray(bootInfoTable + 12, checksum);
/* the rest is reserved, leave at zero */
rc = wcWrite(volInfo, (char*)bootInfoTable, 56);
if(rc <= 0)
return rc;
}
/* WRITE file location and size */
wcSeekSet(volInfo, child->extentLocationOffset);
rc = write733(volInfo, child->extentNumber);
if(rc <= 0)
return rc;
rc = write733(volInfo, FILETW_PTR(child)->size);
if(rc <= 0)
return rc;
if(filenameTypes & FNTYPE_JOLIET)
/* also update location and size on joliet tree */
{
wcSeekSet(volInfo, child->extentLocationOffset2);
rc = write733(volInfo, child->extentNumber);
if(rc <= 0)
return rc;
rc = write733(volInfo, FILETW_PTR(child)->size);
if(rc <= 0)
return rc;
}
wcSeekSet(volInfo, endPos);
/* END WRITE file location and size */
}
else if( IS_DIR(child->posixFileMode) )
{
rc = writeFileContents(volInfo, DIRTW_PTR(child), filenameTypes);
if(rc < 0)
return rc;
}
else if( IS_SYMLINK(child->posixFileMode) )
{
/* WRITE symlink location and size (0) */
endPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, child->extentLocationOffset);
rc = write733(volInfo, 0);
if(rc <= 0)
return rc;
rc = write733(volInfo, 0);
if(rc <= 0)
return rc;
if(filenameTypes & FNTYPE_JOLIET)
/* also update location and size on joliet tree */
{
wcSeekSet(volInfo, child->extentLocationOffset2);
rc = write733(volInfo, 0);
if(rc <= 0)
return rc;
rc = write733(volInfo, 0);
if(rc <= 0)
return rc;
}
wcSeekSet(volInfo, endPos);
/* END WRITE symlink location and size (0) */
}
child = child->next;
} /* while(nextFile != NULL) */
return 1;
}
/* field size must be even. !!check all calls to make sure */
int writeJolietStringField(VolInfo* volInfo, const char* name, size_t fieldSize)
{
char jolietName[512]; /* don't see why would ever want
* to write a longer one */
int srcCount;
size_t destCount;
int rc;
srcCount = 0;
destCount = 0;
while(name[srcCount] != '\0' && destCount < fieldSize)
{
/* first byte zero */
jolietName[destCount] = 0x00;
/* second byte character */
jolietName[destCount + 1] = name[srcCount];
srcCount += 1;
destCount += 2;
}
while(destCount < fieldSize)
/* pad with ucs2 spaces */
{
jolietName[destCount] = 0x00;
jolietName[destCount + 1] = ' ';
destCount += 2;
}
rc = wcWrite(volInfo, jolietName, destCount);
if(rc <= 0)
return rc;
return 1;
}
/* write NM that won't fit in a directory record */
int writeLongNM(VolInfo* volInfo, BaseToWrite* node)
{
bk_off_t startPos;
size_t fullNameLen;
unsigned char CErecord[28];
bool fitsInOneNM;
size_t firstNMlen;
bk_off_t endPos;
int rc;
int lenOfCE;
startPos = wcSeekTell(volInfo);
fullNameLen = strlen(node->nameRock);
/* should have checked for this before getting into this function */
if(fullNameLen > 255)
return BKERROR_SANITY;
if(fullNameLen > 250)
{
fitsInOneNM = false;
firstNMlen = 250;
}
else
{
fitsInOneNM = true;
firstNMlen = fullNameLen;
}
/* NM record(s) */
if(fitsInOneNM)
{
rc = writeRockNM(volInfo, node->nameRock, firstNMlen, false);
if(rc <= 0)
return rc;
}
else
{
rc = writeRockNM(volInfo, node->nameRock, firstNMlen, true);
if(rc <= 0)
return rc;
rc = writeRockNM(volInfo, node->nameRock + firstNMlen, fullNameLen - firstNMlen, false);
if(rc <= 0)
return rc;
}
lenOfCE = wcSeekTell(volInfo) - startPos;
/* write blank to conclude extent */
rc = writeByteBlock(volInfo, 0x00, NBYTES_LOGICAL_BLOCK -
wcSeekTell(volInfo) % NBYTES_LOGICAL_BLOCK);
if(rc < 0)
return rc;
endPos = wcSeekTell(volInfo);
/* CE record back in the directory record */
wcSeekSet(volInfo, node->offsetForCE);
CErecord[0] = 'C';
CErecord[1] = 'E';
CErecord[2] = 28; /* length */
CErecord[3] = 1; /* version */
write733ToByteArray(CErecord + 4, startPos / NBYTES_LOGICAL_BLOCK); /* block location */
/* i'm always using 1 logical block per name */
write733ToByteArray(CErecord + 12, 0); /* offset to start */
write733ToByteArray(CErecord + 20, lenOfCE); /* length */
rc = wcWrite(volInfo, (char*)CErecord, CErecord[2]);
if(rc <= 0)
return rc;
/* END CE record back in the directory record */
wcSeekSet(volInfo, endPos);
return 1;
}
/* write all NMs in the tree that won't fit in directory records */
int writeLongNMsInDir(VolInfo* volInfo, DirToWrite* dir)
{
BaseToWrite* child;
int rc;
child = dir->children;
while(child != NULL)
{
if(child->offsetForCE != 0)
{
rc = writeLongNM(volInfo, child);
if(rc <= 0)
return rc;
}
if( IS_DIR(child->posixFileMode) )
{
rc = writeLongNMsInDir(volInfo, DIRTW_PTR(child));
if(rc <= 0)
return rc;
}
child = child->next;
}
return 1;
}
/* returns path table size (number of bytes not counting the blank) */
int writePathTable(VolInfo* volInfo, const DirToWrite* tree, bool isTypeL,
int filenameType)
{
int treeHeight;
int count;
int level;
int* dirsPerLevel; /* a dynamic array of the number of dirs per level */
int numDirsSoFar;
bk_off_t origPos;
int numBytesWritten;
int rc;
origPos = wcSeekTell(volInfo);
if(origPos % NBYTES_LOGICAL_BLOCK != 0)
return BKERROR_SANITY;
treeHeight = countTreeHeight(tree, 1);
dirsPerLevel = malloc(sizeof(int) * treeHeight);
if(dirsPerLevel == NULL)
return BKERROR_OUT_OF_MEMORY;
for(count = 0; count < treeHeight; count++)
{
dirsPerLevel[count] = countDirsOnLevel(tree, count + 1, 1);
}
for(level = 1; level <= treeHeight; level++)
{
if(level == 1)
/* numDirsSoFar = parent dir num */
numDirsSoFar = 1;
else if(level == 2)
numDirsSoFar = 1;
else
{
/* ex. when i am on level 4 i want number of dirs on levels 1 + 2 */
numDirsSoFar = 0;
for(count = 0; count < level - 2; count++)
{
numDirsSoFar += dirsPerLevel[count];
}
}
rc = writePathTableRecordsOnLevel(volInfo, tree, isTypeL, filenameType,
level, 1, &numDirsSoFar);
if(rc < 0)
{
free(dirsPerLevel);
return rc;
}
}
numBytesWritten = wcSeekTell(volInfo) - origPos;
/* blank to conclude extent */
rc = writeByteBlock(volInfo, 0x00, NBYTES_LOGICAL_BLOCK -
numBytesWritten % NBYTES_LOGICAL_BLOCK);
if(rc < 0)
{
free(dirsPerLevel);
return rc;
}
free(dirsPerLevel);
return numBytesWritten;
}
int writePathTableRecordsOnLevel(VolInfo* volInfo, const DirToWrite* dir,
bool isTypeL, int filenameType,
int targetLevel, int thisLevel,
int* parentDirNum)
{
int rc;
BaseToWrite* child;
unsigned char fileIdLen;
unsigned char byte;
unsigned exentLocation;
unsigned short parentDirId; /* copy of *parentDirNum */
static const char rootId = 0x00;
if(thisLevel == targetLevel)
/* write path table record */
{
/* LENGTH of directory identifier */
if(targetLevel == 1)
/* root */
fileIdLen = 1;
else
{
if(filenameType & FNTYPE_JOLIET)
{
fileIdLen = 2 * strlen(BASETW_PTR(dir)->nameJoliet);
}
else
{
fileIdLen = strlen(BASETW_PTR(dir)->name9660);
}
}
rc = write711(volInfo, fileIdLen);
if(rc <= 0)
return rc;
/* END LENGTH of directory identifier */
/* extended attribute record length */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* LOCATION of extent */
if(filenameType & FNTYPE_JOLIET)
exentLocation = dir->extentNumber2;
else
exentLocation = BASETW_PTR(dir)->extentNumber;
if(isTypeL)
rc = write731(volInfo, exentLocation);
else
rc = write732(volInfo, exentLocation);
if(rc <= 0)
return rc;
/* END LOCATION of extent */
/* PARENT directory number */
parentDirId = *parentDirNum;
if(isTypeL)
rc = write721(volInfo, parentDirId);
else
rc = write722(volInfo, parentDirId);
if(rc <= 0)
return rc;
/* END PARENT directory number */
/* DIRECTORY identifier */
if(targetLevel == 1)
/* root */
{
rc = wcWrite(volInfo, &rootId, 1);
if(rc <= 0)
return rc;
}
else
{
if(filenameType & FNTYPE_JOLIET)
{
rc = writeJolietStringField(volInfo, BASETW_PTR(dir)->nameJoliet, fileIdLen);
if(rc < 0)
return rc;
}
else
{
rc = wcWrite(volInfo, BASETW_PTR(dir)->name9660, fileIdLen);
if(rc <= 0)
return rc;
}
}
/* END DIRECTORY identifier */
/* padding field */
if(fileIdLen % 2 != 0)
{
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
}
}
else /* if(thisLevel < targetLevel) */
{
child = dir->children;
while(child != NULL)
{
if( IS_DIR(child->posixFileMode) )
{
if(thisLevel == targetLevel - 2)
/* am now going throught the list of dirs where the parent is */
{
if(targetLevel != 2)
/* first and second level have the same parent: 1 */
{
(*parentDirNum)++;
}
}
rc = writePathTableRecordsOnLevel(volInfo, DIRTW_PTR(child), isTypeL,
filenameType, targetLevel,
thisLevel + 1, parentDirNum);
if(rc < 0)
return rc;
}
child = child->next;
}
}
return 1;
}
/* This doesn't need support for CE because it's only written in one place,
* the root 'self' directory record. */
int writeRockER(VolInfo* volInfo)
{
int rc;
char record[46];
/* identification */
record[0] = 'E';
record[1] = 'R';
/* record length */
record[2] = 46;
/* entry version */
record[3] = 1;
/* extension identifier length */
record[4] = 10;
/* extension descriptor length */
record[5] = 10;
/* extension source length */
record[6] = 18;
/* extension version */
record[7] = 1;
/* extension identifier */
strncpy(&(record[8]), "IEEE_P1282", 10);
/* extension descriptor */
strncpy(&(record[18]), "DRAFT_1_12", 10);
/* extension source */
strncpy(&(record[28]), "ADOPTED_1994_07_08", 18);
rc = wcWrite(volInfo, record, 46);
if(rc <= 0)
return rc;
return 1;
}
int writeRockNM(VolInfo* volInfo, char* name, size_t nameLen, bool doesContinue)
{
int rc;
char recordStart[5];
/* identification */
recordStart[0] = 'N';
recordStart[1] = 'M';
/* record length */
recordStart[2] = 5 + nameLen;
/* entry version */
recordStart[3] = 1;
/* flags */
if(doesContinue)
recordStart[4] = 0x01;
else
recordStart[4] = 0;
rc = wcWrite(volInfo, recordStart, 5);
if(rc <= 0)
return rc;
rc = wcWrite(volInfo, name, nameLen);
if(rc <= 0)
return rc;
return 1;
}
/* the slackware cd has 36 byte PX entries, missing the file serial number
* so i will do the same */
int writeRockPX(VolInfo* volInfo, unsigned posixFileMode, bool isADir)
{
int rc;
unsigned char record[36];
unsigned posixFileLinks;
/* identification */
record[0] = 'P';
record[1] = 'X';
/* record length */
record[2] = 36;
/* entry version */
record[3] = 1;
/* posix file mode */
write733ToByteArray(&(record[4]), posixFileMode);
/* POSIX file links */
/*
* this i think is number of subdirectories + 2 (self and parent)
* and 1 for a file
* it's probably not used on read-only filesystems
* to add it, i would need to pass the number of links in a parent dir
* recursively in writeDir(). brrrrr.
*/
if(isADir)
posixFileLinks = 2;
else
posixFileLinks = 1;
write733ToByteArray(&(record[12]), posixFileLinks);
/* END POSIX file links */
/* posix file user id, posix file group id */
memset(&(record[20]), 0, 16);
rc = wcWrite(volInfo, (char*)record, 36);
if(rc <= 0)
return rc;
return 1;
}
int writeRockSL(VolInfo* volInfo, SymLinkToWrite* symlink, bool doWrite)
{
size_t stringCount;
size_t targetLen;
size_t numBytesNeeded;
size_t numBytesToSkip;
unsigned char* record;
size_t recordCount;
int rc;
targetLen = strlen(symlink->target);
/* figure out how much room i need */
numBytesNeeded = 0;
numBytesToSkip = 0;
stringCount = 0;
while(stringCount < targetLen)
{
char* nextSlash;
if(symlink->target[stringCount] == '/')
/* root (/) */
{
numBytesNeeded += 2;
numBytesToSkip = 1;
}
else if( symlink->target[stringCount] == '.' &&
(stringCount + 1 == targetLen || symlink->target[stringCount + 1] == '/') )
/* current (.) */
{
numBytesNeeded += 2;
numBytesToSkip = 2;
}
else if( symlink->target[stringCount] == '.' &&
stringCount + 1 < targetLen && symlink->target[stringCount + 1] == '.' )
/* parent (..) */
{
numBytesNeeded += 2;
numBytesToSkip = 3;
}
else
/* regular filename */
{
nextSlash = strchr(symlink->target + stringCount, '/');
if(nextSlash != NULL)
numBytesToSkip = nextSlash - (symlink->target + stringCount);
else
numBytesToSkip = targetLen - stringCount;
numBytesNeeded += 2 + numBytesToSkip;
numBytesToSkip += 1;
}
stringCount += numBytesToSkip;
}
if(!doWrite)
return (int)(5 + numBytesNeeded);
if(numBytesNeeded > NCHARS_SYMLINK_TARGET_MAX - 1)
return BKERROR_SYMLINK_TARGET_TOO_LONG;
record = malloc(5 + numBytesNeeded);
if(record == NULL)
return BKERROR_OUT_OF_MEMORY;
record[0] = 'S';
record[1] = 'L';
record[2] = 5 + numBytesNeeded; /* length */
record[3] = 1; /* version */
record[4] = 0x00; /* flags */
/* write SL */
numBytesToSkip = 0;
stringCount = 0;
recordCount = 5;
while(stringCount < targetLen)
{
char* nextSlash;
if(symlink->target[stringCount] == '/')
/* root (/) */
{
numBytesToSkip = 1;
record[recordCount] = 0x08;
record[recordCount + 1] = 0;
recordCount += 2;
}
else if( symlink->target[stringCount] == '.' &&
(stringCount + 1 == targetLen || symlink->target[stringCount + 1] == '/') )
/* current (.) */
{
numBytesToSkip = 2;
record[recordCount] = 0x02;
record[recordCount + 1] = 0;
recordCount += 2;
}
else if( symlink->target[stringCount] == '.' &&
stringCount + 1 < targetLen && symlink->target[stringCount + 1] == '.' )
/* parent (..) */
{
numBytesToSkip = 3;
record[recordCount] = 0x04;
record[recordCount + 1] = 0;
recordCount += 2;
}
else
/* regular filename */
{
nextSlash = strchr(symlink->target + stringCount, '/');
if(nextSlash != NULL)
numBytesToSkip = nextSlash - (symlink->target + stringCount);
else
numBytesToSkip = targetLen - stringCount;
record[recordCount] = 0x00;
record[recordCount + 1] = numBytesToSkip;
strncpy((char*)record + recordCount + 2, symlink->target + stringCount, numBytesToSkip);
recordCount += 2 + numBytesToSkip;
numBytesToSkip += 1;
}
/* + separator */
stringCount += numBytesToSkip;
}
if(recordCount != numBytesNeeded + 5)
{
free(record);
return BKERROR_SANITY;
}
rc = wcWrite(volInfo, (char*)record, recordCount);
if(rc <= 0)
{
free(record);
return rc;
}
free(record);
return (int)(5 + numBytesNeeded);
}
/* This doesn't need support for CE because it's only written in one place,
* the root 'self' directory record. */
int writeRockSP(VolInfo* volInfo)
{
int rc;
unsigned char record[7];
/* identification */
record[0] = 'S';
record[1] = 'P';
/* record length */
record[2] = 7;
/* entry version */
record[3] = 1;
/* check bytes */
record[4] = 0xBE;
record[5] = 0xEF;
/* bytes skipped */
record[6] = 0;
rc = wcWrite(volInfo, (char*)record, 7);
if(rc <= 0)
return rc;
return 1;
}
int writeVdsetTerminator(VolInfo* volInfo)
{
int rc;
unsigned char byte;
unsigned char aString[6];
/* volume descriptor type */
byte = 255;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* standard identifier */
strcpy((char*)aString, "CD001");
rc = wcWrite(volInfo, (char*)aString, 5);
if(rc <= 0)
return rc;
/* volume descriptor version */
byte = 1;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
rc = writeByteBlock(volInfo, 0, 2041);
if(rc < 0)
return rc;
return 1;
}
/*
* -has to be called after the files were written so that the
* volume size is recorded properly
* -rootdr location, size are in bytes
* -note strings are not terminated on image
*/
int writeVolDescriptor(VolInfo* volInfo, bk_off_t rootDrLocation,
unsigned rootDrSize, bk_off_t lPathTableLoc,
bk_off_t mPathTableLoc, unsigned pathTableSize,
time_t creationTime, bool isPrimary)
{
int rc;
size_t count;
unsigned char byte;
unsigned char aString[129];
unsigned anUnsigned;
unsigned short anUnsignedShort;
bk_off_t currPos;
/* VOLUME descriptor type */
if(isPrimary)
byte = 1;
else
byte = 2;
/* END VOLUME descriptor type */
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* standard identifier */
strcpy((char*)aString, "CD001");
rc = wcWrite(volInfo, (char*)aString, 5);
if(rc <= 0)
return rc;
/* volume descriptor version (always 1) */
byte = 1;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* primary: unused field
* supplementary: volume flags, 0x00 */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* system identifier (32 spaces) */
if(isPrimary)
{
strcpy((char*)aString, " ");
rc = wcWrite(volInfo, (char*)aString, 32);
if(rc <= 0)
return rc;
}
else
{
rc = writeJolietStringField(volInfo, "", 32);
if(rc < 0)
return rc;
}
/* VOLUME identifier */
if(isPrimary)
{
strcpy((char*)aString, volInfo->volId);
for(count = strlen((char*)aString); count < 32; count++)
aString[count] = ' ';
rc = wcWrite(volInfo, (char*)aString, 32);
if(rc <= 0)
return rc;
}
else
{
rc = writeJolietStringField(volInfo, volInfo->volId, 32);
if(rc < 0)
return rc;
}
/* END VOLUME identifier */
/* unused field */
rc = writeByteBlock(volInfo, 0, 8);
if(rc < 0)
return rc;
/* VOLUME space size (number of logical blocks, absolutely everything) */
/* it's safe to not use wcSeek() here since everything is left as it is */
currPos = bkSeekTell(volInfo->imageForWriting);
bkSeekSet(volInfo->imageForWriting, 0, SEEK_END);
anUnsigned = bkSeekTell(volInfo->imageForWriting) /
NBYTES_LOGICAL_BLOCK;
bkSeekSet(volInfo->imageForWriting, currPos, SEEK_SET);
rc = write733(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* END VOLUME space size (number of logical blocks, absolutely everything) */
/* primary: unused field
* joliet: escape sequences */
if(isPrimary)
{
rc = writeByteBlock(volInfo, 0, 32);
if(rc < 0)
return rc;
}
else
{
/* this is the only joliet field that's padded with 0x00 instead of ' ' */
aString[0] = 0x25;
aString[1] = 0x2F;
aString[2] = 0x45;
rc = wcWrite(volInfo, (char*)aString, 3);
if(rc <= 0)
return rc;
rc = writeByteBlock(volInfo, 0, 29);
if(rc < 0)
return rc;
}
/* volume set size (always 1) */
anUnsignedShort = 1;
rc = write723(volInfo, anUnsignedShort);
if(rc <= 0)
return rc;
/* volume sequence number (also always 1) */
rc = write723(volInfo, anUnsignedShort);
if(rc <= 0)
return rc;
/* logical block size (always 2048) */
anUnsignedShort = NBYTES_LOGICAL_BLOCK;
rc = write723(volInfo, anUnsignedShort);
if(rc <= 0)
return rc;
/* path table size */
anUnsigned = pathTableSize;
rc = write733(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* location of occurence of type l path table */
anUnsigned = lPathTableLoc / NBYTES_LOGICAL_BLOCK;
rc = write731(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* location of optional occurence of type l path table */
anUnsigned = 0;
rc = write731(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* location of occurence of type m path table */
anUnsigned = mPathTableLoc / NBYTES_LOGICAL_BLOCK;
rc = write732(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* location of optional occurence of type m path table */
anUnsigned = 0;
rc = write732(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* ROOT dr */
/* record length (always 34 here) */
byte = 34;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* extended attribute record length (always none) */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* location of extent */
anUnsigned = rootDrLocation / NBYTES_LOGICAL_BLOCK;
rc = write733(volInfo, anUnsigned);
if(rc <= 0)
return rc;
/* data length */
rc = write733(volInfo, rootDrSize);
if(rc <= 0)
return rc;
/* recording time */
epochToShortString(creationTime, (char*)aString);
rc = wcWrite(volInfo, (char*)aString, 7);
if(rc <= 0)
return rc;
/* file flags (always binary 00000010 here) */
byte = 0x02;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* file unit size (not in interleaved mode -> 0) */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* interleave gap size (not in interleaved mode -> 0) */
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* volume sequence number */
anUnsignedShort = 1;
rc = write723(volInfo, anUnsignedShort);
if(rc <= 0)
return rc;
/* length of file identifier */
byte = 1;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* file identifier */
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* END ROOT dr */
/* volume set identidier */
if(isPrimary)
{
rc = writeByteBlock(volInfo, ' ', 128);
if(rc < 0)
return rc;
}
else
{
rc = writeJolietStringField(volInfo, "", 128);
if(rc < 0)
return rc;
}
/* PUBLISHER identifier */
strcpy((char*)aString, volInfo->publisher);
if(isPrimary)
{
for(count = strlen((char*)aString); count < 128; count++)
aString[count] = ' ';
rc = wcWrite(volInfo, (char*)aString, 128);
if(rc <= 0)
return rc;
}
else
{
rc = writeJolietStringField(volInfo, (char*)aString, 128);
if(rc < 0)
return rc;
}
/* PUBLISHER identifier */
/* DATA preparer identifier */
if(isPrimary)
{
rc = wcWrite(volInfo, "ISO Master", 10);
if(rc <= 0)
return rc;
rc = writeByteBlock(volInfo, ' ', 118);
if(rc < 0)
return rc;
}
else
{
rc = writeJolietStringField(volInfo, "ISO Master", 128);
if(rc < 0)
return rc;
}
/* END DATA preparer identifier */
/* application identifier, copyright file identifier, abstract file
* identifier, bibliographic file identifier (128 + 3*37) */
if(isPrimary)
{
rc = writeByteBlock(volInfo, ' ', 239);
if(rc < 0)
return rc;
}
else
{
/* application id */
rc = writeJolietStringField(volInfo, "", 128);
if(rc < 0)
return rc;
/* 18 ucs2 spaces + 0x00 */
for(count = 0; count < 3; count++)
{
rc = writeJolietStringField(volInfo, "", 36);
if(rc < 0)
return rc;
byte = 0x00;
rc = wcWrite(volInfo, (char*)&byte, 1);
if(rc <= 0)
return rc;
}
}
/* VOLUME creation date */
epochToLongString(creationTime, (char*)aString);
rc = wcWrite(volInfo, (char*)aString, 17);
if(rc <= 0)
return rc;
/* END VOLUME creation date */
/* volume modification date (same as creation) */
rc = wcWrite(volInfo, (char*)aString, 17);
if(rc <= 0)
return rc;
/* VOLUME expiration date (none) */
rc = writeByteBlock(volInfo, '0', 16);
if(rc < 0)
return rc;
byte = 0;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* END VOLUME expiration date (none) */
/* volume effective date (same as creation) */
rc = wcWrite(volInfo, (char*)aString, 17);
if(rc <= 0)
return rc;
/* file structure version */
byte = 1;
rc = write711(volInfo, byte);
if(rc <= 0)
return rc;
/* reserved, applications use, reserved */
rc = writeByteBlock(volInfo, 0, 1166);
if(rc < 0)
return rc;
return 1;
}
/******************************************************************************
* wroteIsolinuxBootRecord()
* Check whether the file already written to the new iso was a boot record.
* */
int wroteIsolinuxBootRecord(VolInfo* volInfo, FileToWrite* file,
bool* isIsolinux)
{
*isIsolinux = false;
if(volInfo->bootMediaType == BOOT_MEDIA_NO_EMULATION &&
volInfo->bootRecordIsVisible &&
file->origFile == volInfo->bootRecordOnImage)
/* Likely true, do one more check to make sure. The extra check
* is needed for Windows XP isos with SP2 added by c't slipstreamer */
{
bk_off_t origPos;
int rc;
char fourBytes[4];
origPos = wcSeekTell(volInfo);
wcSeekSet(volInfo, BASETW_PTR(file)->extentNumber *
NBYTES_LOGICAL_BLOCK + 8);
rc = bkRead(volInfo->imageForWriting, fourBytes, 4);
if(rc != 4)
return BKERROR_READ_GENERIC;
if(fourBytes[0] == 16 && fourBytes[1] == 0 &&
fourBytes[2] == 0 && fourBytes[3] == 0)
*isIsolinux = true;
wcSeekSet(volInfo, origPos);
}
return 1;
}
|