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