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