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