1224762Smarius/*	$NetBSD: cd9660.c,v 1.31 2011/08/06 23:25:19 christos Exp $	*/
2214921Scognet
3214921Scognet/*
4214921Scognet * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5214921Scognet * Perez-Rathke and Ram Vedam.  All rights reserved.
6214921Scognet *
7214921Scognet * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8214921Scognet * Alan Perez-Rathke and Ram Vedam.
9214921Scognet *
10214921Scognet * Redistribution and use in source and binary forms, with or
11214921Scognet * without modification, are permitted provided that the following
12214921Scognet * conditions are met:
13214921Scognet * 1. Redistributions of source code must retain the above copyright
14214921Scognet *    notice, this list of conditions and the following disclaimer.
15214921Scognet * 2. Redistributions in binary form must reproduce the above
16214921Scognet *    copyright notice, this list of conditions and the following
17214921Scognet *    disclaimer in the documentation and/or other materials provided
18214921Scognet *    with the distribution.
19214921Scognet *
20214921Scognet * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22214921Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23214921Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24214921Scognet * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26214921Scognet * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27214921Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28214921Scognet * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29214921Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30214921Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31214921Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32214921Scognet * OF SUCH DAMAGE.
33214921Scognet */
34214921Scognet/*
35214921Scognet * Copyright (c) 2001 Wasabi Systems, Inc.
36214921Scognet * All rights reserved.
37214921Scognet *
38214921Scognet * Written by Luke Mewburn for Wasabi Systems, Inc.
39214921Scognet *
40214921Scognet * Redistribution and use in source and binary forms, with or without
41214921Scognet * modification, are permitted provided that the following conditions
42214921Scognet * are met:
43214921Scognet * 1. Redistributions of source code must retain the above copyright
44214921Scognet *    notice, this list of conditions and the following disclaimer.
45214921Scognet * 2. Redistributions in binary form must reproduce the above copyright
46214921Scognet *    notice, this list of conditions and the following disclaimer in the
47214921Scognet *    documentation and/or other materials provided with the distribution.
48214921Scognet * 3. All advertising materials mentioning features or use of this software
49214921Scognet *    must display the following acknowledgement:
50214921Scognet *      This product includes software developed for the NetBSD Project by
51214921Scognet *      Wasabi Systems, Inc.
52214921Scognet * 4. The name of Wasabi Systems, Inc. may not be used to endorse
53214921Scognet *    or promote products derived from this software without specific prior
54214921Scognet *    written permission.
55214921Scognet *
56214921Scognet * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
57214921Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58214921Scognet * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59214921Scognet * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
60214921Scognet * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61214921Scognet * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62214921Scognet * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63214921Scognet * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64214921Scognet * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65214921Scognet * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66214921Scognet * POSSIBILITY OF SUCH DAMAGE.
67214921Scognet */
68214921Scognet/*
69214921Scognet * Copyright (c) 1982, 1986, 1989, 1993
70214921Scognet *	The Regents of the University of California.  All rights reserved.
71214921Scognet *
72214921Scognet * Redistribution and use in source and binary forms, with or without
73214921Scognet * modification, are permitted provided that the following conditions
74214921Scognet * are met:
75214921Scognet * 1. Redistributions of source code must retain the above copyright
76214921Scognet *    notice, this list of conditions and the following disclaimer.
77214921Scognet * 2. Redistributions in binary form must reproduce the above copyright
78214921Scognet *    notice, this list of conditions and the following disclaimer in the
79214921Scognet *    documentation and/or other materials provided with the distribution.
80214921Scognet * 3. Neither the name of the University nor the names of its contributors
81214921Scognet *    may be used to endorse or promote products derived from this software
82214921Scognet *    without specific prior written permission.
83214921Scognet *
84214921Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85214921Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86214921Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87214921Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88214921Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89214921Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90214921Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91214921Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92214921Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93214921Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94214921Scognet * SUCH DAMAGE.
95214921Scognet *
96214921Scognet  */
97214921Scognet
98214921Scognet#include <sys/cdefs.h>
99214921Scognet__FBSDID("$FreeBSD: stable/10/usr.sbin/makefs/cd9660.c 316198 2017-03-30 02:02:48Z sevan $");
100214921Scognet
101214921Scognet#include <string.h>
102214921Scognet#include <ctype.h>
103214921Scognet#include <sys/param.h>
104214921Scognet#include <sys/queue.h>
105214921Scognet
106214921Scognet#include "makefs.h"
107214921Scognet#include "cd9660.h"
108214921Scognet#include "cd9660/iso9660_rrip.h"
109214921Scognet#include "cd9660/cd9660_archimedes.h"
110214921Scognet
111214921Scognet/*
112214921Scognet * Global variables
113214921Scognet */
114214921Scognetiso9660_disk diskStructure;
115214921Scognet
116214921Scognetstatic void cd9660_finalize_PVD(void);
117214921Scognetstatic cd9660node *cd9660_allocate_cd9660node(void);
118214921Scognetstatic void cd9660_set_defaults(void);
119214921Scognetstatic int cd9660_arguments_set_string(const char *, const char *, int,
120214921Scognet    char, char *);
121214921Scognetstatic void cd9660_populate_iso_dir_record(
122214921Scognet    struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
123214921Scognet    const char *);
124214921Scognetstatic void cd9660_setup_root_node(void);
125214921Scognetstatic int cd9660_setup_volume_descriptors(void);
126214921Scognet#if 0
127214921Scognetstatic int cd9660_fill_extended_attribute_record(cd9660node *);
128214921Scognet#endif
129214921Scognetstatic void cd9660_sort_nodes(cd9660node *);
130224762Smariusstatic int cd9660_translate_node_common(cd9660node *);
131214921Scognetstatic int cd9660_translate_node(fsnode *, cd9660node *);
132214921Scognetstatic int cd9660_compare_filename(const char *, const char *);
133214921Scognetstatic void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
134214921Scognetstatic int cd9660_handle_collisions(cd9660node *, int);
135214921Scognetstatic cd9660node *cd9660_rename_filename(cd9660node *, int, int);
136214921Scognetstatic void cd9660_copy_filenames(cd9660node *);
137214921Scognetstatic void cd9660_sorting_nodes(cd9660node *);
138214921Scognetstatic int cd9660_count_collisions(cd9660node *);
139214921Scognetstatic cd9660node *cd9660_rrip_move_directory(cd9660node *);
140214921Scognetstatic int cd9660_add_dot_records(cd9660node *);
141214921Scognet
142214921Scognetstatic void cd9660_convert_structure(fsnode *, cd9660node *, int,
143214921Scognet    int *, int *);
144214921Scognetstatic void cd9660_free_structure(cd9660node *);
145214921Scognetstatic int cd9660_generate_path_table(void);
146214921Scognetstatic int cd9660_level1_convert_filename(const char *, char *, int);
147214921Scognetstatic int cd9660_level2_convert_filename(const char *, char *, int);
148214921Scognet#if 0
149214921Scognetstatic int cd9660_joliet_convert_filename(const char *, char *, int);
150214921Scognet#endif
151214921Scognetstatic int cd9660_convert_filename(const char *, char *, int);
152214921Scognetstatic void cd9660_populate_dot_records(cd9660node *);
153214921Scognetstatic int64_t cd9660_compute_offsets(cd9660node *, int64_t);
154214921Scognet#if 0
155214921Scognetstatic int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
156214921Scognet#endif
157214921Scognetstatic cd9660node *cd9660_create_virtual_entry(const char *, cd9660node *, int,
158214921Scognet    int);
159214921Scognetstatic cd9660node *cd9660_create_file(const char *, cd9660node *, cd9660node *);
160214921Scognetstatic cd9660node *cd9660_create_directory(const char *, cd9660node *,
161214921Scognet    cd9660node *);
162214921Scognetstatic cd9660node *cd9660_create_special_directory(u_char, cd9660node *);
163214921Scognet
164214921Scognet
165214921Scognet/*
166293290Sbdrewery * Allocate and initialize a cd9660node
167214921Scognet * @returns struct cd9660node * Pointer to new node, or NULL on error
168214921Scognet */
169214921Scognetstatic cd9660node *
170214921Scognetcd9660_allocate_cd9660node(void)
171214921Scognet{
172214921Scognet	cd9660node *temp;
173214921Scognet
174214921Scognet	if ((temp = calloc(1, sizeof(cd9660node))) == NULL)
175214921Scognet		err(EXIT_FAILURE, "%s: calloc", __func__);
176214921Scognet	TAILQ_INIT(&temp->cn_children);
177214921Scognet	temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
178214921Scognet	temp->ptnext = temp->ptprev = temp->ptlast = NULL;
179214921Scognet	temp->node = NULL;
180214921Scognet	temp->isoDirRecord = NULL;
181214921Scognet	temp->isoExtAttributes = NULL;
182214921Scognet	temp->rr_real_parent = temp->rr_relocated = NULL;
183214921Scognet	temp->su_tail_data = NULL;
184214921Scognet	return temp;
185214921Scognet}
186214921Scognet
187214921Scognetint cd9660_defaults_set = 0;
188214921Scognet
189214921Scognet/**
190214921Scognet* Set default values for cd9660 extension to makefs
191214921Scognet*/
192214921Scognetstatic void
193214921Scognetcd9660_set_defaults(void)
194214921Scognet{
195214921Scognet	/*Fix the sector size for now, though the spec allows for other sizes*/
196214921Scognet	diskStructure.sectorSize = 2048;
197214921Scognet
198214921Scognet	/* Set up defaults in our own structure */
199214921Scognet	diskStructure.verbose_level = 0;
200214921Scognet	diskStructure.keep_bad_images = 0;
201214921Scognet	diskStructure.follow_sym_links = 0;
202214921Scognet	diskStructure.isoLevel = 2;
203214921Scognet
204214921Scognet	diskStructure.rock_ridge_enabled = 0;
205214921Scognet	diskStructure.rock_ridge_renamed_dir_name = 0;
206214921Scognet	diskStructure.rock_ridge_move_count = 0;
207214921Scognet	diskStructure.rr_moved_dir = 0;
208214921Scognet
209214921Scognet	diskStructure.archimedes_enabled = 0;
210222191Snwhitehorn	diskStructure.chrp_boot = 0;
211214921Scognet
212214921Scognet	diskStructure.include_padding_areas = 1;
213214921Scognet
214214921Scognet	/* Spec breaking functionality */
215214921Scognet	diskStructure.allow_deep_trees =
216214921Scognet	    diskStructure.allow_start_dot =
217214921Scognet	    diskStructure.allow_max_name =
218214921Scognet	    diskStructure.allow_illegal_chars =
219214921Scognet	    diskStructure.allow_lowercase =
220214921Scognet	    diskStructure.allow_multidot =
221214921Scognet	    diskStructure.omit_trailing_period = 0;
222214921Scognet
223214921Scognet	/* Make sure the PVD is clear */
224214921Scognet	memset(&diskStructure.primaryDescriptor, 0, 2048);
225214921Scognet
226214921Scognet	memset(diskStructure.primaryDescriptor.publisher_id,	0x20,128);
227214921Scognet	memset(diskStructure.primaryDescriptor.preparer_id,	0x20,128);
228214921Scognet	memset(diskStructure.primaryDescriptor.application_id,	0x20,128);
229224690Smm	memset(diskStructure.primaryDescriptor.copyright_file_id, 0x20,37);
230224690Smm	memset(diskStructure.primaryDescriptor.abstract_file_id, 0x20,37);
231224690Smm	memset(diskStructure.primaryDescriptor.bibliographic_file_id, 0x20,37);
232214921Scognet
233253707Smarius	strcpy(diskStructure.primaryDescriptor.system_id, "FreeBSD");
234214921Scognet
235214921Scognet	cd9660_defaults_set = 1;
236214921Scognet
237214921Scognet	/* Boot support: Initially disabled */
238214921Scognet	diskStructure.has_generic_bootimage = 0;
239214921Scognet	diskStructure.generic_bootimage = NULL;
240214921Scognet
241214921Scognet	diskStructure.boot_image_directory = 0;
242214921Scognet	/*memset(diskStructure.boot_descriptor, 0, 2048);*/
243214921Scognet
244214921Scognet	diskStructure.is_bootable = 0;
245214921Scognet	TAILQ_INIT(&diskStructure.boot_images);
246214921Scognet	LIST_INIT(&diskStructure.boot_entries);
247214921Scognet}
248214921Scognet
249214921Scognetvoid
250214921Scognetcd9660_prep_opts(fsinfo_t *fsopts __unused)
251214921Scognet{
252214921Scognet	cd9660_set_defaults();
253214921Scognet}
254214921Scognet
255214921Scognetvoid
256214921Scognetcd9660_cleanup_opts(fsinfo_t *fsopts __unused)
257214921Scognet{
258214921Scognet
259214921Scognet}
260214921Scognet
261214921Scognetstatic int
262214921Scognetcd9660_arguments_set_string(const char *val, const char *fieldtitle, int length,
263214921Scognet			    char testmode, char * dest)
264214921Scognet{
265214921Scognet	int len, test;
266214921Scognet
267214921Scognet	if (val == NULL)
268214921Scognet		warnx("error: The %s requires a string argument", fieldtitle);
269214921Scognet	else if ((len = strlen(val)) <= length) {
270214921Scognet		if (testmode == 'd')
271214921Scognet			test = cd9660_valid_d_chars(val);
272214921Scognet		else
273214921Scognet			test = cd9660_valid_a_chars(val);
274214921Scognet		if (test) {
275214921Scognet			memcpy(dest, val, len);
276214921Scognet			if (test == 2)
277214921Scognet				cd9660_uppercase_characters(dest, len);
278214921Scognet			return 1;
279214921Scognet		} else
280214921Scognet			warnx("error: The %s must be composed of "
281214921Scognet			      "%c-characters", fieldtitle, testmode);
282214921Scognet	} else
283214921Scognet		warnx("error: The %s must be at most 32 characters long",
284214921Scognet		    fieldtitle);
285214921Scognet	return 0;
286214921Scognet}
287214921Scognet
288214921Scognet/*
289214921Scognet * Command-line parsing function
290214921Scognet */
291214921Scognet
292214921Scognetint
293214921Scognetcd9660_parse_opts(const char *option, fsinfo_t *fsopts)
294214921Scognet{
295214921Scognet	char *var, *val;
296214921Scognet	int	rv;
297214921Scognet	/* Set up allowed options - integer options ONLY */
298214921Scognet	option_t cd9660_options[] = {
299290595Sngie		{ "l", &diskStructure.isoLevel, 1, 2, "ISO Level" },
300290595Sngie		{ "isolevel", &diskStructure.isoLevel, 1, 2, "ISO Level" },
301214921Scognet		{ "verbose",  &diskStructure.verbose_level, 0, 2,
302214921Scognet		  "Turns on verbose output" },
303214921Scognet		{ "v", &diskStructure.verbose_level, 0 , 2,
304214921Scognet		  "Turns on verbose output"},
305214921Scognet		{ .name = NULL }
306214921Scognet	};
307214921Scognet
308214921Scognet	if (cd9660_defaults_set == 0)
309214921Scognet		cd9660_set_defaults();
310214921Scognet
311214921Scognet	/*
312214921Scognet	 * Todo : finish implementing this, and make a function that
313214921Scognet	 * parses them
314214921Scognet	 */
315214921Scognet	/*
316214921Scognet	string_option_t cd9660_string_options[] = {
317214921Scognet		{ "L", "Label", &diskStructure.primaryDescriptor.volume_id, 1, 32, "Disk Label", ISO_STRING_FILTER_DCHARS },
318214921Scognet		{ NULL }
319214921Scognet	}
320214921Scognet	*/
321214921Scognet
322214921Scognet	assert(option != NULL);
323214921Scognet
324214921Scognet	if (debug & DEBUG_FS_PARSE_OPTS)
325214921Scognet		printf("cd9660_parse_opts: got `%s'\n", option);
326214921Scognet
327214921Scognet	if ((var = strdup(option)) == NULL)
328214921Scognet		err(1, "allocating memory for copy of option string");
329214921Scognet	rv = 1;
330214921Scognet
331214921Scognet	val = strchr(var, '=');
332214921Scognet	if (val != NULL)
333214921Scognet		*val++ = '\0';
334214921Scognet
335214921Scognet	/* First handle options with no parameters */
336214921Scognet	if (strcmp(var, "h") == 0) {
337214921Scognet		diskStructure.displayHelp = 1;
338214921Scognet		rv = 1;
339214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "S", "follow-symlinks")) {
340214921Scognet		/* this is not handled yet */
341214921Scognet		diskStructure.follow_sym_links = 1;
342214921Scognet		rv = 1;
343214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "L", "label")) {
344214921Scognet		rv = cd9660_arguments_set_string(val, "Disk Label", 32, 'd',
345214921Scognet			diskStructure.primaryDescriptor.volume_id);
346214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "A", "applicationid")) {
347214921Scognet		rv = cd9660_arguments_set_string(val, "Application Identifier", 128, 'a',
348214921Scognet			diskStructure.primaryDescriptor.application_id);
349214921Scognet	} else if(CD9660_IS_COMMAND_ARG_DUAL(var, "P", "publisher")) {
350214921Scognet		rv = cd9660_arguments_set_string(val, "Publisher Identifier",
351214921Scognet			128, 'a', diskStructure.primaryDescriptor.publisher_id);
352214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "p", "preparer")) {
353214921Scognet		rv = cd9660_arguments_set_string(val, "Preparer Identifier",
354214921Scognet		    128, 'a', diskStructure.primaryDescriptor.preparer_id);
355214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "V", "volumeid")) {
356214921Scognet		rv = cd9660_arguments_set_string(val, "Volume Set Identifier",
357214921Scognet		    128, 'a', diskStructure.primaryDescriptor.volume_set_id);
358214921Scognet	/* Boot options */
359214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "B", "bootimage")) {
360214921Scognet		if (val == NULL)
361214921Scognet			warnx("error: The Boot Image parameter requires a valid boot information string");
362214921Scognet		else
363214921Scognet			rv = cd9660_add_boot_disk(val);
364214921Scognet	} else if (CD9660_IS_COMMAND_ARG(var, "bootimagedir")) {
365214921Scognet		/*
366214921Scognet		 * XXXfvdl this is unused.
367214921Scognet		 */
368214921Scognet		if (val == NULL)
369214921Scognet			errx(1, "error: The Boot Image Directory parameter"
370214921Scognet			     " requires a directory name\n");
371214921Scognet		else {
372214921Scognet			if ((diskStructure.boot_image_directory =
373214921Scognet			     malloc(strlen(val) + 1)) == NULL) {
374214921Scognet				CD9660_MEM_ALLOC_ERROR("cd9660_parse_opts");
375214921Scognet				exit(1);
376214921Scognet			}
377214921Scognet
378214921Scognet			/* BIG TODO: Add the max length function here */
379214921Scognet			cd9660_arguments_set_string(val, "Boot Image Directory",
380214921Scognet			    12 , 'd', diskStructure.boot_image_directory);
381214921Scognet		}
382214921Scognet	} else if (CD9660_IS_COMMAND_ARG_DUAL(var, "G", "generic-bootimage")) {
383214921Scognet		if (val == NULL)
384214921Scognet			warnx("error: The Boot Image parameter requires a valid boot information string");
385214921Scognet		else
386214921Scognet			rv = cd9660_add_generic_bootimage(val);
387214921Scognet	} else if (CD9660_IS_COMMAND_ARG(var, "no-trailing-padding"))
388214921Scognet		diskStructure.include_padding_areas = 0;
389214921Scognet	/* RRIP */
390214921Scognet	else if (CD9660_IS_COMMAND_ARG_DUAL(var, "R", "rockridge"))
391214921Scognet		diskStructure.rock_ridge_enabled = 1;
392214921Scognet	else if (CD9660_IS_COMMAND_ARG_DUAL(var, "A", "archimedes"))
393214921Scognet		diskStructure.archimedes_enabled = 1;
394222191Snwhitehorn	else if (CD9660_IS_COMMAND_ARG(var, "chrp-boot"))
395222191Snwhitehorn		diskStructure.chrp_boot = 1;
396214921Scognet	else if (CD9660_IS_COMMAND_ARG_DUAL(var, "K", "keep-bad-images"))
397214921Scognet		diskStructure.keep_bad_images = 1;
398214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "allow-deep-trees"))
399214921Scognet		diskStructure.allow_deep_trees = 1;
400214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "allow-max-name"))
401214921Scognet		diskStructure.allow_max_name = 1;
402214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "allow-illegal-chars"))
403214921Scognet		diskStructure.allow_illegal_chars = 1;
404214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "allow-lowercase"))
405214921Scognet		diskStructure.allow_lowercase = 1;
406214921Scognet	else if (CD9660_IS_COMMAND_ARG(var,"allow-multidot"))
407214921Scognet		diskStructure.allow_multidot = 1;
408214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "omit-trailing-period"))
409214921Scognet		diskStructure.omit_trailing_period = 1;
410214921Scognet	else if (CD9660_IS_COMMAND_ARG(var, "no-emul-boot") ||
411214921Scognet		 CD9660_IS_COMMAND_ARG(var, "no-boot") ||
412214921Scognet		 CD9660_IS_COMMAND_ARG(var, "hard-disk-boot")) {
413214921Scognet		cd9660_eltorito_add_boot_option(var, 0);
414214921Scognet
415214921Scognet		/* End of flag variables */
416214921Scognet        } else if (CD9660_IS_COMMAND_ARG(var, "boot-load-segment")) {
417214921Scognet		if (val == NULL) {
418214921Scognet			warnx("Option `%s' doesn't contain a value", var);
419214921Scognet			rv = 0;
420214921Scognet		} else {
421214921Scognet			cd9660_eltorito_add_boot_option(var, val);
422214921Scognet		}
423214921Scognet	} else {
424214921Scognet		if (val == NULL) {
425214921Scognet			warnx("Option `%s' doesn't contain a value", var);
426214921Scognet			rv = 0;
427214921Scognet		} else
428214921Scognet			rv = set_option(cd9660_options, var, val);
429214921Scognet	}
430214921Scognet
431289989Sngie	free(var);
432214921Scognet	return (rv);
433214921Scognet}
434214921Scognet
435214921Scognet/*
436214921Scognet * Main function for cd9660_makefs
437214921Scognet * Builds the ISO image file
438214921Scognet * @param const char *image The image filename to create
439214921Scognet * @param const char *dir The directory that is being read
440214921Scognet * @param struct fsnode *root The root node of the filesystem tree
441214921Scognet * @param struct fsinfo_t *fsopts Any options
442214921Scognet */
443214921Scognetvoid
444214921Scognetcd9660_makefs(const char *image, const char *dir, fsnode *root,
445214921Scognet	      fsinfo_t *fsopts)
446214921Scognet{
447214921Scognet	int64_t startoffset;
448214921Scognet	int numDirectories;
449214921Scognet	uint64_t pathTableSectors;
450214921Scognet	int64_t firstAvailableSector;
451214921Scognet	int64_t totalSpace;
452214921Scognet	int error;
453214921Scognet	cd9660node *real_root;
454214921Scognet
455214921Scognet	if (diskStructure.verbose_level > 0)
456214921Scognet		printf("cd9660_makefs: ISO level is %i\n",
457214921Scognet		    diskStructure.isoLevel);
458214921Scognet	if (diskStructure.isoLevel < 2 &&
459214921Scognet	    diskStructure.allow_multidot)
460214921Scognet		errx(1, "allow-multidot requires iso level of 2\n");
461214921Scognet
462214921Scognet	assert(image != NULL);
463214921Scognet	assert(dir != NULL);
464214921Scognet	assert(root != NULL);
465214921Scognet
466214921Scognet	if (diskStructure.displayHelp) {
467214921Scognet		/*
468214921Scognet		 * Display help here - probably want to put it in
469214921Scognet		 * a separate function
470214921Scognet		 */
471214921Scognet		return;
472214921Scognet	}
473214921Scognet
474214921Scognet	if (diskStructure.verbose_level > 0)
475214921Scognet		printf("cd9660_makefs: image %s directory %s root %p\n",
476214921Scognet		    image, dir, root);
477214921Scognet
478214921Scognet	/* Set up some constants. Later, these will be defined with options */
479214921Scognet
480214921Scognet	/* Counter needed for path tables */
481214921Scognet	numDirectories = 0;
482214921Scognet
483214921Scognet	/* Convert tree to our own format */
484214921Scognet	/* Actually, we now need to add the REAL root node, at level 0 */
485214921Scognet
486214921Scognet	real_root = cd9660_allocate_cd9660node();
487214921Scognet	if ((real_root->isoDirRecord =
488214921Scognet		malloc( sizeof(iso_directory_record_cd9660) )) == NULL) {
489214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_makefs");
490214921Scognet		exit(1);
491214921Scognet	}
492214921Scognet
493214921Scognet	/* Leave filename blank for root */
494214921Scognet	memset(real_root->isoDirRecord->name, 0,
495214921Scognet	    ISO_FILENAME_MAXLENGTH_WITH_PADDING);
496214921Scognet
497214921Scognet	real_root->level = 0;
498214921Scognet	diskStructure.rootNode = real_root;
499214921Scognet	real_root->type = CD9660_TYPE_DIR;
500214921Scognet	error = 0;
501214921Scognet	real_root->node = root;
502214921Scognet	cd9660_convert_structure(root, real_root, 1, &numDirectories, &error);
503214921Scognet
504214921Scognet	if (TAILQ_EMPTY(&real_root->cn_children)) {
505214921Scognet		errx(1, "cd9660_makefs: converted directory is empty. "
506214921Scognet			"Tree conversion failed\n");
507214921Scognet	} else if (error != 0) {
508214921Scognet		errx(1, "cd9660_makefs: tree conversion failed\n");
509214921Scognet	} else {
510214921Scognet		if (diskStructure.verbose_level > 0)
511214921Scognet			printf("cd9660_makefs: tree converted\n");
512214921Scognet	}
513214921Scognet
514214921Scognet	/* Add the dot and dot dot records */
515214921Scognet	cd9660_add_dot_records(real_root);
516214921Scognet
517214921Scognet	cd9660_setup_root_node();
518214921Scognet
519214921Scognet	if (diskStructure.verbose_level > 0)
520214921Scognet		printf("cd9660_makefs: done converting tree\n");
521214921Scognet
522214921Scognet	/* non-SUSP extensions */
523214921Scognet	if (diskStructure.archimedes_enabled)
524214921Scognet		archimedes_convert_tree(diskStructure.rootNode);
525214921Scognet
526214921Scognet	/* Rock ridge / SUSP init pass */
527214921Scognet	if (diskStructure.rock_ridge_enabled) {
528214921Scognet		cd9660_susp_initialize(diskStructure.rootNode,
529214921Scognet		    diskStructure.rootNode, NULL);
530214921Scognet	}
531214921Scognet
532214921Scognet	/* Build path table structure */
533214921Scognet	diskStructure.pathTableLength = cd9660_generate_path_table();
534214921Scognet
535214921Scognet	pathTableSectors = CD9660_BLOCKS(diskStructure.sectorSize,
536214921Scognet		diskStructure.pathTableLength);
537214921Scognet
538214921Scognet	firstAvailableSector = cd9660_setup_volume_descriptors();
539214921Scognet	if (diskStructure.is_bootable) {
540214921Scognet		firstAvailableSector = cd9660_setup_boot(firstAvailableSector);
541214921Scognet		if (firstAvailableSector < 0)
542214921Scognet			errx(1, "setup_boot failed");
543214921Scognet	}
544214921Scognet	/* LE first, then BE */
545214921Scognet	diskStructure.primaryLittleEndianTableSector = firstAvailableSector;
546214921Scognet	diskStructure.primaryBigEndianTableSector =
547214921Scognet		diskStructure.primaryLittleEndianTableSector + pathTableSectors;
548214921Scognet
549214921Scognet	/* Set the secondary ones to -1, not going to use them for now */
550214921Scognet	diskStructure.secondaryBigEndianTableSector = -1;
551214921Scognet	diskStructure.secondaryLittleEndianTableSector = -1;
552214921Scognet
553214921Scognet	diskStructure.dataFirstSector =
554214921Scognet	    diskStructure.primaryBigEndianTableSector + pathTableSectors;
555214921Scognet	if (diskStructure.verbose_level > 0)
556214921Scognet		printf("cd9660_makefs: Path table conversion complete. "
557214921Scognet		       "Each table is %i bytes, or %" PRIu64 " sectors.\n",
558214921Scognet		    diskStructure.pathTableLength, pathTableSectors);
559214921Scognet
560214921Scognet	startoffset = diskStructure.sectorSize*diskStructure.dataFirstSector;
561214921Scognet
562214921Scognet	totalSpace = cd9660_compute_offsets(real_root, startoffset);
563214921Scognet
564214921Scognet	diskStructure.totalSectors = diskStructure.dataFirstSector +
565214921Scognet		CD9660_BLOCKS(diskStructure.sectorSize, totalSpace);
566214921Scognet
567214921Scognet	/* Disabled until pass 1 is done */
568214921Scognet	if (diskStructure.rock_ridge_enabled) {
569214921Scognet		diskStructure.susp_continuation_area_start_sector =
570214921Scognet		    diskStructure.totalSectors;
571214921Scognet		diskStructure.totalSectors +=
572214921Scognet		    CD9660_BLOCKS(diskStructure.sectorSize,
573214921Scognet			diskStructure.susp_continuation_area_size);
574214921Scognet		cd9660_susp_finalize(diskStructure.rootNode);
575214921Scognet	}
576214921Scognet
577214921Scognet
578214921Scognet	cd9660_finalize_PVD();
579214921Scognet
580214921Scognet	/* Add padding sectors, just for testing purposes right now */
581214921Scognet	/* diskStructure.totalSectors+=150; */
582214921Scognet
583214921Scognet	/* Debugging output */
584214921Scognet	if (diskStructure.verbose_level > 0) {
585214921Scognet		printf("cd9660_makefs: Sectors 0-15 reserved\n");
586214921Scognet		printf("cd9660_makefs: Primary path tables starts in sector %"
587214921Scognet		    PRId64 "\n", diskStructure.primaryLittleEndianTableSector);
588214921Scognet		printf("cd9660_makefs: File data starts in sector %"
589214921Scognet		    PRId64 "\n", diskStructure.dataFirstSector);
590214921Scognet		printf("cd9660_makefs: Total sectors: %"
591214921Scognet		    PRId64 "\n", diskStructure.totalSectors);
592214921Scognet	}
593214921Scognet
594214921Scognet	/*
595214921Scognet	 * Add padding sectors at the end
596214921Scognet	 * TODO: Clean this up and separate padding
597214921Scognet	 */
598214921Scognet	if (diskStructure.include_padding_areas)
599214921Scognet		diskStructure.totalSectors += 150;
600214921Scognet
601214921Scognet	cd9660_write_image(image);
602214921Scognet
603214921Scognet	if (diskStructure.verbose_level > 1) {
604214921Scognet		debug_print_volume_descriptor_information();
605214921Scognet		debug_print_tree(real_root,0);
606214921Scognet		debug_print_path_tree(real_root);
607214921Scognet	}
608214921Scognet
609214921Scognet	/* Clean up data structures */
610214921Scognet	cd9660_free_structure(real_root);
611214921Scognet
612214921Scognet	if (diskStructure.verbose_level > 0)
613214921Scognet		printf("cd9660_makefs: done\n");
614214921Scognet}
615214921Scognet
616214921Scognet/* Generic function pointer - implement later */
617214921Scognettypedef int (*cd9660node_func)(cd9660node *);
618214921Scognet
619214921Scognetstatic void
620214921Scognetcd9660_finalize_PVD(void)
621214921Scognet{
622214921Scognet	time_t tim;
623214921Scognet
624214921Scognet	/* root should be a fixed size of 34 bytes since it has no name */
625214921Scognet	memcpy(diskStructure.primaryDescriptor.root_directory_record,
626214921Scognet		diskStructure.rootNode->dot_record->isoDirRecord, 34);
627214921Scognet
628214921Scognet	/* In RRIP, this might be longer than 34 */
629214921Scognet	diskStructure.primaryDescriptor.root_directory_record[0] = 34;
630214921Scognet
631214921Scognet	/* Set up all the important numbers in the PVD */
632214921Scognet	cd9660_bothendian_dword(diskStructure.totalSectors,
633214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.volume_space_size);
634214921Scognet	cd9660_bothendian_word(1,
635214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.volume_set_size);
636214921Scognet	cd9660_bothendian_word(1,
637214921Scognet	    (unsigned char *)
638214921Scognet		diskStructure.primaryDescriptor.volume_sequence_number);
639214921Scognet	cd9660_bothendian_word(diskStructure.sectorSize,
640214921Scognet	    (unsigned char *)
641214921Scognet		diskStructure.primaryDescriptor.logical_block_size);
642214921Scognet	cd9660_bothendian_dword(diskStructure.pathTableLength,
643214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.path_table_size);
644214921Scognet
645214921Scognet	cd9660_731(diskStructure.primaryLittleEndianTableSector,
646214921Scognet		(u_char *)diskStructure.primaryDescriptor.type_l_path_table);
647214921Scognet	cd9660_732(diskStructure.primaryBigEndianTableSector,
648214921Scognet		(u_char *)diskStructure.primaryDescriptor.type_m_path_table);
649214921Scognet
650214921Scognet	diskStructure.primaryDescriptor.file_structure_version[0] = 1;
651214921Scognet
652214921Scognet	/* Pad all strings with spaces instead of nulls */
653214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_id, 32);
654214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.system_id, 32);
655214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_set_id,
656214921Scognet	    128);
657214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.publisher_id,
658214921Scognet	    128);
659214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.preparer_id,
660214921Scognet	    128);
661214921Scognet	cd9660_pad_string_spaces(diskStructure.primaryDescriptor.application_id,
662214921Scognet	    128);
663214921Scognet	cd9660_pad_string_spaces(
664224690Smm	    diskStructure.primaryDescriptor.copyright_file_id, 37);
665214921Scognet	cd9660_pad_string_spaces(
666224690Smm		diskStructure.primaryDescriptor.abstract_file_id, 37);
667214921Scognet	cd9660_pad_string_spaces(
668224690Smm		diskStructure.primaryDescriptor.bibliographic_file_id, 37);
669214921Scognet
670214921Scognet	/* Setup dates */
671214921Scognet	time(&tim);
672214921Scognet	cd9660_time_8426(
673214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.creation_date,
674214921Scognet	    tim);
675214921Scognet	cd9660_time_8426(
676214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.modification_date,
677214921Scognet	    tim);
678214921Scognet
679214921Scognet	/*
680214921Scognet	cd9660_set_date(diskStructure.primaryDescriptor.expiration_date, now);
681214921Scognet	*/
682214921Scognet
683253707Smarius	memset(diskStructure.primaryDescriptor.expiration_date, '0', 16);
684253707Smarius	diskStructure.primaryDescriptor.expiration_date[16] = 0;
685214921Scognet	cd9660_time_8426(
686214921Scognet	    (unsigned char *)diskStructure.primaryDescriptor.effective_date,
687214921Scognet	    tim);
688214921Scognet}
689214921Scognet
690214921Scognetstatic void
691214921Scognetcd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
692214921Scognet			       u_char ext_attr_length, u_char flags,
693214921Scognet			       u_char name_len, const char * name)
694214921Scognet{
695214921Scognet	record->ext_attr_length[0] = ext_attr_length;
696214921Scognet	record->flags[0] = ISO_FLAG_CLEAR | flags;
697214921Scognet	record->file_unit_size[0] = 0;
698214921Scognet	record->interleave[0] = 0;
699214921Scognet	cd9660_bothendian_word(1, record->volume_sequence_number);
700214921Scognet	record->name_len[0] = name_len;
701214921Scognet	memset(record->name, '\0', sizeof (record->name));
702214921Scognet	memcpy(record->name, name, name_len);
703214921Scognet	record->length[0] = 33 + name_len;
704214921Scognet
705214921Scognet	/* Todo : better rounding */
706214921Scognet	record->length[0] += (record->length[0] & 1) ? 1 : 0;
707214921Scognet}
708214921Scognet
709214921Scognetstatic void
710214921Scognetcd9660_setup_root_node(void)
711214921Scognet{
712214921Scognet	cd9660_populate_iso_dir_record(diskStructure.rootNode->isoDirRecord,
713214921Scognet	    0, ISO_FLAG_DIRECTORY, 1, "\0");
714214921Scognet
715214921Scognet}
716214921Scognet
717214921Scognet/*********** SUPPORT FUNCTIONS ***********/
718214921Scognetstatic int
719214921Scognetcd9660_setup_volume_descriptors(void)
720214921Scognet{
721214921Scognet	/* Boot volume descriptor should come second */
722214921Scognet	int sector = 16;
723214921Scognet	/* For now, a fixed 2 : PVD and terminator */
724214921Scognet	volume_descriptor *temp, *t;
725214921Scognet
726214921Scognet	/* Set up the PVD */
727214921Scognet	if ((temp = malloc(sizeof(volume_descriptor))) == NULL) {
728214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
729214921Scognet		exit(1);
730214921Scognet	}
731214921Scognet
732214921Scognet	temp->volumeDescriptorData =
733214921Scognet	   (unsigned char *)&diskStructure.primaryDescriptor;
734214921Scognet	temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
735214921Scognet	temp->volumeDescriptorData[6] = 1;
736214921Scognet	temp->sector = sector;
737214921Scognet	memcpy(temp->volumeDescriptorData + 1,
738214921Scognet	    ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
739214921Scognet	diskStructure.firstVolumeDescriptor = temp;
740214921Scognet
741214921Scognet	sector++;
742214921Scognet	/* Set up boot support if enabled. BVD must reside in sector 17 */
743214921Scognet	if (diskStructure.is_bootable) {
744214921Scognet		if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
745214921Scognet			CD9660_MEM_ALLOC_ERROR(
746214921Scognet			    "cd9660_setup_volume_descriptors");
747214921Scognet			exit(1);
748214921Scognet		}
749214921Scognet		if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
750214921Scognet			CD9660_MEM_ALLOC_ERROR(
751214921Scognet			    "cd9660_setup_volume_descriptors");
752214921Scognet			exit(1);
753214921Scognet		}
754214921Scognet		temp->next = t;
755214921Scognet		temp = t;
756214921Scognet		memset(t->volumeDescriptorData, 0, 2048);
757214921Scognet		t->sector = 17;
758214921Scognet		if (diskStructure.verbose_level > 0)
759214921Scognet			printf("Setting up boot volume descriptor\n");
760214921Scognet		cd9660_setup_boot_volume_descriptor(t);
761214921Scognet		sector++;
762214921Scognet	}
763214921Scognet
764214921Scognet	/* Set up the terminator */
765214921Scognet	if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
766214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
767214921Scognet		exit(1);
768214921Scognet	}
769214921Scognet	if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
770214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
771214921Scognet		exit(1);
772214921Scognet	}
773214921Scognet
774214921Scognet	temp->next = t;
775214921Scognet	memset(t->volumeDescriptorData, 0, 2048);
776214921Scognet	t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
777214921Scognet	t->next = 0;
778214921Scognet	t->volumeDescriptorData[6] = 1;
779214921Scognet	t->sector = sector;
780214921Scognet	memcpy(t->volumeDescriptorData + 1,
781214921Scognet	    ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
782214921Scognet
783214921Scognet	sector++;
784214921Scognet	return sector;
785214921Scognet}
786214921Scognet
787214921Scognet#if 0
788214921Scognet/*
789214921Scognet * Populate EAR at some point. Not required, but is used by NetBSD's
790214921Scognet * cd9660 support
791214921Scognet */
792214921Scognetstatic int
793214921Scognetcd9660_fill_extended_attribute_record(cd9660node *node)
794214921Scognet{
795214921Scognet	if ((node->isoExtAttributes =
796214921Scognet	    malloc(sizeof(struct iso_extended_attributes))) == NULL) {
797214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_fill_extended_attribute_record");
798214921Scognet		exit(1);
799214921Scognet	};
800214921Scognet
801214921Scognet	return 1;
802214921Scognet}
803214921Scognet#endif
804214921Scognet
805214921Scognetstatic int
806224762Smariuscd9660_translate_node_common(cd9660node *newnode)
807214921Scognet{
808214921Scognet	time_t tim;
809214921Scognet	int test;
810214921Scognet	u_char flag;
811214921Scognet	char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
812214921Scognet
813214921Scognet	/* Now populate the isoDirRecord structure */
814214921Scognet	memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
815214921Scognet
816214921Scognet	test = cd9660_convert_filename(newnode->node->name,
817214921Scognet		temp, !(S_ISDIR(newnode->node->type)));
818214921Scognet
819214921Scognet	flag = ISO_FLAG_CLEAR;
820214921Scognet	if (S_ISDIR(newnode->node->type))
821214921Scognet		flag |= ISO_FLAG_DIRECTORY;
822214921Scognet
823214921Scognet	cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
824214921Scognet	    flag, strlen(temp), temp);
825214921Scognet
826214921Scognet	/* Set the various dates */
827214921Scognet
828214921Scognet	/* If we want to use the current date and time */
829214921Scognet	time(&tim);
830214921Scognet
831214921Scognet	cd9660_time_915(newnode->isoDirRecord->date, tim);
832214921Scognet
833214921Scognet	cd9660_bothendian_dword(newnode->fileDataLength,
834214921Scognet	    newnode->isoDirRecord->size);
835214921Scognet	/* If the file is a link, we want to set the size to 0 */
836214921Scognet	if (S_ISLNK(newnode->node->type))
837214921Scognet		newnode->fileDataLength = 0;
838214921Scognet
839214921Scognet	return 1;
840214921Scognet}
841214921Scognet
842214921Scognet/*
843224762Smarius * Translate fsnode to cd9660node
844214921Scognet * Translate filenames and other metadata, including dates, sizes,
845214921Scognet * permissions, etc
846214921Scognet * @param struct fsnode * The node generated by makefs
847214921Scognet * @param struct cd9660node * The intermediate node to be written to
848214921Scognet * @returns int 0 on failure, 1 on success
849214921Scognet */
850214921Scognetstatic int
851214921Scognetcd9660_translate_node(fsnode *node, cd9660node *newnode)
852214921Scognet{
853214921Scognet	if (node == NULL) {
854214921Scognet		if (diskStructure.verbose_level > 0)
855214921Scognet			printf("cd9660_translate_node: NULL node passed, "
856214921Scognet			       "returning\n");
857214921Scognet		return 0;
858214921Scognet	}
859214921Scognet	if ((newnode->isoDirRecord =
860214921Scognet		malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
861214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_translate_node");
862214921Scognet		return 0;
863214921Scognet	}
864214921Scognet
865214921Scognet	/* Set the node pointer */
866214921Scognet	newnode->node = node;
867214921Scognet
868214921Scognet	/* Set the size */
869214921Scognet	if (!(S_ISDIR(node->type)))
870214921Scognet		newnode->fileDataLength = node->inode->st.st_size;
871214921Scognet
872224762Smarius	if (cd9660_translate_node_common(newnode) == 0)
873214921Scognet		return 0;
874214921Scognet
875214921Scognet	/* Finally, overwrite some of the values that are set by default */
876214921Scognet	cd9660_time_915(newnode->isoDirRecord->date, node->inode->st.st_mtime);
877214921Scognet
878214921Scognet	return 1;
879214921Scognet}
880214921Scognet
881214921Scognet/*
882214921Scognet * Compares two ISO filenames
883214921Scognet * @param const char * The first file name
884214921Scognet * @param const char * The second file name
885214921Scognet * @returns : -1 if first is less than second, 0 if they are the same, 1 if
886214921Scognet * 	the second is greater than the first
887214921Scognet */
888214921Scognetstatic int
889214921Scognetcd9660_compare_filename(const char *first, const char *second)
890214921Scognet{
891214921Scognet	/*
892214921Scognet	 * This can be made more optimal once it has been tested
893214921Scognet	 * (the extra character, for example, is for testing)
894214921Scognet	 */
895214921Scognet
896214921Scognet	int p1 = 0;
897214921Scognet	int p2 = 0;
898214921Scognet	char c1, c2;
899214921Scognet	/* First, on the filename */
900214921Scognet
901214921Scognet	while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
902214921Scognet		&& p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
903214921Scognet		c1 = first[p1];
904214921Scognet		c2 = second[p2];
905214921Scognet		if (c1 == '.' && c2 =='.')
906214921Scognet			break;
907214921Scognet		else if (c1 == '.') {
908214921Scognet			p2++;
909214921Scognet			c1 = ' ';
910214921Scognet		} else if (c2 == '.') {
911214921Scognet			p1++;
912214921Scognet			c2 = ' ';
913214921Scognet		} else {
914214921Scognet			p1++;
915214921Scognet			p2++;
916214921Scognet		}
917214921Scognet
918214921Scognet		if (c1 < c2)
919214921Scognet			return -1;
920214921Scognet		else if (c1 > c2) {
921214921Scognet			return 1;
922214921Scognet		}
923214921Scognet	}
924214921Scognet
925214921Scognet	if (first[p1] == '.' && second[p2] == '.') {
926214921Scognet		p1++;
927214921Scognet		p2++;
928214921Scognet		while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
929214921Scognet			&& p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
930214921Scognet			c1 = first[p1];
931214921Scognet			c2 = second[p2];
932214921Scognet			if (c1 == ';' && c2 == ';')
933214921Scognet				break;
934214921Scognet			else if (c1 == ';') {
935214921Scognet				p2++;
936214921Scognet				c1 = ' ';
937214921Scognet			} else if (c2 == ';') {
938214921Scognet				p1++;
939214921Scognet				c2 = ' ';
940214921Scognet			} else {
941214921Scognet				p1++;
942214921Scognet				p2++;
943214921Scognet			}
944214921Scognet
945214921Scognet			if (c1 < c2)
946214921Scognet				return -1;
947214921Scognet			else if (c1 > c2)
948214921Scognet				return 1;
949214921Scognet		}
950214921Scognet	}
951214921Scognet	return 0;
952214921Scognet}
953214921Scognet
954214921Scognet/*
955214921Scognet * Insert a node into list with ISO sorting rules
956214921Scognet * @param cd9660node * The head node of the list
957214921Scognet * @param cd9660node * The node to be inserted
958214921Scognet */
959214921Scognetstatic void
960214921Scognetcd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
961214921Scognet{
962214921Scognet	int compare;
963214921Scognet	cd9660node *cn;
964214921Scognet	struct cd9660_children_head *head = &parent->cn_children;
965214921Scognet
966214921Scognet	/* TODO: Optimize? */
967214921Scognet	cn_new->parent = parent;
968214921Scognet
969214921Scognet	/*
970214921Scognet	 * first will either be 0, the . or the ..
971214921Scognet	 * if . or .., this means no other entry may be written before first
972214921Scognet	 * if 0, the new node may be inserted at the head
973214921Scognet	 */
974214921Scognet
975214921Scognet	TAILQ_FOREACH(cn, head, cn_next_child) {
976214921Scognet		/*
977214921Scognet		 * Dont insert a node twice -
978214921Scognet		 * that would cause an infinite loop
979214921Scognet		 */
980214921Scognet		if (cn_new == cn)
981214921Scognet			return;
982214921Scognet
983214921Scognet		compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
984214921Scognet			cn->isoDirRecord->name);
985214921Scognet
986214921Scognet		if (compare == 0)
987214921Scognet			compare = cd9660_compare_filename(cn_new->node->name,
988214921Scognet				cn->node->name);
989214921Scognet
990214921Scognet		if (compare < 0)
991214921Scognet			break;
992214921Scognet	}
993214921Scognet	if (cn == NULL)
994214921Scognet		TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
995214921Scognet	else
996214921Scognet		TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
997214921Scognet}
998214921Scognet
999214921Scognet/*
1000214921Scognet * Called After cd9660_sorted_child_insert
1001214921Scognet * handles file collisions by suffixing each filname with ~n
1002214921Scognet * where n represents the files respective place in the ordering
1003214921Scognet */
1004214921Scognetstatic int
1005214921Scognetcd9660_handle_collisions(cd9660node *colliding, int past)
1006214921Scognet{
1007214921Scognet	cd9660node *iter, *next, *prev;
1008214921Scognet	int skip;
1009214921Scognet	int delete_chars = 0;
1010214921Scognet	int temp_past = past;
1011214921Scognet	int temp_skip;
1012214921Scognet	int flag = 0;
1013214921Scognet	cd9660node *end_of_range;
1014214921Scognet
1015214921Scognet	for (iter = TAILQ_FIRST(&colliding->cn_children);
1016214921Scognet	     iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
1017214921Scognet		if (strcmp(iter->isoDirRecord->name,
1018214921Scognet		           next->isoDirRecord->name) != 0) {
1019214921Scognet			iter = TAILQ_NEXT(iter, cn_next_child);
1020214921Scognet			continue;
1021214921Scognet		}
1022214921Scognet		flag = 1;
1023214921Scognet		temp_skip = skip = cd9660_count_collisions(iter);
1024214921Scognet		end_of_range = iter;
1025214921Scognet		while (temp_skip > 0) {
1026214921Scognet			temp_skip--;
1027214921Scognet			end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
1028214921Scognet		}
1029214921Scognet		temp_past = past;
1030214921Scognet		while (temp_past > 0) {
1031214921Scognet			if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
1032214921Scognet				end_of_range = next;
1033214921Scognet			else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
1034214921Scognet				iter = prev;
1035214921Scognet			else
1036214921Scognet				delete_chars++;
1037214921Scognet			temp_past--;
1038214921Scognet		}
1039214921Scognet		skip += past;
1040214921Scognet		iter = cd9660_rename_filename(iter, skip, delete_chars);
1041214921Scognet	}
1042214921Scognet	return flag;
1043214921Scognet}
1044214921Scognet
1045214921Scognet
1046214921Scognetstatic cd9660node *
1047214921Scognetcd9660_rename_filename(cd9660node *iter, int num, int delete_chars)
1048214921Scognet{
1049214921Scognet	int i = 0;
1050230354Seadler	int numbts, digit, digits, temp, powers, count;
1051214921Scognet	char *naming;
1052214921Scognet	int maxlength;
1053214921Scognet        char *tmp;
1054214921Scognet
1055214921Scognet	if (diskStructure.verbose_level > 0)
1056214921Scognet		printf("Rename_filename called\n");
1057214921Scognet
1058290595Sngie	assert(1 <= diskStructure.isoLevel && diskStructure.isoLevel <= 2);
1059214921Scognet	/* TODO : A LOT of chanes regarding 8.3 filenames */
1060214921Scognet	if (diskStructure.isoLevel == 1)
1061214921Scognet		maxlength = 8;
1062214921Scognet	else if (diskStructure.isoLevel == 2)
1063214921Scognet		maxlength = 31;
1064214921Scognet	else
1065214921Scognet		maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
1066214921Scognet
1067214921Scognet	tmp = malloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1068214921Scognet
1069316198Ssevan	while (i < num && iter) {
1070214921Scognet		powers = 1;
1071214921Scognet		count = 0;
1072214921Scognet		digits = 1;
1073214921Scognet		while (((int)(i / powers) ) >= 10) {
1074214921Scognet			digits++;
1075214921Scognet			powers = powers * 10;
1076214921Scognet		}
1077214921Scognet
1078214921Scognet		naming = iter->o_name;
1079214921Scognet
1080214921Scognet		/*
1081214921Scognet		while ((*naming != '.') && (*naming != ';')) {
1082214921Scognet			naming++;
1083214921Scognet			count++;
1084214921Scognet		}
1085214921Scognet		*/
1086214921Scognet
1087214921Scognet		while (count < maxlength) {
1088230529Shrs			if (*naming == ';')
1089230529Shrs				break;
1090214921Scognet			naming++;
1091214921Scognet			count++;
1092214921Scognet		}
1093214921Scognet
1094214921Scognet		if ((count + digits) < maxlength)
1095214921Scognet			numbts = count;
1096214921Scognet		else
1097214921Scognet			numbts = maxlength - (digits);
1098214921Scognet		numbts -= delete_chars;
1099214921Scognet
1100214921Scognet		/* 8.3 rules - keep the extension, add before the dot */
1101214921Scognet
1102214921Scognet		/*
1103214921Scognet		 * This code makes a bunch of assumptions.
1104214921Scognet		 * See if you can spot them all :)
1105214921Scognet		 */
1106214921Scognet
1107214921Scognet		/*
1108214921Scognet		if (diskStructure.isoLevel == 1) {
1109214921Scognet			numbts = 8 - digits - delete_chars;
1110214921Scognet			if (dot < 0) {
1111214921Scognet
1112214921Scognet			} else {
1113214921Scognet				if (dot < 8) {
1114214921Scognet					memmove(&tmp[numbts],&tmp[dot],4);
1115214921Scognet				}
1116214921Scognet			}
1117214921Scognet		}
1118214921Scognet		*/
1119214921Scognet
1120214921Scognet		/* (copying just the filename before the '.' */
1121214921Scognet		memcpy(tmp, (iter->o_name), numbts);
1122214921Scognet
1123214921Scognet		/* adding the appropriate number following the name */
1124214921Scognet		temp = i;
1125214921Scognet		while (digits > 0) {
1126214921Scognet			digit = (int)(temp / powers);
1127214921Scognet			temp = temp - digit * powers;
1128214921Scognet			sprintf(&tmp[numbts] , "%d", digit);
1129214921Scognet			digits--;
1130214921Scognet			numbts++;
1131214921Scognet			powers = powers / 10;
1132214921Scognet		}
1133214921Scognet
1134214921Scognet		while ((*naming != ';')  && (numbts < maxlength)) {
1135214921Scognet			tmp[numbts] = (*naming);
1136214921Scognet			naming++;
1137214921Scognet			numbts++;
1138214921Scognet		}
1139214921Scognet
1140214921Scognet		tmp[numbts] = ';';
1141214921Scognet		tmp[numbts+1] = '1';
1142214921Scognet		tmp[numbts+2] = '\0';
1143214921Scognet
1144214921Scognet		/*
1145214921Scognet		 * now tmp has exactly the identifier
1146214921Scognet		 * we want so we'll copy it back to record
1147214921Scognet		 */
1148214921Scognet		memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
1149214921Scognet
1150214921Scognet		iter = TAILQ_NEXT(iter, cn_next_child);
1151214921Scognet		i++;
1152214921Scognet	}
1153214921Scognet
1154214921Scognet	free(tmp);
1155214921Scognet	return iter;
1156214921Scognet}
1157214921Scognet
1158214921Scognet/* Todo: Figure out why these functions are nec. */
1159214921Scognetstatic void
1160214921Scognetcd9660_copy_filenames(cd9660node *node)
1161214921Scognet{
1162214921Scognet	cd9660node *cn;
1163214921Scognet
1164214921Scognet	if (TAILQ_EMPTY(&node->cn_children))
1165214921Scognet		return;
1166214921Scognet
1167214921Scognet	if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
1168214921Scognet		debug_print_tree(diskStructure.rootNode, 0);
1169214921Scognet		exit(1);
1170214921Scognet	}
1171214921Scognet
1172214921Scognet	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1173214921Scognet		cd9660_copy_filenames(cn);
1174214921Scognet		memcpy(cn->o_name, cn->isoDirRecord->name,
1175214921Scognet		    ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1176214921Scognet	}
1177214921Scognet}
1178214921Scognet
1179214921Scognetstatic void
1180214921Scognetcd9660_sorting_nodes(cd9660node *node)
1181214921Scognet{
1182214921Scognet	cd9660node *cn;
1183214921Scognet
1184214921Scognet	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
1185214921Scognet		cd9660_sorting_nodes(cn);
1186214921Scognet	cd9660_sort_nodes(node);
1187214921Scognet}
1188214921Scognet
1189214921Scognet/* XXX Bubble sort. */
1190214921Scognetstatic void
1191214921Scognetcd9660_sort_nodes(cd9660node *node)
1192214921Scognet{
1193214921Scognet	cd9660node *cn, *next;
1194214921Scognet
1195214921Scognet	do {
1196214921Scognet		TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1197214921Scognet			if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
1198214921Scognet				return;
1199214921Scognet			else if (strcmp(next->isoDirRecord->name,
1200214921Scognet				        cn->isoDirRecord->name) >= 0)
1201214921Scognet				continue;
1202214921Scognet			TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
1203214921Scognet			TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
1204214921Scognet			break;
1205214921Scognet		}
1206214921Scognet	} while (cn != NULL);
1207214921Scognet}
1208214921Scognet
1209214921Scognetstatic int
1210214921Scognetcd9660_count_collisions(cd9660node *copy)
1211214921Scognet{
1212214921Scognet	int count = 0;
1213214921Scognet	cd9660node *iter, *next;
1214214921Scognet
1215214921Scognet	for (iter = copy;
1216214921Scognet	     (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
1217214921Scognet	     iter = next) {
1218214921Scognet		if (cd9660_compare_filename(iter->isoDirRecord->name,
1219214921Scognet			next->isoDirRecord->name) == 0)
1220214921Scognet			count++;
1221214921Scognet		else
1222214921Scognet			return count;
1223214921Scognet	}
1224214921Scognet#if 0
1225214921Scognet	if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
1226214921Scognet		printf("cd9660_recurse_on_collision: count is %i \n", count);
1227214921Scognet		compare = cd9660_compare_filename(iter->isoDirRecord->name,
1228214921Scognet			next->isoDirRecord->name);
1229214921Scognet		if (compare == 0) {
1230214921Scognet			count++;
1231214921Scognet			return cd9660_recurse_on_collision(next, count);
1232214921Scognet		} else
1233214921Scognet			return count;
1234214921Scognet	}
1235214921Scognet#endif
1236214921Scognet	return count;
1237214921Scognet}
1238214921Scognet
1239214921Scognetstatic cd9660node *
1240214921Scognetcd9660_rrip_move_directory(cd9660node *dir)
1241214921Scognet{
1242214921Scognet	char newname[9];
1243214921Scognet	cd9660node *tfile;
1244214921Scognet
1245214921Scognet	/*
1246214921Scognet	 * This function needs to:
1247214921Scognet	 * 1) Create an empty virtual file in place of the old directory
1248214921Scognet	 * 2) Point the virtual file to the new directory
1249214921Scognet	 * 3) Point the relocated directory to its old parent
1250214921Scognet	 * 4) Move the directory specified by dir into rr_moved_dir,
1251214921Scognet	 * and rename it to "diskStructure.rock_ridge_move_count" (as a string)
1252214921Scognet	 */
1253214921Scognet
1254214921Scognet	/* First see if the moved directory even exists */
1255214921Scognet	if (diskStructure.rr_moved_dir == NULL) {
1256214921Scognet		diskStructure.rr_moved_dir =
1257214921Scognet			cd9660_create_directory(ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
1258214921Scognet				diskStructure.rootNode, dir);
1259214921Scognet		if (diskStructure.rr_moved_dir == NULL)
1260214921Scognet			return 0;
1261214921Scognet	}
1262214921Scognet
1263214921Scognet	/* Create a file with the same ORIGINAL name */
1264214921Scognet	tfile = cd9660_create_file(dir->node->name, dir->parent, dir);
1265214921Scognet	if (tfile == NULL)
1266214921Scognet		return NULL;
1267214921Scognet
1268214921Scognet	diskStructure.rock_ridge_move_count++;
1269214921Scognet	snprintf(newname, sizeof(newname), "%08i",
1270214921Scognet	    diskStructure.rock_ridge_move_count);
1271214921Scognet
1272214921Scognet	/* Point to old parent */
1273214921Scognet	dir->rr_real_parent = dir->parent;
1274214921Scognet
1275214921Scognet	/* Place the placeholder file */
1276214921Scognet	if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
1277214921Scognet		TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
1278214921Scognet		    cn_next_child);
1279214921Scognet	} else {
1280214921Scognet		cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
1281214921Scognet	}
1282214921Scognet
1283214921Scognet	/* Point to new parent */
1284214921Scognet	dir->parent = diskStructure.rr_moved_dir;
1285214921Scognet
1286214921Scognet	/* Point the file to the moved directory */
1287214921Scognet	tfile->rr_relocated = dir;
1288214921Scognet
1289214921Scognet	/* Actually move the directory */
1290214921Scognet	cd9660_sorted_child_insert(diskStructure.rr_moved_dir, dir);
1291214921Scognet
1292214921Scognet	/* TODO: Inherit permissions / ownership (basically the entire inode) */
1293214921Scognet
1294214921Scognet	/* Set the new name */
1295214921Scognet	memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1296214921Scognet	strncpy(dir->isoDirRecord->name, newname, 8);
1297224690Smm	dir->isoDirRecord->length[0] = 34 + 8;
1298224690Smm	dir->isoDirRecord->name_len[0] = 8;
1299214921Scognet
1300214921Scognet	return dir;
1301214921Scognet}
1302214921Scognet
1303214921Scognetstatic int
1304214921Scognetcd9660_add_dot_records(cd9660node *root)
1305214921Scognet{
1306214921Scognet	struct cd9660_children_head *head = &root->cn_children;
1307214921Scognet	cd9660node *cn;
1308214921Scognet
1309214921Scognet	TAILQ_FOREACH(cn, head, cn_next_child) {
1310214921Scognet		if ((cn->type & CD9660_TYPE_DIR) == 0)
1311214921Scognet			continue;
1312214921Scognet		/* Recursion first */
1313214921Scognet		cd9660_add_dot_records(cn);
1314214921Scognet	}
1315214921Scognet	cd9660_create_special_directory(CD9660_TYPE_DOT, root);
1316214921Scognet	cd9660_create_special_directory(CD9660_TYPE_DOTDOT, root);
1317214921Scognet	return 1;
1318214921Scognet}
1319214921Scognet
1320214921Scognet/*
1321214921Scognet * Convert node to cd9660 structure
1322214921Scognet * This function is designed to be called recursively on the root node of
1323214921Scognet * the filesystem
1324214921Scognet * Lots of recursion going on here, want to make sure it is efficient
1325214921Scognet * @param struct fsnode * The root node to be converted
1326214921Scognet * @param struct cd9660* The parent node (should not be NULL)
1327214921Scognet * @param int Current directory depth
1328214921Scognet * @param int* Running count of the number of directories that are being created
1329214921Scognet */
1330214921Scognetstatic void
1331214921Scognetcd9660_convert_structure(fsnode *root, cd9660node *parent_node, int level,
1332214921Scognet			 int *numDirectories, int *error)
1333214921Scognet{
1334214921Scognet	fsnode *iterator = root;
1335214921Scognet	cd9660node *this_node;
1336214921Scognet	int working_level;
1337214921Scognet	int add;
1338214921Scognet	int flag = 0;
1339214921Scognet	int counter = 0;
1340214921Scognet
1341214921Scognet	/*
1342214921Scognet	 * Newer, more efficient method, reduces recursion depth
1343214921Scognet	 */
1344214921Scognet	if (root == NULL) {
1345214921Scognet		warnx("%s: root is null\n", __func__);
1346214921Scognet		return;
1347214921Scognet	}
1348214921Scognet
1349214921Scognet	/* Test for an empty directory - makefs still gives us the . record */
1350214921Scognet	if ((S_ISDIR(root->type)) && (root->name[0] == '.')
1351214921Scognet		&& (root->name[1] == '\0')) {
1352214921Scognet		root = root->next;
1353214921Scognet		if (root == NULL)
1354214921Scognet			return;
1355214921Scognet	}
1356214921Scognet	if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
1357214921Scognet		CD9660_MEM_ALLOC_ERROR(__func__);
1358214921Scognet	}
1359214921Scognet
1360214921Scognet	/*
1361214921Scognet	 * To reduce the number of recursive calls, we will iterate over
1362214921Scognet	 * the next pointers to the right.
1363214921Scognet	 */
1364214921Scognet	while (iterator != NULL) {
1365214921Scognet		add = 1;
1366214921Scognet		/*
1367214921Scognet		 * Increment the directory count if this is a directory
1368214921Scognet		 * Ignore "." entries. We will generate them later
1369214921Scognet		 */
1370214921Scognet		if (!S_ISDIR(iterator->type) ||
1371214921Scognet		    strcmp(iterator->name, ".") != 0) {
1372214921Scognet
1373214921Scognet			/* Translate the node, including its filename */
1374214921Scognet			this_node->parent = parent_node;
1375214921Scognet			cd9660_translate_node(iterator, this_node);
1376214921Scognet			this_node->level = level;
1377214921Scognet
1378214921Scognet			if (S_ISDIR(iterator->type)) {
1379214921Scognet				(*numDirectories)++;
1380214921Scognet				this_node->type = CD9660_TYPE_DIR;
1381214921Scognet				working_level = level + 1;
1382214921Scognet
1383214921Scognet				/*
1384214921Scognet				 * If at level 8, directory would be at 8
1385214921Scognet				 * and have children at 9 which is not
1386214921Scognet				 * allowed as per ISO spec
1387214921Scognet				 */
1388214921Scognet				if (level == 8) {
1389214921Scognet					if ((!diskStructure.allow_deep_trees) &&
1390214921Scognet					  (!diskStructure.rock_ridge_enabled)) {
1391214921Scognet						warnx("error: found entry "
1392214921Scognet						     "with depth greater "
1393214921Scognet						     "than 8.");
1394214921Scognet						(*error) = 1;
1395214921Scognet						return;
1396214921Scognet					} else if (diskStructure.
1397214921Scognet						   rock_ridge_enabled) {
1398214921Scognet						working_level = 3;
1399214921Scognet						/*
1400214921Scognet						 * Moved directory is actually
1401214921Scognet						 * at level 2.
1402214921Scognet						 */
1403214921Scognet						this_node->level =
1404214921Scognet						    working_level - 1;
1405214921Scognet						if (cd9660_rrip_move_directory(
1406214921Scognet							this_node) == 0) {
1407214921Scognet							warnx("Failure in "
1408214921Scognet							      "cd9660_rrip_"
1409214921Scognet							      "move_directory"
1410214921Scognet							);
1411214921Scognet							(*error) = 1;
1412214921Scognet							return;
1413214921Scognet						}
1414214921Scognet						add = 0;
1415214921Scognet					}
1416214921Scognet				}
1417214921Scognet
1418214921Scognet				/* Do the recursive call on the children */
1419214921Scognet				if (iterator->child != 0) {
1420214921Scognet					cd9660_convert_structure(
1421214921Scognet					    iterator->child, this_node,
1422214921Scognet						working_level,
1423214921Scognet						numDirectories, error);
1424214921Scognet
1425214921Scognet					if ((*error) == 1) {
1426214921Scognet						warnx("%s: Error on recursive "
1427214921Scognet						    "call", __func__);
1428214921Scognet						return;
1429214921Scognet					}
1430214921Scognet				}
1431214921Scognet
1432214921Scognet			} else {
1433214921Scognet				/* Only directories should have children */
1434214921Scognet				assert(iterator->child == NULL);
1435214921Scognet
1436214921Scognet				this_node->type = CD9660_TYPE_FILE;
1437214921Scognet			}
1438214921Scognet
1439214921Scognet			/*
1440214921Scognet			 * Finally, do a sorted insert
1441214921Scognet			 */
1442214921Scognet			if (add) {
1443214921Scognet				cd9660_sorted_child_insert(
1444214921Scognet				    parent_node, this_node);
1445214921Scognet			}
1446214921Scognet
1447214921Scognet			/*Allocate new temp_node */
1448214921Scognet			if (iterator->next != 0) {
1449214921Scognet				this_node = cd9660_allocate_cd9660node();
1450214921Scognet				if (this_node == NULL)
1451214921Scognet					CD9660_MEM_ALLOC_ERROR(__func__);
1452214921Scognet			}
1453214921Scognet		}
1454214921Scognet		iterator = iterator->next;
1455214921Scognet	}
1456214921Scognet
1457214921Scognet	/* cd9660_handle_collisions(first_node); */
1458214921Scognet
1459214921Scognet	/* TODO: need cleanup */
1460214921Scognet	cd9660_copy_filenames(parent_node);
1461214921Scognet
1462214921Scognet	do {
1463214921Scognet		flag = cd9660_handle_collisions(parent_node, counter);
1464214921Scognet		counter++;
1465214921Scognet		cd9660_sorting_nodes(parent_node);
1466214921Scognet	} while ((flag == 1) && (counter < 100));
1467214921Scognet}
1468214921Scognet
1469214921Scognet/*
1470214921Scognet * Clean up the cd9660node tree
1471214921Scognet * This is designed to be called recursively on the root node
1472214921Scognet * @param struct cd9660node *root The node to free
1473214921Scognet * @returns void
1474214921Scognet */
1475214921Scognetstatic void
1476214921Scognetcd9660_free_structure(cd9660node *root)
1477214921Scognet{
1478214921Scognet	cd9660node *cn;
1479214921Scognet
1480214921Scognet	while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
1481214921Scognet		TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
1482214921Scognet		cd9660_free_structure(cn);
1483214921Scognet	}
1484214921Scognet	free(root);
1485214921Scognet}
1486214921Scognet
1487214921Scognet/*
1488214921Scognet * Be a little more memory conservative:
1489214921Scognet * instead of having the TAILQ_ENTRY as part of the cd9660node,
1490214921Scognet * just create a temporary structure
1491214921Scognet */
1492214921Scognetstruct ptq_entry
1493214921Scognet{
1494214921Scognet	TAILQ_ENTRY(ptq_entry) ptq;
1495214921Scognet	cd9660node *node;
1496214921Scognet} *n;
1497214921Scognet
1498214921Scognet#define PTQUEUE_NEW(n,s,r,t){\
1499214921Scognet	n = malloc(sizeof(struct s));	\
1500214921Scognet	if (n == NULL)	\
1501214921Scognet		return r; \
1502214921Scognet	n->node = t;\
1503214921Scognet}
1504214921Scognet
1505214921Scognet/*
1506214921Scognet * Generate the path tables
1507214921Scognet * The specific implementation of this function is left as an exercise to the
1508214921Scognet * programmer. It could be done recursively. Make sure you read how the path
1509214921Scognet * table has to be laid out, it has levels.
1510214921Scognet * @param struct iso9660_disk *disk The disk image
1511214921Scognet * @returns int The number of built path tables (between 1 and 4), 0 on failure
1512214921Scognet */
1513214921Scognetstatic int
1514214921Scognetcd9660_generate_path_table(void)
1515214921Scognet{
1516214921Scognet	cd9660node *cn, *dirNode = diskStructure.rootNode;
1517214921Scognet	cd9660node *last = dirNode;
1518214921Scognet	int pathTableSize = 0;	/* computed as we go */
1519214921Scognet	int counter = 1;	/* root gets a count of 0 */
1520214921Scognet
1521214921Scognet	TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
1522214921Scognet	TAILQ_INIT(&pt_head);
1523214921Scognet
1524214921Scognet	PTQUEUE_NEW(n, ptq_entry, -1, diskStructure.rootNode);
1525214921Scognet
1526214921Scognet	/* Push the root node */
1527214921Scognet	TAILQ_INSERT_HEAD(&pt_head, n, ptq);
1528214921Scognet
1529214921Scognet	/* Breadth-first traversal of file structure */
1530214921Scognet	while (pt_head.tqh_first != 0) {
1531214921Scognet		n = pt_head.tqh_first;
1532214921Scognet		dirNode = n->node;
1533214921Scognet		TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
1534214921Scognet		free(n);
1535214921Scognet
1536214921Scognet		/* Update the size */
1537214921Scognet		pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
1538214921Scognet		    + dirNode->isoDirRecord->name_len[0]+
1539214921Scognet			(dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
1540214921Scognet			/* includes the padding bit */
1541214921Scognet
1542214921Scognet		dirNode->ptnumber=counter;
1543214921Scognet		if (dirNode != last) {
1544214921Scognet			last->ptnext = dirNode;
1545214921Scognet			dirNode->ptprev = last;
1546214921Scognet		}
1547214921Scognet		last = dirNode;
1548214921Scognet
1549214921Scognet		/* Push children onto queue */
1550214921Scognet		TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
1551214921Scognet			/*
1552214921Scognet			 * Dont add the DOT and DOTDOT types to the path
1553214921Scognet			 * table.
1554214921Scognet			 */
1555214921Scognet			if ((cn->type != CD9660_TYPE_DOT)
1556214921Scognet				&& (cn->type != CD9660_TYPE_DOTDOT)) {
1557214921Scognet
1558214921Scognet				if (S_ISDIR(cn->node->type)) {
1559214921Scognet					PTQUEUE_NEW(n, ptq_entry, -1, cn);
1560214921Scognet					TAILQ_INSERT_TAIL(&pt_head, n, ptq);
1561214921Scognet				}
1562214921Scognet			}
1563214921Scognet		}
1564214921Scognet		counter++;
1565214921Scognet	}
1566214921Scognet	return pathTableSize;
1567214921Scognet}
1568214921Scognet
1569214921Scognetvoid
1570230795Sjkimcd9660_compute_full_filename(cd9660node *node, char *buf)
1571214921Scognet{
1572230795Sjkim	int len;
1573214921Scognet
1574230795Sjkim	len = CD9660MAXPATH + 1;
1575230795Sjkim	len = snprintf(buf, len, "%s/%s/%s", node->node->root,
1576230795Sjkim	    node->node->path, node->node->name);
1577230795Sjkim	if (len > CD9660MAXPATH)
1578230795Sjkim		errx(1, "Pathname too long.");
1579214921Scognet}
1580214921Scognet
1581214921Scognet/* NEW filename conversion method */
1582214921Scognettypedef int(*cd9660_filename_conversion_functor)(const char *, char *, int);
1583214921Scognet
1584214921Scognet
1585214921Scognet/*
1586214921Scognet * TODO: These two functions are almost identical.
1587214921Scognet * Some code cleanup is possible here
1588214921Scognet *
1589214921Scognet * XXX bounds checking!
1590214921Scognet */
1591214921Scognetstatic int
1592214921Scognetcd9660_level1_convert_filename(const char *oldname, char *newname, int is_file)
1593214921Scognet{
1594214921Scognet	/*
1595214921Scognet	 * ISO 9660 : 10.1
1596214921Scognet	 * File Name shall not contain more than 8 d or d1 characters
1597214921Scognet	 * File Name Extension shall not contain more than 3 d or d1 characters
1598214921Scognet	 * Directory Identifier shall not contain more than 8 d or d1 characters
1599214921Scognet	 */
1600214921Scognet	int namelen = 0;
1601214921Scognet	int extlen = 0;
1602214921Scognet	int found_ext = 0;
1603214921Scognet
1604225121Smm	while (*oldname != '\0' && extlen < 3) {
1605214921Scognet		/* Handle period first, as it is special */
1606214921Scognet		if (*oldname == '.') {
1607214921Scognet			if (found_ext) {
1608214921Scognet				*newname++ = '_';
1609214921Scognet				extlen ++;
1610214921Scognet			}
1611214921Scognet			else {
1612214921Scognet				*newname++ = '.';
1613214921Scognet				found_ext = 1;
1614214921Scognet			}
1615214921Scognet		} else {
1616214921Scognet			/* cut RISC OS file type off ISO name */
1617214921Scognet			if (diskStructure.archimedes_enabled &&
1618214921Scognet			    *oldname == ',' && strlen(oldname) == 4)
1619214921Scognet				break;
1620214921Scognet			/* Enforce 12.3 / 8 */
1621225121Smm			if (namelen == 8 && !found_ext)
1622214921Scognet				break;
1623214921Scognet
1624214921Scognet			if (islower((unsigned char)*oldname))
1625214921Scognet				*newname++ = toupper((unsigned char)*oldname);
1626214921Scognet			else if (isupper((unsigned char)*oldname)
1627214921Scognet				    || isdigit((unsigned char)*oldname))
1628214921Scognet				*newname++ = *oldname;
1629214921Scognet			else
1630214921Scognet				*newname++ = '_';
1631214921Scognet
1632214921Scognet			if (found_ext)
1633214921Scognet				extlen++;
1634214921Scognet			else
1635214921Scognet				namelen++;
1636214921Scognet		}
1637214921Scognet		oldname ++;
1638214921Scognet	}
1639214921Scognet	if (is_file) {
1640214921Scognet		if (!found_ext && !diskStructure.omit_trailing_period)
1641214921Scognet			*newname++ = '.';
1642214921Scognet		/* Add version */
1643214921Scognet		sprintf(newname, ";%i", 1);
1644214921Scognet	}
1645214921Scognet	return namelen + extlen + found_ext;
1646214921Scognet}
1647214921Scognet
1648214921Scognet/* XXX bounds checking! */
1649214921Scognetstatic int
1650214921Scognetcd9660_level2_convert_filename(const char *oldname, char *newname, int is_file)
1651214921Scognet{
1652214921Scognet	/*
1653214921Scognet	 * ISO 9660 : 7.5.1
1654214921Scognet	 * File name : 0+ d or d1 characters
1655214921Scognet	 * separator 1 (.)
1656214921Scognet	 * File name extension : 0+ d or d1 characters
1657214921Scognet	 * separator 2 (;)
1658214921Scognet	 * File version number (5 characters, 1-32767)
1659214921Scognet	 * 1 <= Sum of File name and File name extension <= 30
1660214921Scognet	 */
1661214921Scognet	int namelen = 0;
1662214921Scognet	int extlen = 0;
1663214921Scognet	int found_ext = 0;
1664214921Scognet
1665225121Smm	while (*oldname != '\0' && namelen + extlen < 30) {
1666214921Scognet		/* Handle period first, as it is special */
1667214921Scognet		if (*oldname == '.') {
1668214921Scognet			if (found_ext) {
1669214921Scognet				if (diskStructure.allow_multidot) {
1670214921Scognet					*newname++ = '.';
1671214921Scognet				} else {
1672214921Scognet					*newname++ = '_';
1673214921Scognet				}
1674214921Scognet				extlen ++;
1675214921Scognet			}
1676214921Scognet			else {
1677214921Scognet				*newname++ = '.';
1678214921Scognet				found_ext = 1;
1679214921Scognet			}
1680214921Scognet		} else {
1681214921Scognet			/* cut RISC OS file type off ISO name */
1682214921Scognet			if (diskStructure.archimedes_enabled &&
1683214921Scognet			    *oldname == ',' && strlen(oldname) == 4)
1684214921Scognet				break;
1685214921Scognet
1686214921Scognet			 if (islower((unsigned char)*oldname))
1687214921Scognet				*newname++ = toupper((unsigned char)*oldname);
1688214921Scognet			else if (isupper((unsigned char)*oldname) ||
1689214921Scognet			    isdigit((unsigned char)*oldname))
1690214921Scognet				*newname++ = *oldname;
1691214921Scognet			else if (diskStructure.allow_multidot &&
1692214921Scognet			    *oldname == '.') {
1693214921Scognet			    	*newname++ = '.';
1694214921Scognet			} else {
1695214921Scognet				*newname++ = '_';
1696214921Scognet			}
1697214921Scognet
1698214921Scognet			if (found_ext)
1699214921Scognet				extlen++;
1700214921Scognet			else
1701214921Scognet				namelen++;
1702214921Scognet		}
1703214921Scognet		oldname ++;
1704214921Scognet	}
1705214921Scognet	if (is_file) {
1706214921Scognet		if (!found_ext && !diskStructure.omit_trailing_period)
1707214921Scognet			*newname++ = '.';
1708214921Scognet		/* Add version */
1709214921Scognet		sprintf(newname, ";%i", 1);
1710214921Scognet	}
1711214921Scognet	return namelen + extlen + found_ext;
1712214921Scognet}
1713214921Scognet
1714214921Scognet#if 0
1715214921Scognetstatic int
1716214921Scognetcd9660_joliet_convert_filename(const char *oldname, char *newname, int is_file)
1717214921Scognet{
1718214921Scognet	/* TODO: implement later, move to cd9660_joliet.c ?? */
1719214921Scognet}
1720214921Scognet#endif
1721214921Scognet
1722214921Scognet
1723214921Scognet/*
1724214921Scognet * Convert a file name to ISO compliant file name
1725214921Scognet * @param char * oldname The original filename
1726214921Scognet * @param char ** newname The new file name, in the appropriate character
1727214921Scognet *                        set and of appropriate length
1728214921Scognet * @param int 1 if file, 0 if directory
1729214921Scognet * @returns int The length of the new string
1730214921Scognet */
1731214921Scognetstatic int
1732214921Scognetcd9660_convert_filename(const char *oldname, char *newname, int is_file)
1733214921Scognet{
1734290595Sngie	assert(1 <= diskStructure.isoLevel && diskStructure.isoLevel <= 2);
1735214921Scognet	/* NEW */
1736214921Scognet	cd9660_filename_conversion_functor conversion_function = 0;
1737214921Scognet	if (diskStructure.isoLevel == 1)
1738214921Scognet		conversion_function = &cd9660_level1_convert_filename;
1739214921Scognet	else if (diskStructure.isoLevel == 2)
1740214921Scognet		conversion_function = &cd9660_level2_convert_filename;
1741214921Scognet	return (*conversion_function)(oldname, newname, is_file);
1742214921Scognet}
1743214921Scognet
1744214921Scognetint
1745214921Scognetcd9660_compute_record_size(cd9660node *node)
1746214921Scognet{
1747214921Scognet	int size = node->isoDirRecord->length[0];
1748214921Scognet
1749214921Scognet	if (diskStructure.rock_ridge_enabled)
1750214921Scognet		size += node->susp_entry_size;
1751214921Scognet	size += node->su_tail_size;
1752214921Scognet	size += size & 1; /* Ensure length of record is even. */
1753214921Scognet	assert(size <= 254);
1754214921Scognet	return size;
1755214921Scognet}
1756214921Scognet
1757214921Scognetstatic void
1758214921Scognetcd9660_populate_dot_records(cd9660node *node)
1759214921Scognet{
1760214921Scognet	node->dot_record->fileDataSector = node->fileDataSector;
1761214921Scognet	memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
1762214921Scognet	node->dot_record->isoDirRecord->name_len[0] = 1;
1763214921Scognet	node->dot_record->isoDirRecord->name[0] = 0;
1764214921Scognet	node->dot_record->isoDirRecord->name[1] = 0;
1765214921Scognet	node->dot_record->isoDirRecord->length[0] = 34;
1766214921Scognet	node->dot_record->fileRecordSize =
1767214921Scognet	    cd9660_compute_record_size(node->dot_record);
1768214921Scognet
1769214921Scognet	if (node == diskStructure.rootNode) {
1770214921Scognet		node->dot_dot_record->fileDataSector = node->fileDataSector;
1771214921Scognet		memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
1772214921Scognet		    34);
1773214921Scognet	} else {
1774214921Scognet		node->dot_dot_record->fileDataSector =
1775214921Scognet		    node->parent->fileDataSector;
1776214921Scognet		memcpy(node->dot_dot_record->isoDirRecord,
1777214921Scognet		    node->parent->isoDirRecord,34);
1778214921Scognet	}
1779214921Scognet	node->dot_dot_record->isoDirRecord->name_len[0] = 1;
1780214921Scognet	node->dot_dot_record->isoDirRecord->name[0] = 1;
1781214921Scognet	node->dot_dot_record->isoDirRecord->name[1] = 0;
1782214921Scognet	node->dot_dot_record->isoDirRecord->length[0] = 34;
1783214921Scognet	node->dot_dot_record->fileRecordSize =
1784214921Scognet	    cd9660_compute_record_size(node->dot_dot_record);
1785214921Scognet}
1786214921Scognet
1787214921Scognet/*
1788214921Scognet * @param struct cd9660node *node The node
1789214921Scognet * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
1790214921Scognet * @returns int The total size of files and directory entries (should be
1791214921Scognet *              a multiple of sector size)
1792214921Scognet*/
1793214921Scognetstatic int64_t
1794214921Scognetcd9660_compute_offsets(cd9660node *node, int64_t startOffset)
1795214921Scognet{
1796214921Scognet	/*
1797214921Scognet	 * This function needs to compute the size of directory records and
1798214921Scognet	 * runs, file lengths, and set the appropriate variables both in
1799214921Scognet	 * cd9660node and isoDirEntry
1800214921Scognet	 */
1801214921Scognet	int64_t used_bytes = 0;
1802214921Scognet	int64_t current_sector_usage = 0;
1803214921Scognet	cd9660node *child;
1804214921Scognet	fsinode *inode;
1805214921Scognet	int64_t r;
1806214921Scognet
1807214921Scognet	assert(node != NULL);
1808214921Scognet
1809214921Scognet
1810214921Scognet	/*
1811214921Scognet	 * NOTE : There needs to be some special case detection for
1812214921Scognet	 * the "real root" node, since for it, node->node is undefined
1813214921Scognet	 */
1814214921Scognet
1815214921Scognet	node->fileDataSector = -1;
1816214921Scognet
1817214921Scognet	if (node->type & CD9660_TYPE_DIR) {
1818214921Scognet		node->fileRecordSize = cd9660_compute_record_size(node);
1819214921Scognet		/*Set what sector this directory starts in*/
1820214921Scognet		node->fileDataSector =
1821214921Scognet		    CD9660_BLOCKS(diskStructure.sectorSize,startOffset);
1822214921Scognet
1823214921Scognet		cd9660_bothendian_dword(node->fileDataSector,
1824214921Scognet		    node->isoDirRecord->extent);
1825214921Scognet
1826214921Scognet		/*
1827214921Scognet		 * First loop over children, need to know the size of
1828214921Scognet		 * their directory records
1829214921Scognet		 */
1830214921Scognet		node->fileSectorsUsed = 1;
1831214921Scognet		TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
1832214921Scognet			node->fileDataLength +=
1833214921Scognet			    cd9660_compute_record_size(child);
1834214921Scognet			if ((cd9660_compute_record_size(child) +
1835214921Scognet			    current_sector_usage) >=
1836214921Scognet		 	    diskStructure.sectorSize) {
1837214921Scognet				current_sector_usage = 0;
1838214921Scognet				node->fileSectorsUsed++;
1839214921Scognet			}
1840214921Scognet
1841214921Scognet			current_sector_usage +=
1842214921Scognet			    cd9660_compute_record_size(child);
1843214921Scognet		}
1844214921Scognet
1845214921Scognet		cd9660_bothendian_dword(node->fileSectorsUsed *
1846214921Scognet			diskStructure.sectorSize,node->isoDirRecord->size);
1847214921Scognet
1848214921Scognet		/*
1849214921Scognet		 * This should point to the sector after the directory
1850214921Scognet		 * record (or, the first byte in that sector)
1851214921Scognet		 */
1852214921Scognet		used_bytes += node->fileSectorsUsed * diskStructure.sectorSize;
1853214921Scognet
1854214921Scognet		for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1855214921Scognet		     child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
1856214921Scognet			/* Directories need recursive call */
1857214921Scognet			if (S_ISDIR(child->node->type)) {
1858214921Scognet				r = cd9660_compute_offsets(child,
1859214921Scognet				    used_bytes + startOffset);
1860214921Scognet
1861214921Scognet				if (r != -1)
1862214921Scognet					used_bytes += r;
1863214921Scognet				else
1864214921Scognet					return -1;
1865214921Scognet			}
1866214921Scognet		}
1867214921Scognet
1868214921Scognet		/* Explicitly set the . and .. records */
1869214921Scognet		cd9660_populate_dot_records(node);
1870214921Scognet
1871214921Scognet		/* Finally, do another iteration to write the file data*/
1872214921Scognet		for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1873214921Scognet		     child != NULL;
1874214921Scognet		     child = TAILQ_NEXT(child, cn_next_child)) {
1875214921Scognet			/* Files need extent set */
1876214921Scognet			if (S_ISDIR(child->node->type))
1877214921Scognet				continue;
1878214921Scognet			child->fileRecordSize =
1879214921Scognet			    cd9660_compute_record_size(child);
1880214921Scognet
1881214921Scognet			child->fileSectorsUsed =
1882214921Scognet			    CD9660_BLOCKS(diskStructure.sectorSize,
1883214921Scognet				child->fileDataLength);
1884214921Scognet
1885214921Scognet			inode = child->node->inode;
1886214921Scognet			if ((inode->flags & FI_ALLOCATED) == 0) {
1887214921Scognet				inode->ino =
1888214921Scognet				    CD9660_BLOCKS(diskStructure.sectorSize,
1889214921Scognet				        used_bytes + startOffset);
1890214921Scognet				inode->flags |= FI_ALLOCATED;
1891214921Scognet				used_bytes += child->fileSectorsUsed *
1892214921Scognet				    diskStructure.sectorSize;
1893214921Scognet			} else {
1894214921Scognet				INODE_WARNX(("%s: already allocated inode %d "
1895214921Scognet				      "data sectors at %" PRIu32, __func__,
1896214921Scognet				      (int)inode->st.st_ino, inode->ino));
1897214921Scognet			}
1898214921Scognet			child->fileDataSector = inode->ino;
1899214921Scognet			cd9660_bothendian_dword(child->fileDataSector,
1900214921Scognet				child->isoDirRecord->extent);
1901214921Scognet		}
1902214921Scognet	}
1903214921Scognet
1904214921Scognet	return used_bytes;
1905214921Scognet}
1906214921Scognet
1907214921Scognet#if 0
1908214921Scognet/* Might get rid of this func */
1909214921Scognetstatic int
1910214921Scognetcd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
1911214921Scognet{
1912214921Scognet	to->node->inode->st.st_dev = 0;
1913214921Scognet	to->node->inode->st.st_ino = 0;
1914214921Scognet	to->node->inode->st.st_size = 0;
1915214921Scognet	to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
1916214921Scognet	to->node->inode->st.st_atime = from->node->inode->st.st_atime;
1917214921Scognet	to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
1918214921Scognet	to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
1919214921Scognet	to->node->inode->st.st_uid = from->node->inode->st.st_uid;
1920214921Scognet	to->node->inode->st.st_gid = from->node->inode->st.st_gid;
1921214921Scognet	to->node->inode->st.st_mode = from->node->inode->st.st_mode;
1922214921Scognet	/* Clear out type */
1923214921Scognet	to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
1924214921Scognet	if (file)
1925214921Scognet		to->node->inode->st.st_mode |= S_IFREG;
1926214921Scognet	else
1927214921Scognet		to->node->inode->st.st_mode |= S_IFDIR;
1928214921Scognet	return 1;
1929214921Scognet}
1930214921Scognet#endif
1931214921Scognet
1932214921Scognetstatic cd9660node *
1933214921Scognetcd9660_create_virtual_entry(const char *name, cd9660node *parent, int file,
1934214921Scognet			    int insert)
1935214921Scognet{
1936214921Scognet	cd9660node *temp;
1937214921Scognet	fsnode * tfsnode;
1938214921Scognet
1939214921Scognet	assert(parent != NULL);
1940214921Scognet
1941214921Scognet	temp = cd9660_allocate_cd9660node();
1942214921Scognet	if (temp == NULL)
1943214921Scognet		return NULL;
1944214921Scognet
1945214921Scognet	if ((tfsnode = malloc(sizeof(fsnode))) == NULL) {
1946214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1947214921Scognet		return NULL;
1948214921Scognet	}
1949214921Scognet
1950214921Scognet	/* Assume for now name is a valid length */
1951214921Scognet	if ((tfsnode->name = malloc(strlen(name) + 1)) == NULL) {
1952214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1953214921Scognet		return NULL;
1954214921Scognet	}
1955214921Scognet
1956214921Scognet	if ((temp->isoDirRecord =
1957214921Scognet	    malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
1958214921Scognet		CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1959214921Scognet		return NULL;
1960214921Scognet	}
1961214921Scognet
1962214921Scognet	strcpy(tfsnode->name, name);
1963214921Scognet
1964214921Scognet	cd9660_convert_filename(tfsnode->name, temp->isoDirRecord->name, file);
1965214921Scognet
1966214921Scognet	temp->node = tfsnode;
1967214921Scognet	temp->parent = parent;
1968214921Scognet
1969214921Scognet	if (insert) {
1970214921Scognet		if (temp->parent != NULL) {
1971214921Scognet			temp->level = temp->parent->level + 1;
1972214921Scognet			if (!TAILQ_EMPTY(&temp->parent->cn_children))
1973214921Scognet				cd9660_sorted_child_insert(temp->parent, temp);
1974214921Scognet			else
1975214921Scognet				TAILQ_INSERT_HEAD(&temp->parent->cn_children,
1976214921Scognet				    temp, cn_next_child);
1977214921Scognet		}
1978214921Scognet	}
1979214921Scognet
1980214921Scognet	if (parent->node != NULL) {
1981214921Scognet		tfsnode->type = parent->node->type;
1982214921Scognet	}
1983214921Scognet
1984214921Scognet	/* Clear out file type bits */
1985214921Scognet	tfsnode->type &= ~(S_IFMT);
1986214921Scognet	if (file)
1987214921Scognet		tfsnode->type |= S_IFREG;
1988214921Scognet	else
1989214921Scognet		tfsnode->type |= S_IFDIR;
1990214921Scognet
1991214921Scognet	/* Indicate that there is no spec entry (inode) */
1992214921Scognet	tfsnode->flags &= ~(FSNODE_F_HASSPEC);
1993214921Scognet#if 0
1994214921Scognet	cd9660_copy_stat_info(parent, temp, file);
1995214921Scognet#endif
1996214921Scognet	return temp;
1997214921Scognet}
1998214921Scognet
1999214921Scognetstatic cd9660node *
2000214921Scognetcd9660_create_file(const char * name, cd9660node *parent, cd9660node *me)
2001214921Scognet{
2002214921Scognet	cd9660node *temp;
2003214921Scognet
2004214921Scognet	temp = cd9660_create_virtual_entry(name,parent,1,1);
2005214921Scognet	if (temp == NULL)
2006214921Scognet		return NULL;
2007214921Scognet
2008214921Scognet	temp->fileDataLength = 0;
2009214921Scognet
2010214921Scognet	temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
2011214921Scognet
2012214921Scognet	if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2013214921Scognet		return NULL;
2014214921Scognet	*temp->node->inode = *me->node->inode;
2015214921Scognet
2016224762Smarius	if (cd9660_translate_node_common(temp) == 0)
2017214921Scognet		return NULL;
2018214921Scognet	return temp;
2019214921Scognet}
2020214921Scognet
2021214921Scognet/*
2022214921Scognet * Create a new directory which does not exist on disk
2023214921Scognet * @param const char * name The name to assign to the directory
2024214921Scognet * @param const char * parent Pointer to the parent directory
2025214921Scognet * @returns cd9660node * Pointer to the new directory
2026214921Scognet */
2027214921Scognetstatic cd9660node *
2028214921Scognetcd9660_create_directory(const char *name, cd9660node *parent, cd9660node *me)
2029214921Scognet{
2030214921Scognet	cd9660node *temp;
2031214921Scognet
2032214921Scognet	temp = cd9660_create_virtual_entry(name,parent,0,1);
2033214921Scognet	if (temp == NULL)
2034214921Scognet		return NULL;
2035214921Scognet	temp->node->type |= S_IFDIR;
2036214921Scognet
2037214921Scognet	temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
2038214921Scognet
2039214921Scognet	if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2040214921Scognet		return NULL;
2041214921Scognet	*temp->node->inode = *me->node->inode;
2042214921Scognet
2043224762Smarius	if (cd9660_translate_node_common(temp) == 0)
2044214921Scognet		return NULL;
2045214921Scognet	return temp;
2046214921Scognet}
2047214921Scognet
2048214921Scognetstatic cd9660node *
2049214921Scognetcd9660_create_special_directory(u_char type, cd9660node *parent)
2050214921Scognet{
2051214921Scognet	cd9660node *temp, *first;
2052214921Scognet	char na[2];
2053214921Scognet
2054214921Scognet	assert(parent != NULL);
2055214921Scognet
2056214921Scognet	if (type == CD9660_TYPE_DOT)
2057214921Scognet		na[0] = 0;
2058214921Scognet	else if (type == CD9660_TYPE_DOTDOT)
2059214921Scognet		na[0] = 1;
2060214921Scognet	else
2061214921Scognet		return 0;
2062214921Scognet
2063214921Scognet	na[1] = 0;
2064214921Scognet	if ((temp = cd9660_create_virtual_entry(na, parent, 0, 0)) == NULL)
2065214921Scognet		return NULL;
2066214921Scognet
2067214921Scognet	temp->parent = parent;
2068214921Scognet	temp->type = type;
2069214921Scognet	temp->isoDirRecord->length[0] = 34;
2070214921Scognet	/* Dot record is always first */
2071214921Scognet	if (type == CD9660_TYPE_DOT) {
2072214921Scognet		parent->dot_record = temp;
2073214921Scognet		TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
2074214921Scognet	/* DotDot should be second */
2075214921Scognet	} else if (type == CD9660_TYPE_DOTDOT) {
2076214921Scognet		parent->dot_dot_record = temp;
2077214921Scognet		/*
2078214921Scognet                 * If the first child is the dot record, insert
2079214921Scognet                 * this second.  Otherwise, insert it at the head.
2080214921Scognet		 */
2081214921Scognet		if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
2082214921Scognet		    (first->type & CD9660_TYPE_DOT) == 0) {
2083214921Scognet			TAILQ_INSERT_HEAD(&parent->cn_children, temp,
2084214921Scognet			    cn_next_child);
2085214921Scognet		} else {
2086214921Scognet			TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
2087214921Scognet			    cn_next_child);
2088214921Scognet		}
2089214921Scognet	}
2090214921Scognet
2091214921Scognet	return temp;
2092214921Scognet}
2093214921Scognet
2094214921Scognetint
2095214921Scognetcd9660_add_generic_bootimage(const char *bootimage)
2096214921Scognet{
2097214921Scognet	struct stat stbuf;
2098214921Scognet
2099214921Scognet	assert(bootimage != NULL);
2100214921Scognet
2101214921Scognet	if (*bootimage == '\0') {
2102214921Scognet		warnx("Error: Boot image must be a filename");
2103214921Scognet		return 0;
2104214921Scognet	}
2105214921Scognet
2106214921Scognet	if ((diskStructure.generic_bootimage = strdup(bootimage)) == NULL) {
2107214921Scognet		warn("%s: strdup", __func__);
2108214921Scognet		return 0;
2109214921Scognet	}
2110214921Scognet
2111214921Scognet	/* Get information about the file */
2112214921Scognet	if (lstat(diskStructure.generic_bootimage, &stbuf) == -1)
2113214921Scognet		err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
2114214921Scognet		    diskStructure.generic_bootimage);
2115214921Scognet
2116214921Scognet	if (stbuf.st_size > 32768) {
2117214921Scognet		warnx("Error: Boot image must be no greater than 32768 bytes");
2118214921Scognet		return 0;
2119214921Scognet	}
2120214921Scognet
2121214921Scognet	if (diskStructure.verbose_level > 0) {
2122214921Scognet		printf("Generic boot image image has size %lld\n",
2123214921Scognet		    (long long)stbuf.st_size);
2124214921Scognet	}
2125214921Scognet
2126214921Scognet	diskStructure.has_generic_bootimage = 1;
2127214921Scognet
2128214921Scognet	return 1;
2129214921Scognet}
2130