1 /* $NetBSD: cd9660.c,v 1.56 2019/10/18 04:09:02 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan 5 * Perez-Rathke and Ram Vedam. All rights reserved. 6 * 7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, 8 * Alan Perez-Rathke and Ram Vedam. 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN 21 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN 25 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 28 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 * OF SUCH DAMAGE. 33 */ 34 /* 35 * Copyright (c) 2001 Wasabi Systems, Inc. 36 * All rights reserved. 37 * 38 * Written by Luke Mewburn for Wasabi Systems, Inc. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed for the NetBSD Project by 51 * Wasabi Systems, Inc. 52 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 53 * or promote products derived from this software without specific prior 54 * written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 /* 69 * Copyright (c) 1982, 1986, 1989, 1993 70 * The Regents of the University of California. All rights reserved. 71 * 72 * Redistribution and use in source and binary forms, with or without 73 * modification, are permitted provided that the following conditions 74 * are met: 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 3. Neither the name of the University nor the names of its contributors 81 * may be used to endorse or promote products derived from this software 82 * without specific prior written permission. 83 * 84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 94 * SUCH DAMAGE. 95 * 96 */ 97 98 #if HAVE_NBTOOL_CONFIG_H 99 #include "nbtool_config.h" 100 #else 101 #include <sys/mount.h> 102 #endif 103 104 #include <sys/cdefs.h> 105 #if defined(__RCSID) && !defined(__lint) 106 __RCSID("$NetBSD: cd9660.c,v 1.56 2019/10/18 04:09:02 msaitoh Exp $"); 107 #endif /* !__lint */ 108 109 #include <string.h> 110 #include <ctype.h> 111 #include <sys/param.h> 112 #include <sys/queue.h> 113 #include <util.h> 114 115 #include "makefs.h" 116 #include "cd9660.h" 117 #include "cd9660/iso9660_rrip.h" 118 #include "cd9660/cd9660_archimedes.h" 119 120 /* 121 * Global variables 122 */ 123 124 static void cd9660_finalize_PVD(iso9660_disk *); 125 static cd9660node *cd9660_allocate_cd9660node(void); 126 static void cd9660_set_defaults(iso9660_disk *); 127 static int cd9660_arguments_set_string(const char *, const char *, size_t, 128 char, char *); 129 static void cd9660_populate_iso_dir_record( 130 struct _iso_directory_record_cd9660 *, u_char, u_char, u_char, 131 const char *); 132 static void cd9660_setup_root_node(iso9660_disk *); 133 static int cd9660_setup_volume_descriptors(iso9660_disk *); 134 #if 0 135 static int cd9660_fill_extended_attribute_record(cd9660node *); 136 #endif 137 static void cd9660_sort_nodes(cd9660node *); 138 static int cd9660_translate_node_common(iso9660_disk *, cd9660node *); 139 static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *); 140 static int cd9660_compare_filename(const char *, const char *); 141 static void cd9660_sorted_child_insert(cd9660node *, cd9660node *); 142 static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int); 143 static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int, 144 int); 145 static void cd9660_copy_filenames(iso9660_disk *, cd9660node *); 146 static void cd9660_sorting_nodes(cd9660node *); 147 static int cd9660_count_collisions(cd9660node *); 148 static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *); 149 static int cd9660_add_dot_records(iso9660_disk *, cd9660node *); 150 151 static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int, 152 int *, int *); 153 static void cd9660_free_structure(cd9660node *); 154 static int cd9660_generate_path_table(iso9660_disk *); 155 static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *, 156 int); 157 static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *, 158 int); 159 #if 0 160 static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *, 161 int); 162 #endif 163 static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int); 164 static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *); 165 static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t); 166 #if 0 167 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int); 168 #endif 169 static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *, 170 cd9660node *, int, int); 171 static cd9660node *cd9660_create_file(iso9660_disk *, const char *, 172 cd9660node *, cd9660node *); 173 static cd9660node *cd9660_create_directory(iso9660_disk *, const char *, 174 cd9660node *, cd9660node *); 175 static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char, 176 cd9660node *); 177 static int cd9660_add_generic_bootimage(iso9660_disk *, const char *); 178 179 180 /* 181 * Allocate and initialize a cd9660node 182 * @returns struct cd9660node * Pointer to new node, or NULL on error 183 */ 184 static cd9660node * 185 cd9660_allocate_cd9660node(void) 186 { 187 cd9660node *temp = ecalloc(1, sizeof(*temp)); 188 TAILQ_INIT(&temp->cn_children); 189 temp->parent = temp->dot_record = temp->dot_dot_record = NULL; 190 temp->ptnext = temp->ptprev = temp->ptlast = NULL; 191 temp->node = NULL; 192 temp->isoDirRecord = NULL; 193 temp->isoExtAttributes = NULL; 194 temp->rr_real_parent = temp->rr_relocated = NULL; 195 temp->su_tail_data = NULL; 196 return temp; 197 } 198 199 int cd9660_defaults_set = 0; 200 201 /** 202 * Set default values for cd9660 extension to makefs 203 */ 204 static void 205 cd9660_set_defaults(iso9660_disk *diskStructure) 206 { 207 /*Fix the sector size for now, though the spec allows for other sizes*/ 208 diskStructure->sectorSize = 2048; 209 210 /* Set up defaults in our own structure */ 211 diskStructure->verbose_level = 0; 212 diskStructure->keep_bad_images = 0; 213 diskStructure->follow_sym_links = 0; 214 diskStructure->isoLevel = 2; 215 216 diskStructure->rock_ridge_enabled = 0; 217 diskStructure->rock_ridge_renamed_dir_name = 0; 218 diskStructure->rock_ridge_move_count = 0; 219 diskStructure->rr_moved_dir = 0; 220 221 diskStructure->archimedes_enabled = 0; 222 diskStructure->chrp_boot = 0; 223 224 diskStructure->include_padding_areas = 1; 225 226 /* Spec breaking functionality */ 227 diskStructure->allow_deep_trees = 228 diskStructure->allow_start_dot = 229 diskStructure->allow_max_name = 230 diskStructure->allow_illegal_chars = 231 diskStructure->allow_lowercase = 232 diskStructure->allow_multidot = 233 diskStructure->omit_trailing_period = 0; 234 235 /* Make sure the PVD is clear */ 236 memset(&diskStructure->primaryDescriptor, 0, 2048); 237 238 memset(diskStructure->primaryDescriptor.publisher_id, 0x20,128); 239 memset(diskStructure->primaryDescriptor.preparer_id, 0x20,128); 240 memset(diskStructure->primaryDescriptor.application_id, 0x20,128); 241 memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37); 242 memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37); 243 memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37); 244 245 strcpy(diskStructure->primaryDescriptor.system_id,"NetBSD"); 246 247 cd9660_defaults_set = 1; 248 249 /* Boot support: Initially disabled */ 250 diskStructure->has_generic_bootimage = 0; 251 diskStructure->generic_bootimage = NULL; 252 253 diskStructure->boot_image_directory = 0; 254 /*memset(diskStructure->boot_descriptor, 0, 2048);*/ 255 256 diskStructure->is_bootable = 0; 257 TAILQ_INIT(&diskStructure->boot_images); 258 LIST_INIT(&diskStructure->boot_entries); 259 } 260 261 void 262 cd9660_prep_opts(fsinfo_t *fsopts) 263 { 264 iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure)); 265 266 #define OPT_STR(letter, name, desc) \ 267 { letter, name, NULL, OPT_STRBUF, 0, 0, desc } 268 269 #define OPT_NUM(letter, name, field, min, max, desc) \ 270 { letter, name, &diskStructure->field, \ 271 sizeof(diskStructure->field) == 8 ? OPT_INT64 : \ 272 (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \ 273 (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \ 274 min, max, desc } 275 276 #define OPT_BOOL(letter, name, field, desc) \ 277 OPT_NUM(letter, name, field, 0, 1, desc) 278 279 const option_t cd9660_options[] = { 280 OPT_NUM('l', "isolevel", isoLevel, 281 1, 3, "ISO Level"), 282 OPT_NUM('v', "verbose", verbose_level, 283 0, 2, "Turns on verbose output"), 284 285 OPT_BOOL('h', "help", displayHelp, 286 "Show help message"), 287 OPT_BOOL('S', "follow-symlinks", follow_sym_links, 288 "Resolve symlinks in pathnames"), 289 OPT_BOOL('R', "rockridge", rock_ridge_enabled, 290 "Enable Rock-Ridge extensions"), 291 OPT_BOOL('C', "chrp-boot", chrp_boot, 292 "Enable CHRP boot"), 293 OPT_BOOL('K', "keep-bad-images", keep_bad_images, 294 "Keep bad images"), 295 OPT_BOOL('D', "allow-deep-trees", allow_deep_trees, 296 "Allow trees more than 8 levels"), 297 OPT_BOOL('a', "allow-max-name", allow_max_name, 298 "Allow 37 char filenames (unimplemented)"), 299 OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars, 300 "Allow illegal characters in filenames"), 301 OPT_BOOL('m', "allow-multidot", allow_multidot, 302 "Allow multiple periods in filenames"), 303 OPT_BOOL('o', "omit-trailing-period", omit_trailing_period, 304 "Omit trailing periods in filenames"), 305 OPT_BOOL('\0', "allow-lowercase", allow_lowercase, 306 "Allow lowercase characters in filenames"), 307 OPT_BOOL('\0', "archimedes", archimedes_enabled, 308 "Enable Archimedes structure"), 309 OPT_BOOL('\0', "no-trailing-padding", include_padding_areas, 310 "Include padding areas"), 311 312 OPT_STR('A', "applicationid", "Application Identifier"), 313 OPT_STR('P', "publisher", "Publisher Identifier"), 314 OPT_STR('p', "preparer", "Preparer Identifier"), 315 OPT_STR('L', "label", "Disk Label"), 316 OPT_STR('V', "volumeid", "Volume Set Identifier"), 317 OPT_STR('B', "bootimage", "Boot image parameter"), 318 OPT_STR('G', "generic-bootimage", "Generic boot image param"), 319 OPT_STR('\0', "bootimagedir", "Boot image directory"), 320 OPT_STR('\0', "no-emul-boot", "No boot emulation"), 321 OPT_STR('\0', "no-boot", "No boot support"), 322 OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"), 323 OPT_STR('\0', "boot-load-segment", "Boot load segment"), 324 OPT_STR('\0', "platformid", "Section Header Platform ID"), 325 326 { .name = NULL } 327 }; 328 329 fsopts->fs_specific = diskStructure; 330 fsopts->fs_options = copy_opts(cd9660_options); 331 332 cd9660_set_defaults(diskStructure); 333 } 334 335 void 336 cd9660_cleanup_opts(fsinfo_t *fsopts) 337 { 338 free(fsopts->fs_specific); 339 free(fsopts->fs_options); 340 } 341 342 static int 343 cd9660_arguments_set_string(const char *val, const char *fieldtitle, 344 size_t length, char testmode, char * dest) 345 { 346 size_t len; 347 int test; 348 349 if (val == NULL) 350 warnx("error: The %s requires a string argument", fieldtitle); 351 else if ((len = strlen(val)) <= length) { 352 if (testmode == 'd') 353 test = cd9660_valid_d_chars(val); 354 else 355 test = cd9660_valid_a_chars(val); 356 if (test) { 357 memcpy(dest, val, len); 358 if (test == 2) 359 cd9660_uppercase_characters(dest, len); 360 return 1; 361 } else 362 warnx("error: The %s must be composed of " 363 "%c-characters", fieldtitle, testmode); 364 } else 365 warnx("error: The %s must be at most 32 characters long", 366 fieldtitle); 367 return 0; 368 } 369 370 /* 371 * Command-line parsing function 372 */ 373 374 int 375 cd9660_parse_opts(const char *option, fsinfo_t *fsopts) 376 { 377 int rv, i; 378 iso9660_disk *diskStructure = fsopts->fs_specific; 379 option_t *cd9660_options = fsopts->fs_options; 380 char buf[1024]; 381 const char *name, *desc; 382 383 assert(option != NULL); 384 385 if (debug & DEBUG_FS_PARSE_OPTS) 386 printf("%s: got `%s'\n", __func__, option); 387 388 i = set_option(cd9660_options, option, buf, sizeof(buf)); 389 if (i == -1) 390 return 0; 391 392 if (cd9660_options[i].name == NULL) 393 abort(); 394 395 396 name = cd9660_options[i].name; 397 desc = cd9660_options[i].desc; 398 switch (cd9660_options[i].letter) { 399 case 'h': 400 case 'S': 401 rv = 0; /* this is not handled yet */ 402 break; 403 case 'L': 404 rv = cd9660_arguments_set_string(buf, desc, 32, 'd', 405 diskStructure->primaryDescriptor.volume_id); 406 break; 407 case 'A': 408 rv = cd9660_arguments_set_string(buf, desc, 128, 'a', 409 diskStructure->primaryDescriptor.application_id); 410 break; 411 case 'P': 412 rv = cd9660_arguments_set_string(buf, desc, 128, 'a', 413 diskStructure->primaryDescriptor.publisher_id); 414 break; 415 case 'p': 416 rv = cd9660_arguments_set_string(buf, desc, 128, 'a', 417 diskStructure->primaryDescriptor.preparer_id); 418 break; 419 case 'V': 420 rv = cd9660_arguments_set_string(buf, desc, 128, 'a', 421 diskStructure->primaryDescriptor.volume_set_id); 422 break; 423 /* Boot options */ 424 case 'B': 425 if (buf[0] == '\0') { 426 warnx("The Boot Image parameter requires a valid boot" 427 " information string"); 428 rv = 0; 429 } else 430 rv = cd9660_add_boot_disk(diskStructure, buf); 431 break; 432 case 'G': 433 if (buf[0] == '\0') { 434 warnx("The Generic Boot Image parameter requires a" 435 " valid boot information string"); 436 rv = 0; 437 } else 438 rv = cd9660_add_generic_bootimage(diskStructure, buf); 439 break; 440 default: 441 if (strcmp(name, "bootimagedir") == 0) { 442 /* 443 * XXXfvdl this is unused. 444 */ 445 if (buf[0] == '\0') { 446 warnx("The Boot Image Directory parameter" 447 " requires a directory name"); 448 rv = 0; 449 } else { 450 diskStructure->boot_image_directory = 451 emalloc(strlen(buf) + 1); 452 /* BIG TODO: Add the max length function here */ 453 rv = cd9660_arguments_set_string(buf, desc, 12, 454 'd', diskStructure->boot_image_directory); 455 } 456 } else if (strcmp(name, "no-emul-boot") == 0 || 457 strcmp(name, "no-boot") == 0 || 458 strcmp(name, "hard-disk-boot") == 0) { 459 /* RRIP */ 460 cd9660_eltorito_add_boot_option(diskStructure, name, 0); 461 rv = 1; 462 } else if (strcmp(name, "boot-load-segment") == 0 || 463 strcmp(name, "platformid") == 0) { 464 if (buf[0] == '\0') { 465 warnx("Option `%s' doesn't contain a value", 466 name); 467 rv = 0; 468 } else { 469 cd9660_eltorito_add_boot_option(diskStructure, 470 name, buf); 471 rv = 1; 472 } 473 } else 474 rv = 1; 475 } 476 return rv; 477 } 478 479 /* 480 * Main function for cd9660_makefs 481 * Builds the ISO image file 482 * @param const char *image The image filename to create 483 * @param const char *dir The directory that is being read 484 * @param struct fsnode *root The root node of the filesystem tree 485 * @param struct fsinfo_t *fsopts Any options 486 */ 487 void 488 cd9660_makefs(const char *image, const char *dir, fsnode *root, 489 fsinfo_t *fsopts) 490 { 491 int64_t startoffset; 492 int numDirectories; 493 uint64_t pathTableSectors; 494 int64_t firstAvailableSector; 495 int64_t totalSpace; 496 int error; 497 cd9660node *real_root; 498 iso9660_disk *diskStructure = fsopts->fs_specific; 499 500 if (diskStructure->verbose_level > 0) 501 printf("%s: ISO level is %i\n", __func__, 502 diskStructure->isoLevel); 503 if (diskStructure->isoLevel < 2 && 504 diskStructure->allow_multidot) 505 errx(EXIT_FAILURE, "allow-multidot requires iso level of 2"); 506 507 assert(image != NULL); 508 assert(dir != NULL); 509 assert(root != NULL); 510 511 if (diskStructure->displayHelp) { 512 /* 513 * Display help here - probably want to put it in 514 * a separate function 515 */ 516 return; 517 } 518 519 if (diskStructure->verbose_level > 0) 520 printf("%s: image %s directory %s root %p\n", __func__, 521 image, dir, root); 522 523 /* Set up some constants. Later, these will be defined with options */ 524 525 /* Counter needed for path tables */ 526 numDirectories = 0; 527 528 /* Convert tree to our own format */ 529 /* Actually, we now need to add the REAL root node, at level 0 */ 530 531 real_root = cd9660_allocate_cd9660node(); 532 real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord)); 533 /* Leave filename blank for root */ 534 memset(real_root->isoDirRecord->name, 0, 535 ISO_FILENAME_MAXLENGTH_WITH_PADDING); 536 537 real_root->level = 0; 538 diskStructure->rootNode = real_root; 539 real_root->type = CD9660_TYPE_DIR; 540 error = 0; 541 real_root->node = root; 542 cd9660_convert_structure(diskStructure, root, real_root, 1, 543 &numDirectories, &error); 544 545 if (TAILQ_EMPTY(&real_root->cn_children)) { 546 errx(EXIT_FAILURE, "%s: converted directory is empty. " 547 "Tree conversion failed", __func__); 548 } else if (error != 0) { 549 errx(EXIT_FAILURE, "%s: tree conversion failed", __func__); 550 } else { 551 if (diskStructure->verbose_level > 0) 552 printf("%s: tree converted\n", __func__); 553 } 554 555 /* Add the dot and dot dot records */ 556 cd9660_add_dot_records(diskStructure, real_root); 557 558 cd9660_setup_root_node(diskStructure); 559 560 if (diskStructure->verbose_level > 0) 561 printf("%s: done converting tree\n", __func__); 562 563 /* non-SUSP extensions */ 564 if (diskStructure->archimedes_enabled) 565 archimedes_convert_tree(diskStructure->rootNode); 566 567 /* Rock ridge / SUSP init pass */ 568 if (diskStructure->rock_ridge_enabled) { 569 cd9660_susp_initialize(diskStructure, diskStructure->rootNode, 570 diskStructure->rootNode, NULL); 571 } 572 573 /* Build path table structure */ 574 diskStructure->pathTableLength = cd9660_generate_path_table( 575 diskStructure); 576 577 pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize, 578 diskStructure->pathTableLength); 579 580 firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure); 581 if (diskStructure->is_bootable) { 582 firstAvailableSector = cd9660_setup_boot(diskStructure, 583 firstAvailableSector); 584 if (firstAvailableSector < 0) 585 errx(EXIT_FAILURE, "setup_boot failed"); 586 } 587 /* LE first, then BE */ 588 diskStructure->primaryLittleEndianTableSector = firstAvailableSector; 589 diskStructure->primaryBigEndianTableSector = 590 diskStructure->primaryLittleEndianTableSector + pathTableSectors; 591 592 /* Set the secondary ones to -1, not going to use them for now */ 593 diskStructure->secondaryBigEndianTableSector = -1; 594 diskStructure->secondaryLittleEndianTableSector = -1; 595 596 diskStructure->dataFirstSector = 597 diskStructure->primaryBigEndianTableSector + pathTableSectors; 598 if (diskStructure->verbose_level > 0) 599 printf("%s: Path table conversion complete. " 600 "Each table is %i bytes, or %" PRIu64 " sectors.\n", 601 __func__, 602 diskStructure->pathTableLength, pathTableSectors); 603 604 startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector; 605 606 totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset); 607 608 diskStructure->totalSectors = diskStructure->dataFirstSector + 609 CD9660_BLOCKS(diskStructure->sectorSize, totalSpace); 610 611 /* Disabled until pass 1 is done */ 612 if (diskStructure->rock_ridge_enabled) { 613 diskStructure->susp_continuation_area_start_sector = 614 diskStructure->totalSectors; 615 diskStructure->totalSectors += 616 CD9660_BLOCKS(diskStructure->sectorSize, 617 diskStructure->susp_continuation_area_size); 618 cd9660_susp_finalize(diskStructure, diskStructure->rootNode); 619 } 620 621 622 cd9660_finalize_PVD(diskStructure); 623 624 /* Add padding sectors, just for testing purposes right now */ 625 /* diskStructure->totalSectors+=150; */ 626 627 /* Debugging output */ 628 if (diskStructure->verbose_level > 0) { 629 printf("%s: Sectors 0-15 reserved\n", __func__); 630 printf("%s: Primary path tables starts in sector %" 631 PRId64 "\n", __func__, 632 diskStructure->primaryLittleEndianTableSector); 633 printf("%s: File data starts in sector %" 634 PRId64 "\n", __func__, diskStructure->dataFirstSector); 635 printf("%s: Total sectors: %" 636 PRId64 "\n", __func__, diskStructure->totalSectors); 637 } 638 639 /* 640 * Add padding sectors at the end 641 * TODO: Clean this up and separate padding 642 */ 643 if (diskStructure->include_padding_areas) 644 diskStructure->totalSectors += 150; 645 646 cd9660_write_image(diskStructure, image); 647 648 if (diskStructure->verbose_level > 1) { 649 debug_print_volume_descriptor_information(diskStructure); 650 debug_print_tree(diskStructure, real_root, 0); 651 debug_print_path_tree(real_root); 652 } 653 654 /* Clean up data structures */ 655 cd9660_free_structure(real_root); 656 657 if (diskStructure->verbose_level > 0) 658 printf("%s: done\n", __func__); 659 } 660 661 /* Generic function pointer - implement later */ 662 typedef int (*cd9660node_func)(cd9660node *); 663 664 static void 665 cd9660_finalize_PVD(iso9660_disk *diskStructure) 666 { 667 time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); 668 669 /* root should be a fixed size of 34 bytes since it has no name */ 670 memcpy(diskStructure->primaryDescriptor.root_directory_record, 671 diskStructure->rootNode->dot_record->isoDirRecord, 34); 672 673 /* In RRIP, this might be longer than 34 */ 674 diskStructure->primaryDescriptor.root_directory_record[0] = 34; 675 676 /* Set up all the important numbers in the PVD */ 677 cd9660_bothendian_dword(diskStructure->totalSectors, 678 (unsigned char *)diskStructure->primaryDescriptor.volume_space_size); 679 cd9660_bothendian_word(1, 680 (unsigned char *)diskStructure->primaryDescriptor.volume_set_size); 681 cd9660_bothendian_word(1, 682 (unsigned char *) 683 diskStructure->primaryDescriptor.volume_sequence_number); 684 cd9660_bothendian_word(diskStructure->sectorSize, 685 (unsigned char *) 686 diskStructure->primaryDescriptor.logical_block_size); 687 cd9660_bothendian_dword(diskStructure->pathTableLength, 688 (unsigned char *)diskStructure->primaryDescriptor.path_table_size); 689 690 cd9660_731(diskStructure->primaryLittleEndianTableSector, 691 (u_char *)diskStructure->primaryDescriptor.type_l_path_table); 692 cd9660_732(diskStructure->primaryBigEndianTableSector, 693 (u_char *)diskStructure->primaryDescriptor.type_m_path_table); 694 695 diskStructure->primaryDescriptor.file_structure_version[0] = 1; 696 697 /* Pad all strings with spaces instead of nulls */ 698 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32); 699 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32); 700 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id, 701 128); 702 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id, 703 128); 704 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id, 705 128); 706 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id, 707 128); 708 cd9660_pad_string_spaces( 709 diskStructure->primaryDescriptor.copyright_file_id, 37); 710 cd9660_pad_string_spaces( 711 diskStructure->primaryDescriptor.abstract_file_id, 37); 712 cd9660_pad_string_spaces( 713 diskStructure->primaryDescriptor.bibliographic_file_id, 37); 714 715 /* Setup dates */ 716 cd9660_time_8426( 717 (unsigned char *)diskStructure->primaryDescriptor.creation_date, 718 tstamp); 719 cd9660_time_8426( 720 (unsigned char *)diskStructure->primaryDescriptor.modification_date, 721 tstamp); 722 723 #if 0 724 cd9660_set_date(diskStructure->primaryDescriptor.expiration_date, 725 tstamp); 726 #endif 727 memset(diskStructure->primaryDescriptor.expiration_date, '0' ,16); 728 diskStructure->primaryDescriptor.expiration_date[16] = 0; 729 730 cd9660_time_8426( 731 (unsigned char *)diskStructure->primaryDescriptor.effective_date, 732 tstamp); 733 } 734 735 static void 736 cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record, 737 u_char ext_attr_length, u_char flags, 738 u_char name_len, const char * name) 739 { 740 record->ext_attr_length[0] = ext_attr_length; 741 record->flags[0] = ISO_FLAG_CLEAR | flags; 742 record->file_unit_size[0] = 0; 743 record->interleave[0] = 0; 744 cd9660_bothendian_word(1, record->volume_sequence_number); 745 record->name_len[0] = name_len; 746 memset(record->name, '\0', sizeof (record->name)); 747 memcpy(record->name, name, name_len); 748 record->length[0] = 33 + name_len; 749 750 /* Todo : better rounding */ 751 record->length[0] += (record->length[0] & 1) ? 1 : 0; 752 } 753 754 static void 755 cd9660_setup_root_node(iso9660_disk *diskStructure) 756 { 757 cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord, 758 0, ISO_FLAG_DIRECTORY, 1, "\0"); 759 760 } 761 762 /*********** SUPPORT FUNCTIONS ***********/ 763 static int 764 cd9660_setup_volume_descriptors(iso9660_disk *diskStructure) 765 { 766 /* Boot volume descriptor should come second */ 767 int sector = 16; 768 /* For now, a fixed 2 : PVD and terminator */ 769 volume_descriptor *temp, *t; 770 771 /* Set up the PVD */ 772 temp = emalloc(sizeof(*temp)); 773 temp->volumeDescriptorData = 774 (unsigned char *)&diskStructure->primaryDescriptor; 775 temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD; 776 temp->volumeDescriptorData[6] = 1; 777 temp->sector = sector; 778 memcpy(temp->volumeDescriptorData + 1, 779 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); 780 diskStructure->firstVolumeDescriptor = temp; 781 782 sector++; 783 /* Set up boot support if enabled. BVD must reside in sector 17 */ 784 if (diskStructure->is_bootable) { 785 t = emalloc(sizeof(*t)); 786 t->volumeDescriptorData = ecalloc(1, 2048); 787 temp->next = t; 788 temp = t; 789 t->sector = 17; 790 if (diskStructure->verbose_level > 0) 791 printf("Setting up boot volume descriptor\n"); 792 cd9660_setup_boot_volume_descriptor(diskStructure, t); 793 sector++; 794 } 795 796 /* Set up the terminator */ 797 t = emalloc(sizeof(*t)); 798 t->volumeDescriptorData = ecalloc(1, 2048); 799 temp->next = t; 800 t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR; 801 t->next = 0; 802 t->volumeDescriptorData[6] = 1; 803 t->sector = sector; 804 memcpy(t->volumeDescriptorData + 1, 805 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); 806 807 sector++; 808 return sector; 809 } 810 811 #if 0 812 /* 813 * Populate EAR at some point. Not required, but is used by NetBSD's 814 * cd9660 support 815 */ 816 static int 817 cd9660_fill_extended_attribute_record(cd9660node *node) 818 { 819 node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes)); 820 return 1; 821 } 822 #endif 823 824 static int 825 cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode) 826 { 827 time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); 828 u_char flag; 829 char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING]; 830 831 /* Now populate the isoDirRecord structure */ 832 memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); 833 834 (void)cd9660_convert_filename(diskStructure, newnode->node->name, 835 temp, !(S_ISDIR(newnode->node->type))); 836 837 flag = ISO_FLAG_CLEAR; 838 if (S_ISDIR(newnode->node->type)) 839 flag |= ISO_FLAG_DIRECTORY; 840 841 cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0, 842 flag, strlen(temp), temp); 843 844 /* Set the various dates */ 845 846 /* If we want to use the current date and time */ 847 848 cd9660_time_915(newnode->isoDirRecord->date, tstamp); 849 850 cd9660_bothendian_dword(newnode->fileDataLength, 851 newnode->isoDirRecord->size); 852 /* If the file is a link, we want to set the size to 0 */ 853 if (S_ISLNK(newnode->node->type)) 854 newnode->fileDataLength = 0; 855 856 return 1; 857 } 858 859 /* 860 * Translate fsnode to cd9660node 861 * Translate filenames and other metadata, including dates, sizes, 862 * permissions, etc 863 * @param struct fsnode * The node generated by makefs 864 * @param struct cd9660node * The intermediate node to be written to 865 * @returns int 0 on failure, 1 on success 866 */ 867 static int 868 cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node, 869 cd9660node *newnode) 870 { 871 if (node == NULL) { 872 if (diskStructure->verbose_level > 0) 873 printf("%s: NULL node passed, returning\n", __func__); 874 return 0; 875 } 876 newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord)); 877 /* Set the node pointer */ 878 newnode->node = node; 879 880 /* Set the size */ 881 if (!(S_ISDIR(node->type))) 882 newnode->fileDataLength = node->inode->st.st_size; 883 884 if (cd9660_translate_node_common(diskStructure, newnode) == 0) 885 return 0; 886 887 /* Finally, overwrite some of the values that are set by default */ 888 cd9660_time_915(newnode->isoDirRecord->date, 889 stampst.st_ino ? stampst.st_mtime : node->inode->st.st_mtime); 890 891 return 1; 892 } 893 894 /* 895 * Compares two ISO filenames 896 * @param const char * The first file name 897 * @param const char * The second file name 898 * @returns : -1 if first is less than second, 0 if they are the same, 1 if 899 * the second is greater than the first 900 */ 901 static int 902 cd9660_compare_filename(const char *first, const char *second) 903 { 904 /* 905 * This can be made more optimal once it has been tested 906 * (the extra character, for example, is for testing) 907 */ 908 909 int p1 = 0; 910 int p2 = 0; 911 char c1, c2; 912 /* First, on the filename */ 913 914 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1 915 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) { 916 c1 = first[p1]; 917 c2 = second[p2]; 918 if (c1 == '.' && c2 =='.') 919 break; 920 else if (c1 == '.') { 921 p2++; 922 c1 = ' '; 923 } else if (c2 == '.') { 924 p1++; 925 c2 = ' '; 926 } else { 927 p1++; 928 p2++; 929 } 930 931 if (c1 < c2) 932 return -1; 933 else if (c1 > c2) { 934 return 1; 935 } 936 } 937 938 if (first[p1] == '.' && second[p2] == '.') { 939 p1++; 940 p2++; 941 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1 942 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) { 943 c1 = first[p1]; 944 c2 = second[p2]; 945 if (c1 == ';' && c2 == ';') 946 break; 947 else if (c1 == ';') { 948 p2++; 949 c1 = ' '; 950 } else if (c2 == ';') { 951 p1++; 952 c2 = ' '; 953 } else { 954 p1++; 955 p2++; 956 } 957 958 if (c1 < c2) 959 return -1; 960 else if (c1 > c2) 961 return 1; 962 } 963 } 964 return 0; 965 } 966 967 /* 968 * Insert a node into list with ISO sorting rules 969 * @param cd9660node * The head node of the list 970 * @param cd9660node * The node to be inserted 971 */ 972 static void 973 cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new) 974 { 975 int compare; 976 cd9660node *cn; 977 struct cd9660_children_head *head = &parent->cn_children; 978 979 /* TODO: Optimize? */ 980 cn_new->parent = parent; 981 982 /* 983 * first will either be 0, the . or the .. 984 * if . or .., this means no other entry may be written before first 985 * if 0, the new node may be inserted at the head 986 */ 987 988 TAILQ_FOREACH(cn, head, cn_next_child) { 989 /* 990 * Dont insert a node twice - 991 * that would cause an infinite loop 992 */ 993 if (cn_new == cn) 994 return; 995 996 compare = cd9660_compare_filename(cn_new->isoDirRecord->name, 997 cn->isoDirRecord->name); 998 999 if (compare == 0) 1000 compare = cd9660_compare_filename(cn_new->node->name, 1001 cn->node->name); 1002 1003 if (compare < 0) 1004 break; 1005 } 1006 if (cn == NULL) 1007 TAILQ_INSERT_TAIL(head, cn_new, cn_next_child); 1008 else 1009 TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child); 1010 } 1011 1012 /* 1013 * Called After cd9660_sorted_child_insert 1014 * handles file collisions by suffixing each filname with ~n 1015 * where n represents the files respective place in the ordering 1016 */ 1017 static int 1018 cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding, 1019 int past) 1020 { 1021 cd9660node *iter, *next, *prev; 1022 int skip; 1023 int delete_chars = 0; 1024 int temp_past = past; 1025 int temp_skip; 1026 int flag = 0; 1027 cd9660node *end_of_range; 1028 1029 for (iter = TAILQ_FIRST(&colliding->cn_children); 1030 iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) { 1031 if (strcmp(iter->isoDirRecord->name, 1032 next->isoDirRecord->name) != 0) { 1033 iter = TAILQ_NEXT(iter, cn_next_child); 1034 continue; 1035 } 1036 flag = 1; 1037 temp_skip = skip = cd9660_count_collisions(iter); 1038 end_of_range = iter; 1039 while (temp_skip > 0) { 1040 temp_skip--; 1041 end_of_range = TAILQ_NEXT(end_of_range, cn_next_child); 1042 } 1043 temp_past = past; 1044 while (temp_past > 0) { 1045 if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL) 1046 end_of_range = next; 1047 else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL) 1048 iter = prev; 1049 else 1050 delete_chars++; 1051 temp_past--; 1052 } 1053 skip += past; 1054 iter = cd9660_rename_filename(diskStructure, iter, skip, 1055 delete_chars); 1056 } 1057 return flag; 1058 } 1059 1060 1061 static cd9660node * 1062 cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num, 1063 int delete_chars) 1064 { 1065 int i = 0; 1066 int numbts, dot, semi, digit, digits, temp, powers, multiplier, count; 1067 char *naming; 1068 int maxlength; 1069 char *tmp; 1070 1071 if (diskStructure->verbose_level > 0) 1072 printf("Rename_filename called\n"); 1073 1074 /* TODO : A LOT of chanes regarding 8.3 filenames */ 1075 if (diskStructure->isoLevel == 1) 1076 maxlength = 8; 1077 else if (diskStructure->isoLevel == 2) 1078 maxlength = 31; 1079 else 1080 maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION; 1081 1082 tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING); 1083 1084 while (i < num && iter) { 1085 powers = 1; 1086 count = 0; 1087 digits = 1; 1088 multiplier = 1; 1089 while (((int)(i / powers) ) >= 10) { 1090 digits++; 1091 powers = powers * 10; 1092 } 1093 1094 naming = iter->o_name; 1095 1096 /* 1097 while ((*naming != '.') && (*naming != ';')) { 1098 naming++; 1099 count++; 1100 } 1101 */ 1102 1103 dot = -1; 1104 semi = -1; 1105 while (count < maxlength) { 1106 if (*naming == '.') 1107 dot = count; 1108 else if (*naming == ';') { 1109 semi = count; 1110 break; 1111 } 1112 naming++; 1113 count++; 1114 } 1115 1116 if ((count + digits) < maxlength) 1117 numbts = count; 1118 else 1119 numbts = maxlength - (digits); 1120 numbts -= delete_chars; 1121 1122 /* 8.3 rules - keep the extension, add before the dot */ 1123 1124 /* 1125 * This code makes a bunch of assumptions. 1126 * See if you can spot them all :) 1127 */ 1128 1129 #if 0 1130 if (diskStructure->isoLevel == 1) { 1131 numbts = 8 - digits - delete_chars; 1132 if (dot < 0) { 1133 1134 } else { 1135 if (dot < 8) { 1136 memmove(&tmp[numbts],&tmp[dot],4); 1137 } 1138 } 1139 } 1140 #else 1141 __USE(dot); 1142 __USE(semi); 1143 __USE(multiplier); 1144 #endif 1145 1146 /* (copying just the filename before the '.' */ 1147 memcpy(tmp, (iter->o_name), numbts); 1148 1149 /* adding the appropriate number following the name */ 1150 temp = i; 1151 while (digits > 0) { 1152 digit = (int)(temp / powers); 1153 temp = temp - digit * powers; 1154 sprintf(&tmp[numbts] , "%d", digit); 1155 digits--; 1156 numbts++; 1157 powers = powers / 10; 1158 } 1159 1160 while ((*naming != ';') && (numbts < maxlength)) { 1161 tmp[numbts] = (*naming); 1162 naming++; 1163 numbts++; 1164 } 1165 1166 tmp[numbts] = ';'; 1167 tmp[numbts+1] = '1'; 1168 tmp[numbts+2] = '\0'; 1169 1170 /* 1171 * now tmp has exactly the identifier 1172 * we want so we'll copy it back to record 1173 */ 1174 memcpy((iter->isoDirRecord->name), tmp, numbts + 3); 1175 1176 iter = TAILQ_NEXT(iter, cn_next_child); 1177 i++; 1178 } 1179 1180 free(tmp); 1181 return iter; 1182 } 1183 1184 /* Todo: Figure out why these functions are nec. */ 1185 static void 1186 cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node) 1187 { 1188 cd9660node *cn; 1189 1190 if (TAILQ_EMPTY(&node->cn_children)) 1191 return; 1192 1193 if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) { 1194 debug_print_tree(diskStructure, diskStructure->rootNode, 0); 1195 exit(1); 1196 } 1197 1198 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { 1199 cd9660_copy_filenames(diskStructure, cn); 1200 memcpy(cn->o_name, cn->isoDirRecord->name, 1201 ISO_FILENAME_MAXLENGTH_WITH_PADDING); 1202 } 1203 } 1204 1205 static void 1206 cd9660_sorting_nodes(cd9660node *node) 1207 { 1208 cd9660node *cn; 1209 1210 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) 1211 cd9660_sorting_nodes(cn); 1212 cd9660_sort_nodes(node); 1213 } 1214 1215 /* XXX Bubble sort. */ 1216 static void 1217 cd9660_sort_nodes(cd9660node *node) 1218 { 1219 cd9660node *cn, *next; 1220 1221 do { 1222 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { 1223 if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL) 1224 return; 1225 else if (strcmp(next->isoDirRecord->name, 1226 cn->isoDirRecord->name) >= 0) 1227 continue; 1228 TAILQ_REMOVE(&node->cn_children, next, cn_next_child); 1229 TAILQ_INSERT_BEFORE(cn, next, cn_next_child); 1230 break; 1231 } 1232 } while (cn != NULL); 1233 } 1234 1235 static int 1236 cd9660_count_collisions(cd9660node *copy) 1237 { 1238 int count = 0; 1239 cd9660node *iter, *next; 1240 1241 for (iter = copy; 1242 (next = TAILQ_NEXT(iter, cn_next_child)) != NULL; 1243 iter = next) { 1244 if (cd9660_compare_filename(iter->isoDirRecord->name, 1245 next->isoDirRecord->name) == 0) 1246 count++; 1247 else 1248 return count; 1249 } 1250 #if 0 1251 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) { 1252 printf("%s: count is %i \n", __func__, count); 1253 compare = cd9660_compare_filename(iter->isoDirRecord->name, 1254 next->isoDirRecord->name); 1255 if (compare == 0) { 1256 count++; 1257 return cd9660_recurse_on_collision(next, count); 1258 } else 1259 return count; 1260 } 1261 #endif 1262 return count; 1263 } 1264 1265 static cd9660node * 1266 cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir) 1267 { 1268 char newname[9]; 1269 cd9660node *tfile; 1270 1271 /* 1272 * This function needs to: 1273 * 1) Create an empty virtual file in place of the old directory 1274 * 2) Point the virtual file to the new directory 1275 * 3) Point the relocated directory to its old parent 1276 * 4) Move the directory specified by dir into rr_moved_dir, 1277 * and rename it to "diskStructure->rock_ridge_move_count" (as a string) 1278 */ 1279 1280 /* First see if the moved directory even exists */ 1281 if (diskStructure->rr_moved_dir == NULL) { 1282 diskStructure->rr_moved_dir = cd9660_create_directory( 1283 diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME, 1284 diskStructure->rootNode, dir); 1285 if (diskStructure->rr_moved_dir == NULL) 1286 return 0; 1287 cd9660_time_915(diskStructure->rr_moved_dir->isoDirRecord->date, 1288 stampst.st_ino ? stampst.st_mtime : start_time.tv_sec); 1289 } 1290 1291 /* Create a file with the same ORIGINAL name */ 1292 tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent, 1293 dir); 1294 if (tfile == NULL) 1295 return NULL; 1296 1297 diskStructure->rock_ridge_move_count++; 1298 snprintf(newname, sizeof(newname), "%08i", 1299 diskStructure->rock_ridge_move_count); 1300 1301 /* Point to old parent */ 1302 dir->rr_real_parent = dir->parent; 1303 1304 /* Place the placeholder file */ 1305 if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) { 1306 TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile, 1307 cn_next_child); 1308 } else { 1309 cd9660_sorted_child_insert(dir->rr_real_parent, tfile); 1310 } 1311 1312 /* Point to new parent */ 1313 dir->parent = diskStructure->rr_moved_dir; 1314 1315 /* Point the file to the moved directory */ 1316 tfile->rr_relocated = dir; 1317 1318 /* Actually move the directory */ 1319 cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir); 1320 1321 /* TODO: Inherit permissions / ownership (basically the entire inode) */ 1322 1323 /* Set the new name */ 1324 memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); 1325 strncpy(dir->isoDirRecord->name, newname, 8); 1326 dir->isoDirRecord->length[0] = 34 + 8; 1327 dir->isoDirRecord->name_len[0] = 8; 1328 1329 return dir; 1330 } 1331 1332 static int 1333 cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root) 1334 { 1335 struct cd9660_children_head *head = &root->cn_children; 1336 cd9660node *cn; 1337 1338 TAILQ_FOREACH(cn, head, cn_next_child) { 1339 if ((cn->type & CD9660_TYPE_DIR) == 0) 1340 continue; 1341 /* Recursion first */ 1342 cd9660_add_dot_records(diskStructure, cn); 1343 } 1344 cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root); 1345 cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT, 1346 root); 1347 return 1; 1348 } 1349 1350 /* 1351 * Convert node to cd9660 structure 1352 * This function is designed to be called recursively on the root node of 1353 * the filesystem 1354 * Lots of recursion going on here, want to make sure it is efficient 1355 * @param struct fsnode * The root node to be converted 1356 * @param struct cd9660* The parent node (should not be NULL) 1357 * @param int Current directory depth 1358 * @param int* Running count of the number of directories that are being created 1359 */ 1360 static void 1361 cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root, 1362 cd9660node *parent_node, int level, int *numDirectories, int *error) 1363 { 1364 fsnode *iterator = root; 1365 cd9660node *this_node; 1366 int working_level; 1367 int add; 1368 int flag = 0; 1369 int counter = 0; 1370 1371 /* 1372 * Newer, more efficient method, reduces recursion depth 1373 */ 1374 if (root == NULL) { 1375 warnx("%s: root is null", __func__); 1376 return; 1377 } 1378 1379 /* Test for an empty directory - makefs still gives us the . record */ 1380 if ((S_ISDIR(root->type)) && (root->name[0] == '.') 1381 && (root->name[1] == '\0')) { 1382 root = root->next; 1383 if (root == NULL) 1384 return; 1385 } 1386 if ((this_node = cd9660_allocate_cd9660node()) == NULL) { 1387 CD9660_MEM_ALLOC_ERROR(__func__); 1388 } 1389 1390 /* 1391 * To reduce the number of recursive calls, we will iterate over 1392 * the next pointers to the right. 1393 */ 1394 while (iterator != NULL) { 1395 add = 1; 1396 /* 1397 * Increment the directory count if this is a directory 1398 * Ignore "." entries. We will generate them later 1399 */ 1400 if (!S_ISDIR(iterator->type) || 1401 strcmp(iterator->name, ".") != 0) { 1402 1403 /* Translate the node, including its filename */ 1404 this_node->parent = parent_node; 1405 cd9660_translate_node(diskStructure, iterator, 1406 this_node); 1407 this_node->level = level; 1408 1409 if (S_ISDIR(iterator->type)) { 1410 (*numDirectories)++; 1411 this_node->type = CD9660_TYPE_DIR; 1412 working_level = level + 1; 1413 1414 /* 1415 * If at level 8, directory would be at 8 1416 * and have children at 9 which is not 1417 * allowed as per ISO spec 1418 */ 1419 if (level == 8) { 1420 if ((!diskStructure->allow_deep_trees) && 1421 (!diskStructure->rock_ridge_enabled)) { 1422 warnx("error: found entry " 1423 "with depth greater " 1424 "than 8."); 1425 (*error) = 1; 1426 return; 1427 } else if (diskStructure-> 1428 rock_ridge_enabled) { 1429 working_level = 3; 1430 /* 1431 * Moved directory is actually 1432 * at level 2. 1433 */ 1434 this_node->level = 1435 working_level - 1; 1436 if (cd9660_rrip_move_directory( 1437 diskStructure, 1438 this_node) == 0) { 1439 warnx("Failure in " 1440 "cd9660_rrip_" 1441 "move_directory" 1442 ); 1443 (*error) = 1; 1444 return; 1445 } 1446 add = 0; 1447 } 1448 } 1449 1450 /* Do the recursive call on the children */ 1451 if (iterator->child != 0) { 1452 cd9660_convert_structure(diskStructure, 1453 iterator->child, this_node, 1454 working_level, 1455 numDirectories, error); 1456 1457 if ((*error) == 1) { 1458 warnx("%s: Error on recursive " 1459 "call", __func__); 1460 return; 1461 } 1462 } 1463 1464 } else { 1465 /* Only directories should have children */ 1466 assert(iterator->child == NULL); 1467 1468 this_node->type = CD9660_TYPE_FILE; 1469 } 1470 1471 /* 1472 * Finally, do a sorted insert 1473 */ 1474 if (add) { 1475 cd9660_sorted_child_insert( 1476 parent_node, this_node); 1477 } 1478 1479 /*Allocate new temp_node */ 1480 if (iterator->next != 0) { 1481 this_node = cd9660_allocate_cd9660node(); 1482 if (this_node == NULL) 1483 CD9660_MEM_ALLOC_ERROR(__func__); 1484 } 1485 } 1486 iterator = iterator->next; 1487 } 1488 1489 /* cd9660_handle_collisions(first_node); */ 1490 1491 /* TODO: need cleanup */ 1492 cd9660_copy_filenames(diskStructure, parent_node); 1493 1494 do { 1495 flag = cd9660_handle_collisions(diskStructure, parent_node, 1496 counter); 1497 counter++; 1498 cd9660_sorting_nodes(parent_node); 1499 } while ((flag == 1) && (counter < 100)); 1500 } 1501 1502 /* 1503 * Clean up the cd9660node tree 1504 * This is designed to be called recursively on the root node 1505 * @param struct cd9660node *root The node to free 1506 * @returns void 1507 */ 1508 static void 1509 cd9660_free_structure(cd9660node *root) 1510 { 1511 cd9660node *cn; 1512 1513 while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) { 1514 TAILQ_REMOVE(&root->cn_children, cn, cn_next_child); 1515 cd9660_free_structure(cn); 1516 } 1517 free(root); 1518 } 1519 1520 /* 1521 * Be a little more memory conservative: 1522 * instead of having the TAILQ_ENTRY as part of the cd9660node, 1523 * just create a temporary structure 1524 */ 1525 struct ptq_entry 1526 { 1527 TAILQ_ENTRY(ptq_entry) ptq; 1528 cd9660node *node; 1529 } *n; 1530 1531 #define PTQUEUE_NEW(n,s,r,t){\ 1532 n = emalloc(sizeof(struct s)); \ 1533 if (n == NULL) \ 1534 return r; \ 1535 n->node = t;\ 1536 } 1537 1538 /* 1539 * Generate the path tables 1540 * The specific implementation of this function is left as an exercise to the 1541 * programmer. It could be done recursively. Make sure you read how the path 1542 * table has to be laid out, it has levels. 1543 * @param struct iso9660_disk *disk The disk image 1544 * @returns int The number of built path tables (between 1 and 4), 0 on failure 1545 */ 1546 static int 1547 cd9660_generate_path_table(iso9660_disk *diskStructure) 1548 { 1549 cd9660node *cn, *dirNode = diskStructure->rootNode; 1550 cd9660node *last = dirNode; 1551 int pathTableSize = 0; /* computed as we go */ 1552 int counter = 1; /* root gets a count of 0 */ 1553 1554 TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head; 1555 TAILQ_INIT(&pt_head); 1556 1557 PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode); 1558 1559 /* Push the root node */ 1560 TAILQ_INSERT_HEAD(&pt_head, n, ptq); 1561 1562 /* Breadth-first traversal of file structure */ 1563 while (pt_head.tqh_first != 0) { 1564 n = pt_head.tqh_first; 1565 dirNode = n->node; 1566 TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq); 1567 free(n); 1568 1569 /* Update the size */ 1570 pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE 1571 + dirNode->isoDirRecord->name_len[0]+ 1572 (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1); 1573 /* includes the padding bit */ 1574 1575 dirNode->ptnumber=counter; 1576 if (dirNode != last) { 1577 last->ptnext = dirNode; 1578 dirNode->ptprev = last; 1579 } 1580 last = dirNode; 1581 1582 /* Push children onto queue */ 1583 TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) { 1584 /* 1585 * Dont add the DOT and DOTDOT types to the path 1586 * table. 1587 */ 1588 if ((cn->type != CD9660_TYPE_DOT) 1589 && (cn->type != CD9660_TYPE_DOTDOT)) { 1590 1591 if (S_ISDIR(cn->node->type)) { 1592 PTQUEUE_NEW(n, ptq_entry, -1, cn); 1593 TAILQ_INSERT_TAIL(&pt_head, n, ptq); 1594 } 1595 } 1596 } 1597 counter++; 1598 } 1599 return pathTableSize; 1600 } 1601 1602 void 1603 cd9660_compute_full_filename(cd9660node *node, char *buf) 1604 { 1605 int len; 1606 1607 len = CD9660MAXPATH + 1; 1608 len = snprintf(buf, len, "%s/%s/%s", node->node->root, 1609 node->node->path, node->node->name); 1610 if (len > CD9660MAXPATH) 1611 errx(EXIT_FAILURE, "Pathname too long."); 1612 } 1613 1614 /* NEW filename conversion method */ 1615 typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *, 1616 char *, int); 1617 1618 1619 /* 1620 * TODO: These two functions are almost identical. 1621 * Some code cleanup is possible here 1622 * 1623 * XXX bounds checking! 1624 */ 1625 static int 1626 cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname, 1627 char *newname, int is_file) 1628 { 1629 /* 1630 * ISO 9660 : 10.1 1631 * File Name shall not contain more than 8 d or d1 characters 1632 * File Name Extension shall not contain more than 3 d or d1 characters 1633 * Directory Identifier shall not contain more than 8 d or d1 characters 1634 */ 1635 int namelen = 0; 1636 int extlen = 0; 1637 int found_ext = 0; 1638 1639 while (*oldname != '\0' && extlen < 3) { 1640 /* Handle period first, as it is special */ 1641 if (*oldname == '.') { 1642 if (found_ext) { 1643 *newname++ = '_'; 1644 extlen ++; 1645 } 1646 else { 1647 *newname++ = '.'; 1648 found_ext = 1; 1649 } 1650 } else { 1651 /* cut RISC OS file type off ISO name */ 1652 if (diskStructure->archimedes_enabled && 1653 *oldname == ',' && strlen(oldname) == 4) 1654 break; 1655 1656 /* Enforce 12.3 / 8 */ 1657 if (namelen == 8 && !found_ext) 1658 break; 1659 1660 if (islower((unsigned char)*oldname)) 1661 *newname++ = toupper((unsigned char)*oldname); 1662 else if (isupper((unsigned char)*oldname) 1663 || isdigit((unsigned char)*oldname)) 1664 *newname++ = *oldname; 1665 else 1666 *newname++ = '_'; 1667 1668 if (found_ext) 1669 extlen++; 1670 else 1671 namelen++; 1672 } 1673 oldname++; 1674 } 1675 if (is_file) { 1676 if (!found_ext && !diskStructure->omit_trailing_period) 1677 *newname++ = '.'; 1678 /* Add version */ 1679 sprintf(newname, ";%i", 1); 1680 } 1681 return namelen + extlen + found_ext; 1682 } 1683 1684 /* XXX bounds checking! */ 1685 static int 1686 cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname, 1687 char *newname, int is_file) 1688 { 1689 /* 1690 * ISO 9660 : 7.5.1 1691 * File name : 0+ d or d1 characters 1692 * separator 1 (.) 1693 * File name extension : 0+ d or d1 characters 1694 * separator 2 (;) 1695 * File version number (5 characters, 1-32767) 1696 * 1 <= Sum of File name and File name extension <= 30 1697 */ 1698 int namelen = 0; 1699 int extlen = 0; 1700 int found_ext = 0; 1701 1702 while (*oldname != '\0' && namelen + extlen < 30) { 1703 /* Handle period first, as it is special */ 1704 if (*oldname == '.') { 1705 if (found_ext) { 1706 if (diskStructure->allow_multidot) { 1707 *newname++ = '.'; 1708 } else { 1709 *newname++ = '_'; 1710 } 1711 extlen ++; 1712 } 1713 else { 1714 *newname++ = '.'; 1715 found_ext = 1; 1716 } 1717 } else { 1718 /* cut RISC OS file type off ISO name */ 1719 if (diskStructure->archimedes_enabled && 1720 *oldname == ',' && strlen(oldname) == 4) 1721 break; 1722 1723 if (islower((unsigned char)*oldname)) 1724 *newname++ = toupper((unsigned char)*oldname); 1725 else if (isupper((unsigned char)*oldname) || 1726 isdigit((unsigned char)*oldname)) 1727 *newname++ = *oldname; 1728 else if (diskStructure->allow_multidot && 1729 *oldname == '.') { 1730 *newname++ = '.'; 1731 } else { 1732 *newname++ = '_'; 1733 } 1734 1735 if (found_ext) 1736 extlen++; 1737 else 1738 namelen++; 1739 } 1740 oldname ++; 1741 } 1742 if (is_file) { 1743 if (!found_ext && !diskStructure->omit_trailing_period) 1744 *newname++ = '.'; 1745 /* Add version */ 1746 sprintf(newname, ";%i", 1); 1747 } 1748 return namelen + extlen + found_ext; 1749 } 1750 1751 #if 0 1752 static int 1753 cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname, 1754 char *newname, int is_file) 1755 { 1756 /* TODO: implement later, move to cd9660_joliet.c ?? */ 1757 } 1758 #endif 1759 1760 1761 /* 1762 * Convert a file name to ISO compliant file name 1763 * @param char * oldname The original filename 1764 * @param char ** newname The new file name, in the appropriate character 1765 * set and of appropriate length 1766 * @param int 1 if file, 0 if directory 1767 * @returns int The length of the new string 1768 */ 1769 static int 1770 cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname, 1771 char *newname, int is_file) 1772 { 1773 /* NEW */ 1774 cd9660_filename_conversion_functor conversion_function = 0; 1775 if (diskStructure->isoLevel == 1) 1776 conversion_function = &cd9660_level1_convert_filename; 1777 else if (diskStructure->isoLevel == 2) 1778 conversion_function = &cd9660_level2_convert_filename; 1779 return (*conversion_function)(diskStructure, oldname, newname, is_file); 1780 } 1781 1782 int 1783 cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node) 1784 { 1785 int size = node->isoDirRecord->length[0]; 1786 1787 if (diskStructure->rock_ridge_enabled) 1788 size += node->susp_entry_size; 1789 size += node->su_tail_size; 1790 size += size & 1; /* Ensure length of record is even. */ 1791 assert(size <= 254); 1792 return size; 1793 } 1794 1795 static void 1796 cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node) 1797 { 1798 node->dot_record->fileDataSector = node->fileDataSector; 1799 memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34); 1800 node->dot_record->isoDirRecord->name_len[0] = 1; 1801 node->dot_record->isoDirRecord->name[0] = 0; 1802 node->dot_record->isoDirRecord->name[1] = 0; 1803 node->dot_record->isoDirRecord->length[0] = 34; 1804 node->dot_record->fileRecordSize = 1805 cd9660_compute_record_size(diskStructure, node->dot_record); 1806 1807 if (node == diskStructure->rootNode) { 1808 node->dot_dot_record->fileDataSector = node->fileDataSector; 1809 memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord, 1810 34); 1811 } else { 1812 node->dot_dot_record->fileDataSector = 1813 node->parent->fileDataSector; 1814 memcpy(node->dot_dot_record->isoDirRecord, 1815 node->parent->isoDirRecord,34); 1816 } 1817 node->dot_dot_record->isoDirRecord->name_len[0] = 1; 1818 node->dot_dot_record->isoDirRecord->name[0] = 1; 1819 node->dot_dot_record->isoDirRecord->name[1] = 0; 1820 node->dot_dot_record->isoDirRecord->length[0] = 34; 1821 node->dot_dot_record->fileRecordSize = 1822 cd9660_compute_record_size(diskStructure, node->dot_dot_record); 1823 } 1824 1825 /* 1826 * @param struct cd9660node *node The node 1827 * @param int The offset (in bytes) - SHOULD align to the beginning of a sector 1828 * @returns int The total size of files and directory entries (should be 1829 * a multiple of sector size) 1830 */ 1831 static int64_t 1832 cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node, 1833 int64_t startOffset) 1834 { 1835 /* 1836 * This function needs to compute the size of directory records and 1837 * runs, file lengths, and set the appropriate variables both in 1838 * cd9660node and isoDirEntry 1839 */ 1840 int64_t used_bytes = 0; 1841 int64_t current_sector_usage = 0; 1842 cd9660node *child; 1843 fsinode *inode; 1844 int64_t r; 1845 1846 assert(node != NULL); 1847 1848 1849 /* 1850 * NOTE : There needs to be some special case detection for 1851 * the "real root" node, since for it, node->node is undefined 1852 */ 1853 1854 node->fileDataSector = -1; 1855 1856 if (node->type & CD9660_TYPE_DIR) { 1857 node->fileRecordSize = cd9660_compute_record_size( 1858 diskStructure, node); 1859 /*Set what sector this directory starts in*/ 1860 node->fileDataSector = 1861 CD9660_BLOCKS(diskStructure->sectorSize,startOffset); 1862 1863 cd9660_bothendian_dword(node->fileDataSector, 1864 node->isoDirRecord->extent); 1865 1866 /* 1867 * First loop over children, need to know the size of 1868 * their directory records 1869 */ 1870 node->fileSectorsUsed = 1; 1871 TAILQ_FOREACH(child, &node->cn_children, cn_next_child) { 1872 node->fileDataLength += 1873 cd9660_compute_record_size(diskStructure, child); 1874 if ((cd9660_compute_record_size(diskStructure, child) + 1875 current_sector_usage) >= 1876 diskStructure->sectorSize) { 1877 current_sector_usage = 0; 1878 node->fileSectorsUsed++; 1879 } 1880 1881 current_sector_usage += 1882 cd9660_compute_record_size(diskStructure, child); 1883 } 1884 1885 cd9660_bothendian_dword(node->fileSectorsUsed * 1886 diskStructure->sectorSize,node->isoDirRecord->size); 1887 1888 /* 1889 * This should point to the sector after the directory 1890 * record (or, the first byte in that sector) 1891 */ 1892 used_bytes += node->fileSectorsUsed * diskStructure->sectorSize; 1893 1894 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); 1895 child != NULL; child = TAILQ_NEXT(child, cn_next_child)) { 1896 /* Directories need recursive call */ 1897 if (S_ISDIR(child->node->type)) { 1898 r = cd9660_compute_offsets(diskStructure, child, 1899 used_bytes + startOffset); 1900 1901 if (r != -1) 1902 used_bytes += r; 1903 else 1904 return -1; 1905 } 1906 } 1907 1908 /* Explicitly set the . and .. records */ 1909 cd9660_populate_dot_records(diskStructure, node); 1910 1911 /* Finally, do another iteration to write the file data*/ 1912 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); 1913 child != NULL; 1914 child = TAILQ_NEXT(child, cn_next_child)) { 1915 /* Files need extent set */ 1916 if (S_ISDIR(child->node->type)) 1917 continue; 1918 child->fileRecordSize = 1919 cd9660_compute_record_size(diskStructure, child); 1920 1921 child->fileSectorsUsed = 1922 CD9660_BLOCKS(diskStructure->sectorSize, 1923 child->fileDataLength); 1924 1925 inode = child->node->inode; 1926 if ((inode->flags & FI_ALLOCATED) == 0) { 1927 inode->ino = 1928 CD9660_BLOCKS(diskStructure->sectorSize, 1929 used_bytes + startOffset); 1930 inode->flags |= FI_ALLOCATED; 1931 used_bytes += child->fileSectorsUsed * 1932 diskStructure->sectorSize; 1933 } else { 1934 INODE_WARNX(("%s: already allocated inode %d " 1935 "data sectors at %" PRIu32, __func__, 1936 (int)inode->st.st_ino, inode->ino)); 1937 } 1938 child->fileDataSector = inode->ino; 1939 cd9660_bothendian_dword(child->fileDataSector, 1940 child->isoDirRecord->extent); 1941 } 1942 } 1943 1944 return used_bytes; 1945 } 1946 1947 #if 0 1948 /* Might get rid of this func */ 1949 static int 1950 cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file) 1951 { 1952 to->node->inode->st.st_dev = 0; 1953 to->node->inode->st.st_ino = 0; 1954 to->node->inode->st.st_size = 0; 1955 to->node->inode->st.st_blksize = from->node->inode->st.st_blksize; 1956 to->node->inode->st.st_atime = from->node->inode->st.st_atime; 1957 to->node->inode->st.st_mtime = from->node->inode->st.st_mtime; 1958 to->node->inode->st.st_ctime = from->node->inode->st.st_ctime; 1959 to->node->inode->st.st_uid = from->node->inode->st.st_uid; 1960 to->node->inode->st.st_gid = from->node->inode->st.st_gid; 1961 to->node->inode->st.st_mode = from->node->inode->st.st_mode; 1962 /* Clear out type */ 1963 to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT); 1964 if (file) 1965 to->node->inode->st.st_mode |= S_IFREG; 1966 else 1967 to->node->inode->st.st_mode |= S_IFDIR; 1968 return 1; 1969 } 1970 #endif 1971 1972 static cd9660node * 1973 cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name, 1974 cd9660node *parent, int file, int insert) 1975 { 1976 cd9660node *temp; 1977 fsnode * tfsnode; 1978 1979 assert(parent != NULL); 1980 1981 temp = cd9660_allocate_cd9660node(); 1982 if (temp == NULL) 1983 return NULL; 1984 1985 tfsnode = emalloc(sizeof(*tfsnode)); 1986 tfsnode->name = estrdup(name); 1987 temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord)); 1988 1989 cd9660_convert_filename(diskStructure, tfsnode->name, 1990 temp->isoDirRecord->name, file); 1991 1992 temp->node = tfsnode; 1993 temp->parent = parent; 1994 1995 if (insert) { 1996 if (temp->parent != NULL) { 1997 temp->level = temp->parent->level + 1; 1998 if (!TAILQ_EMPTY(&temp->parent->cn_children)) 1999 cd9660_sorted_child_insert(temp->parent, temp); 2000 else 2001 TAILQ_INSERT_HEAD(&temp->parent->cn_children, 2002 temp, cn_next_child); 2003 } 2004 } 2005 2006 if (parent->node != NULL) { 2007 tfsnode->type = parent->node->type; 2008 } 2009 2010 /* Clear out file type bits */ 2011 tfsnode->type &= ~(S_IFMT); 2012 if (file) 2013 tfsnode->type |= S_IFREG; 2014 else 2015 tfsnode->type |= S_IFDIR; 2016 2017 /* Indicate that there is no spec entry (inode) */ 2018 tfsnode->flags &= ~(FSNODE_F_HASSPEC); 2019 #if 0 2020 cd9660_copy_stat_info(parent, temp, file); 2021 #endif 2022 return temp; 2023 } 2024 2025 static cd9660node * 2026 cd9660_create_file(iso9660_disk *diskStructure, const char *name, 2027 cd9660node *parent, cd9660node *me) 2028 { 2029 cd9660node *temp; 2030 2031 temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1); 2032 if (temp == NULL) 2033 return NULL; 2034 2035 temp->fileDataLength = 0; 2036 2037 temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL; 2038 2039 temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); 2040 *temp->node->inode = *me->node->inode; 2041 2042 if (cd9660_translate_node_common(diskStructure, temp) == 0) 2043 return NULL; 2044 return temp; 2045 } 2046 2047 /* 2048 * Create a new directory which does not exist on disk 2049 * @param const char * name The name to assign to the directory 2050 * @param const char * parent Pointer to the parent directory 2051 * @returns cd9660node * Pointer to the new directory 2052 */ 2053 static cd9660node * 2054 cd9660_create_directory(iso9660_disk *diskStructure, const char *name, 2055 cd9660node *parent, cd9660node *me) 2056 { 2057 cd9660node *temp; 2058 2059 temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1); 2060 if (temp == NULL) 2061 return NULL; 2062 temp->node->type |= S_IFDIR; 2063 2064 temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL; 2065 2066 temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); 2067 *temp->node->inode = *me->node->inode; 2068 2069 if (cd9660_translate_node_common(diskStructure, temp) == 0) 2070 return NULL; 2071 return temp; 2072 } 2073 2074 static cd9660node * 2075 cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type, 2076 cd9660node *parent) 2077 { 2078 cd9660node *temp, *first; 2079 char na[2]; 2080 2081 assert(parent != NULL); 2082 2083 if (type == CD9660_TYPE_DOT) 2084 na[0] = 0; 2085 else if (type == CD9660_TYPE_DOTDOT) 2086 na[0] = 1; 2087 else 2088 return 0; 2089 2090 na[1] = 0; 2091 if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent, 2092 0, 0)) == NULL) 2093 return NULL; 2094 2095 temp->parent = parent; 2096 temp->type = type; 2097 temp->isoDirRecord->length[0] = 34; 2098 /* Dot record is always first */ 2099 if (type == CD9660_TYPE_DOT) { 2100 parent->dot_record = temp; 2101 TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child); 2102 /* DotDot should be second */ 2103 } else if (type == CD9660_TYPE_DOTDOT) { 2104 parent->dot_dot_record = temp; 2105 /* 2106 * If the first child is the dot record, insert 2107 * this second. Otherwise, insert it at the head. 2108 */ 2109 if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL || 2110 (first->type & CD9660_TYPE_DOT) == 0) { 2111 TAILQ_INSERT_HEAD(&parent->cn_children, temp, 2112 cn_next_child); 2113 } else { 2114 TAILQ_INSERT_AFTER(&parent->cn_children, first, temp, 2115 cn_next_child); 2116 } 2117 } 2118 2119 return temp; 2120 } 2121 2122 static int 2123 cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage) 2124 { 2125 struct stat stbuf; 2126 2127 assert(bootimage != NULL); 2128 2129 if (*bootimage == '\0') { 2130 warnx("Error: Boot image must be a filename"); 2131 return 0; 2132 } 2133 2134 diskStructure->generic_bootimage = estrdup(bootimage); 2135 2136 /* Get information about the file */ 2137 if (lstat(diskStructure->generic_bootimage, &stbuf) == -1) 2138 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__, 2139 diskStructure->generic_bootimage); 2140 2141 if (stbuf.st_size > 32768) { 2142 warnx("Error: Boot image must be no greater than 32768 bytes"); 2143 return 0; 2144 } 2145 2146 if (diskStructure->verbose_level > 0) { 2147 printf("Generic boot image image has size %lld\n", 2148 (long long)stbuf.st_size); 2149 } 2150 2151 diskStructure->has_generic_bootimage = 1; 2152 2153 return 1; 2154 } 2155