1 /* $NetBSD: cd9660.c,v 1.31 2011/08/06 23:25:19 christos Exp $ */
4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 * Perez-Rathke and Ram Vedam. All rights reserved.
7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 * Alan Perez-Rathke and Ram Vedam.
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * Copyright (c) 2001 Wasabi Systems, Inc.
36 * All rights reserved.
38 * Written by Luke Mewburn for Wasabi Systems, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed for the NetBSD Project by
51 * Wasabi Systems, Inc.
52 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
53 * or promote products derived from this software without specific prior
56 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
69 * Copyright (c) 1982, 1986, 1989, 1993
70 * The Regents of the University of California. All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the University nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 #include <sys/cdefs.h>
99 __FBSDID("$FreeBSD$");
103 #include <sys/param.h>
104 #include <sys/queue.h>
108 #include "cd9660/iso9660_rrip.h"
109 #include "cd9660/cd9660_archimedes.h"
114 iso9660_disk diskStructure;
116 static void cd9660_finalize_PVD(void);
117 static cd9660node *cd9660_allocate_cd9660node(void);
118 static void cd9660_set_defaults(void);
119 static int cd9660_arguments_set_string(const char *, const char *, int,
121 static void cd9660_populate_iso_dir_record(
122 struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
124 static void cd9660_setup_root_node(void);
125 static int cd9660_setup_volume_descriptors(void);
127 static int cd9660_fill_extended_attribute_record(cd9660node *);
129 static void cd9660_sort_nodes(cd9660node *);
130 static int cd9660_translate_node_common(cd9660node *);
131 static int cd9660_translate_node(fsnode *, cd9660node *);
132 static int cd9660_compare_filename(const char *, const char *);
133 static void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
134 static int cd9660_handle_collisions(cd9660node *, int);
135 static cd9660node *cd9660_rename_filename(cd9660node *, int, int);
136 static void cd9660_copy_filenames(cd9660node *);
137 static void cd9660_sorting_nodes(cd9660node *);
138 static int cd9660_count_collisions(cd9660node *);
139 static cd9660node *cd9660_rrip_move_directory(cd9660node *);
140 static int cd9660_add_dot_records(cd9660node *);
142 static void cd9660_convert_structure(fsnode *, cd9660node *, int,
144 static void cd9660_free_structure(cd9660node *);
145 static int cd9660_generate_path_table(void);
146 static int cd9660_level1_convert_filename(const char *, char *, int);
147 static int cd9660_level2_convert_filename(const char *, char *, int);
149 static int cd9660_joliet_convert_filename(const char *, char *, int);
151 static int cd9660_convert_filename(const char *, char *, int);
152 static void cd9660_populate_dot_records(cd9660node *);
153 static int64_t cd9660_compute_offsets(cd9660node *, int64_t);
155 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
157 static cd9660node *cd9660_create_virtual_entry(const char *, cd9660node *, int,
159 static cd9660node *cd9660_create_file(const char *, cd9660node *, cd9660node *);
160 static cd9660node *cd9660_create_directory(const char *, cd9660node *,
162 static cd9660node *cd9660_create_special_directory(u_char, cd9660node *);
166 * Allocate and initalize a cd9660node
167 * @returns struct cd9660node * Pointer to new node, or NULL on error
170 cd9660_allocate_cd9660node(void)
174 if ((temp = calloc(1, sizeof(cd9660node))) == NULL)
175 err(EXIT_FAILURE, "%s: calloc", __func__);
176 TAILQ_INIT(&temp->cn_children);
177 temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
178 temp->ptnext = temp->ptprev = temp->ptlast = NULL;
180 temp->isoDirRecord = NULL;
181 temp->isoExtAttributes = NULL;
182 temp->rr_real_parent = temp->rr_relocated = NULL;
183 temp->su_tail_data = NULL;
187 int cd9660_defaults_set = 0;
190 * Set default values for cd9660 extension to makefs
193 cd9660_set_defaults(void)
195 /*Fix the sector size for now, though the spec allows for other sizes*/
196 diskStructure.sectorSize = 2048;
198 /* Set up defaults in our own structure */
199 diskStructure.verbose_level = 0;
200 diskStructure.keep_bad_images = 0;
201 diskStructure.follow_sym_links = 0;
202 diskStructure.isoLevel = 2;
204 diskStructure.rock_ridge_enabled = 0;
205 diskStructure.rock_ridge_renamed_dir_name = 0;
206 diskStructure.rock_ridge_move_count = 0;
207 diskStructure.rr_moved_dir = 0;
209 diskStructure.archimedes_enabled = 0;
210 diskStructure.chrp_boot = 0;
212 diskStructure.include_padding_areas = 1;
214 /* Spec breaking functionality */
215 diskStructure.allow_deep_trees =
216 diskStructure.allow_start_dot =
217 diskStructure.allow_max_name =
218 diskStructure.allow_illegal_chars =
219 diskStructure.allow_lowercase =
220 diskStructure.allow_multidot =
221 diskStructure.omit_trailing_period = 0;
223 /* Make sure the PVD is clear */
224 memset(&diskStructure.primaryDescriptor, 0, 2048);
226 memset(diskStructure.primaryDescriptor.publisher_id, 0x20,128);
227 memset(diskStructure.primaryDescriptor.preparer_id, 0x20,128);
228 memset(diskStructure.primaryDescriptor.application_id, 0x20,128);
229 memset(diskStructure.primaryDescriptor.copyright_file_id, 0x20,37);
230 memset(diskStructure.primaryDescriptor.abstract_file_id, 0x20,37);
231 memset(diskStructure.primaryDescriptor.bibliographic_file_id, 0x20,37);
233 strcpy(diskStructure.primaryDescriptor.system_id,"NetBSD");
235 cd9660_defaults_set = 1;
237 /* Boot support: Initially disabled */
238 diskStructure.has_generic_bootimage = 0;
239 diskStructure.generic_bootimage = NULL;
241 diskStructure.boot_image_directory = 0;
242 /*memset(diskStructure.boot_descriptor, 0, 2048);*/
244 diskStructure.is_bootable = 0;
245 TAILQ_INIT(&diskStructure.boot_images);
246 LIST_INIT(&diskStructure.boot_entries);
250 cd9660_prep_opts(fsinfo_t *fsopts __unused)
252 cd9660_set_defaults();
256 cd9660_cleanup_opts(fsinfo_t *fsopts __unused)
262 cd9660_arguments_set_string(const char *val, const char *fieldtitle, int length,
263 char testmode, char * dest)
268 warnx("error: The %s requires a string argument", fieldtitle);
269 else if ((len = strlen(val)) <= length) {
271 test = cd9660_valid_d_chars(val);
273 test = cd9660_valid_a_chars(val);
275 memcpy(dest, val, len);
277 cd9660_uppercase_characters(dest, len);
280 warnx("error: The %s must be composed of "
281 "%c-characters", fieldtitle, testmode);
283 warnx("error: The %s must be at most 32 characters long",
289 * Command-line parsing function
293 cd9660_parse_opts(const char *option, fsinfo_t *fsopts)
297 /* Set up allowed options - integer options ONLY */
298 option_t cd9660_options[] = {
299 { "l", &diskStructure.isoLevel, 1, 3, "ISO Level" },
300 { "isolevel", &diskStructure.isoLevel, 1, 3, "ISO Level" },
301 { "verbose", &diskStructure.verbose_level, 0, 2,
302 "Turns on verbose output" },
303 { "v", &diskStructure.verbose_level, 0 , 2,
304 "Turns on verbose output"},
308 if (cd9660_defaults_set == 0)
309 cd9660_set_defaults();
312 * Todo : finish implementing this, and make a function that
316 string_option_t cd9660_string_options[] = {
317 { "L", "Label", &diskStructure.primaryDescriptor.volume_id, 1, 32, "Disk Label", ISO_STRING_FILTER_DCHARS },
322 assert(option != NULL);
324 if (debug & DEBUG_FS_PARSE_OPTS)
325 printf("cd9660_parse_opts: got `%s'\n", option);
327 if ((var = strdup(option)) == NULL)
328 err(1, "allocating memory for copy of option string");
331 val = strchr(var, '=');
335 /* First handle options with no parameters */
336 if (strcmp(var, "h") == 0) {
337 diskStructure.displayHelp = 1;
339 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "S", "follow-symlinks")) {
340 /* this is not handled yet */
341 diskStructure.follow_sym_links = 1;
343 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "L", "label")) {
344 rv = cd9660_arguments_set_string(val, "Disk Label", 32, 'd',
345 diskStructure.primaryDescriptor.volume_id);
346 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "A", "applicationid")) {
347 rv = cd9660_arguments_set_string(val, "Application Identifier", 128, 'a',
348 diskStructure.primaryDescriptor.application_id);
349 } else if(CD9660_IS_COMMAND_ARG_DUAL(var, "P", "publisher")) {
350 rv = cd9660_arguments_set_string(val, "Publisher Identifier",
351 128, 'a', diskStructure.primaryDescriptor.publisher_id);
352 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "p", "preparer")) {
353 rv = cd9660_arguments_set_string(val, "Preparer Identifier",
354 128, 'a', diskStructure.primaryDescriptor.preparer_id);
355 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "V", "volumeid")) {
356 rv = cd9660_arguments_set_string(val, "Volume Set Identifier",
357 128, 'a', diskStructure.primaryDescriptor.volume_set_id);
359 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "B", "bootimage")) {
361 warnx("error: The Boot Image parameter requires a valid boot information string");
363 rv = cd9660_add_boot_disk(val);
364 } else if (CD9660_IS_COMMAND_ARG(var, "bootimagedir")) {
366 * XXXfvdl this is unused.
369 errx(1, "error: The Boot Image Directory parameter"
370 " requires a directory name\n");
372 if ((diskStructure.boot_image_directory =
373 malloc(strlen(val) + 1)) == NULL) {
374 CD9660_MEM_ALLOC_ERROR("cd9660_parse_opts");
378 /* BIG TODO: Add the max length function here */
379 cd9660_arguments_set_string(val, "Boot Image Directory",
380 12 , 'd', diskStructure.boot_image_directory);
382 } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "G", "generic-bootimage")) {
384 warnx("error: The Boot Image parameter requires a valid boot information string");
386 rv = cd9660_add_generic_bootimage(val);
387 } else if (CD9660_IS_COMMAND_ARG(var, "no-trailing-padding"))
388 diskStructure.include_padding_areas = 0;
390 else if (CD9660_IS_COMMAND_ARG_DUAL(var, "R", "rockridge"))
391 diskStructure.rock_ridge_enabled = 1;
392 else if (CD9660_IS_COMMAND_ARG_DUAL(var, "A", "archimedes"))
393 diskStructure.archimedes_enabled = 1;
394 else if (CD9660_IS_COMMAND_ARG(var, "chrp-boot"))
395 diskStructure.chrp_boot = 1;
396 else if (CD9660_IS_COMMAND_ARG_DUAL(var, "K", "keep-bad-images"))
397 diskStructure.keep_bad_images = 1;
398 else if (CD9660_IS_COMMAND_ARG(var, "allow-deep-trees"))
399 diskStructure.allow_deep_trees = 1;
400 else if (CD9660_IS_COMMAND_ARG(var, "allow-max-name"))
401 diskStructure.allow_max_name = 1;
402 else if (CD9660_IS_COMMAND_ARG(var, "allow-illegal-chars"))
403 diskStructure.allow_illegal_chars = 1;
404 else if (CD9660_IS_COMMAND_ARG(var, "allow-lowercase"))
405 diskStructure.allow_lowercase = 1;
406 else if (CD9660_IS_COMMAND_ARG(var,"allow-multidot"))
407 diskStructure.allow_multidot = 1;
408 else if (CD9660_IS_COMMAND_ARG(var, "omit-trailing-period"))
409 diskStructure.omit_trailing_period = 1;
410 else if (CD9660_IS_COMMAND_ARG(var, "no-emul-boot") ||
411 CD9660_IS_COMMAND_ARG(var, "no-boot") ||
412 CD9660_IS_COMMAND_ARG(var, "hard-disk-boot")) {
413 cd9660_eltorito_add_boot_option(var, 0);
415 /* End of flag variables */
416 } else if (CD9660_IS_COMMAND_ARG(var, "boot-load-segment")) {
418 warnx("Option `%s' doesn't contain a value", var);
421 cd9660_eltorito_add_boot_option(var, val);
425 warnx("Option `%s' doesn't contain a value", var);
428 rv = set_option(cd9660_options, var, val);
437 * Main function for cd9660_makefs
438 * Builds the ISO image file
439 * @param const char *image The image filename to create
440 * @param const char *dir The directory that is being read
441 * @param struct fsnode *root The root node of the filesystem tree
442 * @param struct fsinfo_t *fsopts Any options
445 cd9660_makefs(const char *image, const char *dir, fsnode *root,
450 uint64_t pathTableSectors;
451 int64_t firstAvailableSector;
454 cd9660node *real_root;
456 if (diskStructure.verbose_level > 0)
457 printf("cd9660_makefs: ISO level is %i\n",
458 diskStructure.isoLevel);
459 if (diskStructure.isoLevel < 2 &&
460 diskStructure.allow_multidot)
461 errx(1, "allow-multidot requires iso level of 2\n");
463 assert(image != NULL);
465 assert(root != NULL);
467 if (diskStructure.displayHelp) {
469 * Display help here - probably want to put it in
470 * a separate function
475 diskStructure.rootFilesystemPath = dir;
477 if (diskStructure.verbose_level > 0)
478 printf("cd9660_makefs: image %s directory %s root %p\n",
481 /* Set up some constants. Later, these will be defined with options */
483 /* Counter needed for path tables */
486 /* Convert tree to our own format */
487 /* Actually, we now need to add the REAL root node, at level 0 */
489 real_root = cd9660_allocate_cd9660node();
490 if ((real_root->isoDirRecord =
491 malloc( sizeof(iso_directory_record_cd9660) )) == NULL) {
492 CD9660_MEM_ALLOC_ERROR("cd9660_makefs");
496 /* Leave filename blank for root */
497 memset(real_root->isoDirRecord->name, 0,
498 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
500 real_root->level = 0;
501 diskStructure.rootNode = real_root;
502 real_root->type = CD9660_TYPE_DIR;
504 real_root->node = root;
505 cd9660_convert_structure(root, real_root, 1, &numDirectories, &error);
507 if (TAILQ_EMPTY(&real_root->cn_children)) {
508 errx(1, "cd9660_makefs: converted directory is empty. "
509 "Tree conversion failed\n");
510 } else if (error != 0) {
511 errx(1, "cd9660_makefs: tree conversion failed\n");
513 if (diskStructure.verbose_level > 0)
514 printf("cd9660_makefs: tree converted\n");
517 /* Add the dot and dot dot records */
518 cd9660_add_dot_records(real_root);
520 cd9660_setup_root_node();
522 if (diskStructure.verbose_level > 0)
523 printf("cd9660_makefs: done converting tree\n");
525 /* non-SUSP extensions */
526 if (diskStructure.archimedes_enabled)
527 archimedes_convert_tree(diskStructure.rootNode);
529 /* Rock ridge / SUSP init pass */
530 if (diskStructure.rock_ridge_enabled) {
531 cd9660_susp_initialize(diskStructure.rootNode,
532 diskStructure.rootNode, NULL);
535 /* Build path table structure */
536 diskStructure.pathTableLength = cd9660_generate_path_table();
538 pathTableSectors = CD9660_BLOCKS(diskStructure.sectorSize,
539 diskStructure.pathTableLength);
541 firstAvailableSector = cd9660_setup_volume_descriptors();
542 if (diskStructure.is_bootable) {
543 firstAvailableSector = cd9660_setup_boot(firstAvailableSector);
544 if (firstAvailableSector < 0)
545 errx(1, "setup_boot failed");
547 /* LE first, then BE */
548 diskStructure.primaryLittleEndianTableSector = firstAvailableSector;
549 diskStructure.primaryBigEndianTableSector =
550 diskStructure.primaryLittleEndianTableSector + pathTableSectors;
552 /* Set the secondary ones to -1, not going to use them for now */
553 diskStructure.secondaryBigEndianTableSector = -1;
554 diskStructure.secondaryLittleEndianTableSector = -1;
556 diskStructure.dataFirstSector =
557 diskStructure.primaryBigEndianTableSector + pathTableSectors;
558 if (diskStructure.verbose_level > 0)
559 printf("cd9660_makefs: Path table conversion complete. "
560 "Each table is %i bytes, or %" PRIu64 " sectors.\n",
561 diskStructure.pathTableLength, pathTableSectors);
563 startoffset = diskStructure.sectorSize*diskStructure.dataFirstSector;
565 totalSpace = cd9660_compute_offsets(real_root, startoffset);
567 diskStructure.totalSectors = diskStructure.dataFirstSector +
568 CD9660_BLOCKS(diskStructure.sectorSize, totalSpace);
570 /* Disabled until pass 1 is done */
571 if (diskStructure.rock_ridge_enabled) {
572 diskStructure.susp_continuation_area_start_sector =
573 diskStructure.totalSectors;
574 diskStructure.totalSectors +=
575 CD9660_BLOCKS(diskStructure.sectorSize,
576 diskStructure.susp_continuation_area_size);
577 cd9660_susp_finalize(diskStructure.rootNode);
581 cd9660_finalize_PVD();
583 /* Add padding sectors, just for testing purposes right now */
584 /* diskStructure.totalSectors+=150; */
586 /* Debugging output */
587 if (diskStructure.verbose_level > 0) {
588 printf("cd9660_makefs: Sectors 0-15 reserved\n");
589 printf("cd9660_makefs: Primary path tables starts in sector %"
590 PRId64 "\n", diskStructure.primaryLittleEndianTableSector);
591 printf("cd9660_makefs: File data starts in sector %"
592 PRId64 "\n", diskStructure.dataFirstSector);
593 printf("cd9660_makefs: Total sectors: %"
594 PRId64 "\n", diskStructure.totalSectors);
598 * Add padding sectors at the end
599 * TODO: Clean this up and separate padding
601 if (diskStructure.include_padding_areas)
602 diskStructure.totalSectors += 150;
604 cd9660_write_image(image);
606 if (diskStructure.verbose_level > 1) {
607 debug_print_volume_descriptor_information();
608 debug_print_tree(real_root,0);
609 debug_print_path_tree(real_root);
612 /* Clean up data structures */
613 cd9660_free_structure(real_root);
615 if (diskStructure.verbose_level > 0)
616 printf("cd9660_makefs: done\n");
619 /* Generic function pointer - implement later */
620 typedef int (*cd9660node_func)(cd9660node *);
623 cd9660_finalize_PVD(void)
628 /* Copy the root directory record */
629 temp = (unsigned char *) &diskStructure.primaryDescriptor;
631 /* root should be a fixed size of 34 bytes since it has no name */
632 memcpy(diskStructure.primaryDescriptor.root_directory_record,
633 diskStructure.rootNode->dot_record->isoDirRecord, 34);
635 /* In RRIP, this might be longer than 34 */
636 diskStructure.primaryDescriptor.root_directory_record[0] = 34;
638 /* Set up all the important numbers in the PVD */
639 cd9660_bothendian_dword(diskStructure.totalSectors,
640 (unsigned char *)diskStructure.primaryDescriptor.volume_space_size);
641 cd9660_bothendian_word(1,
642 (unsigned char *)diskStructure.primaryDescriptor.volume_set_size);
643 cd9660_bothendian_word(1,
645 diskStructure.primaryDescriptor.volume_sequence_number);
646 cd9660_bothendian_word(diskStructure.sectorSize,
648 diskStructure.primaryDescriptor.logical_block_size);
649 cd9660_bothendian_dword(diskStructure.pathTableLength,
650 (unsigned char *)diskStructure.primaryDescriptor.path_table_size);
652 cd9660_731(diskStructure.primaryLittleEndianTableSector,
653 (u_char *)diskStructure.primaryDescriptor.type_l_path_table);
654 cd9660_732(diskStructure.primaryBigEndianTableSector,
655 (u_char *)diskStructure.primaryDescriptor.type_m_path_table);
657 diskStructure.primaryDescriptor.file_structure_version[0] = 1;
659 /* Pad all strings with spaces instead of nulls */
660 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_id, 32);
661 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.system_id, 32);
662 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_set_id,
664 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.publisher_id,
666 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.preparer_id,
668 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.application_id,
670 cd9660_pad_string_spaces(
671 diskStructure.primaryDescriptor.copyright_file_id, 37);
672 cd9660_pad_string_spaces(
673 diskStructure.primaryDescriptor.abstract_file_id, 37);
674 cd9660_pad_string_spaces(
675 diskStructure.primaryDescriptor.bibliographic_file_id, 37);
680 (unsigned char *)diskStructure.primaryDescriptor.creation_date,
683 (unsigned char *)diskStructure.primaryDescriptor.modification_date,
687 cd9660_set_date(diskStructure.primaryDescriptor.expiration_date, now);
690 memset(diskStructure.primaryDescriptor.expiration_date, '0' ,17);
692 (unsigned char *)diskStructure.primaryDescriptor.effective_date,
697 cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
698 u_char ext_attr_length, u_char flags,
699 u_char name_len, const char * name)
701 record->ext_attr_length[0] = ext_attr_length;
702 record->flags[0] = ISO_FLAG_CLEAR | flags;
703 record->file_unit_size[0] = 0;
704 record->interleave[0] = 0;
705 cd9660_bothendian_word(1, record->volume_sequence_number);
706 record->name_len[0] = name_len;
707 memset(record->name, '\0', sizeof (record->name));
708 memcpy(record->name, name, name_len);
709 record->length[0] = 33 + name_len;
711 /* Todo : better rounding */
712 record->length[0] += (record->length[0] & 1) ? 1 : 0;
716 cd9660_setup_root_node(void)
718 cd9660_populate_iso_dir_record(diskStructure.rootNode->isoDirRecord,
719 0, ISO_FLAG_DIRECTORY, 1, "\0");
723 /*********** SUPPORT FUNCTIONS ***********/
725 cd9660_setup_volume_descriptors(void)
727 /* Boot volume descriptor should come second */
729 /* For now, a fixed 2 : PVD and terminator */
730 volume_descriptor *temp, *t;
733 if ((temp = malloc(sizeof(volume_descriptor))) == NULL) {
734 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
738 temp->volumeDescriptorData =
739 (unsigned char *)&diskStructure.primaryDescriptor;
740 temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
741 temp->volumeDescriptorData[6] = 1;
742 temp->sector = sector;
743 memcpy(temp->volumeDescriptorData + 1,
744 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
745 diskStructure.firstVolumeDescriptor = temp;
748 /* Set up boot support if enabled. BVD must reside in sector 17 */
749 if (diskStructure.is_bootable) {
750 if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
751 CD9660_MEM_ALLOC_ERROR(
752 "cd9660_setup_volume_descriptors");
755 if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
756 CD9660_MEM_ALLOC_ERROR(
757 "cd9660_setup_volume_descriptors");
762 memset(t->volumeDescriptorData, 0, 2048);
764 if (diskStructure.verbose_level > 0)
765 printf("Setting up boot volume descriptor\n");
766 cd9660_setup_boot_volume_descriptor(t);
770 /* Set up the terminator */
771 if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
772 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
775 if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
776 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
781 memset(t->volumeDescriptorData, 0, 2048);
782 t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
784 t->volumeDescriptorData[6] = 1;
786 memcpy(t->volumeDescriptorData + 1,
787 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
795 * Populate EAR at some point. Not required, but is used by NetBSD's
799 cd9660_fill_extended_attribute_record(cd9660node *node)
801 if ((node->isoExtAttributes =
802 malloc(sizeof(struct iso_extended_attributes))) == NULL) {
803 CD9660_MEM_ALLOC_ERROR("cd9660_fill_extended_attribute_record");
812 cd9660_translate_node_common(cd9660node *newnode)
817 char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
819 /* Now populate the isoDirRecord structure */
820 memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
822 test = cd9660_convert_filename(newnode->node->name,
823 temp, !(S_ISDIR(newnode->node->type)));
825 flag = ISO_FLAG_CLEAR;
826 if (S_ISDIR(newnode->node->type))
827 flag |= ISO_FLAG_DIRECTORY;
829 cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
830 flag, strlen(temp), temp);
832 /* Set the various dates */
834 /* If we want to use the current date and time */
837 cd9660_time_915(newnode->isoDirRecord->date, tim);
839 cd9660_bothendian_dword(newnode->fileDataLength,
840 newnode->isoDirRecord->size);
841 /* If the file is a link, we want to set the size to 0 */
842 if (S_ISLNK(newnode->node->type))
843 newnode->fileDataLength = 0;
849 * Translate fsnode to cd9660node
850 * Translate filenames and other metadata, including dates, sizes,
852 * @param struct fsnode * The node generated by makefs
853 * @param struct cd9660node * The intermediate node to be written to
854 * @returns int 0 on failure, 1 on success
857 cd9660_translate_node(fsnode *node, cd9660node *newnode)
860 if (diskStructure.verbose_level > 0)
861 printf("cd9660_translate_node: NULL node passed, "
865 if ((newnode->isoDirRecord =
866 malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
867 CD9660_MEM_ALLOC_ERROR("cd9660_translate_node");
871 /* Set the node pointer */
872 newnode->node = node;
875 if (!(S_ISDIR(node->type)))
876 newnode->fileDataLength = node->inode->st.st_size;
878 if (cd9660_translate_node_common(newnode) == 0)
881 /* Finally, overwrite some of the values that are set by default */
882 cd9660_time_915(newnode->isoDirRecord->date, node->inode->st.st_mtime);
888 * Compares two ISO filenames
889 * @param const char * The first file name
890 * @param const char * The second file name
891 * @returns : -1 if first is less than second, 0 if they are the same, 1 if
892 * the second is greater than the first
895 cd9660_compare_filename(const char *first, const char *second)
898 * This can be made more optimal once it has been tested
899 * (the extra character, for example, is for testing)
905 /* First, on the filename */
907 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
908 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
911 if (c1 == '.' && c2 =='.')
913 else if (c1 == '.') {
916 } else if (c2 == '.') {
931 if (first[p1] == '.' && second[p2] == '.') {
934 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
935 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
938 if (c1 == ';' && c2 == ';')
940 else if (c1 == ';') {
943 } else if (c2 == ';') {
961 * Insert a node into list with ISO sorting rules
962 * @param cd9660node * The head node of the list
963 * @param cd9660node * The node to be inserted
966 cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
970 struct cd9660_children_head *head = &parent->cn_children;
972 /* TODO: Optimize? */
973 cn_new->parent = parent;
976 * first will either be 0, the . or the ..
977 * if . or .., this means no other entry may be written before first
978 * if 0, the new node may be inserted at the head
981 TAILQ_FOREACH(cn, head, cn_next_child) {
983 * Dont insert a node twice -
984 * that would cause an infinite loop
989 compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
990 cn->isoDirRecord->name);
993 compare = cd9660_compare_filename(cn_new->node->name,
1000 TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
1002 TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
1006 * Called After cd9660_sorted_child_insert
1007 * handles file collisions by suffixing each filname with ~n
1008 * where n represents the files respective place in the ordering
1011 cd9660_handle_collisions(cd9660node *colliding, int past)
1013 cd9660node *iter, *next, *prev;
1015 int delete_chars = 0;
1016 int temp_past = past;
1019 cd9660node *end_of_range;
1021 for (iter = TAILQ_FIRST(&colliding->cn_children);
1022 iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
1023 if (strcmp(iter->isoDirRecord->name,
1024 next->isoDirRecord->name) != 0) {
1025 iter = TAILQ_NEXT(iter, cn_next_child);
1029 temp_skip = skip = cd9660_count_collisions(iter);
1030 end_of_range = iter;
1031 while (temp_skip > 0) {
1033 end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
1036 while (temp_past > 0) {
1037 if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
1038 end_of_range = next;
1039 else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
1046 iter = cd9660_rename_filename(iter, skip, delete_chars);
1053 cd9660_rename_filename(cd9660node *iter, int num, int delete_chars)
1056 int numbts, dot, semi, digit, digits, temp, powers, multiplier, count;
1061 if (diskStructure.verbose_level > 0)
1062 printf("Rename_filename called\n");
1064 /* TODO : A LOT of chanes regarding 8.3 filenames */
1065 if (diskStructure.isoLevel == 1)
1067 else if (diskStructure.isoLevel == 2)
1070 maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
1072 tmp = malloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1079 while (((int)(i / powers) ) >= 10) {
1081 powers = powers * 10;
1084 naming = iter->o_name;
1087 while ((*naming != '.') && (*naming != ';')) {
1095 while (count < maxlength) {
1098 else if (*naming == ';') {
1106 if ((count + digits) < maxlength)
1109 numbts = maxlength - (digits);
1110 numbts -= delete_chars;
1112 /* 8.3 rules - keep the extension, add before the dot */
1115 * This code makes a bunch of assumptions.
1116 * See if you can spot them all :)
1120 if (diskStructure.isoLevel == 1) {
1121 numbts = 8 - digits - delete_chars;
1126 memmove(&tmp[numbts],&tmp[dot],4);
1132 /* (copying just the filename before the '.' */
1133 memcpy(tmp, (iter->o_name), numbts);
1135 /* adding the appropriate number following the name */
1137 while (digits > 0) {
1138 digit = (int)(temp / powers);
1139 temp = temp - digit * powers;
1140 sprintf(&tmp[numbts] , "%d", digit);
1143 powers = powers / 10;
1146 while ((*naming != ';') && (numbts < maxlength)) {
1147 tmp[numbts] = (*naming);
1153 tmp[numbts+1] = '1';
1154 tmp[numbts+2] = '\0';
1157 * now tmp has exactly the identifier
1158 * we want so we'll copy it back to record
1160 memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
1162 iter = TAILQ_NEXT(iter, cn_next_child);
1170 /* Todo: Figure out why these functions are nec. */
1172 cd9660_copy_filenames(cd9660node *node)
1176 if (TAILQ_EMPTY(&node->cn_children))
1179 if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
1180 debug_print_tree(diskStructure.rootNode, 0);
1184 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1185 cd9660_copy_filenames(cn);
1186 memcpy(cn->o_name, cn->isoDirRecord->name,
1187 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1192 cd9660_sorting_nodes(cd9660node *node)
1196 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
1197 cd9660_sorting_nodes(cn);
1198 cd9660_sort_nodes(node);
1201 /* XXX Bubble sort. */
1203 cd9660_sort_nodes(cd9660node *node)
1205 cd9660node *cn, *next;
1208 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1209 if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
1211 else if (strcmp(next->isoDirRecord->name,
1212 cn->isoDirRecord->name) >= 0)
1214 TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
1215 TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
1218 } while (cn != NULL);
1222 cd9660_count_collisions(cd9660node *copy)
1225 cd9660node *iter, *next;
1228 (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
1230 if (cd9660_compare_filename(iter->isoDirRecord->name,
1231 next->isoDirRecord->name) == 0)
1237 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
1238 printf("cd9660_recurse_on_collision: count is %i \n", count);
1239 compare = cd9660_compare_filename(iter->isoDirRecord->name,
1240 next->isoDirRecord->name);
1243 return cd9660_recurse_on_collision(next, count);
1252 cd9660_rrip_move_directory(cd9660node *dir)
1258 * This function needs to:
1259 * 1) Create an empty virtual file in place of the old directory
1260 * 2) Point the virtual file to the new directory
1261 * 3) Point the relocated directory to its old parent
1262 * 4) Move the directory specified by dir into rr_moved_dir,
1263 * and rename it to "diskStructure.rock_ridge_move_count" (as a string)
1266 /* First see if the moved directory even exists */
1267 if (diskStructure.rr_moved_dir == NULL) {
1268 diskStructure.rr_moved_dir =
1269 cd9660_create_directory(ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
1270 diskStructure.rootNode, dir);
1271 if (diskStructure.rr_moved_dir == NULL)
1275 /* Create a file with the same ORIGINAL name */
1276 tfile = cd9660_create_file(dir->node->name, dir->parent, dir);
1280 diskStructure.rock_ridge_move_count++;
1281 snprintf(newname, sizeof(newname), "%08i",
1282 diskStructure.rock_ridge_move_count);
1284 /* Point to old parent */
1285 dir->rr_real_parent = dir->parent;
1287 /* Place the placeholder file */
1288 if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
1289 TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
1292 cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
1295 /* Point to new parent */
1296 dir->parent = diskStructure.rr_moved_dir;
1298 /* Point the file to the moved directory */
1299 tfile->rr_relocated = dir;
1301 /* Actually move the directory */
1302 cd9660_sorted_child_insert(diskStructure.rr_moved_dir, dir);
1304 /* TODO: Inherit permissions / ownership (basically the entire inode) */
1306 /* Set the new name */
1307 memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1308 strncpy(dir->isoDirRecord->name, newname, 8);
1309 dir->isoDirRecord->length[0] = 34 + 8;
1310 dir->isoDirRecord->name_len[0] = 8;
1316 cd9660_add_dot_records(cd9660node *root)
1318 struct cd9660_children_head *head = &root->cn_children;
1321 TAILQ_FOREACH(cn, head, cn_next_child) {
1322 if ((cn->type & CD9660_TYPE_DIR) == 0)
1324 /* Recursion first */
1325 cd9660_add_dot_records(cn);
1327 cd9660_create_special_directory(CD9660_TYPE_DOT, root);
1328 cd9660_create_special_directory(CD9660_TYPE_DOTDOT, root);
1333 * Convert node to cd9660 structure
1334 * This function is designed to be called recursively on the root node of
1336 * Lots of recursion going on here, want to make sure it is efficient
1337 * @param struct fsnode * The root node to be converted
1338 * @param struct cd9660* The parent node (should not be NULL)
1339 * @param int Current directory depth
1340 * @param int* Running count of the number of directories that are being created
1343 cd9660_convert_structure(fsnode *root, cd9660node *parent_node, int level,
1344 int *numDirectories, int *error)
1346 fsnode *iterator = root;
1347 cd9660node *this_node;
1354 * Newer, more efficient method, reduces recursion depth
1357 warnx("%s: root is null\n", __func__);
1361 /* Test for an empty directory - makefs still gives us the . record */
1362 if ((S_ISDIR(root->type)) && (root->name[0] == '.')
1363 && (root->name[1] == '\0')) {
1368 if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
1369 CD9660_MEM_ALLOC_ERROR(__func__);
1373 * To reduce the number of recursive calls, we will iterate over
1374 * the next pointers to the right.
1376 while (iterator != NULL) {
1379 * Increment the directory count if this is a directory
1380 * Ignore "." entries. We will generate them later
1382 if (!S_ISDIR(iterator->type) ||
1383 strcmp(iterator->name, ".") != 0) {
1385 /* Translate the node, including its filename */
1386 this_node->parent = parent_node;
1387 cd9660_translate_node(iterator, this_node);
1388 this_node->level = level;
1390 if (S_ISDIR(iterator->type)) {
1391 (*numDirectories)++;
1392 this_node->type = CD9660_TYPE_DIR;
1393 working_level = level + 1;
1396 * If at level 8, directory would be at 8
1397 * and have children at 9 which is not
1398 * allowed as per ISO spec
1401 if ((!diskStructure.allow_deep_trees) &&
1402 (!diskStructure.rock_ridge_enabled)) {
1403 warnx("error: found entry "
1404 "with depth greater "
1408 } else if (diskStructure.
1409 rock_ridge_enabled) {
1412 * Moved directory is actually
1417 if (cd9660_rrip_move_directory(
1430 /* Do the recursive call on the children */
1431 if (iterator->child != 0) {
1432 cd9660_convert_structure(
1433 iterator->child, this_node,
1435 numDirectories, error);
1437 if ((*error) == 1) {
1438 warnx("%s: Error on recursive "
1445 /* Only directories should have children */
1446 assert(iterator->child == NULL);
1448 this_node->type = CD9660_TYPE_FILE;
1452 * Finally, do a sorted insert
1455 cd9660_sorted_child_insert(
1456 parent_node, this_node);
1459 /*Allocate new temp_node */
1460 if (iterator->next != 0) {
1461 this_node = cd9660_allocate_cd9660node();
1462 if (this_node == NULL)
1463 CD9660_MEM_ALLOC_ERROR(__func__);
1466 iterator = iterator->next;
1469 /* cd9660_handle_collisions(first_node); */
1471 /* TODO: need cleanup */
1472 cd9660_copy_filenames(parent_node);
1475 flag = cd9660_handle_collisions(parent_node, counter);
1477 cd9660_sorting_nodes(parent_node);
1478 } while ((flag == 1) && (counter < 100));
1482 * Clean up the cd9660node tree
1483 * This is designed to be called recursively on the root node
1484 * @param struct cd9660node *root The node to free
1488 cd9660_free_structure(cd9660node *root)
1492 while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
1493 TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
1494 cd9660_free_structure(cn);
1500 * Be a little more memory conservative:
1501 * instead of having the TAILQ_ENTRY as part of the cd9660node,
1502 * just create a temporary structure
1506 TAILQ_ENTRY(ptq_entry) ptq;
1510 #define PTQUEUE_NEW(n,s,r,t){\
1511 n = malloc(sizeof(struct s)); \
1518 * Generate the path tables
1519 * The specific implementation of this function is left as an exercise to the
1520 * programmer. It could be done recursively. Make sure you read how the path
1521 * table has to be laid out, it has levels.
1522 * @param struct iso9660_disk *disk The disk image
1523 * @returns int The number of built path tables (between 1 and 4), 0 on failure
1526 cd9660_generate_path_table(void)
1528 cd9660node *cn, *dirNode = diskStructure.rootNode;
1529 cd9660node *last = dirNode;
1530 int pathTableSize = 0; /* computed as we go */
1531 int counter = 1; /* root gets a count of 0 */
1532 int parentRecNum = 0; /* root's parent is '0' */
1534 TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
1535 TAILQ_INIT(&pt_head);
1537 PTQUEUE_NEW(n, ptq_entry, -1, diskStructure.rootNode);
1539 /* Push the root node */
1540 TAILQ_INSERT_HEAD(&pt_head, n, ptq);
1542 /* Breadth-first traversal of file structure */
1543 while (pt_head.tqh_first != 0) {
1544 n = pt_head.tqh_first;
1546 TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
1549 /* Update the size */
1550 pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
1551 + dirNode->isoDirRecord->name_len[0]+
1552 (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
1553 /* includes the padding bit */
1555 dirNode->ptnumber=counter;
1556 if (dirNode != last) {
1557 last->ptnext = dirNode;
1558 dirNode->ptprev = last;
1563 if (dirNode->parent != 0)
1564 parentRecNum = dirNode->parent->ptnumber;
1566 /* Push children onto queue */
1567 TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
1569 * Dont add the DOT and DOTDOT types to the path
1572 if ((cn->type != CD9660_TYPE_DOT)
1573 && (cn->type != CD9660_TYPE_DOTDOT)) {
1575 if (S_ISDIR(cn->node->type)) {
1576 PTQUEUE_NEW(n, ptq_entry, -1, cn);
1577 TAILQ_INSERT_TAIL(&pt_head, n, ptq);
1583 return pathTableSize;
1587 cd9660_compute_full_filename(cd9660node *node, char *buf, int level)
1591 parent = (node->rr_real_parent == NULL ?
1592 node->parent : node->rr_real_parent);
1593 if (parent != NULL) {
1594 cd9660_compute_full_filename(parent, buf, level + 1);
1595 strcat(buf, node->node->name);
1597 /* We are at the root */
1598 strcat(buf, diskStructure.rootFilesystemPath);
1599 if (buf[strlen(buf) - 1] == '/')
1600 buf[strlen(buf) - 1] = '\0';
1607 /* NEW filename conversion method */
1608 typedef int(*cd9660_filename_conversion_functor)(const char *, char *, int);
1612 * TODO: These two functions are almost identical.
1613 * Some code cleanup is possible here
1615 * XXX bounds checking!
1618 cd9660_level1_convert_filename(const char *oldname, char *newname, int is_file)
1622 * File Name shall not contain more than 8 d or d1 characters
1623 * File Name Extension shall not contain more than 3 d or d1 characters
1624 * Directory Identifier shall not contain more than 8 d or d1 characters
1630 while (*oldname != '\0' && extlen < 3) {
1631 /* Handle period first, as it is special */
1632 if (*oldname == '.') {
1642 /* cut RISC OS file type off ISO name */
1643 if (diskStructure.archimedes_enabled &&
1644 *oldname == ',' && strlen(oldname) == 4)
1646 /* Enforce 12.3 / 8 */
1647 if (namelen == 8 && !found_ext)
1650 if (islower((unsigned char)*oldname))
1651 *newname++ = toupper((unsigned char)*oldname);
1652 else if (isupper((unsigned char)*oldname)
1653 || isdigit((unsigned char)*oldname))
1654 *newname++ = *oldname;
1666 if (!found_ext && !diskStructure.omit_trailing_period)
1669 sprintf(newname, ";%i", 1);
1671 return namelen + extlen + found_ext;
1674 /* XXX bounds checking! */
1676 cd9660_level2_convert_filename(const char *oldname, char *newname, int is_file)
1680 * File name : 0+ d or d1 characters
1682 * File name extension : 0+ d or d1 characters
1684 * File version number (5 characters, 1-32767)
1685 * 1 <= Sum of File name and File name extension <= 30
1691 while (*oldname != '\0' && namelen + extlen < 30) {
1692 /* Handle period first, as it is special */
1693 if (*oldname == '.') {
1695 if (diskStructure.allow_multidot) {
1707 /* cut RISC OS file type off ISO name */
1708 if (diskStructure.archimedes_enabled &&
1709 *oldname == ',' && strlen(oldname) == 4)
1712 if (islower((unsigned char)*oldname))
1713 *newname++ = toupper((unsigned char)*oldname);
1714 else if (isupper((unsigned char)*oldname) ||
1715 isdigit((unsigned char)*oldname))
1716 *newname++ = *oldname;
1717 else if (diskStructure.allow_multidot &&
1732 if (!found_ext && !diskStructure.omit_trailing_period)
1735 sprintf(newname, ";%i", 1);
1737 return namelen + extlen + found_ext;
1742 cd9660_joliet_convert_filename(const char *oldname, char *newname, int is_file)
1744 /* TODO: implement later, move to cd9660_joliet.c ?? */
1750 * Convert a file name to ISO compliant file name
1751 * @param char * oldname The original filename
1752 * @param char ** newname The new file name, in the appropriate character
1753 * set and of appropriate length
1754 * @param int 1 if file, 0 if directory
1755 * @returns int The length of the new string
1758 cd9660_convert_filename(const char *oldname, char *newname, int is_file)
1761 cd9660_filename_conversion_functor conversion_function = 0;
1762 if (diskStructure.isoLevel == 1)
1763 conversion_function = &cd9660_level1_convert_filename;
1764 else if (diskStructure.isoLevel == 2)
1765 conversion_function = &cd9660_level2_convert_filename;
1766 return (*conversion_function)(oldname, newname, is_file);
1770 cd9660_compute_record_size(cd9660node *node)
1772 int size = node->isoDirRecord->length[0];
1774 if (diskStructure.rock_ridge_enabled)
1775 size += node->susp_entry_size;
1776 size += node->su_tail_size;
1777 size += size & 1; /* Ensure length of record is even. */
1778 assert(size <= 254);
1783 cd9660_populate_dot_records(cd9660node *node)
1785 node->dot_record->fileDataSector = node->fileDataSector;
1786 memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
1787 node->dot_record->isoDirRecord->name_len[0] = 1;
1788 node->dot_record->isoDirRecord->name[0] = 0;
1789 node->dot_record->isoDirRecord->name[1] = 0;
1790 node->dot_record->isoDirRecord->length[0] = 34;
1791 node->dot_record->fileRecordSize =
1792 cd9660_compute_record_size(node->dot_record);
1794 if (node == diskStructure.rootNode) {
1795 node->dot_dot_record->fileDataSector = node->fileDataSector;
1796 memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
1799 node->dot_dot_record->fileDataSector =
1800 node->parent->fileDataSector;
1801 memcpy(node->dot_dot_record->isoDirRecord,
1802 node->parent->isoDirRecord,34);
1804 node->dot_dot_record->isoDirRecord->name_len[0] = 1;
1805 node->dot_dot_record->isoDirRecord->name[0] = 1;
1806 node->dot_dot_record->isoDirRecord->name[1] = 0;
1807 node->dot_dot_record->isoDirRecord->length[0] = 34;
1808 node->dot_dot_record->fileRecordSize =
1809 cd9660_compute_record_size(node->dot_dot_record);
1813 * @param struct cd9660node *node The node
1814 * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
1815 * @returns int The total size of files and directory entries (should be
1816 * a multiple of sector size)
1819 cd9660_compute_offsets(cd9660node *node, int64_t startOffset)
1822 * This function needs to compute the size of directory records and
1823 * runs, file lengths, and set the appropriate variables both in
1824 * cd9660node and isoDirEntry
1826 int64_t used_bytes = 0;
1827 int64_t current_sector_usage = 0;
1832 assert(node != NULL);
1836 * NOTE : There needs to be some special case detection for
1837 * the "real root" node, since for it, node->node is undefined
1840 node->fileDataSector = -1;
1842 if (node->type & CD9660_TYPE_DIR) {
1843 node->fileRecordSize = cd9660_compute_record_size(node);
1844 /*Set what sector this directory starts in*/
1845 node->fileDataSector =
1846 CD9660_BLOCKS(diskStructure.sectorSize,startOffset);
1848 cd9660_bothendian_dword(node->fileDataSector,
1849 node->isoDirRecord->extent);
1852 * First loop over children, need to know the size of
1853 * their directory records
1855 node->fileSectorsUsed = 1;
1856 TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
1857 node->fileDataLength +=
1858 cd9660_compute_record_size(child);
1859 if ((cd9660_compute_record_size(child) +
1860 current_sector_usage) >=
1861 diskStructure.sectorSize) {
1862 current_sector_usage = 0;
1863 node->fileSectorsUsed++;
1866 current_sector_usage +=
1867 cd9660_compute_record_size(child);
1870 cd9660_bothendian_dword(node->fileSectorsUsed *
1871 diskStructure.sectorSize,node->isoDirRecord->size);
1874 * This should point to the sector after the directory
1875 * record (or, the first byte in that sector)
1877 used_bytes += node->fileSectorsUsed * diskStructure.sectorSize;
1879 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1880 child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
1881 /* Directories need recursive call */
1882 if (S_ISDIR(child->node->type)) {
1883 r = cd9660_compute_offsets(child,
1884 used_bytes + startOffset);
1893 /* Explicitly set the . and .. records */
1894 cd9660_populate_dot_records(node);
1896 /* Finally, do another iteration to write the file data*/
1897 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1899 child = TAILQ_NEXT(child, cn_next_child)) {
1900 /* Files need extent set */
1901 if (S_ISDIR(child->node->type))
1903 child->fileRecordSize =
1904 cd9660_compute_record_size(child);
1906 child->fileSectorsUsed =
1907 CD9660_BLOCKS(diskStructure.sectorSize,
1908 child->fileDataLength);
1910 inode = child->node->inode;
1911 if ((inode->flags & FI_ALLOCATED) == 0) {
1913 CD9660_BLOCKS(diskStructure.sectorSize,
1914 used_bytes + startOffset);
1915 inode->flags |= FI_ALLOCATED;
1916 used_bytes += child->fileSectorsUsed *
1917 diskStructure.sectorSize;
1919 INODE_WARNX(("%s: already allocated inode %d "
1920 "data sectors at %" PRIu32, __func__,
1921 (int)inode->st.st_ino, inode->ino));
1923 child->fileDataSector = inode->ino;
1924 cd9660_bothendian_dword(child->fileDataSector,
1925 child->isoDirRecord->extent);
1933 /* Might get rid of this func */
1935 cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
1937 to->node->inode->st.st_dev = 0;
1938 to->node->inode->st.st_ino = 0;
1939 to->node->inode->st.st_size = 0;
1940 to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
1941 to->node->inode->st.st_atime = from->node->inode->st.st_atime;
1942 to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
1943 to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
1944 to->node->inode->st.st_uid = from->node->inode->st.st_uid;
1945 to->node->inode->st.st_gid = from->node->inode->st.st_gid;
1946 to->node->inode->st.st_mode = from->node->inode->st.st_mode;
1947 /* Clear out type */
1948 to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
1950 to->node->inode->st.st_mode |= S_IFREG;
1952 to->node->inode->st.st_mode |= S_IFDIR;
1958 cd9660_create_virtual_entry(const char *name, cd9660node *parent, int file,
1964 assert(parent != NULL);
1966 temp = cd9660_allocate_cd9660node();
1970 if ((tfsnode = malloc(sizeof(fsnode))) == NULL) {
1971 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1975 /* Assume for now name is a valid length */
1976 if ((tfsnode->name = malloc(strlen(name) + 1)) == NULL) {
1977 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1981 if ((temp->isoDirRecord =
1982 malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
1983 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1987 strcpy(tfsnode->name, name);
1989 cd9660_convert_filename(tfsnode->name, temp->isoDirRecord->name, file);
1991 temp->node = tfsnode;
1992 temp->parent = parent;
1995 if (temp->parent != NULL) {
1996 temp->level = temp->parent->level + 1;
1997 if (!TAILQ_EMPTY(&temp->parent->cn_children))
1998 cd9660_sorted_child_insert(temp->parent, temp);
2000 TAILQ_INSERT_HEAD(&temp->parent->cn_children,
2001 temp, cn_next_child);
2005 if (parent->node != NULL) {
2006 tfsnode->type = parent->node->type;
2009 /* Clear out file type bits */
2010 tfsnode->type &= ~(S_IFMT);
2012 tfsnode->type |= S_IFREG;
2014 tfsnode->type |= S_IFDIR;
2016 /* Indicate that there is no spec entry (inode) */
2017 tfsnode->flags &= ~(FSNODE_F_HASSPEC);
2019 cd9660_copy_stat_info(parent, temp, file);
2025 cd9660_create_file(const char * name, cd9660node *parent, cd9660node *me)
2029 temp = cd9660_create_virtual_entry(name,parent,1,1);
2033 temp->fileDataLength = 0;
2035 temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
2037 if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2039 *temp->node->inode = *me->node->inode;
2041 if (cd9660_translate_node_common(temp) == 0)
2047 * Create a new directory which does not exist on disk
2048 * @param const char * name The name to assign to the directory
2049 * @param const char * parent Pointer to the parent directory
2050 * @returns cd9660node * Pointer to the new directory
2053 cd9660_create_directory(const char *name, cd9660node *parent, cd9660node *me)
2057 temp = cd9660_create_virtual_entry(name,parent,0,1);
2060 temp->node->type |= S_IFDIR;
2062 temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
2064 if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2066 *temp->node->inode = *me->node->inode;
2068 if (cd9660_translate_node_common(temp) == 0)
2074 cd9660_create_special_directory(u_char type, cd9660node *parent)
2076 cd9660node *temp, *first;
2079 assert(parent != NULL);
2081 if (type == CD9660_TYPE_DOT)
2083 else if (type == CD9660_TYPE_DOTDOT)
2089 if ((temp = cd9660_create_virtual_entry(na, parent, 0, 0)) == NULL)
2092 temp->parent = parent;
2094 temp->isoDirRecord->length[0] = 34;
2095 /* Dot record is always first */
2096 if (type == CD9660_TYPE_DOT) {
2097 parent->dot_record = temp;
2098 TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
2099 /* DotDot should be second */
2100 } else if (type == CD9660_TYPE_DOTDOT) {
2101 parent->dot_dot_record = temp;
2103 * If the first child is the dot record, insert
2104 * this second. Otherwise, insert it at the head.
2106 if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
2107 (first->type & CD9660_TYPE_DOT) == 0) {
2108 TAILQ_INSERT_HEAD(&parent->cn_children, temp,
2111 TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
2120 cd9660_add_generic_bootimage(const char *bootimage)
2124 assert(bootimage != NULL);
2126 if (*bootimage == '\0') {
2127 warnx("Error: Boot image must be a filename");
2131 if ((diskStructure.generic_bootimage = strdup(bootimage)) == NULL) {
2132 warn("%s: strdup", __func__);
2136 /* Get information about the file */
2137 if (lstat(diskStructure.generic_bootimage, &stbuf) == -1)
2138 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
2139 diskStructure.generic_bootimage);
2141 if (stbuf.st_size > 32768) {
2142 warnx("Error: Boot image must be no greater than 32768 bytes");
2146 if (diskStructure.verbose_level > 0) {
2147 printf("Generic boot image image has size %lld\n",
2148 (long long)stbuf.st_size);
2151 diskStructure.has_generic_bootimage = 1;