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