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