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