1 /* $NetBSD: cd9660.c,v 1.32 2011/08/23 17:09:11 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-4-Clause
6 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7 * Perez-Rathke and Ram Vedam. All rights reserved.
9 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10 * Alan Perez-Rathke and Ram Vedam.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
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.
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
37 * Copyright (c) 2001 Wasabi Systems, Inc.
38 * All rights reserved.
40 * Written by Luke Mewburn for Wasabi Systems, Inc.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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
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.
71 * Copyright (c) 1982, 1986, 1989, 1993
72 * The Regents of the University of California. All rights reserved.
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
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.
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
100 #include <sys/cdefs.h>
101 __FBSDID("$FreeBSD$");
103 #include <sys/param.h>
104 #include <sys/queue.h>
112 #include "cd9660/iso9660_rrip.h"
113 #include "cd9660/cd9660_archimedes.h"
115 static void cd9660_finalize_PVD(iso9660_disk *);
116 static cd9660node *cd9660_allocate_cd9660node(void);
117 static void cd9660_set_defaults(iso9660_disk *);
118 static int cd9660_arguments_set_string(const char *, const char *, int,
120 static void cd9660_populate_iso_dir_record(
121 struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
123 static void cd9660_setup_root_node(iso9660_disk *);
124 static int cd9660_setup_volume_descriptors(iso9660_disk *);
126 static int cd9660_fill_extended_attribute_record(cd9660node *);
128 static void cd9660_sort_nodes(cd9660node *);
129 static int cd9660_translate_node_common(iso9660_disk *, cd9660node *);
130 static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *);
131 static int cd9660_compare_filename(const char *, const char *);
132 static void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
133 static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int);
134 static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int,
136 static void cd9660_copy_filenames(iso9660_disk *, cd9660node *);
137 static void cd9660_sorting_nodes(cd9660node *);
138 static int cd9660_count_collisions(cd9660node *);
139 static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *);
140 static int cd9660_add_dot_records(iso9660_disk *, cd9660node *);
142 static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int,
144 static void cd9660_free_structure(cd9660node *);
145 static int cd9660_generate_path_table(iso9660_disk *);
146 static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *,
148 static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *,
151 static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *,
154 static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int);
155 static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *);
156 static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t);
158 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
160 static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *,
161 cd9660node *, int, int);
162 static cd9660node *cd9660_create_file(iso9660_disk *, const char *,
163 cd9660node *, cd9660node *);
164 static cd9660node *cd9660_create_directory(iso9660_disk *, const char *,
165 cd9660node *, cd9660node *);
166 static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char,
168 static int cd9660_add_generic_bootimage(iso9660_disk *, const char *);
172 * Allocate and initialize a cd9660node
173 * @returns struct cd9660node * Pointer to new node, or NULL on error
176 cd9660_allocate_cd9660node(void)
178 cd9660node *temp = ecalloc(1, sizeof(*temp));
180 TAILQ_INIT(&temp->cn_children);
181 temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
182 temp->ptnext = temp->ptprev = temp->ptlast = NULL;
184 temp->isoDirRecord = NULL;
185 temp->isoExtAttributes = NULL;
186 temp->rr_real_parent = temp->rr_relocated = NULL;
187 temp->su_tail_data = NULL;
192 * Set default values for cd9660 extension to makefs
195 cd9660_set_defaults(iso9660_disk *diskStructure)
197 /*Fix the sector size for now, though the spec allows for other sizes*/
198 diskStructure->sectorSize = 2048;
200 /* Set up defaults in our own structure */
201 diskStructure->verbose_level = 0;
202 diskStructure->keep_bad_images = 0;
203 diskStructure->follow_sym_links = 0;
204 diskStructure->isoLevel = 2;
206 diskStructure->rock_ridge_enabled = 0;
207 diskStructure->rock_ridge_renamed_dir_name = 0;
208 diskStructure->rock_ridge_move_count = 0;
209 diskStructure->rr_moved_dir = 0;
211 diskStructure->archimedes_enabled = 0;
212 diskStructure->chrp_boot = 0;
214 diskStructure->include_padding_areas = 1;
216 /* Spec breaking functionality */
217 diskStructure->allow_deep_trees =
218 diskStructure->allow_start_dot =
219 diskStructure->allow_max_name =
220 diskStructure->allow_illegal_chars =
221 diskStructure->allow_lowercase =
222 diskStructure->allow_multidot =
223 diskStructure->omit_trailing_period = 0;
225 /* Make sure the PVD is clear */
226 memset(&diskStructure->primaryDescriptor, 0, 2048);
228 memset(diskStructure->primaryDescriptor.publisher_id, 0x20,128);
229 memset(diskStructure->primaryDescriptor.preparer_id, 0x20,128);
230 memset(diskStructure->primaryDescriptor.application_id, 0x20,128);
231 memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37);
232 memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37);
233 memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37);
235 strcpy(diskStructure->primaryDescriptor.system_id, "FreeBSD");
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)
252 iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
254 #define OPT_STR(letter, name, desc) \
255 { letter, name, NULL, OPT_STRBUF, 0, 0, desc }
257 #define OPT_NUM(letter, name, field, min, max, desc) \
258 { letter, name, &diskStructure->field, \
259 sizeof(diskStructure->field) == 8 ? OPT_INT64 : \
260 (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \
261 (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \
264 #define OPT_BOOL(letter, name, field, desc) \
265 OPT_NUM(letter, name, field, 0, 1, desc)
267 const option_t cd9660_options[] = {
268 OPT_NUM('l', "isolevel", isoLevel,
270 OPT_NUM('v', "verbose", verbose_level,
271 0, 2, "Turns on verbose output"),
273 OPT_BOOL('h', "help", displayHelp,
274 "Show help message"),
275 OPT_BOOL('S', "follow-symlinks", follow_sym_links,
276 "Resolve symlinks in pathnames"),
277 OPT_BOOL('R', "rockridge", rock_ridge_enabled,
278 "Enable Rock-Ridge extensions"),
279 OPT_BOOL('C', "chrp-boot", chrp_boot,
281 OPT_BOOL('K', "keep-bad-images", keep_bad_images,
283 OPT_BOOL('D', "allow-deep-trees", allow_deep_trees,
284 "Allow trees more than 8 levels"),
285 OPT_BOOL('a', "allow-max-name", allow_max_name,
286 "Allow 37 char filenames (unimplemented)"),
287 OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars,
288 "Allow illegal characters in filenames"),
289 OPT_BOOL('m', "allow-multidot", allow_multidot,
290 "Allow multiple periods in filenames"),
291 OPT_BOOL('o', "omit-trailing-period", omit_trailing_period,
292 "Omit trailing periods in filenames"),
293 OPT_BOOL('\0', "allow-lowercase", allow_lowercase,
294 "Allow lowercase characters in filenames"),
295 OPT_BOOL('\0', "archimedes", archimedes_enabled,
296 "Enable Archimedes structure"),
297 OPT_BOOL('\0', "no-trailing-padding", include_padding_areas,
298 "Include padding areas"),
300 OPT_STR('A', "applicationid", "Application Identifier"),
301 OPT_STR('P', "publisher", "Publisher Identifier"),
302 OPT_STR('p', "preparer", "Preparer Identifier"),
303 OPT_STR('L', "label", "Disk Label"),
304 OPT_STR('V', "volumeid", "Volume Set Identifier"),
305 OPT_STR('B', "bootimage", "Boot image parameter"),
306 OPT_STR('G', "generic-bootimage", "Generic boot image param"),
307 OPT_STR('\0', "bootimagedir", "Boot image directory"),
308 OPT_STR('\0', "no-emul-boot", "No boot emulation"),
309 OPT_STR('\0', "no-boot", "No boot support"),
310 OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"),
311 OPT_STR('\0', "boot-load-segment", "Boot load segment"),
312 OPT_STR('\0', "platformid", "Section Header Platform ID"),
317 fsopts->fs_specific = diskStructure;
318 fsopts->fs_options = copy_opts(cd9660_options);
320 cd9660_set_defaults(diskStructure);
324 cd9660_cleanup_opts(fsinfo_t *fsopts)
326 free(fsopts->fs_specific);
327 free(fsopts->fs_options);
331 cd9660_arguments_set_string(const char *val, const char *fieldtitle, int length,
332 char testmode, char * dest)
337 warnx("error: The %s requires a string argument", fieldtitle);
338 else if ((len = strlen(val)) <= length) {
340 test = cd9660_valid_d_chars(val);
342 test = cd9660_valid_a_chars(val);
344 memcpy(dest, val, len);
346 cd9660_uppercase_characters(dest, len);
349 warnx("error: The %s must be composed of "
350 "%c-characters", fieldtitle, testmode);
352 warnx("error: The %s must be at most 32 characters long",
358 * Command-line parsing function
362 cd9660_parse_opts(const char *option, fsinfo_t *fsopts)
365 iso9660_disk *diskStructure = fsopts->fs_specific;
366 option_t *cd9660_options = fsopts->fs_options;
368 const char *name, *desc;
370 assert(option != NULL);
372 if (debug & DEBUG_FS_PARSE_OPTS)
373 printf("%s: got `%s'\n", __func__, option);
375 i = set_option(cd9660_options, option, buf, sizeof(buf));
379 if (cd9660_options[i].name == NULL)
382 name = cd9660_options[i].name;
383 desc = cd9660_options[i].desc;
384 switch (cd9660_options[i].letter) {
387 rv = 0; /* this is not handled yet */
390 rv = cd9660_arguments_set_string(buf, desc, 32, 'd',
391 diskStructure->primaryDescriptor.volume_id);
394 rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
395 diskStructure->primaryDescriptor.application_id);
398 rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
399 diskStructure->primaryDescriptor.publisher_id);
402 rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
403 diskStructure->primaryDescriptor.preparer_id);
406 rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
407 diskStructure->primaryDescriptor.volume_set_id);
411 if (buf[0] == '\0') {
412 warnx("The Boot Image parameter requires a valid boot"
413 "information string");
416 rv = cd9660_add_boot_disk(diskStructure, buf);
419 if (buf[0] == '\0') {
420 warnx("The Generic Boot Image parameter requires a"
421 " valid boot information string");
424 rv = cd9660_add_generic_bootimage(diskStructure, buf);
427 if (strcmp(name, "bootimagedir") == 0) {
429 * XXXfvdl this is unused.
431 if (buf[0] == '\0') {
432 warnx("The Boot Image Directory parameter"
433 " requires a directory name");
436 diskStructure->boot_image_directory =
437 emalloc(strlen(buf) + 1);
438 /* BIG TODO: Add the max length function here */
439 rv = cd9660_arguments_set_string(buf, desc, 12,
440 'd', diskStructure->boot_image_directory);
442 } else if (strcmp(name, "no-emul-boot") == 0 ||
443 strcmp(name, "no-boot") == 0 ||
444 strcmp(name, "hard-disk-boot") == 0) {
446 cd9660_eltorito_add_boot_option(diskStructure, name, 0);
448 } else if (strcmp(name, "boot-load-segment") == 0 ||
449 strcmp(name, "platformid") == 0) {
450 if (buf[0] == '\0') {
451 warnx("Option `%s' doesn't contain a value",
455 cd9660_eltorito_add_boot_option(diskStructure,
466 * Main function for cd9660_makefs
467 * Builds the ISO image file
468 * @param const char *image The image filename to create
469 * @param const char *dir The directory that is being read
470 * @param struct fsnode *root The root node of the filesystem tree
471 * @param struct fsinfo_t *fsopts Any options
474 cd9660_makefs(const char *image, const char *dir, fsnode *root,
479 uint64_t pathTableSectors;
480 int64_t firstAvailableSector;
483 cd9660node *real_root;
484 iso9660_disk *diskStructure = fsopts->fs_specific;
486 if (diskStructure->verbose_level > 0)
487 printf("%s: ISO level is %i\n", __func__,
488 diskStructure->isoLevel);
489 if (diskStructure->isoLevel < 2 &&
490 diskStructure->allow_multidot)
491 errx(EXIT_FAILURE, "allow-multidot requires iso level of 2");
493 assert(image != NULL);
495 assert(root != NULL);
497 if (diskStructure->displayHelp) {
499 * Display help here - probably want to put it in
500 * a separate function
505 if (diskStructure->verbose_level > 0)
506 printf("%s: image %s directory %s root %p\n", __func__,
509 /* Set up some constants. Later, these will be defined with options */
511 /* Counter needed for path tables */
514 /* Convert tree to our own format */
515 /* Actually, we now need to add the REAL root node, at level 0 */
517 real_root = cd9660_allocate_cd9660node();
518 real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord));
519 /* Leave filename blank for root */
520 memset(real_root->isoDirRecord->name, 0,
521 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
523 real_root->level = 0;
524 diskStructure->rootNode = real_root;
525 real_root->type = CD9660_TYPE_DIR;
527 real_root->node = root;
528 cd9660_convert_structure(diskStructure, root, real_root, 1,
529 &numDirectories, &error);
531 if (TAILQ_EMPTY(&real_root->cn_children)) {
532 errx(EXIT_FAILURE, "%s: converted directory is empty. "
533 "Tree conversion failed", __func__);
534 } else if (error != 0) {
535 errx(EXIT_FAILURE, "%s: tree conversion failed", __func__);
537 if (diskStructure->verbose_level > 0)
538 printf("%s: tree converted\n", __func__);
541 /* Add the dot and dot dot records */
542 cd9660_add_dot_records(diskStructure, real_root);
544 cd9660_setup_root_node(diskStructure);
546 if (diskStructure->verbose_level > 0)
547 printf("%s: done converting tree\n", __func__);
549 /* non-SUSP extensions */
550 if (diskStructure->archimedes_enabled)
551 archimedes_convert_tree(diskStructure->rootNode);
553 /* Rock ridge / SUSP init pass */
554 if (diskStructure->rock_ridge_enabled) {
555 cd9660_susp_initialize(diskStructure, diskStructure->rootNode,
556 diskStructure->rootNode, NULL);
559 /* Build path table structure */
560 diskStructure->pathTableLength = cd9660_generate_path_table(
563 pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize,
564 diskStructure->pathTableLength);
566 firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure);
567 if (diskStructure->is_bootable) {
568 firstAvailableSector = cd9660_setup_boot(diskStructure,
569 firstAvailableSector);
570 if (firstAvailableSector < 0)
571 errx(EXIT_FAILURE, "setup_boot failed");
573 /* LE first, then BE */
574 diskStructure->primaryLittleEndianTableSector = firstAvailableSector;
575 diskStructure->primaryBigEndianTableSector =
576 diskStructure->primaryLittleEndianTableSector + pathTableSectors;
578 /* Set the secondary ones to -1, not going to use them for now */
579 diskStructure->secondaryBigEndianTableSector = -1;
580 diskStructure->secondaryLittleEndianTableSector = -1;
582 diskStructure->dataFirstSector =
583 diskStructure->primaryBigEndianTableSector + pathTableSectors;
584 if (diskStructure->verbose_level > 0)
585 printf("%s: Path table conversion complete. "
586 "Each table is %i bytes, or %" PRIu64 " sectors.\n",
588 diskStructure->pathTableLength, pathTableSectors);
590 startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector;
592 totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset);
594 diskStructure->totalSectors = diskStructure->dataFirstSector +
595 CD9660_BLOCKS(diskStructure->sectorSize, totalSpace);
597 /* Disabled until pass 1 is done */
598 if (diskStructure->rock_ridge_enabled) {
599 diskStructure->susp_continuation_area_start_sector =
600 diskStructure->totalSectors;
601 diskStructure->totalSectors +=
602 CD9660_BLOCKS(diskStructure->sectorSize,
603 diskStructure->susp_continuation_area_size);
604 cd9660_susp_finalize(diskStructure, diskStructure->rootNode);
608 cd9660_finalize_PVD(diskStructure);
610 /* Add padding sectors, just for testing purposes right now */
611 /* diskStructure->totalSectors+=150; */
613 /* Debugging output */
614 if (diskStructure->verbose_level > 0) {
615 printf("%s: Sectors 0-15 reserved\n", __func__);
616 printf("%s: Primary path tables starts in sector %"
617 PRId64 "\n", __func__,
618 diskStructure->primaryLittleEndianTableSector);
619 printf("%s: File data starts in sector %"
620 PRId64 "\n", __func__, diskStructure->dataFirstSector);
621 printf("%s: Total sectors: %"
622 PRId64 "\n", __func__, diskStructure->totalSectors);
626 * Add padding sectors at the end
627 * TODO: Clean this up and separate padding
629 if (diskStructure->include_padding_areas)
630 diskStructure->totalSectors += 150;
632 cd9660_write_image(diskStructure, image);
634 if (diskStructure->verbose_level > 1) {
635 debug_print_volume_descriptor_information(diskStructure);
636 debug_print_tree(diskStructure, real_root, 0);
637 debug_print_path_tree(real_root);
640 /* Clean up data structures */
641 cd9660_free_structure(real_root);
643 if (diskStructure->verbose_level > 0)
644 printf("%s: done\n", __func__);
647 /* Generic function pointer - implement later */
648 typedef int (*cd9660node_func)(cd9660node *);
651 cd9660_finalize_PVD(iso9660_disk *diskStructure)
653 time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL);
655 /* root should be a fixed size of 34 bytes since it has no name */
656 memcpy(diskStructure->primaryDescriptor.root_directory_record,
657 diskStructure->rootNode->dot_record->isoDirRecord, 34);
659 /* In RRIP, this might be longer than 34 */
660 diskStructure->primaryDescriptor.root_directory_record[0] = 34;
662 /* Set up all the important numbers in the PVD */
663 cd9660_bothendian_dword(diskStructure->totalSectors,
664 (unsigned char *)diskStructure->primaryDescriptor.volume_space_size);
665 cd9660_bothendian_word(1,
666 (unsigned char *)diskStructure->primaryDescriptor.volume_set_size);
667 cd9660_bothendian_word(1,
669 diskStructure->primaryDescriptor.volume_sequence_number);
670 cd9660_bothendian_word(diskStructure->sectorSize,
672 diskStructure->primaryDescriptor.logical_block_size);
673 cd9660_bothendian_dword(diskStructure->pathTableLength,
674 (unsigned char *)diskStructure->primaryDescriptor.path_table_size);
676 cd9660_731(diskStructure->primaryLittleEndianTableSector,
677 (u_char *)diskStructure->primaryDescriptor.type_l_path_table);
678 cd9660_732(diskStructure->primaryBigEndianTableSector,
679 (u_char *)diskStructure->primaryDescriptor.type_m_path_table);
681 diskStructure->primaryDescriptor.file_structure_version[0] = 1;
683 /* Pad all strings with spaces instead of nulls */
684 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32);
685 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32);
686 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id,
688 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id,
690 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id,
692 cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id,
694 cd9660_pad_string_spaces(
695 diskStructure->primaryDescriptor.copyright_file_id, 37);
696 cd9660_pad_string_spaces(
697 diskStructure->primaryDescriptor.abstract_file_id, 37);
698 cd9660_pad_string_spaces(
699 diskStructure->primaryDescriptor.bibliographic_file_id, 37);
703 (unsigned char *)diskStructure->primaryDescriptor.creation_date,
706 (unsigned char *)diskStructure->primaryDescriptor.modification_date,
710 cd9660_set_date(diskStructure->primaryDescriptor.expiration_date,
714 memset(diskStructure->primaryDescriptor.expiration_date, '0', 16);
715 diskStructure->primaryDescriptor.expiration_date[16] = 0;
718 (unsigned char *)diskStructure->primaryDescriptor.effective_date,
721 cd9660_time_915(diskStructure->rootNode->dot_record->isoDirRecord->date,
726 cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
727 u_char ext_attr_length, u_char flags,
728 u_char name_len, const char * name)
730 record->ext_attr_length[0] = ext_attr_length;
731 record->flags[0] = ISO_FLAG_CLEAR | flags;
732 record->file_unit_size[0] = 0;
733 record->interleave[0] = 0;
734 cd9660_bothendian_word(1, record->volume_sequence_number);
735 record->name_len[0] = name_len;
736 memset(record->name, '\0', sizeof (record->name));
737 memcpy(record->name, name, name_len);
738 record->length[0] = 33 + name_len;
740 /* Todo : better rounding */
741 record->length[0] += (record->length[0] & 1) ? 1 : 0;
745 cd9660_setup_root_node(iso9660_disk *diskStructure)
747 cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord,
748 0, ISO_FLAG_DIRECTORY, 1, "\0");
752 /*********** SUPPORT FUNCTIONS ***********/
754 cd9660_setup_volume_descriptors(iso9660_disk *diskStructure)
756 /* Boot volume descriptor should come second */
758 /* For now, a fixed 2 : PVD and terminator */
759 volume_descriptor *temp, *t;
762 temp = emalloc(sizeof(*temp));
763 temp->volumeDescriptorData =
764 (unsigned char *)&diskStructure->primaryDescriptor;
765 temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
766 temp->volumeDescriptorData[6] = 1;
767 temp->sector = sector;
768 memcpy(temp->volumeDescriptorData + 1,
769 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
770 diskStructure->firstVolumeDescriptor = temp;
773 /* Set up boot support if enabled. BVD must reside in sector 17 */
774 if (diskStructure->is_bootable) {
775 t = emalloc(sizeof(*t));
776 t->volumeDescriptorData = ecalloc(1, 2048);
780 if (diskStructure->verbose_level > 0)
781 printf("Setting up boot volume descriptor\n");
782 cd9660_setup_boot_volume_descriptor(diskStructure, t);
786 /* Set up the terminator */
787 t = emalloc(sizeof(*t));
788 t->volumeDescriptorData = ecalloc(1, 2048);
790 t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
792 t->volumeDescriptorData[6] = 1;
794 memcpy(t->volumeDescriptorData + 1,
795 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
803 * Populate EAR at some point. Not required, but is used by NetBSD's
807 cd9660_fill_extended_attribute_record(cd9660node *node)
809 node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes));
815 cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode)
817 time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL);
819 char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
821 /* Now populate the isoDirRecord structure */
822 memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
824 (void)cd9660_convert_filename(diskStructure, newnode->node->name,
825 temp, !(S_ISDIR(newnode->node->type)));
827 flag = ISO_FLAG_CLEAR;
828 if (S_ISDIR(newnode->node->type))
829 flag |= ISO_FLAG_DIRECTORY;
831 cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
832 flag, strlen(temp), temp);
834 /* Set the various dates */
836 /* If we want to use the current date and time */
838 cd9660_time_915(newnode->isoDirRecord->date, tstamp);
840 cd9660_bothendian_dword(newnode->fileDataLength,
841 newnode->isoDirRecord->size);
842 /* If the file is a link, we want to set the size to 0 */
843 if (S_ISLNK(newnode->node->type))
844 newnode->fileDataLength = 0;
850 * Translate fsnode to cd9660node
851 * Translate filenames and other metadata, including dates, sizes,
853 * @param struct fsnode * The node generated by makefs
854 * @param struct cd9660node * The intermediate node to be written to
855 * @returns int 0 on failure, 1 on success
858 cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node,
862 if (diskStructure->verbose_level > 0)
863 printf("%s: NULL node passed, returning\n", __func__);
866 newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord));
867 /* Set the node pointer */
868 newnode->node = node;
871 if (!(S_ISDIR(node->type)))
872 newnode->fileDataLength = node->inode->st.st_size;
874 if (cd9660_translate_node_common(diskStructure, newnode) == 0)
877 /* Finally, overwrite some of the values that are set by default */
878 cd9660_time_915(newnode->isoDirRecord->date,
879 stampst.st_ino ? stampst.st_mtime : node->inode->st.st_mtime);
885 * Compares two ISO filenames
886 * @param const char * The first file name
887 * @param const char * The second file name
888 * @returns : -1 if first is less than second, 0 if they are the same, 1 if
889 * the second is greater than the first
892 cd9660_compare_filename(const char *first, const char *second)
895 * This can be made more optimal once it has been tested
896 * (the extra character, for example, is for testing)
902 /* First, on the filename */
904 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
905 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
908 if (c1 == '.' && c2 =='.')
910 else if (c1 == '.') {
913 } else if (c2 == '.') {
928 if (first[p1] == '.' && second[p2] == '.') {
931 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
932 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
935 if (c1 == ';' && c2 == ';')
937 else if (c1 == ';') {
940 } else if (c2 == ';') {
958 * Insert a node into list with ISO sorting rules
959 * @param cd9660node * The head node of the list
960 * @param cd9660node * The node to be inserted
963 cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
967 struct cd9660_children_head *head = &parent->cn_children;
969 /* TODO: Optimize? */
970 cn_new->parent = parent;
973 * first will either be 0, the . or the ..
974 * if . or .., this means no other entry may be written before first
975 * if 0, the new node may be inserted at the head
978 TAILQ_FOREACH(cn, head, cn_next_child) {
980 * Dont insert a node twice -
981 * that would cause an infinite loop
986 compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
987 cn->isoDirRecord->name);
990 compare = cd9660_compare_filename(cn_new->node->name,
997 TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
999 TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
1003 * Called After cd9660_sorted_child_insert
1004 * handles file collisions by suffixing each filname with ~n
1005 * where n represents the files respective place in the ordering
1008 cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding,
1011 cd9660node *iter, *next, *prev;
1013 int delete_chars = 0;
1014 int temp_past = past;
1017 cd9660node *end_of_range;
1019 for (iter = TAILQ_FIRST(&colliding->cn_children);
1020 iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
1021 if (strcmp(iter->isoDirRecord->name,
1022 next->isoDirRecord->name) != 0) {
1023 iter = TAILQ_NEXT(iter, cn_next_child);
1027 temp_skip = skip = cd9660_count_collisions(iter);
1028 end_of_range = iter;
1029 while (temp_skip > 0) {
1031 end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
1034 while (temp_past > 0) {
1035 if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
1036 end_of_range = next;
1037 else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
1044 iter = cd9660_rename_filename(diskStructure, iter, skip,
1052 cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num,
1056 int numbts, digit, digits, temp, powers, count;
1061 if (diskStructure->verbose_level > 0)
1062 printf("Rename_filename called\n");
1064 assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2);
1065 /* TODO : A LOT of chanes regarding 8.3 filenames */
1066 if (diskStructure->isoLevel == 1)
1068 else if (diskStructure->isoLevel == 2)
1071 maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
1073 tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1075 while (i < num && iter) {
1079 while (((int)(i / powers) ) >= 10) {
1081 powers = powers * 10;
1084 naming = iter->o_name;
1087 while ((*naming != '.') && (*naming != ';')) {
1093 while (count < maxlength) {
1100 if ((count + digits) < maxlength)
1103 numbts = maxlength - (digits);
1104 numbts -= delete_chars;
1106 /* 8.3 rules - keep the extension, add before the dot */
1109 * This code makes a bunch of assumptions.
1110 * See if you can spot them all :)
1114 if (diskStructure->isoLevel == 1) {
1115 numbts = 8 - digits - delete_chars;
1120 memmove(&tmp[numbts],&tmp[dot],4);
1126 /* (copying just the filename before the '.' */
1127 memcpy(tmp, (iter->o_name), numbts);
1129 /* adding the appropriate number following the name */
1131 while (digits > 0) {
1132 digit = (int)(temp / powers);
1133 temp = temp - digit * powers;
1134 sprintf(&tmp[numbts] , "%d", digit);
1137 powers = powers / 10;
1140 while ((*naming != ';') && (numbts < maxlength)) {
1141 tmp[numbts] = (*naming);
1147 tmp[numbts+1] = '1';
1148 tmp[numbts+2] = '\0';
1151 * now tmp has exactly the identifier
1152 * we want so we'll copy it back to record
1154 memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
1156 iter = TAILQ_NEXT(iter, cn_next_child);
1164 /* Todo: Figure out why these functions are nec. */
1166 cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node)
1170 if (TAILQ_EMPTY(&node->cn_children))
1173 if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
1174 debug_print_tree(diskStructure, diskStructure->rootNode, 0);
1178 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1179 cd9660_copy_filenames(diskStructure, cn);
1180 memcpy(cn->o_name, cn->isoDirRecord->name,
1181 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1186 cd9660_sorting_nodes(cd9660node *node)
1190 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
1191 cd9660_sorting_nodes(cn);
1192 cd9660_sort_nodes(node);
1195 /* XXX Bubble sort. */
1197 cd9660_sort_nodes(cd9660node *node)
1199 cd9660node *cn, *next;
1202 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1203 if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
1205 else if (strcmp(next->isoDirRecord->name,
1206 cn->isoDirRecord->name) >= 0)
1208 TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
1209 TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
1212 } while (cn != NULL);
1216 cd9660_count_collisions(cd9660node *copy)
1219 cd9660node *iter, *next;
1222 (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
1224 if (cd9660_compare_filename(iter->isoDirRecord->name,
1225 next->isoDirRecord->name) == 0)
1231 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
1232 printf("%s: count is %i\n", __func__, count);
1233 compare = cd9660_compare_filename(iter->isoDirRecord->name,
1234 next->isoDirRecord->name);
1237 return cd9660_recurse_on_collision(next, count);
1246 cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir)
1252 * This function needs to:
1253 * 1) Create an empty virtual file in place of the old directory
1254 * 2) Point the virtual file to the new directory
1255 * 3) Point the relocated directory to its old parent
1256 * 4) Move the directory specified by dir into rr_moved_dir,
1257 * and rename it to "diskStructure->rock_ridge_move_count" (as a string)
1260 /* First see if the moved directory even exists */
1261 if (diskStructure->rr_moved_dir == NULL) {
1262 diskStructure->rr_moved_dir = cd9660_create_directory(
1263 diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
1264 diskStructure->rootNode, dir);
1265 if (diskStructure->rr_moved_dir == NULL)
1267 cd9660_time_915(diskStructure->rr_moved_dir->isoDirRecord->date,
1268 stampst.st_ino ? stampst.st_mtime : start_time.tv_sec);
1271 /* Create a file with the same ORIGINAL name */
1272 tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent,
1277 diskStructure->rock_ridge_move_count++;
1278 snprintf(newname, sizeof(newname), "%08i",
1279 diskStructure->rock_ridge_move_count);
1281 /* Point to old parent */
1282 dir->rr_real_parent = dir->parent;
1284 /* Place the placeholder file */
1285 if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
1286 TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
1289 cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
1292 /* Point to new parent */
1293 dir->parent = diskStructure->rr_moved_dir;
1295 /* Point the file to the moved directory */
1296 tfile->rr_relocated = dir;
1298 /* Actually move the directory */
1299 cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir);
1301 /* TODO: Inherit permissions / ownership (basically the entire inode) */
1303 /* Set the new name */
1304 memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1305 strncpy(dir->isoDirRecord->name, newname, 8);
1306 dir->isoDirRecord->length[0] = 34 + 8;
1307 dir->isoDirRecord->name_len[0] = 8;
1313 cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root)
1315 struct cd9660_children_head *head = &root->cn_children;
1318 TAILQ_FOREACH(cn, head, cn_next_child) {
1319 if ((cn->type & CD9660_TYPE_DIR) == 0)
1321 /* Recursion first */
1322 cd9660_add_dot_records(diskStructure, cn);
1324 cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root);
1325 cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT,
1331 * Convert node to cd9660 structure
1332 * This function is designed to be called recursively on the root node of
1334 * Lots of recursion going on here, want to make sure it is efficient
1335 * @param struct fsnode * The root node to be converted
1336 * @param struct cd9660* The parent node (should not be NULL)
1337 * @param int Current directory depth
1338 * @param int* Running count of the number of directories that are being created
1341 cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root,
1342 cd9660node *parent_node, int level, int *numDirectories, int *error)
1344 fsnode *iterator = root;
1345 cd9660node *this_node;
1352 * Newer, more efficient method, reduces recursion depth
1355 warnx("%s: root is null", __func__);
1359 /* Test for an empty directory - makefs still gives us the . record */
1360 if ((S_ISDIR(root->type)) && (root->name[0] == '.')
1361 && (root->name[1] == '\0')) {
1366 if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
1367 CD9660_MEM_ALLOC_ERROR(__func__);
1371 * To reduce the number of recursive calls, we will iterate over
1372 * the next pointers to the right.
1374 while (iterator != NULL) {
1377 * Increment the directory count if this is a directory
1378 * Ignore "." entries. We will generate them later
1380 if (!S_ISDIR(iterator->type) ||
1381 strcmp(iterator->name, ".") != 0) {
1383 /* Translate the node, including its filename */
1384 this_node->parent = parent_node;
1385 cd9660_translate_node(diskStructure, iterator,
1387 this_node->level = level;
1389 if (S_ISDIR(iterator->type)) {
1390 (*numDirectories)++;
1391 this_node->type = CD9660_TYPE_DIR;
1392 working_level = level + 1;
1395 * If at level 8, directory would be at 8
1396 * and have children at 9 which is not
1397 * allowed as per ISO spec
1400 if ((!diskStructure->allow_deep_trees) &&
1401 (!diskStructure->rock_ridge_enabled)) {
1402 warnx("error: found entry "
1403 "with depth greater "
1407 } else if (diskStructure->
1408 rock_ridge_enabled) {
1411 * Moved directory is actually
1416 if (cd9660_rrip_move_directory(
1418 this_node) == NULL) {
1430 /* Do the recursive call on the children */
1431 if (iterator->child != NULL) {
1432 cd9660_convert_structure(diskStructure,
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 != NULL) {
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(diskStructure, parent_node);
1475 flag = cd9660_handle_collisions(diskStructure, parent_node,
1478 cd9660_sorting_nodes(parent_node);
1479 } while ((flag == 1) && (counter < 100));
1483 * Clean up the cd9660node tree
1484 * This is designed to be called recursively on the root node
1485 * @param struct cd9660node *root The node to free
1489 cd9660_free_structure(cd9660node *root)
1493 while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
1494 TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
1495 cd9660_free_structure(cn);
1501 * Be a little more memory conservative:
1502 * instead of having the TAILQ_ENTRY as part of the cd9660node,
1503 * just create a temporary structure
1505 static struct ptq_entry
1507 TAILQ_ENTRY(ptq_entry) ptq;
1511 #define PTQUEUE_NEW(n,s,r,t){\
1512 n = emalloc(sizeof(struct s)); \
1517 * Generate the path tables
1518 * The specific implementation of this function is left as an exercise to the
1519 * programmer. It could be done recursively. Make sure you read how the path
1520 * table has to be laid out, it has levels.
1521 * @param struct iso9660_disk *disk The disk image
1522 * @returns int The number of built path tables (between 1 and 4), 0 on failure
1525 cd9660_generate_path_table(iso9660_disk *diskStructure)
1527 cd9660node *cn, *dirNode = diskStructure->rootNode;
1528 cd9660node *last = dirNode;
1529 int pathTableSize = 0; /* computed as we go */
1530 int counter = 1; /* root gets a count of 0 */
1532 TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
1533 TAILQ_INIT(&pt_head);
1535 PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode);
1537 /* Push the root node */
1538 TAILQ_INSERT_HEAD(&pt_head, n, ptq);
1540 /* Breadth-first traversal of file structure */
1541 while (pt_head.tqh_first != 0) {
1542 n = pt_head.tqh_first;
1544 TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
1547 /* Update the size */
1548 pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
1549 + dirNode->isoDirRecord->name_len[0]+
1550 (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
1551 /* includes the padding bit */
1553 dirNode->ptnumber=counter;
1554 if (dirNode != last) {
1555 last->ptnext = dirNode;
1556 dirNode->ptprev = last;
1560 /* Push children onto queue */
1561 TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
1563 * Dont add the DOT and DOTDOT types to the path
1566 if ((cn->type != CD9660_TYPE_DOT)
1567 && (cn->type != CD9660_TYPE_DOTDOT)) {
1569 if (S_ISDIR(cn->node->type)) {
1570 PTQUEUE_NEW(n, ptq_entry, -1, cn);
1571 TAILQ_INSERT_TAIL(&pt_head, n, ptq);
1577 return pathTableSize;
1581 cd9660_compute_full_filename(cd9660node *node, char *buf)
1585 len = CD9660MAXPATH + 1;
1586 len = snprintf(buf, len, "%s/%s/%s", node->node->root,
1587 node->node->path, node->node->name);
1588 if (len > CD9660MAXPATH)
1589 errx(EXIT_FAILURE, "Pathname too long.");
1592 /* NEW filename conversion method */
1593 typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *,
1598 * TODO: These two functions are almost identical.
1599 * Some code cleanup is possible here
1601 * XXX bounds checking!
1604 cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1605 char *newname, int is_file)
1609 * File Name shall not contain more than 8 d or d1 characters
1610 * File Name Extension shall not contain more than 3 d or d1 characters
1611 * Directory Identifier shall not contain more than 8 d or d1 characters
1617 while (*oldname != '\0' && extlen < 3) {
1618 /* Handle period first, as it is special */
1619 if (*oldname == '.') {
1629 /* cut RISC OS file type off ISO name */
1630 if (diskStructure->archimedes_enabled &&
1631 *oldname == ',' && strlen(oldname) == 4)
1634 /* Enforce 12.3 / 8 */
1635 if (namelen == 8 && !found_ext)
1638 if (islower((unsigned char)*oldname))
1639 *newname++ = toupper((unsigned char)*oldname);
1640 else if (isupper((unsigned char)*oldname)
1641 || isdigit((unsigned char)*oldname))
1642 *newname++ = *oldname;
1654 if (!found_ext && !diskStructure->omit_trailing_period)
1657 sprintf(newname, ";%i", 1);
1659 return namelen + extlen + found_ext;
1662 /* XXX bounds checking! */
1664 cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1665 char *newname, int is_file)
1669 * File name : 0+ d or d1 characters
1671 * File name extension : 0+ d or d1 characters
1673 * File version number (5 characters, 1-32767)
1674 * 1 <= Sum of File name and File name extension <= 30
1680 while (*oldname != '\0' && namelen + extlen < 30) {
1681 /* Handle period first, as it is special */
1682 if (*oldname == '.') {
1684 if (diskStructure->allow_multidot) {
1696 /* cut RISC OS file type off ISO name */
1697 if (diskStructure->archimedes_enabled &&
1698 *oldname == ',' && strlen(oldname) == 4)
1701 if (islower((unsigned char)*oldname))
1702 *newname++ = toupper((unsigned char)*oldname);
1703 else if (isupper((unsigned char)*oldname) ||
1704 isdigit((unsigned char)*oldname))
1705 *newname++ = *oldname;
1706 else if (diskStructure->allow_multidot &&
1721 if (!found_ext && !diskStructure->omit_trailing_period)
1724 sprintf(newname, ";%i", 1);
1726 return namelen + extlen + found_ext;
1731 cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1732 char *newname, int is_file)
1734 /* TODO: implement later, move to cd9660_joliet.c ?? */
1740 * Convert a file name to ISO compliant file name
1741 * @param char * oldname The original filename
1742 * @param char ** newname The new file name, in the appropriate character
1743 * set and of appropriate length
1744 * @param int 1 if file, 0 if directory
1745 * @returns int The length of the new string
1748 cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1749 char *newname, int is_file)
1751 assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2);
1753 cd9660_filename_conversion_functor conversion_function = NULL;
1754 if (diskStructure->isoLevel == 1)
1755 conversion_function = &cd9660_level1_convert_filename;
1756 else if (diskStructure->isoLevel == 2)
1757 conversion_function = &cd9660_level2_convert_filename;
1758 return (*conversion_function)(diskStructure, oldname, newname, is_file);
1762 cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node)
1764 int size = node->isoDirRecord->length[0];
1766 if (diskStructure->rock_ridge_enabled)
1767 size += node->susp_entry_size;
1768 size += node->su_tail_size;
1769 size += size & 1; /* Ensure length of record is even. */
1770 assert(size <= 254);
1775 cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node)
1777 node->dot_record->fileDataSector = node->fileDataSector;
1778 memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
1779 node->dot_record->isoDirRecord->name_len[0] = 1;
1780 node->dot_record->isoDirRecord->name[0] = 0;
1781 node->dot_record->isoDirRecord->name[1] = 0;
1782 node->dot_record->isoDirRecord->length[0] = 34;
1783 node->dot_record->fileRecordSize =
1784 cd9660_compute_record_size(diskStructure, node->dot_record);
1786 if (node == diskStructure->rootNode) {
1787 node->dot_dot_record->fileDataSector = node->fileDataSector;
1788 memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
1791 node->dot_dot_record->fileDataSector =
1792 node->parent->fileDataSector;
1793 memcpy(node->dot_dot_record->isoDirRecord,
1794 node->parent->isoDirRecord,34);
1796 node->dot_dot_record->isoDirRecord->name_len[0] = 1;
1797 node->dot_dot_record->isoDirRecord->name[0] = 1;
1798 node->dot_dot_record->isoDirRecord->name[1] = 0;
1799 node->dot_dot_record->isoDirRecord->length[0] = 34;
1800 node->dot_dot_record->fileRecordSize =
1801 cd9660_compute_record_size(diskStructure, node->dot_dot_record);
1805 * @param struct cd9660node *node The node
1806 * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
1807 * @returns int The total size of files and directory entries (should be
1808 * a multiple of sector size)
1811 cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node,
1812 int64_t startOffset)
1815 * This function needs to compute the size of directory records and
1816 * runs, file lengths, and set the appropriate variables both in
1817 * cd9660node and isoDirEntry
1819 int64_t used_bytes = 0;
1820 int64_t current_sector_usage = 0;
1825 assert(node != NULL);
1829 * NOTE : There needs to be some special case detection for
1830 * the "real root" node, since for it, node->node is undefined
1833 node->fileDataSector = -1;
1835 if (node->type & CD9660_TYPE_DIR) {
1836 node->fileRecordSize = cd9660_compute_record_size(
1837 diskStructure, node);
1838 /*Set what sector this directory starts in*/
1839 node->fileDataSector =
1840 CD9660_BLOCKS(diskStructure->sectorSize,startOffset);
1842 cd9660_bothendian_dword(node->fileDataSector,
1843 node->isoDirRecord->extent);
1846 * First loop over children, need to know the size of
1847 * their directory records
1849 node->fileSectorsUsed = 1;
1850 TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
1851 node->fileDataLength +=
1852 cd9660_compute_record_size(diskStructure, child);
1853 if ((cd9660_compute_record_size(diskStructure, child) +
1854 current_sector_usage) >=
1855 diskStructure->sectorSize) {
1856 current_sector_usage = 0;
1857 node->fileSectorsUsed++;
1860 current_sector_usage +=
1861 cd9660_compute_record_size(diskStructure, child);
1864 cd9660_bothendian_dword(node->fileSectorsUsed *
1865 diskStructure->sectorSize,node->isoDirRecord->size);
1868 * This should point to the sector after the directory
1869 * record (or, the first byte in that sector)
1871 used_bytes += node->fileSectorsUsed * diskStructure->sectorSize;
1873 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1874 child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
1875 /* Directories need recursive call */
1876 if (S_ISDIR(child->node->type)) {
1877 r = cd9660_compute_offsets(diskStructure, child,
1878 used_bytes + startOffset);
1887 /* Explicitly set the . and .. records */
1888 cd9660_populate_dot_records(diskStructure, node);
1890 /* Finally, do another iteration to write the file data*/
1891 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1893 child = TAILQ_NEXT(child, cn_next_child)) {
1894 /* Files need extent set */
1895 if (S_ISDIR(child->node->type))
1897 child->fileRecordSize =
1898 cd9660_compute_record_size(diskStructure, child);
1900 child->fileSectorsUsed =
1901 CD9660_BLOCKS(diskStructure->sectorSize,
1902 child->fileDataLength);
1904 inode = child->node->inode;
1905 if ((inode->flags & FI_ALLOCATED) == 0) {
1907 CD9660_BLOCKS(diskStructure->sectorSize,
1908 used_bytes + startOffset);
1909 inode->flags |= FI_ALLOCATED;
1910 used_bytes += child->fileSectorsUsed *
1911 diskStructure->sectorSize;
1913 INODE_WARNX(("%s: already allocated inode %d "
1914 "data sectors at %" PRIu32, __func__,
1915 (int)inode->st.st_ino, inode->ino));
1917 child->fileDataSector = inode->ino;
1918 cd9660_bothendian_dword(child->fileDataSector,
1919 child->isoDirRecord->extent);
1927 /* Might get rid of this func */
1929 cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
1931 to->node->inode->st.st_dev = 0;
1932 to->node->inode->st.st_ino = 0;
1933 to->node->inode->st.st_size = 0;
1934 to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
1935 to->node->inode->st.st_atime = from->node->inode->st.st_atime;
1936 to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
1937 to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
1938 to->node->inode->st.st_uid = from->node->inode->st.st_uid;
1939 to->node->inode->st.st_gid = from->node->inode->st.st_gid;
1940 to->node->inode->st.st_mode = from->node->inode->st.st_mode;
1941 /* Clear out type */
1942 to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
1944 to->node->inode->st.st_mode |= S_IFREG;
1946 to->node->inode->st.st_mode |= S_IFDIR;
1952 cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name,
1953 cd9660node *parent, int file, int insert)
1958 assert(parent != NULL);
1960 temp = cd9660_allocate_cd9660node();
1964 tfsnode = emalloc(sizeof(*tfsnode));
1965 tfsnode->name = estrdup(name);
1966 temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord));
1968 cd9660_convert_filename(diskStructure, tfsnode->name,
1969 temp->isoDirRecord->name, file);
1971 temp->node = tfsnode;
1972 temp->parent = parent;
1975 if (temp->parent != NULL) {
1976 temp->level = temp->parent->level + 1;
1977 if (!TAILQ_EMPTY(&temp->parent->cn_children))
1978 cd9660_sorted_child_insert(temp->parent, temp);
1980 TAILQ_INSERT_HEAD(&temp->parent->cn_children,
1981 temp, cn_next_child);
1985 if (parent->node != NULL) {
1986 tfsnode->type = parent->node->type;
1989 /* Clear out file type bits */
1990 tfsnode->type &= ~(S_IFMT);
1992 tfsnode->type |= S_IFREG;
1994 tfsnode->type |= S_IFDIR;
1996 /* Indicate that there is no spec entry (inode) */
1997 tfsnode->flags &= ~(FSNODE_F_HASSPEC);
1999 cd9660_copy_stat_info(parent, temp, file);
2005 cd9660_create_file(iso9660_disk *diskStructure, const char *name,
2006 cd9660node *parent, cd9660node *me)
2010 temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1);
2014 temp->fileDataLength = 0;
2016 temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
2018 temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
2019 *temp->node->inode = *me->node->inode;
2021 if (cd9660_translate_node_common(diskStructure, temp) == 0)
2027 * Create a new directory which does not exist on disk
2028 * @param const char * name The name to assign to the directory
2029 * @param const char * parent Pointer to the parent directory
2030 * @returns cd9660node * Pointer to the new directory
2033 cd9660_create_directory(iso9660_disk *diskStructure, const char *name,
2034 cd9660node *parent, cd9660node *me)
2038 temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1);
2041 temp->node->type |= S_IFDIR;
2043 temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
2045 temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
2046 *temp->node->inode = *me->node->inode;
2048 if (cd9660_translate_node_common(diskStructure, temp) == 0)
2054 cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type,
2057 cd9660node *temp, *first;
2060 assert(parent != NULL);
2062 if (type == CD9660_TYPE_DOT)
2064 else if (type == CD9660_TYPE_DOTDOT)
2070 if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent,
2074 temp->parent = parent;
2076 temp->isoDirRecord->length[0] = 34;
2077 /* Dot record is always first */
2078 if (type == CD9660_TYPE_DOT) {
2079 parent->dot_record = temp;
2080 TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
2081 /* DotDot should be second */
2082 } else if (type == CD9660_TYPE_DOTDOT) {
2083 parent->dot_dot_record = temp;
2085 * If the first child is the dot record, insert
2086 * this second. Otherwise, insert it at the head.
2088 if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
2089 (first->type & CD9660_TYPE_DOT) == 0) {
2090 TAILQ_INSERT_HEAD(&parent->cn_children, temp,
2093 TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
2102 cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage)
2106 assert(bootimage != NULL);
2108 if (*bootimage == '\0') {
2109 warnx("Error: Boot image must be a filename");
2113 diskStructure->generic_bootimage = estrdup(bootimage);
2115 /* Get information about the file */
2116 if (lstat(diskStructure->generic_bootimage, &stbuf) == -1)
2117 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
2118 diskStructure->generic_bootimage);
2120 if (stbuf.st_size > 32768) {
2121 warnx("Error: Boot image must be no greater than 32768 bytes");
2125 if (diskStructure->verbose_level > 0) {
2126 printf("Generic boot image has size %lld\n",
2127 (long long)stbuf.st_size);
2130 diskStructure->has_generic_bootimage = 1;