xref: /minix3/usr.sbin/makefs/cd9660.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: cd9660.c,v 1.49 2015/06/17 01:05:41 christos Exp $	*/
29f988b79SJean-Baptiste Boric 
39f988b79SJean-Baptiste Boric /*
49f988b79SJean-Baptiste Boric  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
59f988b79SJean-Baptiste Boric  * Perez-Rathke and Ram Vedam.  All rights reserved.
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
89f988b79SJean-Baptiste Boric  * Alan Perez-Rathke and Ram Vedam.
99f988b79SJean-Baptiste Boric  *
109f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or
119f988b79SJean-Baptiste Boric  * without modification, are permitted provided that the following
129f988b79SJean-Baptiste Boric  * conditions are met:
139f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
149f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
159f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above
169f988b79SJean-Baptiste Boric  *    copyright notice, this list of conditions and the following
179f988b79SJean-Baptiste Boric  *    disclaimer in the documentation and/or other materials provided
189f988b79SJean-Baptiste Boric  *    with the distribution.
199f988b79SJean-Baptiste Boric  *
209f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
219f988b79SJean-Baptiste Boric  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
229f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239f988b79SJean-Baptiste Boric  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
249f988b79SJean-Baptiste Boric  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
259f988b79SJean-Baptiste Boric  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
269f988b79SJean-Baptiste Boric  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
279f988b79SJean-Baptiste Boric  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
289f988b79SJean-Baptiste Boric  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
299f988b79SJean-Baptiste Boric  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
309f988b79SJean-Baptiste Boric  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319f988b79SJean-Baptiste Boric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
329f988b79SJean-Baptiste Boric  * OF SUCH DAMAGE.
339f988b79SJean-Baptiste Boric  */
349f988b79SJean-Baptiste Boric /*
359f988b79SJean-Baptiste Boric  * Copyright (c) 2001 Wasabi Systems, Inc.
369f988b79SJean-Baptiste Boric  * All rights reserved.
379f988b79SJean-Baptiste Boric  *
389f988b79SJean-Baptiste Boric  * Written by Luke Mewburn for Wasabi Systems, Inc.
399f988b79SJean-Baptiste Boric  *
409f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
419f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
429f988b79SJean-Baptiste Boric  * are met:
439f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
449f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
459f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
469f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
479f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
489f988b79SJean-Baptiste Boric  * 3. All advertising materials mentioning features or use of this software
499f988b79SJean-Baptiste Boric  *    must display the following acknowledgement:
509f988b79SJean-Baptiste Boric  *      This product includes software developed for the NetBSD Project by
519f988b79SJean-Baptiste Boric  *      Wasabi Systems, Inc.
529f988b79SJean-Baptiste Boric  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
539f988b79SJean-Baptiste Boric  *    or promote products derived from this software without specific prior
549f988b79SJean-Baptiste Boric  *    written permission.
559f988b79SJean-Baptiste Boric  *
569f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
579f988b79SJean-Baptiste Boric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
589f988b79SJean-Baptiste Boric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
599f988b79SJean-Baptiste Boric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
609f988b79SJean-Baptiste Boric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
619f988b79SJean-Baptiste Boric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
629f988b79SJean-Baptiste Boric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
639f988b79SJean-Baptiste Boric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
649f988b79SJean-Baptiste Boric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
659f988b79SJean-Baptiste Boric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
669f988b79SJean-Baptiste Boric  * POSSIBILITY OF SUCH DAMAGE.
679f988b79SJean-Baptiste Boric  */
689f988b79SJean-Baptiste Boric /*
699f988b79SJean-Baptiste Boric  * Copyright (c) 1982, 1986, 1989, 1993
709f988b79SJean-Baptiste Boric  *	The Regents of the University of California.  All rights reserved.
719f988b79SJean-Baptiste Boric  *
729f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
739f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
749f988b79SJean-Baptiste Boric  * are met:
759f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
769f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
779f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
789f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
799f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
809f988b79SJean-Baptiste Boric  * 3. Neither the name of the University nor the names of its contributors
819f988b79SJean-Baptiste Boric  *    may be used to endorse or promote products derived from this software
829f988b79SJean-Baptiste Boric  *    without specific prior written permission.
839f988b79SJean-Baptiste Boric  *
849f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
859f988b79SJean-Baptiste Boric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
869f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
879f988b79SJean-Baptiste Boric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
889f988b79SJean-Baptiste Boric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
899f988b79SJean-Baptiste Boric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
909f988b79SJean-Baptiste Boric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
919f988b79SJean-Baptiste Boric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
929f988b79SJean-Baptiste Boric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
939f988b79SJean-Baptiste Boric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
949f988b79SJean-Baptiste Boric  * SUCH DAMAGE.
959f988b79SJean-Baptiste Boric  *
969f988b79SJean-Baptiste Boric   */
979f988b79SJean-Baptiste Boric 
989f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
999f988b79SJean-Baptiste Boric #include "nbtool_config.h"
1009f988b79SJean-Baptiste Boric #else
1019f988b79SJean-Baptiste Boric #include <sys/mount.h>
1029f988b79SJean-Baptiste Boric #endif
1039f988b79SJean-Baptiste Boric 
1049f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
1059f988b79SJean-Baptiste Boric #if defined(__RCSID) && !defined(__lint)
106*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cd9660.c,v 1.49 2015/06/17 01:05:41 christos Exp $");
1079f988b79SJean-Baptiste Boric #endif  /* !__lint */
1089f988b79SJean-Baptiste Boric 
1099f988b79SJean-Baptiste Boric #include <string.h>
1109f988b79SJean-Baptiste Boric #include <ctype.h>
1119f988b79SJean-Baptiste Boric #include <sys/param.h>
1129f988b79SJean-Baptiste Boric #include <sys/queue.h>
1139f988b79SJean-Baptiste Boric #include <util.h>
1149f988b79SJean-Baptiste Boric 
1159f988b79SJean-Baptiste Boric #include "makefs.h"
1169f988b79SJean-Baptiste Boric #include "cd9660.h"
1179f988b79SJean-Baptiste Boric #include "cd9660/iso9660_rrip.h"
1189f988b79SJean-Baptiste Boric #include "cd9660/cd9660_archimedes.h"
1199f988b79SJean-Baptiste Boric 
1209f988b79SJean-Baptiste Boric /*
1219f988b79SJean-Baptiste Boric  * Global variables
1229f988b79SJean-Baptiste Boric  */
1239f988b79SJean-Baptiste Boric 
1249f988b79SJean-Baptiste Boric static void cd9660_finalize_PVD(iso9660_disk *);
1259f988b79SJean-Baptiste Boric static cd9660node *cd9660_allocate_cd9660node(void);
1269f988b79SJean-Baptiste Boric static void cd9660_set_defaults(iso9660_disk *);
1279f988b79SJean-Baptiste Boric static int cd9660_arguments_set_string(const char *, const char *, int,
1289f988b79SJean-Baptiste Boric     char, char *);
1299f988b79SJean-Baptiste Boric static void cd9660_populate_iso_dir_record(
1309f988b79SJean-Baptiste Boric     struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
1319f988b79SJean-Baptiste Boric     const char *);
1329f988b79SJean-Baptiste Boric static void cd9660_setup_root_node(iso9660_disk *);
1339f988b79SJean-Baptiste Boric static int cd9660_setup_volume_descriptors(iso9660_disk *);
1349f988b79SJean-Baptiste Boric #if 0
1359f988b79SJean-Baptiste Boric static int cd9660_fill_extended_attribute_record(cd9660node *);
1369f988b79SJean-Baptiste Boric #endif
1379f988b79SJean-Baptiste Boric static void cd9660_sort_nodes(cd9660node *);
1389f988b79SJean-Baptiste Boric static int cd9660_translate_node_common(iso9660_disk *, cd9660node *);
1399f988b79SJean-Baptiste Boric static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *);
1409f988b79SJean-Baptiste Boric static int cd9660_compare_filename(const char *, const char *);
1419f988b79SJean-Baptiste Boric static void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
1429f988b79SJean-Baptiste Boric static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int);
1439f988b79SJean-Baptiste Boric static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int,
1449f988b79SJean-Baptiste Boric     int);
1459f988b79SJean-Baptiste Boric static void cd9660_copy_filenames(iso9660_disk *, cd9660node *);
1469f988b79SJean-Baptiste Boric static void cd9660_sorting_nodes(cd9660node *);
1479f988b79SJean-Baptiste Boric static int cd9660_count_collisions(cd9660node *);
1489f988b79SJean-Baptiste Boric static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *);
1499f988b79SJean-Baptiste Boric static int cd9660_add_dot_records(iso9660_disk *, cd9660node *);
1509f988b79SJean-Baptiste Boric 
1519f988b79SJean-Baptiste Boric static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int,
1529f988b79SJean-Baptiste Boric     int *, int *);
1539f988b79SJean-Baptiste Boric static void cd9660_free_structure(cd9660node *);
1549f988b79SJean-Baptiste Boric static int cd9660_generate_path_table(iso9660_disk *);
1559f988b79SJean-Baptiste Boric static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *,
1569f988b79SJean-Baptiste Boric     int);
1579f988b79SJean-Baptiste Boric static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *,
1589f988b79SJean-Baptiste Boric     int);
1599f988b79SJean-Baptiste Boric #if 0
1609f988b79SJean-Baptiste Boric static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *,
1619f988b79SJean-Baptiste Boric     int);
1629f988b79SJean-Baptiste Boric #endif
1639f988b79SJean-Baptiste Boric static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int);
1649f988b79SJean-Baptiste Boric static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *);
1659f988b79SJean-Baptiste Boric static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t);
1669f988b79SJean-Baptiste Boric #if 0
1679f988b79SJean-Baptiste Boric static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
1689f988b79SJean-Baptiste Boric #endif
1699f988b79SJean-Baptiste Boric static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *,
1709f988b79SJean-Baptiste Boric     cd9660node *, int, int);
1719f988b79SJean-Baptiste Boric static cd9660node *cd9660_create_file(iso9660_disk *, const char *,
1729f988b79SJean-Baptiste Boric     cd9660node *, cd9660node *);
1739f988b79SJean-Baptiste Boric static cd9660node *cd9660_create_directory(iso9660_disk *, const char *,
1749f988b79SJean-Baptiste Boric     cd9660node *, cd9660node *);
1759f988b79SJean-Baptiste Boric static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char,
1769f988b79SJean-Baptiste Boric     cd9660node *);
1779f988b79SJean-Baptiste Boric static int  cd9660_add_generic_bootimage(iso9660_disk *, const char *);
1789f988b79SJean-Baptiste Boric 
1799f988b79SJean-Baptiste Boric 
1809f988b79SJean-Baptiste Boric /*
1819f988b79SJean-Baptiste Boric  * Allocate and initalize a cd9660node
1829f988b79SJean-Baptiste Boric  * @returns struct cd9660node * Pointer to new node, or NULL on error
1839f988b79SJean-Baptiste Boric  */
1849f988b79SJean-Baptiste Boric static cd9660node *
cd9660_allocate_cd9660node(void)1859f988b79SJean-Baptiste Boric cd9660_allocate_cd9660node(void)
1869f988b79SJean-Baptiste Boric {
1879f988b79SJean-Baptiste Boric 	cd9660node *temp = ecalloc(1, sizeof(*temp));
1889f988b79SJean-Baptiste Boric 	TAILQ_INIT(&temp->cn_children);
1899f988b79SJean-Baptiste Boric 	temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
1909f988b79SJean-Baptiste Boric 	temp->ptnext = temp->ptprev = temp->ptlast = NULL;
1919f988b79SJean-Baptiste Boric 	temp->node = NULL;
1929f988b79SJean-Baptiste Boric 	temp->isoDirRecord = NULL;
1939f988b79SJean-Baptiste Boric 	temp->isoExtAttributes = NULL;
1949f988b79SJean-Baptiste Boric 	temp->rr_real_parent = temp->rr_relocated = NULL;
1959f988b79SJean-Baptiste Boric 	temp->su_tail_data = NULL;
1969f988b79SJean-Baptiste Boric 	return temp;
1979f988b79SJean-Baptiste Boric }
1989f988b79SJean-Baptiste Boric 
1999f988b79SJean-Baptiste Boric int cd9660_defaults_set = 0;
2009f988b79SJean-Baptiste Boric 
2019f988b79SJean-Baptiste Boric /**
2029f988b79SJean-Baptiste Boric * Set default values for cd9660 extension to makefs
2039f988b79SJean-Baptiste Boric */
2049f988b79SJean-Baptiste Boric static void
cd9660_set_defaults(iso9660_disk * diskStructure)2059f988b79SJean-Baptiste Boric cd9660_set_defaults(iso9660_disk *diskStructure)
2069f988b79SJean-Baptiste Boric {
2079f988b79SJean-Baptiste Boric 	/*Fix the sector size for now, though the spec allows for other sizes*/
2089f988b79SJean-Baptiste Boric 	diskStructure->sectorSize = 2048;
2099f988b79SJean-Baptiste Boric 
2109f988b79SJean-Baptiste Boric 	/* Set up defaults in our own structure */
2119f988b79SJean-Baptiste Boric 	diskStructure->verbose_level = 0;
2129f988b79SJean-Baptiste Boric 	diskStructure->keep_bad_images = 0;
2139f988b79SJean-Baptiste Boric 	diskStructure->follow_sym_links = 0;
2149f988b79SJean-Baptiste Boric 	diskStructure->isoLevel = 2;
2159f988b79SJean-Baptiste Boric 
2169f988b79SJean-Baptiste Boric 	diskStructure->rock_ridge_enabled = 0;
2179f988b79SJean-Baptiste Boric 	diskStructure->rock_ridge_renamed_dir_name = 0;
2189f988b79SJean-Baptiste Boric 	diskStructure->rock_ridge_move_count = 0;
2199f988b79SJean-Baptiste Boric 	diskStructure->rr_moved_dir = 0;
2209f988b79SJean-Baptiste Boric 
2219f988b79SJean-Baptiste Boric 	diskStructure->archimedes_enabled = 0;
2229f988b79SJean-Baptiste Boric 	diskStructure->chrp_boot = 0;
2239f988b79SJean-Baptiste Boric 
2249f988b79SJean-Baptiste Boric 	diskStructure->include_padding_areas = 1;
2259f988b79SJean-Baptiste Boric 
2269f988b79SJean-Baptiste Boric 	/* Spec breaking functionality */
2279f988b79SJean-Baptiste Boric 	diskStructure->allow_deep_trees =
2289f988b79SJean-Baptiste Boric 	    diskStructure->allow_start_dot =
2299f988b79SJean-Baptiste Boric 	    diskStructure->allow_max_name =
2309f988b79SJean-Baptiste Boric 	    diskStructure->allow_illegal_chars =
2319f988b79SJean-Baptiste Boric 	    diskStructure->allow_lowercase =
2329f988b79SJean-Baptiste Boric 	    diskStructure->allow_multidot =
2339f988b79SJean-Baptiste Boric 	    diskStructure->omit_trailing_period = 0;
2349f988b79SJean-Baptiste Boric 
2359f988b79SJean-Baptiste Boric 	/* Make sure the PVD is clear */
2369f988b79SJean-Baptiste Boric 	memset(&diskStructure->primaryDescriptor, 0, 2048);
2379f988b79SJean-Baptiste Boric 
2389f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.publisher_id,	0x20,128);
2399f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.preparer_id,	0x20,128);
2409f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.application_id,	0x20,128);
2419f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37);
2429f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37);
2439f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37);
2449f988b79SJean-Baptiste Boric 
2459f988b79SJean-Baptiste Boric 	strcpy(diskStructure->primaryDescriptor.system_id,"NetBSD");
2469f988b79SJean-Baptiste Boric 
2479f988b79SJean-Baptiste Boric 	cd9660_defaults_set = 1;
2489f988b79SJean-Baptiste Boric 
2499f988b79SJean-Baptiste Boric 	/* Boot support: Initially disabled */
2509f988b79SJean-Baptiste Boric 	diskStructure->has_generic_bootimage = 0;
2519f988b79SJean-Baptiste Boric 	diskStructure->generic_bootimage = NULL;
2529f988b79SJean-Baptiste Boric 
2539f988b79SJean-Baptiste Boric 	diskStructure->boot_image_directory = 0;
2549f988b79SJean-Baptiste Boric 	/*memset(diskStructure->boot_descriptor, 0, 2048);*/
2559f988b79SJean-Baptiste Boric 
2569f988b79SJean-Baptiste Boric 	diskStructure->is_bootable = 0;
2579f988b79SJean-Baptiste Boric 	TAILQ_INIT(&diskStructure->boot_images);
2589f988b79SJean-Baptiste Boric 	LIST_INIT(&diskStructure->boot_entries);
2599f988b79SJean-Baptiste Boric }
2609f988b79SJean-Baptiste Boric 
2619f988b79SJean-Baptiste Boric void
cd9660_prep_opts(fsinfo_t * fsopts)2629f988b79SJean-Baptiste Boric cd9660_prep_opts(fsinfo_t *fsopts)
2639f988b79SJean-Baptiste Boric {
2649f988b79SJean-Baptiste Boric 	iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
2659f988b79SJean-Baptiste Boric 
2669f988b79SJean-Baptiste Boric #define OPT_STR(letter, name, desc)  \
2679f988b79SJean-Baptiste Boric 	{ letter, name, NULL, OPT_STRBUF, 0, 0, desc }
2689f988b79SJean-Baptiste Boric 
2699f988b79SJean-Baptiste Boric #define OPT_NUM(letter, name, field, min, max, desc) \
2709f988b79SJean-Baptiste Boric 	{ letter, name, &diskStructure->field, \
2719f988b79SJean-Baptiste Boric 	  sizeof(diskStructure->field) == 8 ? OPT_INT64 : \
2729f988b79SJean-Baptiste Boric 	  (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \
2739f988b79SJean-Baptiste Boric 	  (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \
2749f988b79SJean-Baptiste Boric 	  min, max, desc }
2759f988b79SJean-Baptiste Boric 
2769f988b79SJean-Baptiste Boric #define OPT_BOOL(letter, name, field, desc) \
2779f988b79SJean-Baptiste Boric 	OPT_NUM(letter, name, field, 0, 1, desc)
2789f988b79SJean-Baptiste Boric 
2799f988b79SJean-Baptiste Boric 	const option_t cd9660_options[] = {
2809f988b79SJean-Baptiste Boric 		OPT_NUM('l', "isolevel", isoLevel,
2819f988b79SJean-Baptiste Boric 		    1, 3, "ISO Level"),
2829f988b79SJean-Baptiste Boric 		OPT_NUM('v', "verbose",  verbose_level,
2839f988b79SJean-Baptiste Boric 		    0, 2, "Turns on verbose output"),
2849f988b79SJean-Baptiste Boric 
2859f988b79SJean-Baptiste Boric 		OPT_BOOL('h', "help", displayHelp,
2869f988b79SJean-Baptiste Boric 		    "Show help message"),
2879f988b79SJean-Baptiste Boric 		OPT_BOOL('S', "follow-symlinks", follow_sym_links,
2889f988b79SJean-Baptiste Boric 		    "Resolve symlinks in pathnames"),
2899f988b79SJean-Baptiste Boric 	        OPT_BOOL('R', "rockridge", rock_ridge_enabled,
2909f988b79SJean-Baptiste Boric 		    "Enable Rock-Ridge extensions"),
2919f988b79SJean-Baptiste Boric 	        OPT_BOOL('C', "chrp-boot", chrp_boot,
2929f988b79SJean-Baptiste Boric 		    "Enable CHRP boot"),
2939f988b79SJean-Baptiste Boric 	        OPT_BOOL('K', "keep-bad-images", keep_bad_images,
2949f988b79SJean-Baptiste Boric 		    "Keep bad images"),
2959f988b79SJean-Baptiste Boric 	        OPT_BOOL('D', "allow-deep-trees", allow_deep_trees,
2969f988b79SJean-Baptiste Boric 		    "Allow trees more than 8 levels"),
2979f988b79SJean-Baptiste Boric 	        OPT_BOOL('a', "allow-max-name", allow_max_name,
2989f988b79SJean-Baptiste Boric 		    "Allow 37 char filenames (unimplemented)"),
2999f988b79SJean-Baptiste Boric 	        OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars,
3009f988b79SJean-Baptiste Boric 		    "Allow illegal characters in filenames"),
3019f988b79SJean-Baptiste Boric 	        OPT_BOOL('D', "allow-multidot", allow_multidot,
3029f988b79SJean-Baptiste Boric 		    "Allow multiple periods in filenames"),
3039f988b79SJean-Baptiste Boric 	        OPT_BOOL('o', "omit-trailing-period", omit_trailing_period,
3049f988b79SJean-Baptiste Boric 		    "Omit trailing periods in filenames"),
3059f988b79SJean-Baptiste Boric 	        OPT_BOOL('\0', "allow-lowercase", allow_lowercase,
3069f988b79SJean-Baptiste Boric 		    "Allow lowercase characters in filenames"),
3079f988b79SJean-Baptiste Boric 	        OPT_BOOL('\0', "archimedes", archimedes_enabled,
3089f988b79SJean-Baptiste Boric 		    "Enable Archimedes structure"),
3099f988b79SJean-Baptiste Boric 		OPT_BOOL('\0', "no-trailing-padding", include_padding_areas,
3109f988b79SJean-Baptiste Boric 		    "Include padding areas"),
3119f988b79SJean-Baptiste Boric 
3129f988b79SJean-Baptiste Boric 		OPT_STR('A', "applicationid", "Application Identifier"),
3139f988b79SJean-Baptiste Boric 		OPT_STR('P', "publisher", "Publisher Identifier"),
3149f988b79SJean-Baptiste Boric 		OPT_STR('p', "preparer", "Preparer Identifier"),
3159f988b79SJean-Baptiste Boric 		OPT_STR('L', "label", "Disk Label"),
3169f988b79SJean-Baptiste Boric 		OPT_STR('V', "volumeid", "Volume Set Identifier"),
3179f988b79SJean-Baptiste Boric 		OPT_STR('B', "bootimage", "Boot image parameter"),
3189f988b79SJean-Baptiste Boric 		OPT_STR('G', "generic-bootimage", "Generic boot image param"),
3199f988b79SJean-Baptiste Boric 		OPT_STR('\0', "bootimagedir", "Boot image directory"),
3209f988b79SJean-Baptiste Boric 		OPT_STR('\0', "no-emul-boot", "No boot emulation"),
3219f988b79SJean-Baptiste Boric 		OPT_STR('\0', "no-boot", "No boot support"),
3229f988b79SJean-Baptiste Boric 		OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"),
3239f988b79SJean-Baptiste Boric 		OPT_STR('\0', "boot-load-segment", "Boot load segment"),
3249f988b79SJean-Baptiste Boric 
3259f988b79SJean-Baptiste Boric 		{ .name = NULL }
3269f988b79SJean-Baptiste Boric 	};
3279f988b79SJean-Baptiste Boric 
3289f988b79SJean-Baptiste Boric 	fsopts->fs_specific = diskStructure;
3299f988b79SJean-Baptiste Boric 	fsopts->fs_options = copy_opts(cd9660_options);
3309f988b79SJean-Baptiste Boric 
3319f988b79SJean-Baptiste Boric 	cd9660_set_defaults(diskStructure);
3329f988b79SJean-Baptiste Boric }
3339f988b79SJean-Baptiste Boric 
3349f988b79SJean-Baptiste Boric void
cd9660_cleanup_opts(fsinfo_t * fsopts)3359f988b79SJean-Baptiste Boric cd9660_cleanup_opts(fsinfo_t *fsopts)
3369f988b79SJean-Baptiste Boric {
3379f988b79SJean-Baptiste Boric 	free(fsopts->fs_specific);
3389f988b79SJean-Baptiste Boric 	free(fsopts->fs_options);
3399f988b79SJean-Baptiste Boric }
3409f988b79SJean-Baptiste Boric 
3419f988b79SJean-Baptiste Boric static int
cd9660_arguments_set_string(const char * val,const char * fieldtitle,int length,char testmode,char * dest)3429f988b79SJean-Baptiste Boric cd9660_arguments_set_string(const char *val, const char *fieldtitle, int length,
3439f988b79SJean-Baptiste Boric 			    char testmode, char * dest)
3449f988b79SJean-Baptiste Boric {
3459f988b79SJean-Baptiste Boric 	int len, test;
3469f988b79SJean-Baptiste Boric 
3479f988b79SJean-Baptiste Boric 	if (val == NULL)
3489f988b79SJean-Baptiste Boric 		warnx("error: The %s requires a string argument", fieldtitle);
3499f988b79SJean-Baptiste Boric 	else if ((len = strlen(val)) <= length) {
3509f988b79SJean-Baptiste Boric 		if (testmode == 'd')
3519f988b79SJean-Baptiste Boric 			test = cd9660_valid_d_chars(val);
3529f988b79SJean-Baptiste Boric 		else
3539f988b79SJean-Baptiste Boric 			test = cd9660_valid_a_chars(val);
3549f988b79SJean-Baptiste Boric 		if (test) {
3559f988b79SJean-Baptiste Boric 			memcpy(dest, val, len);
3569f988b79SJean-Baptiste Boric 			if (test == 2)
3579f988b79SJean-Baptiste Boric 				cd9660_uppercase_characters(dest, len);
3589f988b79SJean-Baptiste Boric 			return 1;
3599f988b79SJean-Baptiste Boric 		} else
3609f988b79SJean-Baptiste Boric 			warnx("error: The %s must be composed of "
3619f988b79SJean-Baptiste Boric 			      "%c-characters", fieldtitle, testmode);
3629f988b79SJean-Baptiste Boric 	} else
3639f988b79SJean-Baptiste Boric 		warnx("error: The %s must be at most 32 characters long",
3649f988b79SJean-Baptiste Boric 		    fieldtitle);
3659f988b79SJean-Baptiste Boric 	return 0;
3669f988b79SJean-Baptiste Boric }
3679f988b79SJean-Baptiste Boric 
3689f988b79SJean-Baptiste Boric /*
3699f988b79SJean-Baptiste Boric  * Command-line parsing function
3709f988b79SJean-Baptiste Boric  */
3719f988b79SJean-Baptiste Boric 
3729f988b79SJean-Baptiste Boric int
cd9660_parse_opts(const char * option,fsinfo_t * fsopts)3739f988b79SJean-Baptiste Boric cd9660_parse_opts(const char *option, fsinfo_t *fsopts)
3749f988b79SJean-Baptiste Boric {
3759f988b79SJean-Baptiste Boric 	int	rv, i;
3769f988b79SJean-Baptiste Boric 	iso9660_disk *diskStructure = fsopts->fs_specific;
3779f988b79SJean-Baptiste Boric 	option_t *cd9660_options = fsopts->fs_options;
3789f988b79SJean-Baptiste Boric 	char buf[1024];
3799f988b79SJean-Baptiste Boric 	const char *name, *desc;
3809f988b79SJean-Baptiste Boric 
3819f988b79SJean-Baptiste Boric 	assert(option != NULL);
3829f988b79SJean-Baptiste Boric 
3839f988b79SJean-Baptiste Boric 	if (debug & DEBUG_FS_PARSE_OPTS)
384*0a6a1f1dSLionel Sambuc 		printf("%s: got `%s'\n", __func__, option);
3859f988b79SJean-Baptiste Boric 
3869f988b79SJean-Baptiste Boric 	i = set_option(cd9660_options, option, buf, sizeof(buf));
3879f988b79SJean-Baptiste Boric 	if (i == -1)
3889f988b79SJean-Baptiste Boric 		return 0;
3899f988b79SJean-Baptiste Boric 
3909f988b79SJean-Baptiste Boric 	if (cd9660_options[i].name == NULL)
3919f988b79SJean-Baptiste Boric 		abort();
3929f988b79SJean-Baptiste Boric 
3939f988b79SJean-Baptiste Boric 
3949f988b79SJean-Baptiste Boric 	name = cd9660_options[i].name;
3959f988b79SJean-Baptiste Boric 	desc = cd9660_options[i].desc;
3969f988b79SJean-Baptiste Boric 	switch (cd9660_options[i].letter) {
3979f988b79SJean-Baptiste Boric 	case 'h':
3989f988b79SJean-Baptiste Boric 	case 'S':
3999f988b79SJean-Baptiste Boric 		rv = 0;	/* this is not handled yet */
4009f988b79SJean-Baptiste Boric 		break;
4019f988b79SJean-Baptiste Boric 	case 'L':
4029f988b79SJean-Baptiste Boric 		rv = cd9660_arguments_set_string(buf, desc, 32, 'd',
4039f988b79SJean-Baptiste Boric 		    diskStructure->primaryDescriptor.volume_id);
4049f988b79SJean-Baptiste Boric 		break;
4059f988b79SJean-Baptiste Boric 	case 'A':
4069f988b79SJean-Baptiste Boric 		rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
4079f988b79SJean-Baptiste Boric 		    diskStructure->primaryDescriptor.application_id);
4089f988b79SJean-Baptiste Boric 		break;
4099f988b79SJean-Baptiste Boric 	case 'P':
4109f988b79SJean-Baptiste Boric 		rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
4119f988b79SJean-Baptiste Boric 		    diskStructure->primaryDescriptor.publisher_id);
4129f988b79SJean-Baptiste Boric 		break;
4139f988b79SJean-Baptiste Boric 	case 'p':
4149f988b79SJean-Baptiste Boric 		rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
4159f988b79SJean-Baptiste Boric 		    diskStructure->primaryDescriptor.preparer_id);
4169f988b79SJean-Baptiste Boric 		break;
4179f988b79SJean-Baptiste Boric 	case 'V':
4189f988b79SJean-Baptiste Boric 		rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
4199f988b79SJean-Baptiste Boric 		    diskStructure->primaryDescriptor.volume_set_id);
4209f988b79SJean-Baptiste Boric 		break;
4219f988b79SJean-Baptiste Boric 	/* Boot options */
4229f988b79SJean-Baptiste Boric 	case 'B':
4239f988b79SJean-Baptiste Boric 		if (buf[0] == '\0') {
4249f988b79SJean-Baptiste Boric 			warnx("The Boot Image parameter requires a valid boot"
4259f988b79SJean-Baptiste Boric 			" information string");
4269f988b79SJean-Baptiste Boric 			rv = 0;
4279f988b79SJean-Baptiste Boric 		} else
4289f988b79SJean-Baptiste Boric 			rv = cd9660_add_boot_disk(diskStructure, buf);
4299f988b79SJean-Baptiste Boric 		break;
4309f988b79SJean-Baptiste Boric 	case 'G':
4319f988b79SJean-Baptiste Boric 		if (buf[0] == '\0') {
4329f988b79SJean-Baptiste Boric 			warnx("The Generic Boot Image parameter requires a"
4339f988b79SJean-Baptiste Boric 			    " valid boot information string");
4349f988b79SJean-Baptiste Boric 			rv = 0;
4359f988b79SJean-Baptiste Boric 		} else
4369f988b79SJean-Baptiste Boric 			rv = cd9660_add_generic_bootimage(diskStructure, buf);
4379f988b79SJean-Baptiste Boric 		break;
4389f988b79SJean-Baptiste Boric 	default:
4399f988b79SJean-Baptiste Boric 		if (strcmp(name, "bootimagedir") == 0) {
4409f988b79SJean-Baptiste Boric 			/*
4419f988b79SJean-Baptiste Boric 			 * XXXfvdl this is unused.
4429f988b79SJean-Baptiste Boric 			 */
4439f988b79SJean-Baptiste Boric 			if (buf[0] == '\0') {
4449f988b79SJean-Baptiste Boric 				warnx("The Boot Image Directory parameter"
445*0a6a1f1dSLionel Sambuc 				 " requires a directory name");
4469f988b79SJean-Baptiste Boric 				rv = 0;
4479f988b79SJean-Baptiste Boric 			} else {
4489f988b79SJean-Baptiste Boric 				diskStructure->boot_image_directory =
4499f988b79SJean-Baptiste Boric 				     emalloc(strlen(buf) + 1);
4509f988b79SJean-Baptiste Boric 				/* BIG TODO: Add the max length function here */
4519f988b79SJean-Baptiste Boric 				rv = cd9660_arguments_set_string(buf, desc, 12,
4529f988b79SJean-Baptiste Boric 				    'd', diskStructure->boot_image_directory);
4539f988b79SJean-Baptiste Boric 			}
4549f988b79SJean-Baptiste Boric 		} else if (strcmp(name, "no-emul-boot") == 0 ||
4559f988b79SJean-Baptiste Boric 		    strcmp(name, "no-boot") == 0 ||
4569f988b79SJean-Baptiste Boric 		    strcmp(name, "hard-disk-boot") == 0) {
4579f988b79SJean-Baptiste Boric 			/* RRIP */
4589f988b79SJean-Baptiste Boric 			cd9660_eltorito_add_boot_option(diskStructure, name, 0);
4599f988b79SJean-Baptiste Boric 			rv = 1;
4609f988b79SJean-Baptiste Boric 		} else if (strcmp(name, "boot-load-segment") == 0) {
4619f988b79SJean-Baptiste Boric 			if (buf[0] == '\0') {
4629f988b79SJean-Baptiste Boric 				warnx("Option `%s' doesn't contain a value",
4639f988b79SJean-Baptiste Boric 				    name);
4649f988b79SJean-Baptiste Boric 				rv = 0;
4659f988b79SJean-Baptiste Boric 			} else {
4669f988b79SJean-Baptiste Boric 				cd9660_eltorito_add_boot_option(diskStructure,
4679f988b79SJean-Baptiste Boric 				    name, buf);
4689f988b79SJean-Baptiste Boric 				rv = 1;
4699f988b79SJean-Baptiste Boric 			}
4709f988b79SJean-Baptiste Boric 		} else
4719f988b79SJean-Baptiste Boric 			rv = 1;
4729f988b79SJean-Baptiste Boric 	}
4739f988b79SJean-Baptiste Boric 	return rv;
4749f988b79SJean-Baptiste Boric }
4759f988b79SJean-Baptiste Boric 
4769f988b79SJean-Baptiste Boric /*
4779f988b79SJean-Baptiste Boric  * Main function for cd9660_makefs
4789f988b79SJean-Baptiste Boric  * Builds the ISO image file
4799f988b79SJean-Baptiste Boric  * @param const char *image The image filename to create
4809f988b79SJean-Baptiste Boric  * @param const char *dir The directory that is being read
4819f988b79SJean-Baptiste Boric  * @param struct fsnode *root The root node of the filesystem tree
4829f988b79SJean-Baptiste Boric  * @param struct fsinfo_t *fsopts Any options
4839f988b79SJean-Baptiste Boric  */
4849f988b79SJean-Baptiste Boric void
cd9660_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)4859f988b79SJean-Baptiste Boric cd9660_makefs(const char *image, const char *dir, fsnode *root,
4869f988b79SJean-Baptiste Boric 	      fsinfo_t *fsopts)
4879f988b79SJean-Baptiste Boric {
4889f988b79SJean-Baptiste Boric 	int64_t startoffset;
4899f988b79SJean-Baptiste Boric 	int numDirectories;
4909f988b79SJean-Baptiste Boric 	uint64_t pathTableSectors;
4919f988b79SJean-Baptiste Boric 	int64_t firstAvailableSector;
4929f988b79SJean-Baptiste Boric 	int64_t totalSpace;
4939f988b79SJean-Baptiste Boric 	int error;
4949f988b79SJean-Baptiste Boric 	cd9660node *real_root;
4959f988b79SJean-Baptiste Boric 	iso9660_disk *diskStructure = fsopts->fs_specific;
4969f988b79SJean-Baptiste Boric 
4979f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
498*0a6a1f1dSLionel Sambuc 		printf("%s: ISO level is %i\n", __func__,
4999f988b79SJean-Baptiste Boric 		    diskStructure->isoLevel);
5009f988b79SJean-Baptiste Boric 	if (diskStructure->isoLevel < 2 &&
5019f988b79SJean-Baptiste Boric 	    diskStructure->allow_multidot)
502*0a6a1f1dSLionel Sambuc 		errx(EXIT_FAILURE, "allow-multidot requires iso level of 2");
5039f988b79SJean-Baptiste Boric 
5049f988b79SJean-Baptiste Boric 	assert(image != NULL);
5059f988b79SJean-Baptiste Boric 	assert(dir != NULL);
5069f988b79SJean-Baptiste Boric 	assert(root != NULL);
5079f988b79SJean-Baptiste Boric 
5089f988b79SJean-Baptiste Boric 	if (diskStructure->displayHelp) {
5099f988b79SJean-Baptiste Boric 		/*
5109f988b79SJean-Baptiste Boric 		 * Display help here - probably want to put it in
5119f988b79SJean-Baptiste Boric 		 * a separate function
5129f988b79SJean-Baptiste Boric 		 */
5139f988b79SJean-Baptiste Boric 		return;
5149f988b79SJean-Baptiste Boric 	}
5159f988b79SJean-Baptiste Boric 
5169f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
517*0a6a1f1dSLionel Sambuc 		printf("%s: image %s directory %s root %p\n", __func__,
5189f988b79SJean-Baptiste Boric 		    image, dir, root);
5199f988b79SJean-Baptiste Boric 
5209f988b79SJean-Baptiste Boric 	/* Set up some constants. Later, these will be defined with options */
5219f988b79SJean-Baptiste Boric 
5229f988b79SJean-Baptiste Boric 	/* Counter needed for path tables */
5239f988b79SJean-Baptiste Boric 	numDirectories = 0;
5249f988b79SJean-Baptiste Boric 
5259f988b79SJean-Baptiste Boric 	/* Convert tree to our own format */
5269f988b79SJean-Baptiste Boric 	/* Actually, we now need to add the REAL root node, at level 0 */
5279f988b79SJean-Baptiste Boric 
5289f988b79SJean-Baptiste Boric 	real_root = cd9660_allocate_cd9660node();
5299f988b79SJean-Baptiste Boric 	real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord));
5309f988b79SJean-Baptiste Boric 	/* Leave filename blank for root */
5319f988b79SJean-Baptiste Boric 	memset(real_root->isoDirRecord->name, 0,
5329f988b79SJean-Baptiste Boric 	    ISO_FILENAME_MAXLENGTH_WITH_PADDING);
5339f988b79SJean-Baptiste Boric 
5349f988b79SJean-Baptiste Boric 	real_root->level = 0;
5359f988b79SJean-Baptiste Boric 	diskStructure->rootNode = real_root;
5369f988b79SJean-Baptiste Boric 	real_root->type = CD9660_TYPE_DIR;
5379f988b79SJean-Baptiste Boric 	error = 0;
5389f988b79SJean-Baptiste Boric 	real_root->node = root;
5399f988b79SJean-Baptiste Boric 	cd9660_convert_structure(diskStructure, root, real_root, 1,
5409f988b79SJean-Baptiste Boric 	    &numDirectories, &error);
5419f988b79SJean-Baptiste Boric 
5429f988b79SJean-Baptiste Boric 	if (TAILQ_EMPTY(&real_root->cn_children)) {
543*0a6a1f1dSLionel Sambuc 		errx(EXIT_FAILURE, "%s: converted directory is empty. "
544*0a6a1f1dSLionel Sambuc 		    "Tree conversion failed", __func__);
5459f988b79SJean-Baptiste Boric 	} else if (error != 0) {
546*0a6a1f1dSLionel Sambuc 		errx(EXIT_FAILURE, "%s: tree conversion failed", __func__);
5479f988b79SJean-Baptiste Boric 	} else {
5489f988b79SJean-Baptiste Boric 		if (diskStructure->verbose_level > 0)
549*0a6a1f1dSLionel Sambuc 			printf("%s: tree converted\n", __func__);
5509f988b79SJean-Baptiste Boric 	}
5519f988b79SJean-Baptiste Boric 
5529f988b79SJean-Baptiste Boric 	/* Add the dot and dot dot records */
5539f988b79SJean-Baptiste Boric 	cd9660_add_dot_records(diskStructure, real_root);
5549f988b79SJean-Baptiste Boric 
5559f988b79SJean-Baptiste Boric 	cd9660_setup_root_node(diskStructure);
5569f988b79SJean-Baptiste Boric 
5579f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
558*0a6a1f1dSLionel Sambuc 		printf("%s: done converting tree\n", __func__);
5599f988b79SJean-Baptiste Boric 
5609f988b79SJean-Baptiste Boric 	/* non-SUSP extensions */
5619f988b79SJean-Baptiste Boric 	if (diskStructure->archimedes_enabled)
5629f988b79SJean-Baptiste Boric 		archimedes_convert_tree(diskStructure->rootNode);
5639f988b79SJean-Baptiste Boric 
5649f988b79SJean-Baptiste Boric 	/* Rock ridge / SUSP init pass */
5659f988b79SJean-Baptiste Boric 	if (diskStructure->rock_ridge_enabled) {
5669f988b79SJean-Baptiste Boric 		cd9660_susp_initialize(diskStructure, diskStructure->rootNode,
5679f988b79SJean-Baptiste Boric 		    diskStructure->rootNode, NULL);
5689f988b79SJean-Baptiste Boric 	}
5699f988b79SJean-Baptiste Boric 
5709f988b79SJean-Baptiste Boric 	/* Build path table structure */
5719f988b79SJean-Baptiste Boric 	diskStructure->pathTableLength = cd9660_generate_path_table(
5729f988b79SJean-Baptiste Boric 	    diskStructure);
5739f988b79SJean-Baptiste Boric 
5749f988b79SJean-Baptiste Boric 	pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize,
5759f988b79SJean-Baptiste Boric 		diskStructure->pathTableLength);
5769f988b79SJean-Baptiste Boric 
5779f988b79SJean-Baptiste Boric 	firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure);
5789f988b79SJean-Baptiste Boric 	if (diskStructure->is_bootable) {
5799f988b79SJean-Baptiste Boric 		firstAvailableSector = cd9660_setup_boot(diskStructure,
5809f988b79SJean-Baptiste Boric 		    firstAvailableSector);
5819f988b79SJean-Baptiste Boric 		if (firstAvailableSector < 0)
582*0a6a1f1dSLionel Sambuc 			errx(EXIT_FAILURE, "setup_boot failed");
5839f988b79SJean-Baptiste Boric 	}
5849f988b79SJean-Baptiste Boric 	/* LE first, then BE */
5859f988b79SJean-Baptiste Boric 	diskStructure->primaryLittleEndianTableSector = firstAvailableSector;
5869f988b79SJean-Baptiste Boric 	diskStructure->primaryBigEndianTableSector =
5879f988b79SJean-Baptiste Boric 		diskStructure->primaryLittleEndianTableSector + pathTableSectors;
5889f988b79SJean-Baptiste Boric 
5899f988b79SJean-Baptiste Boric 	/* Set the secondary ones to -1, not going to use them for now */
5909f988b79SJean-Baptiste Boric 	diskStructure->secondaryBigEndianTableSector = -1;
5919f988b79SJean-Baptiste Boric 	diskStructure->secondaryLittleEndianTableSector = -1;
5929f988b79SJean-Baptiste Boric 
5939f988b79SJean-Baptiste Boric 	diskStructure->dataFirstSector =
5949f988b79SJean-Baptiste Boric 	    diskStructure->primaryBigEndianTableSector + pathTableSectors;
5959f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
596*0a6a1f1dSLionel Sambuc 		printf("%s: Path table conversion complete. "
5979f988b79SJean-Baptiste Boric 		    "Each table is %i bytes, or %" PRIu64 " sectors.\n",
598*0a6a1f1dSLionel Sambuc 		    __func__,
5999f988b79SJean-Baptiste Boric 		    diskStructure->pathTableLength, pathTableSectors);
6009f988b79SJean-Baptiste Boric 
6019f988b79SJean-Baptiste Boric 	startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector;
6029f988b79SJean-Baptiste Boric 
6039f988b79SJean-Baptiste Boric 	totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset);
6049f988b79SJean-Baptiste Boric 
6059f988b79SJean-Baptiste Boric 	diskStructure->totalSectors = diskStructure->dataFirstSector +
6069f988b79SJean-Baptiste Boric 		CD9660_BLOCKS(diskStructure->sectorSize, totalSpace);
6079f988b79SJean-Baptiste Boric 
6089f988b79SJean-Baptiste Boric 	/* Disabled until pass 1 is done */
6099f988b79SJean-Baptiste Boric 	if (diskStructure->rock_ridge_enabled) {
6109f988b79SJean-Baptiste Boric 		diskStructure->susp_continuation_area_start_sector =
6119f988b79SJean-Baptiste Boric 		    diskStructure->totalSectors;
6129f988b79SJean-Baptiste Boric 		diskStructure->totalSectors +=
6139f988b79SJean-Baptiste Boric 		    CD9660_BLOCKS(diskStructure->sectorSize,
6149f988b79SJean-Baptiste Boric 			diskStructure->susp_continuation_area_size);
6159f988b79SJean-Baptiste Boric 		cd9660_susp_finalize(diskStructure, diskStructure->rootNode);
6169f988b79SJean-Baptiste Boric 	}
6179f988b79SJean-Baptiste Boric 
6189f988b79SJean-Baptiste Boric 
6199f988b79SJean-Baptiste Boric 	cd9660_finalize_PVD(diskStructure);
6209f988b79SJean-Baptiste Boric 
6219f988b79SJean-Baptiste Boric 	/* Add padding sectors, just for testing purposes right now */
6229f988b79SJean-Baptiste Boric 	/* diskStructure->totalSectors+=150; */
6239f988b79SJean-Baptiste Boric 
6249f988b79SJean-Baptiste Boric 	/* Debugging output */
6259f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0) {
626*0a6a1f1dSLionel Sambuc 		printf("%s: Sectors 0-15 reserved\n", __func__);
627*0a6a1f1dSLionel Sambuc 		printf("%s: Primary path tables starts in sector %"
628*0a6a1f1dSLionel Sambuc 		    PRId64 "\n", __func__,
629*0a6a1f1dSLionel Sambuc 		    diskStructure->primaryLittleEndianTableSector);
630*0a6a1f1dSLionel Sambuc 		printf("%s: File data starts in sector %"
631*0a6a1f1dSLionel Sambuc 		    PRId64 "\n", __func__, diskStructure->dataFirstSector);
632*0a6a1f1dSLionel Sambuc 		printf("%s: Total sectors: %"
633*0a6a1f1dSLionel Sambuc 		    PRId64 "\n", __func__, diskStructure->totalSectors);
6349f988b79SJean-Baptiste Boric 	}
6359f988b79SJean-Baptiste Boric 
6369f988b79SJean-Baptiste Boric 	/*
6379f988b79SJean-Baptiste Boric 	 * Add padding sectors at the end
6389f988b79SJean-Baptiste Boric 	 * TODO: Clean this up and separate padding
6399f988b79SJean-Baptiste Boric 	 */
6409f988b79SJean-Baptiste Boric 	if (diskStructure->include_padding_areas)
6419f988b79SJean-Baptiste Boric 		diskStructure->totalSectors += 150;
6429f988b79SJean-Baptiste Boric 
6439f988b79SJean-Baptiste Boric 	cd9660_write_image(diskStructure, image);
6449f988b79SJean-Baptiste Boric 
6459f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 1) {
6469f988b79SJean-Baptiste Boric 		debug_print_volume_descriptor_information(diskStructure);
6479f988b79SJean-Baptiste Boric 		debug_print_tree(diskStructure, real_root, 0);
6489f988b79SJean-Baptiste Boric 		debug_print_path_tree(real_root);
6499f988b79SJean-Baptiste Boric 	}
6509f988b79SJean-Baptiste Boric 
6519f988b79SJean-Baptiste Boric 	/* Clean up data structures */
6529f988b79SJean-Baptiste Boric 	cd9660_free_structure(real_root);
6539f988b79SJean-Baptiste Boric 
6549f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
655*0a6a1f1dSLionel Sambuc 		printf("%s: done\n", __func__);
6569f988b79SJean-Baptiste Boric }
6579f988b79SJean-Baptiste Boric 
6589f988b79SJean-Baptiste Boric /* Generic function pointer - implement later */
6599f988b79SJean-Baptiste Boric typedef int (*cd9660node_func)(cd9660node *);
6609f988b79SJean-Baptiste Boric 
6619f988b79SJean-Baptiste Boric static void
cd9660_finalize_PVD(iso9660_disk * diskStructure)6629f988b79SJean-Baptiste Boric cd9660_finalize_PVD(iso9660_disk *diskStructure)
6639f988b79SJean-Baptiste Boric {
6649f988b79SJean-Baptiste Boric 	time_t tim;
6659f988b79SJean-Baptiste Boric 
6669f988b79SJean-Baptiste Boric 	/* root should be a fixed size of 34 bytes since it has no name */
6679f988b79SJean-Baptiste Boric 	memcpy(diskStructure->primaryDescriptor.root_directory_record,
6689f988b79SJean-Baptiste Boric 		diskStructure->rootNode->dot_record->isoDirRecord, 34);
6699f988b79SJean-Baptiste Boric 
6709f988b79SJean-Baptiste Boric 	/* In RRIP, this might be longer than 34 */
6719f988b79SJean-Baptiste Boric 	diskStructure->primaryDescriptor.root_directory_record[0] = 34;
6729f988b79SJean-Baptiste Boric 
6739f988b79SJean-Baptiste Boric 	/* Set up all the important numbers in the PVD */
6749f988b79SJean-Baptiste Boric 	cd9660_bothendian_dword(diskStructure->totalSectors,
6759f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.volume_space_size);
6769f988b79SJean-Baptiste Boric 	cd9660_bothendian_word(1,
6779f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.volume_set_size);
6789f988b79SJean-Baptiste Boric 	cd9660_bothendian_word(1,
6799f988b79SJean-Baptiste Boric 	    (unsigned char *)
6809f988b79SJean-Baptiste Boric 		diskStructure->primaryDescriptor.volume_sequence_number);
6819f988b79SJean-Baptiste Boric 	cd9660_bothendian_word(diskStructure->sectorSize,
6829f988b79SJean-Baptiste Boric 	    (unsigned char *)
6839f988b79SJean-Baptiste Boric 		diskStructure->primaryDescriptor.logical_block_size);
6849f988b79SJean-Baptiste Boric 	cd9660_bothendian_dword(diskStructure->pathTableLength,
6859f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.path_table_size);
6869f988b79SJean-Baptiste Boric 
6879f988b79SJean-Baptiste Boric 	cd9660_731(diskStructure->primaryLittleEndianTableSector,
6889f988b79SJean-Baptiste Boric 		(u_char *)diskStructure->primaryDescriptor.type_l_path_table);
6899f988b79SJean-Baptiste Boric 	cd9660_732(diskStructure->primaryBigEndianTableSector,
6909f988b79SJean-Baptiste Boric 		(u_char *)diskStructure->primaryDescriptor.type_m_path_table);
6919f988b79SJean-Baptiste Boric 
6929f988b79SJean-Baptiste Boric 	diskStructure->primaryDescriptor.file_structure_version[0] = 1;
6939f988b79SJean-Baptiste Boric 
6949f988b79SJean-Baptiste Boric 	/* Pad all strings with spaces instead of nulls */
6959f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32);
6969f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32);
6979f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id,
6989f988b79SJean-Baptiste Boric 	    128);
6999f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id,
7009f988b79SJean-Baptiste Boric 	    128);
7019f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id,
7029f988b79SJean-Baptiste Boric 	    128);
7039f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id,
7049f988b79SJean-Baptiste Boric 	    128);
7059f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(
7069f988b79SJean-Baptiste Boric 	    diskStructure->primaryDescriptor.copyright_file_id, 37);
7079f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(
7089f988b79SJean-Baptiste Boric 		diskStructure->primaryDescriptor.abstract_file_id, 37);
7099f988b79SJean-Baptiste Boric 	cd9660_pad_string_spaces(
7109f988b79SJean-Baptiste Boric 		diskStructure->primaryDescriptor.bibliographic_file_id, 37);
7119f988b79SJean-Baptiste Boric 
7129f988b79SJean-Baptiste Boric 	/* Setup dates */
7139f988b79SJean-Baptiste Boric 	time(&tim);
7149f988b79SJean-Baptiste Boric 	cd9660_time_8426(
7159f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.creation_date,
7169f988b79SJean-Baptiste Boric 	    tim);
7179f988b79SJean-Baptiste Boric 	cd9660_time_8426(
7189f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.modification_date,
7199f988b79SJean-Baptiste Boric 	    tim);
7209f988b79SJean-Baptiste Boric 
7219f988b79SJean-Baptiste Boric 	/*
7229f988b79SJean-Baptiste Boric 	cd9660_set_date(diskStructure->primaryDescriptor.expiration_date, now);
7239f988b79SJean-Baptiste Boric 	*/
7249f988b79SJean-Baptiste Boric 	memset(diskStructure->primaryDescriptor.expiration_date, '0' ,16);
7259f988b79SJean-Baptiste Boric 	diskStructure->primaryDescriptor.expiration_date[16] = 0;
7269f988b79SJean-Baptiste Boric 
7279f988b79SJean-Baptiste Boric 	cd9660_time_8426(
7289f988b79SJean-Baptiste Boric 	    (unsigned char *)diskStructure->primaryDescriptor.effective_date,
7299f988b79SJean-Baptiste Boric 	    tim);
7309f988b79SJean-Baptiste Boric }
7319f988b79SJean-Baptiste Boric 
7329f988b79SJean-Baptiste Boric static void
cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 * record,u_char ext_attr_length,u_char flags,u_char name_len,const char * name)7339f988b79SJean-Baptiste Boric cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
7349f988b79SJean-Baptiste Boric 			       u_char ext_attr_length, u_char flags,
7359f988b79SJean-Baptiste Boric 			       u_char name_len, const char * name)
7369f988b79SJean-Baptiste Boric {
7379f988b79SJean-Baptiste Boric 	record->ext_attr_length[0] = ext_attr_length;
7389f988b79SJean-Baptiste Boric 	record->flags[0] = ISO_FLAG_CLEAR | flags;
7399f988b79SJean-Baptiste Boric 	record->file_unit_size[0] = 0;
7409f988b79SJean-Baptiste Boric 	record->interleave[0] = 0;
7419f988b79SJean-Baptiste Boric 	cd9660_bothendian_word(1, record->volume_sequence_number);
7429f988b79SJean-Baptiste Boric 	record->name_len[0] = name_len;
7439f988b79SJean-Baptiste Boric 	memset(record->name, '\0', sizeof (record->name));
7449f988b79SJean-Baptiste Boric 	memcpy(record->name, name, name_len);
7459f988b79SJean-Baptiste Boric 	record->length[0] = 33 + name_len;
7469f988b79SJean-Baptiste Boric 
7479f988b79SJean-Baptiste Boric 	/* Todo : better rounding */
7489f988b79SJean-Baptiste Boric 	record->length[0] += (record->length[0] & 1) ? 1 : 0;
7499f988b79SJean-Baptiste Boric }
7509f988b79SJean-Baptiste Boric 
7519f988b79SJean-Baptiste Boric static void
cd9660_setup_root_node(iso9660_disk * diskStructure)7529f988b79SJean-Baptiste Boric cd9660_setup_root_node(iso9660_disk *diskStructure)
7539f988b79SJean-Baptiste Boric {
7549f988b79SJean-Baptiste Boric 	cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord,
7559f988b79SJean-Baptiste Boric 	    0, ISO_FLAG_DIRECTORY, 1, "\0");
7569f988b79SJean-Baptiste Boric 
7579f988b79SJean-Baptiste Boric }
7589f988b79SJean-Baptiste Boric 
7599f988b79SJean-Baptiste Boric /*********** SUPPORT FUNCTIONS ***********/
7609f988b79SJean-Baptiste Boric static int
cd9660_setup_volume_descriptors(iso9660_disk * diskStructure)7619f988b79SJean-Baptiste Boric cd9660_setup_volume_descriptors(iso9660_disk *diskStructure)
7629f988b79SJean-Baptiste Boric {
7639f988b79SJean-Baptiste Boric 	/* Boot volume descriptor should come second */
7649f988b79SJean-Baptiste Boric 	int sector = 16;
7659f988b79SJean-Baptiste Boric 	/* For now, a fixed 2 : PVD and terminator */
7669f988b79SJean-Baptiste Boric 	volume_descriptor *temp, *t;
7679f988b79SJean-Baptiste Boric 
7689f988b79SJean-Baptiste Boric 	/* Set up the PVD */
7699f988b79SJean-Baptiste Boric 	temp = emalloc(sizeof(*temp));
7709f988b79SJean-Baptiste Boric 	temp->volumeDescriptorData =
7719f988b79SJean-Baptiste Boric 	   (unsigned char *)&diskStructure->primaryDescriptor;
7729f988b79SJean-Baptiste Boric 	temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
7739f988b79SJean-Baptiste Boric 	temp->volumeDescriptorData[6] = 1;
7749f988b79SJean-Baptiste Boric 	temp->sector = sector;
7759f988b79SJean-Baptiste Boric 	memcpy(temp->volumeDescriptorData + 1,
7769f988b79SJean-Baptiste Boric 	    ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
7779f988b79SJean-Baptiste Boric 	diskStructure->firstVolumeDescriptor = temp;
7789f988b79SJean-Baptiste Boric 
7799f988b79SJean-Baptiste Boric 	sector++;
7809f988b79SJean-Baptiste Boric 	/* Set up boot support if enabled. BVD must reside in sector 17 */
7819f988b79SJean-Baptiste Boric 	if (diskStructure->is_bootable) {
7829f988b79SJean-Baptiste Boric 		t = emalloc(sizeof(*t));
7839f988b79SJean-Baptiste Boric 		t->volumeDescriptorData = ecalloc(1, 2048);
7849f988b79SJean-Baptiste Boric 		temp->next = t;
7859f988b79SJean-Baptiste Boric 		temp = t;
7869f988b79SJean-Baptiste Boric 		t->sector = 17;
7879f988b79SJean-Baptiste Boric 		if (diskStructure->verbose_level > 0)
7889f988b79SJean-Baptiste Boric 			printf("Setting up boot volume descriptor\n");
7899f988b79SJean-Baptiste Boric 		cd9660_setup_boot_volume_descriptor(diskStructure, t);
7909f988b79SJean-Baptiste Boric 		sector++;
7919f988b79SJean-Baptiste Boric 	}
7929f988b79SJean-Baptiste Boric 
7939f988b79SJean-Baptiste Boric 	/* Set up the terminator */
7949f988b79SJean-Baptiste Boric 	t = emalloc(sizeof(*t));
7959f988b79SJean-Baptiste Boric 	t->volumeDescriptorData = ecalloc(1, 2048);
7969f988b79SJean-Baptiste Boric 	temp->next = t;
7979f988b79SJean-Baptiste Boric 	t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
7989f988b79SJean-Baptiste Boric 	t->next = 0;
7999f988b79SJean-Baptiste Boric 	t->volumeDescriptorData[6] = 1;
8009f988b79SJean-Baptiste Boric 	t->sector = sector;
8019f988b79SJean-Baptiste Boric 	memcpy(t->volumeDescriptorData + 1,
8029f988b79SJean-Baptiste Boric 	    ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
8039f988b79SJean-Baptiste Boric 
8049f988b79SJean-Baptiste Boric 	sector++;
8059f988b79SJean-Baptiste Boric 	return sector;
8069f988b79SJean-Baptiste Boric }
8079f988b79SJean-Baptiste Boric 
8089f988b79SJean-Baptiste Boric #if 0
8099f988b79SJean-Baptiste Boric /*
8109f988b79SJean-Baptiste Boric  * Populate EAR at some point. Not required, but is used by NetBSD's
8119f988b79SJean-Baptiste Boric  * cd9660 support
8129f988b79SJean-Baptiste Boric  */
8139f988b79SJean-Baptiste Boric static int
8149f988b79SJean-Baptiste Boric cd9660_fill_extended_attribute_record(cd9660node *node)
8159f988b79SJean-Baptiste Boric {
8169f988b79SJean-Baptiste Boric 	node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes));
8179f988b79SJean-Baptiste Boric 	return 1;
8189f988b79SJean-Baptiste Boric }
8199f988b79SJean-Baptiste Boric #endif
8209f988b79SJean-Baptiste Boric 
8219f988b79SJean-Baptiste Boric static int
cd9660_translate_node_common(iso9660_disk * diskStructure,cd9660node * newnode)8229f988b79SJean-Baptiste Boric cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode)
8239f988b79SJean-Baptiste Boric {
8249f988b79SJean-Baptiste Boric 	time_t tim;
8259f988b79SJean-Baptiste Boric 	u_char flag;
8269f988b79SJean-Baptiste Boric 	char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
8279f988b79SJean-Baptiste Boric 
8289f988b79SJean-Baptiste Boric 	/* Now populate the isoDirRecord structure */
8299f988b79SJean-Baptiste Boric 	memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
8309f988b79SJean-Baptiste Boric 
8319f988b79SJean-Baptiste Boric 	(void)cd9660_convert_filename(diskStructure, newnode->node->name,
8329f988b79SJean-Baptiste Boric 	    temp, !(S_ISDIR(newnode->node->type)));
8339f988b79SJean-Baptiste Boric 
8349f988b79SJean-Baptiste Boric 	flag = ISO_FLAG_CLEAR;
8359f988b79SJean-Baptiste Boric 	if (S_ISDIR(newnode->node->type))
8369f988b79SJean-Baptiste Boric 		flag |= ISO_FLAG_DIRECTORY;
8379f988b79SJean-Baptiste Boric 
8389f988b79SJean-Baptiste Boric 	cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
8399f988b79SJean-Baptiste Boric 	    flag, strlen(temp), temp);
8409f988b79SJean-Baptiste Boric 
8419f988b79SJean-Baptiste Boric 	/* Set the various dates */
8429f988b79SJean-Baptiste Boric 
8439f988b79SJean-Baptiste Boric 	/* If we want to use the current date and time */
8449f988b79SJean-Baptiste Boric 	time(&tim);
8459f988b79SJean-Baptiste Boric 
8469f988b79SJean-Baptiste Boric 	cd9660_time_915(newnode->isoDirRecord->date, tim);
8479f988b79SJean-Baptiste Boric 
8489f988b79SJean-Baptiste Boric 	cd9660_bothendian_dword(newnode->fileDataLength,
8499f988b79SJean-Baptiste Boric 	    newnode->isoDirRecord->size);
8509f988b79SJean-Baptiste Boric 	/* If the file is a link, we want to set the size to 0 */
8519f988b79SJean-Baptiste Boric 	if (S_ISLNK(newnode->node->type))
8529f988b79SJean-Baptiste Boric 		newnode->fileDataLength = 0;
8539f988b79SJean-Baptiste Boric 
8549f988b79SJean-Baptiste Boric 	return 1;
8559f988b79SJean-Baptiste Boric }
8569f988b79SJean-Baptiste Boric 
8579f988b79SJean-Baptiste Boric /*
8589f988b79SJean-Baptiste Boric  * Translate fsnode to cd9660node
8599f988b79SJean-Baptiste Boric  * Translate filenames and other metadata, including dates, sizes,
8609f988b79SJean-Baptiste Boric  * permissions, etc
8619f988b79SJean-Baptiste Boric  * @param struct fsnode * The node generated by makefs
8629f988b79SJean-Baptiste Boric  * @param struct cd9660node * The intermediate node to be written to
8639f988b79SJean-Baptiste Boric  * @returns int 0 on failure, 1 on success
8649f988b79SJean-Baptiste Boric  */
8659f988b79SJean-Baptiste Boric static int
cd9660_translate_node(iso9660_disk * diskStructure,fsnode * node,cd9660node * newnode)8669f988b79SJean-Baptiste Boric cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node,
8679f988b79SJean-Baptiste Boric     cd9660node *newnode)
8689f988b79SJean-Baptiste Boric {
8699f988b79SJean-Baptiste Boric 	if (node == NULL) {
8709f988b79SJean-Baptiste Boric 		if (diskStructure->verbose_level > 0)
871*0a6a1f1dSLionel Sambuc 			printf("%s: NULL node passed, returning\n", __func__);
8729f988b79SJean-Baptiste Boric 		return 0;
8739f988b79SJean-Baptiste Boric 	}
8749f988b79SJean-Baptiste Boric 	newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord));
8759f988b79SJean-Baptiste Boric 	/* Set the node pointer */
8769f988b79SJean-Baptiste Boric 	newnode->node = node;
8779f988b79SJean-Baptiste Boric 
8789f988b79SJean-Baptiste Boric 	/* Set the size */
8799f988b79SJean-Baptiste Boric 	if (!(S_ISDIR(node->type)))
8809f988b79SJean-Baptiste Boric 		newnode->fileDataLength = node->inode->st.st_size;
8819f988b79SJean-Baptiste Boric 
8829f988b79SJean-Baptiste Boric 	if (cd9660_translate_node_common(diskStructure, newnode) == 0)
8839f988b79SJean-Baptiste Boric 		return 0;
8849f988b79SJean-Baptiste Boric 
8859f988b79SJean-Baptiste Boric 	/* Finally, overwrite some of the values that are set by default */
8869f988b79SJean-Baptiste Boric 	cd9660_time_915(newnode->isoDirRecord->date, node->inode->st.st_mtime);
8879f988b79SJean-Baptiste Boric 
8889f988b79SJean-Baptiste Boric 	return 1;
8899f988b79SJean-Baptiste Boric }
8909f988b79SJean-Baptiste Boric 
8919f988b79SJean-Baptiste Boric /*
8929f988b79SJean-Baptiste Boric  * Compares two ISO filenames
8939f988b79SJean-Baptiste Boric  * @param const char * The first file name
8949f988b79SJean-Baptiste Boric  * @param const char * The second file name
8959f988b79SJean-Baptiste Boric  * @returns : -1 if first is less than second, 0 if they are the same, 1 if
8969f988b79SJean-Baptiste Boric  * 	the second is greater than the first
8979f988b79SJean-Baptiste Boric  */
8989f988b79SJean-Baptiste Boric static int
cd9660_compare_filename(const char * first,const char * second)8999f988b79SJean-Baptiste Boric cd9660_compare_filename(const char *first, const char *second)
9009f988b79SJean-Baptiste Boric {
9019f988b79SJean-Baptiste Boric 	/*
9029f988b79SJean-Baptiste Boric 	 * This can be made more optimal once it has been tested
9039f988b79SJean-Baptiste Boric 	 * (the extra character, for example, is for testing)
9049f988b79SJean-Baptiste Boric 	 */
9059f988b79SJean-Baptiste Boric 
9069f988b79SJean-Baptiste Boric 	int p1 = 0;
9079f988b79SJean-Baptiste Boric 	int p2 = 0;
9089f988b79SJean-Baptiste Boric 	char c1, c2;
9099f988b79SJean-Baptiste Boric 	/* First, on the filename */
9109f988b79SJean-Baptiste Boric 
9119f988b79SJean-Baptiste Boric 	while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
9129f988b79SJean-Baptiste Boric 		&& p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
9139f988b79SJean-Baptiste Boric 		c1 = first[p1];
9149f988b79SJean-Baptiste Boric 		c2 = second[p2];
9159f988b79SJean-Baptiste Boric 		if (c1 == '.' && c2 =='.')
9169f988b79SJean-Baptiste Boric 			break;
9179f988b79SJean-Baptiste Boric 		else if (c1 == '.') {
9189f988b79SJean-Baptiste Boric 			p2++;
9199f988b79SJean-Baptiste Boric 			c1 = ' ';
9209f988b79SJean-Baptiste Boric 		} else if (c2 == '.') {
9219f988b79SJean-Baptiste Boric 			p1++;
9229f988b79SJean-Baptiste Boric 			c2 = ' ';
9239f988b79SJean-Baptiste Boric 		} else {
9249f988b79SJean-Baptiste Boric 			p1++;
9259f988b79SJean-Baptiste Boric 			p2++;
9269f988b79SJean-Baptiste Boric 		}
9279f988b79SJean-Baptiste Boric 
9289f988b79SJean-Baptiste Boric 		if (c1 < c2)
9299f988b79SJean-Baptiste Boric 			return -1;
9309f988b79SJean-Baptiste Boric 		else if (c1 > c2) {
9319f988b79SJean-Baptiste Boric 			return 1;
9329f988b79SJean-Baptiste Boric 		}
9339f988b79SJean-Baptiste Boric 	}
9349f988b79SJean-Baptiste Boric 
9359f988b79SJean-Baptiste Boric 	if (first[p1] == '.' && second[p2] == '.') {
9369f988b79SJean-Baptiste Boric 		p1++;
9379f988b79SJean-Baptiste Boric 		p2++;
9389f988b79SJean-Baptiste Boric 		while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
9399f988b79SJean-Baptiste Boric 			&& p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
9409f988b79SJean-Baptiste Boric 			c1 = first[p1];
9419f988b79SJean-Baptiste Boric 			c2 = second[p2];
9429f988b79SJean-Baptiste Boric 			if (c1 == ';' && c2 == ';')
9439f988b79SJean-Baptiste Boric 				break;
9449f988b79SJean-Baptiste Boric 			else if (c1 == ';') {
9459f988b79SJean-Baptiste Boric 				p2++;
9469f988b79SJean-Baptiste Boric 				c1 = ' ';
9479f988b79SJean-Baptiste Boric 			} else if (c2 == ';') {
9489f988b79SJean-Baptiste Boric 				p1++;
9499f988b79SJean-Baptiste Boric 				c2 = ' ';
9509f988b79SJean-Baptiste Boric 			} else {
9519f988b79SJean-Baptiste Boric 				p1++;
9529f988b79SJean-Baptiste Boric 				p2++;
9539f988b79SJean-Baptiste Boric 			}
9549f988b79SJean-Baptiste Boric 
9559f988b79SJean-Baptiste Boric 			if (c1 < c2)
9569f988b79SJean-Baptiste Boric 				return -1;
9579f988b79SJean-Baptiste Boric 			else if (c1 > c2)
9589f988b79SJean-Baptiste Boric 				return 1;
9599f988b79SJean-Baptiste Boric 		}
9609f988b79SJean-Baptiste Boric 	}
9619f988b79SJean-Baptiste Boric 	return 0;
9629f988b79SJean-Baptiste Boric }
9639f988b79SJean-Baptiste Boric 
9649f988b79SJean-Baptiste Boric /*
9659f988b79SJean-Baptiste Boric  * Insert a node into list with ISO sorting rules
9669f988b79SJean-Baptiste Boric  * @param cd9660node * The head node of the list
9679f988b79SJean-Baptiste Boric  * @param cd9660node * The node to be inserted
9689f988b79SJean-Baptiste Boric  */
9699f988b79SJean-Baptiste Boric static void
cd9660_sorted_child_insert(cd9660node * parent,cd9660node * cn_new)9709f988b79SJean-Baptiste Boric cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
9719f988b79SJean-Baptiste Boric {
9729f988b79SJean-Baptiste Boric 	int compare;
9739f988b79SJean-Baptiste Boric 	cd9660node *cn;
9749f988b79SJean-Baptiste Boric 	struct cd9660_children_head *head = &parent->cn_children;
9759f988b79SJean-Baptiste Boric 
9769f988b79SJean-Baptiste Boric 	/* TODO: Optimize? */
9779f988b79SJean-Baptiste Boric 	cn_new->parent = parent;
9789f988b79SJean-Baptiste Boric 
9799f988b79SJean-Baptiste Boric 	/*
9809f988b79SJean-Baptiste Boric 	 * first will either be 0, the . or the ..
9819f988b79SJean-Baptiste Boric 	 * if . or .., this means no other entry may be written before first
9829f988b79SJean-Baptiste Boric 	 * if 0, the new node may be inserted at the head
9839f988b79SJean-Baptiste Boric 	 */
9849f988b79SJean-Baptiste Boric 
9859f988b79SJean-Baptiste Boric 	TAILQ_FOREACH(cn, head, cn_next_child) {
9869f988b79SJean-Baptiste Boric 		/*
9879f988b79SJean-Baptiste Boric 		 * Dont insert a node twice -
9889f988b79SJean-Baptiste Boric 		 * that would cause an infinite loop
9899f988b79SJean-Baptiste Boric 		 */
9909f988b79SJean-Baptiste Boric 		if (cn_new == cn)
9919f988b79SJean-Baptiste Boric 			return;
9929f988b79SJean-Baptiste Boric 
9939f988b79SJean-Baptiste Boric 		compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
9949f988b79SJean-Baptiste Boric 			cn->isoDirRecord->name);
9959f988b79SJean-Baptiste Boric 
9969f988b79SJean-Baptiste Boric 		if (compare == 0)
9979f988b79SJean-Baptiste Boric 			compare = cd9660_compare_filename(cn_new->node->name,
9989f988b79SJean-Baptiste Boric 				cn->node->name);
9999f988b79SJean-Baptiste Boric 
10009f988b79SJean-Baptiste Boric 		if (compare < 0)
10019f988b79SJean-Baptiste Boric 			break;
10029f988b79SJean-Baptiste Boric 	}
10039f988b79SJean-Baptiste Boric 	if (cn == NULL)
10049f988b79SJean-Baptiste Boric 		TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
10059f988b79SJean-Baptiste Boric 	else
10069f988b79SJean-Baptiste Boric 		TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
10079f988b79SJean-Baptiste Boric }
10089f988b79SJean-Baptiste Boric 
10099f988b79SJean-Baptiste Boric /*
10109f988b79SJean-Baptiste Boric  * Called After cd9660_sorted_child_insert
10119f988b79SJean-Baptiste Boric  * handles file collisions by suffixing each filname with ~n
10129f988b79SJean-Baptiste Boric  * where n represents the files respective place in the ordering
10139f988b79SJean-Baptiste Boric  */
10149f988b79SJean-Baptiste Boric static int
cd9660_handle_collisions(iso9660_disk * diskStructure,cd9660node * colliding,int past)10159f988b79SJean-Baptiste Boric cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding,
10169f988b79SJean-Baptiste Boric     int past)
10179f988b79SJean-Baptiste Boric {
10189f988b79SJean-Baptiste Boric 	cd9660node *iter, *next, *prev;
10199f988b79SJean-Baptiste Boric 	int skip;
10209f988b79SJean-Baptiste Boric 	int delete_chars = 0;
10219f988b79SJean-Baptiste Boric 	int temp_past = past;
10229f988b79SJean-Baptiste Boric 	int temp_skip;
10239f988b79SJean-Baptiste Boric 	int flag = 0;
10249f988b79SJean-Baptiste Boric 	cd9660node *end_of_range;
10259f988b79SJean-Baptiste Boric 
10269f988b79SJean-Baptiste Boric 	for (iter = TAILQ_FIRST(&colliding->cn_children);
10279f988b79SJean-Baptiste Boric 	     iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
10289f988b79SJean-Baptiste Boric 		if (strcmp(iter->isoDirRecord->name,
10299f988b79SJean-Baptiste Boric 		           next->isoDirRecord->name) != 0) {
10309f988b79SJean-Baptiste Boric 			iter = TAILQ_NEXT(iter, cn_next_child);
10319f988b79SJean-Baptiste Boric 			continue;
10329f988b79SJean-Baptiste Boric 		}
10339f988b79SJean-Baptiste Boric 		flag = 1;
10349f988b79SJean-Baptiste Boric 		temp_skip = skip = cd9660_count_collisions(iter);
10359f988b79SJean-Baptiste Boric 		end_of_range = iter;
10369f988b79SJean-Baptiste Boric 		while (temp_skip > 0) {
10379f988b79SJean-Baptiste Boric 			temp_skip--;
10389f988b79SJean-Baptiste Boric 			end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
10399f988b79SJean-Baptiste Boric 		}
10409f988b79SJean-Baptiste Boric 		temp_past = past;
10419f988b79SJean-Baptiste Boric 		while (temp_past > 0) {
10429f988b79SJean-Baptiste Boric 			if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
10439f988b79SJean-Baptiste Boric 				end_of_range = next;
10449f988b79SJean-Baptiste Boric 			else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
10459f988b79SJean-Baptiste Boric 				iter = prev;
10469f988b79SJean-Baptiste Boric 			else
10479f988b79SJean-Baptiste Boric 				delete_chars++;
10489f988b79SJean-Baptiste Boric 			temp_past--;
10499f988b79SJean-Baptiste Boric 		}
10509f988b79SJean-Baptiste Boric 		skip += past;
10519f988b79SJean-Baptiste Boric 		iter = cd9660_rename_filename(diskStructure, iter, skip,
10529f988b79SJean-Baptiste Boric 		    delete_chars);
10539f988b79SJean-Baptiste Boric 	}
10549f988b79SJean-Baptiste Boric 	return flag;
10559f988b79SJean-Baptiste Boric }
10569f988b79SJean-Baptiste Boric 
10579f988b79SJean-Baptiste Boric 
10589f988b79SJean-Baptiste Boric static cd9660node *
cd9660_rename_filename(iso9660_disk * diskStructure,cd9660node * iter,int num,int delete_chars)10599f988b79SJean-Baptiste Boric cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num,
10609f988b79SJean-Baptiste Boric     int delete_chars)
10619f988b79SJean-Baptiste Boric {
10629f988b79SJean-Baptiste Boric 	int i = 0;
10639f988b79SJean-Baptiste Boric 	int numbts, dot, semi, digit, digits, temp, powers, multiplier, count;
10649f988b79SJean-Baptiste Boric 	char *naming;
10659f988b79SJean-Baptiste Boric 	int maxlength;
10669f988b79SJean-Baptiste Boric         char *tmp;
10679f988b79SJean-Baptiste Boric 
10689f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0)
10699f988b79SJean-Baptiste Boric 		printf("Rename_filename called\n");
10709f988b79SJean-Baptiste Boric 
10719f988b79SJean-Baptiste Boric 	/* TODO : A LOT of chanes regarding 8.3 filenames */
10729f988b79SJean-Baptiste Boric 	if (diskStructure->isoLevel == 1)
10739f988b79SJean-Baptiste Boric 		maxlength = 8;
10749f988b79SJean-Baptiste Boric 	else if (diskStructure->isoLevel == 2)
10759f988b79SJean-Baptiste Boric 		maxlength = 31;
10769f988b79SJean-Baptiste Boric 	else
10779f988b79SJean-Baptiste Boric 		maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
10789f988b79SJean-Baptiste Boric 
10799f988b79SJean-Baptiste Boric 	tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
10809f988b79SJean-Baptiste Boric 
10819f988b79SJean-Baptiste Boric 	while (i < num) {
10829f988b79SJean-Baptiste Boric 		powers = 1;
10839f988b79SJean-Baptiste Boric 		count = 0;
10849f988b79SJean-Baptiste Boric 		digits = 1;
10859f988b79SJean-Baptiste Boric 		multiplier = 1;
10869f988b79SJean-Baptiste Boric 		while (((int)(i / powers) ) >= 10) {
10879f988b79SJean-Baptiste Boric 			digits++;
10889f988b79SJean-Baptiste Boric 			powers = powers * 10;
10899f988b79SJean-Baptiste Boric 		}
10909f988b79SJean-Baptiste Boric 
10919f988b79SJean-Baptiste Boric 		naming = iter->o_name;
10929f988b79SJean-Baptiste Boric 
10939f988b79SJean-Baptiste Boric 		/*
10949f988b79SJean-Baptiste Boric 		while ((*naming != '.') && (*naming != ';')) {
10959f988b79SJean-Baptiste Boric 			naming++;
10969f988b79SJean-Baptiste Boric 			count++;
10979f988b79SJean-Baptiste Boric 		}
10989f988b79SJean-Baptiste Boric 		*/
10999f988b79SJean-Baptiste Boric 
11009f988b79SJean-Baptiste Boric 		dot = -1;
11019f988b79SJean-Baptiste Boric 		semi = -1;
11029f988b79SJean-Baptiste Boric 		while (count < maxlength) {
11039f988b79SJean-Baptiste Boric 			if (*naming == '.')
11049f988b79SJean-Baptiste Boric 				dot = count;
11059f988b79SJean-Baptiste Boric 			else if (*naming == ';') {
11069f988b79SJean-Baptiste Boric 				semi = count;
11079f988b79SJean-Baptiste Boric 				break;
11089f988b79SJean-Baptiste Boric 			}
11099f988b79SJean-Baptiste Boric 			naming++;
11109f988b79SJean-Baptiste Boric 			count++;
11119f988b79SJean-Baptiste Boric 		}
11129f988b79SJean-Baptiste Boric 
11139f988b79SJean-Baptiste Boric 		if ((count + digits) < maxlength)
11149f988b79SJean-Baptiste Boric 			numbts = count;
11159f988b79SJean-Baptiste Boric 		else
11169f988b79SJean-Baptiste Boric 			numbts = maxlength - (digits);
11179f988b79SJean-Baptiste Boric 		numbts -= delete_chars;
11189f988b79SJean-Baptiste Boric 
11199f988b79SJean-Baptiste Boric 		/* 8.3 rules - keep the extension, add before the dot */
11209f988b79SJean-Baptiste Boric 
11219f988b79SJean-Baptiste Boric 		/*
11229f988b79SJean-Baptiste Boric 		 * This code makes a bunch of assumptions.
11239f988b79SJean-Baptiste Boric 		 * See if you can spot them all :)
11249f988b79SJean-Baptiste Boric 		 */
11259f988b79SJean-Baptiste Boric 
11269f988b79SJean-Baptiste Boric #if 0
11279f988b79SJean-Baptiste Boric 		if (diskStructure->isoLevel == 1) {
11289f988b79SJean-Baptiste Boric 			numbts = 8 - digits - delete_chars;
11299f988b79SJean-Baptiste Boric 			if (dot < 0) {
11309f988b79SJean-Baptiste Boric 
11319f988b79SJean-Baptiste Boric 			} else {
11329f988b79SJean-Baptiste Boric 				if (dot < 8) {
11339f988b79SJean-Baptiste Boric 					memmove(&tmp[numbts],&tmp[dot],4);
11349f988b79SJean-Baptiste Boric 				}
11359f988b79SJean-Baptiste Boric 			}
11369f988b79SJean-Baptiste Boric 		}
11379f988b79SJean-Baptiste Boric #else
11389f988b79SJean-Baptiste Boric 		__USE(dot);
11399f988b79SJean-Baptiste Boric 		__USE(semi);
11409f988b79SJean-Baptiste Boric 		__USE(multiplier);
11419f988b79SJean-Baptiste Boric #endif
11429f988b79SJean-Baptiste Boric 
11439f988b79SJean-Baptiste Boric 		/* (copying just the filename before the '.' */
11449f988b79SJean-Baptiste Boric 		memcpy(tmp, (iter->o_name), numbts);
11459f988b79SJean-Baptiste Boric 
11469f988b79SJean-Baptiste Boric 		/* adding the appropriate number following the name */
11479f988b79SJean-Baptiste Boric 		temp = i;
11489f988b79SJean-Baptiste Boric 		while (digits > 0) {
11499f988b79SJean-Baptiste Boric 			digit = (int)(temp / powers);
11509f988b79SJean-Baptiste Boric 			temp = temp - digit * powers;
11519f988b79SJean-Baptiste Boric 			sprintf(&tmp[numbts] , "%d", digit);
11529f988b79SJean-Baptiste Boric 			digits--;
11539f988b79SJean-Baptiste Boric 			numbts++;
11549f988b79SJean-Baptiste Boric 			powers = powers / 10;
11559f988b79SJean-Baptiste Boric 		}
11569f988b79SJean-Baptiste Boric 
11579f988b79SJean-Baptiste Boric 		while ((*naming != ';')  && (numbts < maxlength)) {
11589f988b79SJean-Baptiste Boric 			tmp[numbts] = (*naming);
11599f988b79SJean-Baptiste Boric 			naming++;
11609f988b79SJean-Baptiste Boric 			numbts++;
11619f988b79SJean-Baptiste Boric 		}
11629f988b79SJean-Baptiste Boric 
11639f988b79SJean-Baptiste Boric 		tmp[numbts] = ';';
11649f988b79SJean-Baptiste Boric 		tmp[numbts+1] = '1';
11659f988b79SJean-Baptiste Boric 		tmp[numbts+2] = '\0';
11669f988b79SJean-Baptiste Boric 
11679f988b79SJean-Baptiste Boric 		/*
11689f988b79SJean-Baptiste Boric 		 * now tmp has exactly the identifier
11699f988b79SJean-Baptiste Boric 		 * we want so we'll copy it back to record
11709f988b79SJean-Baptiste Boric 		 */
11719f988b79SJean-Baptiste Boric 		memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
11729f988b79SJean-Baptiste Boric 
11739f988b79SJean-Baptiste Boric 		iter = TAILQ_NEXT(iter, cn_next_child);
11749f988b79SJean-Baptiste Boric 		i++;
11759f988b79SJean-Baptiste Boric 	}
11769f988b79SJean-Baptiste Boric 
11779f988b79SJean-Baptiste Boric 	free(tmp);
11789f988b79SJean-Baptiste Boric 	return iter;
11799f988b79SJean-Baptiste Boric }
11809f988b79SJean-Baptiste Boric 
11819f988b79SJean-Baptiste Boric /* Todo: Figure out why these functions are nec. */
11829f988b79SJean-Baptiste Boric static void
cd9660_copy_filenames(iso9660_disk * diskStructure,cd9660node * node)11839f988b79SJean-Baptiste Boric cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node)
11849f988b79SJean-Baptiste Boric {
11859f988b79SJean-Baptiste Boric 	cd9660node *cn;
11869f988b79SJean-Baptiste Boric 
11879f988b79SJean-Baptiste Boric 	if (TAILQ_EMPTY(&node->cn_children))
11889f988b79SJean-Baptiste Boric 		return;
11899f988b79SJean-Baptiste Boric 
11909f988b79SJean-Baptiste Boric 	if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
11919f988b79SJean-Baptiste Boric 		debug_print_tree(diskStructure, diskStructure->rootNode, 0);
11929f988b79SJean-Baptiste Boric 		exit(1);
11939f988b79SJean-Baptiste Boric 	}
11949f988b79SJean-Baptiste Boric 
11959f988b79SJean-Baptiste Boric 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
11969f988b79SJean-Baptiste Boric 		cd9660_copy_filenames(diskStructure, cn);
11979f988b79SJean-Baptiste Boric 		memcpy(cn->o_name, cn->isoDirRecord->name,
11989f988b79SJean-Baptiste Boric 		    ISO_FILENAME_MAXLENGTH_WITH_PADDING);
11999f988b79SJean-Baptiste Boric 	}
12009f988b79SJean-Baptiste Boric }
12019f988b79SJean-Baptiste Boric 
12029f988b79SJean-Baptiste Boric static void
cd9660_sorting_nodes(cd9660node * node)12039f988b79SJean-Baptiste Boric cd9660_sorting_nodes(cd9660node *node)
12049f988b79SJean-Baptiste Boric {
12059f988b79SJean-Baptiste Boric 	cd9660node *cn;
12069f988b79SJean-Baptiste Boric 
12079f988b79SJean-Baptiste Boric 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
12089f988b79SJean-Baptiste Boric 		cd9660_sorting_nodes(cn);
12099f988b79SJean-Baptiste Boric 	cd9660_sort_nodes(node);
12109f988b79SJean-Baptiste Boric }
12119f988b79SJean-Baptiste Boric 
12129f988b79SJean-Baptiste Boric /* XXX Bubble sort. */
12139f988b79SJean-Baptiste Boric static void
cd9660_sort_nodes(cd9660node * node)12149f988b79SJean-Baptiste Boric cd9660_sort_nodes(cd9660node *node)
12159f988b79SJean-Baptiste Boric {
12169f988b79SJean-Baptiste Boric 	cd9660node *cn, *next;
12179f988b79SJean-Baptiste Boric 
12189f988b79SJean-Baptiste Boric 	do {
12199f988b79SJean-Baptiste Boric 		TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
12209f988b79SJean-Baptiste Boric 			if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
12219f988b79SJean-Baptiste Boric 				return;
12229f988b79SJean-Baptiste Boric 			else if (strcmp(next->isoDirRecord->name,
12239f988b79SJean-Baptiste Boric 				        cn->isoDirRecord->name) >= 0)
12249f988b79SJean-Baptiste Boric 				continue;
12259f988b79SJean-Baptiste Boric 			TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
12269f988b79SJean-Baptiste Boric 			TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
12279f988b79SJean-Baptiste Boric 			break;
12289f988b79SJean-Baptiste Boric 		}
12299f988b79SJean-Baptiste Boric 	} while (cn != NULL);
12309f988b79SJean-Baptiste Boric }
12319f988b79SJean-Baptiste Boric 
12329f988b79SJean-Baptiste Boric static int
cd9660_count_collisions(cd9660node * copy)12339f988b79SJean-Baptiste Boric cd9660_count_collisions(cd9660node *copy)
12349f988b79SJean-Baptiste Boric {
12359f988b79SJean-Baptiste Boric 	int count = 0;
12369f988b79SJean-Baptiste Boric 	cd9660node *iter, *next;
12379f988b79SJean-Baptiste Boric 
12389f988b79SJean-Baptiste Boric 	for (iter = copy;
12399f988b79SJean-Baptiste Boric 	     (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
12409f988b79SJean-Baptiste Boric 	     iter = next) {
12419f988b79SJean-Baptiste Boric 		if (cd9660_compare_filename(iter->isoDirRecord->name,
12429f988b79SJean-Baptiste Boric 			next->isoDirRecord->name) == 0)
12439f988b79SJean-Baptiste Boric 			count++;
12449f988b79SJean-Baptiste Boric 		else
12459f988b79SJean-Baptiste Boric 			return count;
12469f988b79SJean-Baptiste Boric 	}
12479f988b79SJean-Baptiste Boric #if 0
12489f988b79SJean-Baptiste Boric 	if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
1249*0a6a1f1dSLionel Sambuc 		printf("%s: count is %i \n", __func__, count);
12509f988b79SJean-Baptiste Boric 		compare = cd9660_compare_filename(iter->isoDirRecord->name,
12519f988b79SJean-Baptiste Boric 			next->isoDirRecord->name);
12529f988b79SJean-Baptiste Boric 		if (compare == 0) {
12539f988b79SJean-Baptiste Boric 			count++;
12549f988b79SJean-Baptiste Boric 			return cd9660_recurse_on_collision(next, count);
12559f988b79SJean-Baptiste Boric 		} else
12569f988b79SJean-Baptiste Boric 			return count;
12579f988b79SJean-Baptiste Boric 	}
12589f988b79SJean-Baptiste Boric #endif
12599f988b79SJean-Baptiste Boric 	return count;
12609f988b79SJean-Baptiste Boric }
12619f988b79SJean-Baptiste Boric 
12629f988b79SJean-Baptiste Boric static cd9660node *
cd9660_rrip_move_directory(iso9660_disk * diskStructure,cd9660node * dir)12639f988b79SJean-Baptiste Boric cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir)
12649f988b79SJean-Baptiste Boric {
12659f988b79SJean-Baptiste Boric 	char newname[9];
12669f988b79SJean-Baptiste Boric 	cd9660node *tfile;
12679f988b79SJean-Baptiste Boric 
12689f988b79SJean-Baptiste Boric 	/*
12699f988b79SJean-Baptiste Boric 	 * This function needs to:
12709f988b79SJean-Baptiste Boric 	 * 1) Create an empty virtual file in place of the old directory
12719f988b79SJean-Baptiste Boric 	 * 2) Point the virtual file to the new directory
12729f988b79SJean-Baptiste Boric 	 * 3) Point the relocated directory to its old parent
12739f988b79SJean-Baptiste Boric 	 * 4) Move the directory specified by dir into rr_moved_dir,
12749f988b79SJean-Baptiste Boric 	 * and rename it to "diskStructure->rock_ridge_move_count" (as a string)
12759f988b79SJean-Baptiste Boric 	 */
12769f988b79SJean-Baptiste Boric 
12779f988b79SJean-Baptiste Boric 	/* First see if the moved directory even exists */
12789f988b79SJean-Baptiste Boric 	if (diskStructure->rr_moved_dir == NULL) {
12799f988b79SJean-Baptiste Boric 		diskStructure->rr_moved_dir = cd9660_create_directory(
12809f988b79SJean-Baptiste Boric 		    diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
12819f988b79SJean-Baptiste Boric 		    diskStructure->rootNode, dir);
12829f988b79SJean-Baptiste Boric 		if (diskStructure->rr_moved_dir == NULL)
12839f988b79SJean-Baptiste Boric 			return 0;
12849f988b79SJean-Baptiste Boric 	}
12859f988b79SJean-Baptiste Boric 
12869f988b79SJean-Baptiste Boric 	/* Create a file with the same ORIGINAL name */
12879f988b79SJean-Baptiste Boric 	tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent,
12889f988b79SJean-Baptiste Boric 	    dir);
12899f988b79SJean-Baptiste Boric 	if (tfile == NULL)
12909f988b79SJean-Baptiste Boric 		return NULL;
12919f988b79SJean-Baptiste Boric 
12929f988b79SJean-Baptiste Boric 	diskStructure->rock_ridge_move_count++;
12939f988b79SJean-Baptiste Boric 	snprintf(newname, sizeof(newname), "%08i",
12949f988b79SJean-Baptiste Boric 	    diskStructure->rock_ridge_move_count);
12959f988b79SJean-Baptiste Boric 
12969f988b79SJean-Baptiste Boric 	/* Point to old parent */
12979f988b79SJean-Baptiste Boric 	dir->rr_real_parent = dir->parent;
12989f988b79SJean-Baptiste Boric 
12999f988b79SJean-Baptiste Boric 	/* Place the placeholder file */
13009f988b79SJean-Baptiste Boric 	if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
13019f988b79SJean-Baptiste Boric 		TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
13029f988b79SJean-Baptiste Boric 		    cn_next_child);
13039f988b79SJean-Baptiste Boric 	} else {
13049f988b79SJean-Baptiste Boric 		cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
13059f988b79SJean-Baptiste Boric 	}
13069f988b79SJean-Baptiste Boric 
13079f988b79SJean-Baptiste Boric 	/* Point to new parent */
13089f988b79SJean-Baptiste Boric 	dir->parent = diskStructure->rr_moved_dir;
13099f988b79SJean-Baptiste Boric 
13109f988b79SJean-Baptiste Boric 	/* Point the file to the moved directory */
13119f988b79SJean-Baptiste Boric 	tfile->rr_relocated = dir;
13129f988b79SJean-Baptiste Boric 
13139f988b79SJean-Baptiste Boric 	/* Actually move the directory */
13149f988b79SJean-Baptiste Boric 	cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir);
13159f988b79SJean-Baptiste Boric 
13169f988b79SJean-Baptiste Boric 	/* TODO: Inherit permissions / ownership (basically the entire inode) */
13179f988b79SJean-Baptiste Boric 
13189f988b79SJean-Baptiste Boric 	/* Set the new name */
13199f988b79SJean-Baptiste Boric 	memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
13209f988b79SJean-Baptiste Boric 	strncpy(dir->isoDirRecord->name, newname, 8);
13219f988b79SJean-Baptiste Boric 	dir->isoDirRecord->length[0] = 34 + 8;
13229f988b79SJean-Baptiste Boric 	dir->isoDirRecord->name_len[0] = 8;
13239f988b79SJean-Baptiste Boric 
13249f988b79SJean-Baptiste Boric 	return dir;
13259f988b79SJean-Baptiste Boric }
13269f988b79SJean-Baptiste Boric 
13279f988b79SJean-Baptiste Boric static int
cd9660_add_dot_records(iso9660_disk * diskStructure,cd9660node * root)13289f988b79SJean-Baptiste Boric cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root)
13299f988b79SJean-Baptiste Boric {
13309f988b79SJean-Baptiste Boric 	struct cd9660_children_head *head = &root->cn_children;
13319f988b79SJean-Baptiste Boric 	cd9660node *cn;
13329f988b79SJean-Baptiste Boric 
13339f988b79SJean-Baptiste Boric 	TAILQ_FOREACH(cn, head, cn_next_child) {
13349f988b79SJean-Baptiste Boric 		if ((cn->type & CD9660_TYPE_DIR) == 0)
13359f988b79SJean-Baptiste Boric 			continue;
13369f988b79SJean-Baptiste Boric 		/* Recursion first */
13379f988b79SJean-Baptiste Boric 		cd9660_add_dot_records(diskStructure, cn);
13389f988b79SJean-Baptiste Boric 	}
13399f988b79SJean-Baptiste Boric 	cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root);
13409f988b79SJean-Baptiste Boric 	cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT,
13419f988b79SJean-Baptiste Boric 	    root);
13429f988b79SJean-Baptiste Boric 	return 1;
13439f988b79SJean-Baptiste Boric }
13449f988b79SJean-Baptiste Boric 
13459f988b79SJean-Baptiste Boric /*
13469f988b79SJean-Baptiste Boric  * Convert node to cd9660 structure
13479f988b79SJean-Baptiste Boric  * This function is designed to be called recursively on the root node of
13489f988b79SJean-Baptiste Boric  * the filesystem
13499f988b79SJean-Baptiste Boric  * Lots of recursion going on here, want to make sure it is efficient
13509f988b79SJean-Baptiste Boric  * @param struct fsnode * The root node to be converted
13519f988b79SJean-Baptiste Boric  * @param struct cd9660* The parent node (should not be NULL)
13529f988b79SJean-Baptiste Boric  * @param int Current directory depth
13539f988b79SJean-Baptiste Boric  * @param int* Running count of the number of directories that are being created
13549f988b79SJean-Baptiste Boric  */
13559f988b79SJean-Baptiste Boric static void
cd9660_convert_structure(iso9660_disk * diskStructure,fsnode * root,cd9660node * parent_node,int level,int * numDirectories,int * error)13569f988b79SJean-Baptiste Boric cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root,
13579f988b79SJean-Baptiste Boric     cd9660node *parent_node, int level, int *numDirectories, int *error)
13589f988b79SJean-Baptiste Boric {
13599f988b79SJean-Baptiste Boric 	fsnode *iterator = root;
13609f988b79SJean-Baptiste Boric 	cd9660node *this_node;
13619f988b79SJean-Baptiste Boric 	int working_level;
13629f988b79SJean-Baptiste Boric 	int add;
13639f988b79SJean-Baptiste Boric 	int flag = 0;
13649f988b79SJean-Baptiste Boric 	int counter = 0;
13659f988b79SJean-Baptiste Boric 
13669f988b79SJean-Baptiste Boric 	/*
13679f988b79SJean-Baptiste Boric 	 * Newer, more efficient method, reduces recursion depth
13689f988b79SJean-Baptiste Boric 	 */
13699f988b79SJean-Baptiste Boric 	if (root == NULL) {
1370*0a6a1f1dSLionel Sambuc 		warnx("%s: root is null", __func__);
13719f988b79SJean-Baptiste Boric 		return;
13729f988b79SJean-Baptiste Boric 	}
13739f988b79SJean-Baptiste Boric 
13749f988b79SJean-Baptiste Boric 	/* Test for an empty directory - makefs still gives us the . record */
13759f988b79SJean-Baptiste Boric 	if ((S_ISDIR(root->type)) && (root->name[0] == '.')
13769f988b79SJean-Baptiste Boric 		&& (root->name[1] == '\0')) {
13779f988b79SJean-Baptiste Boric 		root = root->next;
13789f988b79SJean-Baptiste Boric 		if (root == NULL)
13799f988b79SJean-Baptiste Boric 			return;
13809f988b79SJean-Baptiste Boric 	}
13819f988b79SJean-Baptiste Boric 	if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
13829f988b79SJean-Baptiste Boric 		CD9660_MEM_ALLOC_ERROR(__func__);
13839f988b79SJean-Baptiste Boric 	}
13849f988b79SJean-Baptiste Boric 
13859f988b79SJean-Baptiste Boric 	/*
13869f988b79SJean-Baptiste Boric 	 * To reduce the number of recursive calls, we will iterate over
13879f988b79SJean-Baptiste Boric 	 * the next pointers to the right.
13889f988b79SJean-Baptiste Boric 	 */
13899f988b79SJean-Baptiste Boric 	while (iterator != NULL) {
13909f988b79SJean-Baptiste Boric 		add = 1;
13919f988b79SJean-Baptiste Boric 		/*
13929f988b79SJean-Baptiste Boric 		 * Increment the directory count if this is a directory
13939f988b79SJean-Baptiste Boric 		 * Ignore "." entries. We will generate them later
13949f988b79SJean-Baptiste Boric 		 */
13959f988b79SJean-Baptiste Boric 		if (!S_ISDIR(iterator->type) ||
13969f988b79SJean-Baptiste Boric 		    strcmp(iterator->name, ".") != 0) {
13979f988b79SJean-Baptiste Boric 
13989f988b79SJean-Baptiste Boric 			/* Translate the node, including its filename */
13999f988b79SJean-Baptiste Boric 			this_node->parent = parent_node;
14009f988b79SJean-Baptiste Boric 			cd9660_translate_node(diskStructure, iterator,
14019f988b79SJean-Baptiste Boric 			    this_node);
14029f988b79SJean-Baptiste Boric 			this_node->level = level;
14039f988b79SJean-Baptiste Boric 
14049f988b79SJean-Baptiste Boric 			if (S_ISDIR(iterator->type)) {
14059f988b79SJean-Baptiste Boric 				(*numDirectories)++;
14069f988b79SJean-Baptiste Boric 				this_node->type = CD9660_TYPE_DIR;
14079f988b79SJean-Baptiste Boric 				working_level = level + 1;
14089f988b79SJean-Baptiste Boric 
14099f988b79SJean-Baptiste Boric 				/*
14109f988b79SJean-Baptiste Boric 				 * If at level 8, directory would be at 8
14119f988b79SJean-Baptiste Boric 				 * and have children at 9 which is not
14129f988b79SJean-Baptiste Boric 				 * allowed as per ISO spec
14139f988b79SJean-Baptiste Boric 				 */
14149f988b79SJean-Baptiste Boric 				if (level == 8) {
14159f988b79SJean-Baptiste Boric 					if ((!diskStructure->allow_deep_trees) &&
14169f988b79SJean-Baptiste Boric 					  (!diskStructure->rock_ridge_enabled)) {
14179f988b79SJean-Baptiste Boric 						warnx("error: found entry "
14189f988b79SJean-Baptiste Boric 						     "with depth greater "
14199f988b79SJean-Baptiste Boric 						     "than 8.");
14209f988b79SJean-Baptiste Boric 						(*error) = 1;
14219f988b79SJean-Baptiste Boric 						return;
14229f988b79SJean-Baptiste Boric 					} else if (diskStructure->
14239f988b79SJean-Baptiste Boric 						   rock_ridge_enabled) {
14249f988b79SJean-Baptiste Boric 						working_level = 3;
14259f988b79SJean-Baptiste Boric 						/*
14269f988b79SJean-Baptiste Boric 						 * Moved directory is actually
14279f988b79SJean-Baptiste Boric 						 * at level 2.
14289f988b79SJean-Baptiste Boric 						 */
14299f988b79SJean-Baptiste Boric 						this_node->level =
14309f988b79SJean-Baptiste Boric 						    working_level - 1;
14319f988b79SJean-Baptiste Boric 						if (cd9660_rrip_move_directory(
14329f988b79SJean-Baptiste Boric 							diskStructure,
14339f988b79SJean-Baptiste Boric 							this_node) == 0) {
14349f988b79SJean-Baptiste Boric 							warnx("Failure in "
14359f988b79SJean-Baptiste Boric 							      "cd9660_rrip_"
14369f988b79SJean-Baptiste Boric 							      "move_directory"
14379f988b79SJean-Baptiste Boric 							);
14389f988b79SJean-Baptiste Boric 							(*error) = 1;
14399f988b79SJean-Baptiste Boric 							return;
14409f988b79SJean-Baptiste Boric 						}
14419f988b79SJean-Baptiste Boric 						add = 0;
14429f988b79SJean-Baptiste Boric 					}
14439f988b79SJean-Baptiste Boric 				}
14449f988b79SJean-Baptiste Boric 
14459f988b79SJean-Baptiste Boric 				/* Do the recursive call on the children */
14469f988b79SJean-Baptiste Boric 				if (iterator->child != 0) {
14479f988b79SJean-Baptiste Boric 					cd9660_convert_structure(diskStructure,
14489f988b79SJean-Baptiste Boric 						iterator->child, this_node,
14499f988b79SJean-Baptiste Boric 						working_level,
14509f988b79SJean-Baptiste Boric 						numDirectories, error);
14519f988b79SJean-Baptiste Boric 
14529f988b79SJean-Baptiste Boric 					if ((*error) == 1) {
14539f988b79SJean-Baptiste Boric 						warnx("%s: Error on recursive "
14549f988b79SJean-Baptiste Boric 						    "call", __func__);
14559f988b79SJean-Baptiste Boric 						return;
14569f988b79SJean-Baptiste Boric 					}
14579f988b79SJean-Baptiste Boric 				}
14589f988b79SJean-Baptiste Boric 
14599f988b79SJean-Baptiste Boric 			} else {
14609f988b79SJean-Baptiste Boric 				/* Only directories should have children */
14619f988b79SJean-Baptiste Boric 				assert(iterator->child == NULL);
14629f988b79SJean-Baptiste Boric 
14639f988b79SJean-Baptiste Boric 				this_node->type = CD9660_TYPE_FILE;
14649f988b79SJean-Baptiste Boric 			}
14659f988b79SJean-Baptiste Boric 
14669f988b79SJean-Baptiste Boric 			/*
14679f988b79SJean-Baptiste Boric 			 * Finally, do a sorted insert
14689f988b79SJean-Baptiste Boric 			 */
14699f988b79SJean-Baptiste Boric 			if (add) {
14709f988b79SJean-Baptiste Boric 				cd9660_sorted_child_insert(
14719f988b79SJean-Baptiste Boric 				    parent_node, this_node);
14729f988b79SJean-Baptiste Boric 			}
14739f988b79SJean-Baptiste Boric 
14749f988b79SJean-Baptiste Boric 			/*Allocate new temp_node */
14759f988b79SJean-Baptiste Boric 			if (iterator->next != 0) {
14769f988b79SJean-Baptiste Boric 				this_node = cd9660_allocate_cd9660node();
14779f988b79SJean-Baptiste Boric 				if (this_node == NULL)
14789f988b79SJean-Baptiste Boric 					CD9660_MEM_ALLOC_ERROR(__func__);
14799f988b79SJean-Baptiste Boric 			}
14809f988b79SJean-Baptiste Boric 		}
14819f988b79SJean-Baptiste Boric 		iterator = iterator->next;
14829f988b79SJean-Baptiste Boric 	}
14839f988b79SJean-Baptiste Boric 
14849f988b79SJean-Baptiste Boric 	/* cd9660_handle_collisions(first_node); */
14859f988b79SJean-Baptiste Boric 
14869f988b79SJean-Baptiste Boric 	/* TODO: need cleanup */
14879f988b79SJean-Baptiste Boric 	cd9660_copy_filenames(diskStructure, parent_node);
14889f988b79SJean-Baptiste Boric 
14899f988b79SJean-Baptiste Boric 	do {
14909f988b79SJean-Baptiste Boric 		flag = cd9660_handle_collisions(diskStructure, parent_node,
14919f988b79SJean-Baptiste Boric 		    counter);
14929f988b79SJean-Baptiste Boric 		counter++;
14939f988b79SJean-Baptiste Boric 		cd9660_sorting_nodes(parent_node);
14949f988b79SJean-Baptiste Boric 	} while ((flag == 1) && (counter < 100));
14959f988b79SJean-Baptiste Boric }
14969f988b79SJean-Baptiste Boric 
14979f988b79SJean-Baptiste Boric /*
14989f988b79SJean-Baptiste Boric  * Clean up the cd9660node tree
14999f988b79SJean-Baptiste Boric  * This is designed to be called recursively on the root node
15009f988b79SJean-Baptiste Boric  * @param struct cd9660node *root The node to free
15019f988b79SJean-Baptiste Boric  * @returns void
15029f988b79SJean-Baptiste Boric  */
15039f988b79SJean-Baptiste Boric static void
cd9660_free_structure(cd9660node * root)15049f988b79SJean-Baptiste Boric cd9660_free_structure(cd9660node *root)
15059f988b79SJean-Baptiste Boric {
15069f988b79SJean-Baptiste Boric 	cd9660node *cn;
15079f988b79SJean-Baptiste Boric 
15089f988b79SJean-Baptiste Boric 	while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
15099f988b79SJean-Baptiste Boric 		TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
15109f988b79SJean-Baptiste Boric 		cd9660_free_structure(cn);
15119f988b79SJean-Baptiste Boric 	}
15129f988b79SJean-Baptiste Boric 	free(root);
15139f988b79SJean-Baptiste Boric }
15149f988b79SJean-Baptiste Boric 
15159f988b79SJean-Baptiste Boric /*
15169f988b79SJean-Baptiste Boric  * Be a little more memory conservative:
15179f988b79SJean-Baptiste Boric  * instead of having the TAILQ_ENTRY as part of the cd9660node,
15189f988b79SJean-Baptiste Boric  * just create a temporary structure
15199f988b79SJean-Baptiste Boric  */
15209f988b79SJean-Baptiste Boric struct ptq_entry
15219f988b79SJean-Baptiste Boric {
15229f988b79SJean-Baptiste Boric 	TAILQ_ENTRY(ptq_entry) ptq;
15239f988b79SJean-Baptiste Boric 	cd9660node *node;
15249f988b79SJean-Baptiste Boric } *n;
15259f988b79SJean-Baptiste Boric 
15269f988b79SJean-Baptiste Boric #define PTQUEUE_NEW(n,s,r,t){\
15279f988b79SJean-Baptiste Boric 	n = emalloc(sizeof(struct s));	\
15289f988b79SJean-Baptiste Boric 	if (n == NULL)	\
15299f988b79SJean-Baptiste Boric 		return r; \
15309f988b79SJean-Baptiste Boric 	n->node = t;\
15319f988b79SJean-Baptiste Boric }
15329f988b79SJean-Baptiste Boric 
15339f988b79SJean-Baptiste Boric /*
15349f988b79SJean-Baptiste Boric  * Generate the path tables
15359f988b79SJean-Baptiste Boric  * The specific implementation of this function is left as an exercise to the
15369f988b79SJean-Baptiste Boric  * programmer. It could be done recursively. Make sure you read how the path
15379f988b79SJean-Baptiste Boric  * table has to be laid out, it has levels.
15389f988b79SJean-Baptiste Boric  * @param struct iso9660_disk *disk The disk image
15399f988b79SJean-Baptiste Boric  * @returns int The number of built path tables (between 1 and 4), 0 on failure
15409f988b79SJean-Baptiste Boric  */
15419f988b79SJean-Baptiste Boric static int
cd9660_generate_path_table(iso9660_disk * diskStructure)15429f988b79SJean-Baptiste Boric cd9660_generate_path_table(iso9660_disk *diskStructure)
15439f988b79SJean-Baptiste Boric {
15449f988b79SJean-Baptiste Boric 	cd9660node *cn, *dirNode = diskStructure->rootNode;
15459f988b79SJean-Baptiste Boric 	cd9660node *last = dirNode;
15469f988b79SJean-Baptiste Boric 	int pathTableSize = 0;	/* computed as we go */
15479f988b79SJean-Baptiste Boric 	int counter = 1;	/* root gets a count of 0 */
15489f988b79SJean-Baptiste Boric 
15499f988b79SJean-Baptiste Boric 	TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
15509f988b79SJean-Baptiste Boric 	TAILQ_INIT(&pt_head);
15519f988b79SJean-Baptiste Boric 
15529f988b79SJean-Baptiste Boric 	PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode);
15539f988b79SJean-Baptiste Boric 
15549f988b79SJean-Baptiste Boric 	/* Push the root node */
15559f988b79SJean-Baptiste Boric 	TAILQ_INSERT_HEAD(&pt_head, n, ptq);
15569f988b79SJean-Baptiste Boric 
15579f988b79SJean-Baptiste Boric 	/* Breadth-first traversal of file structure */
15589f988b79SJean-Baptiste Boric 	while (pt_head.tqh_first != 0) {
15599f988b79SJean-Baptiste Boric 		n = pt_head.tqh_first;
15609f988b79SJean-Baptiste Boric 		dirNode = n->node;
15619f988b79SJean-Baptiste Boric 		TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
15629f988b79SJean-Baptiste Boric 		free(n);
15639f988b79SJean-Baptiste Boric 
15649f988b79SJean-Baptiste Boric 		/* Update the size */
15659f988b79SJean-Baptiste Boric 		pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
15669f988b79SJean-Baptiste Boric 		    + dirNode->isoDirRecord->name_len[0]+
15679f988b79SJean-Baptiste Boric 			(dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
15689f988b79SJean-Baptiste Boric 			/* includes the padding bit */
15699f988b79SJean-Baptiste Boric 
15709f988b79SJean-Baptiste Boric 		dirNode->ptnumber=counter;
15719f988b79SJean-Baptiste Boric 		if (dirNode != last) {
15729f988b79SJean-Baptiste Boric 			last->ptnext = dirNode;
15739f988b79SJean-Baptiste Boric 			dirNode->ptprev = last;
15749f988b79SJean-Baptiste Boric 		}
15759f988b79SJean-Baptiste Boric 		last = dirNode;
15769f988b79SJean-Baptiste Boric 
15779f988b79SJean-Baptiste Boric 		/* Push children onto queue */
15789f988b79SJean-Baptiste Boric 		TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
15799f988b79SJean-Baptiste Boric 			/*
15809f988b79SJean-Baptiste Boric 			 * Dont add the DOT and DOTDOT types to the path
15819f988b79SJean-Baptiste Boric 			 * table.
15829f988b79SJean-Baptiste Boric 			 */
15839f988b79SJean-Baptiste Boric 			if ((cn->type != CD9660_TYPE_DOT)
15849f988b79SJean-Baptiste Boric 				&& (cn->type != CD9660_TYPE_DOTDOT)) {
15859f988b79SJean-Baptiste Boric 
15869f988b79SJean-Baptiste Boric 				if (S_ISDIR(cn->node->type)) {
15879f988b79SJean-Baptiste Boric 					PTQUEUE_NEW(n, ptq_entry, -1, cn);
15889f988b79SJean-Baptiste Boric 					TAILQ_INSERT_TAIL(&pt_head, n, ptq);
15899f988b79SJean-Baptiste Boric 				}
15909f988b79SJean-Baptiste Boric 			}
15919f988b79SJean-Baptiste Boric 		}
15929f988b79SJean-Baptiste Boric 		counter++;
15939f988b79SJean-Baptiste Boric 	}
15949f988b79SJean-Baptiste Boric 	return pathTableSize;
15959f988b79SJean-Baptiste Boric }
15969f988b79SJean-Baptiste Boric 
15979f988b79SJean-Baptiste Boric void
cd9660_compute_full_filename(cd9660node * node,char * buf)15989f988b79SJean-Baptiste Boric cd9660_compute_full_filename(cd9660node *node, char *buf)
15999f988b79SJean-Baptiste Boric {
16009f988b79SJean-Baptiste Boric 	int len;
16019f988b79SJean-Baptiste Boric 
16029f988b79SJean-Baptiste Boric 	len = CD9660MAXPATH + 1;
16039f988b79SJean-Baptiste Boric 	len = snprintf(buf, len, "%s/%s/%s", node->node->root,
16049f988b79SJean-Baptiste Boric 	    node->node->path, node->node->name);
16059f988b79SJean-Baptiste Boric 	if (len > CD9660MAXPATH)
1606*0a6a1f1dSLionel Sambuc 		errx(EXIT_FAILURE, "Pathname too long.");
16079f988b79SJean-Baptiste Boric }
16089f988b79SJean-Baptiste Boric 
16099f988b79SJean-Baptiste Boric /* NEW filename conversion method */
16109f988b79SJean-Baptiste Boric typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *,
16119f988b79SJean-Baptiste Boric     char *, int);
16129f988b79SJean-Baptiste Boric 
16139f988b79SJean-Baptiste Boric 
16149f988b79SJean-Baptiste Boric /*
16159f988b79SJean-Baptiste Boric  * TODO: These two functions are almost identical.
16169f988b79SJean-Baptiste Boric  * Some code cleanup is possible here
16179f988b79SJean-Baptiste Boric  *
16189f988b79SJean-Baptiste Boric  * XXX bounds checking!
16199f988b79SJean-Baptiste Boric  */
16209f988b79SJean-Baptiste Boric static int
cd9660_level1_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)16219f988b79SJean-Baptiste Boric cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname,
16229f988b79SJean-Baptiste Boric     char *newname, int is_file)
16239f988b79SJean-Baptiste Boric {
16249f988b79SJean-Baptiste Boric 	/*
16259f988b79SJean-Baptiste Boric 	 * ISO 9660 : 10.1
16269f988b79SJean-Baptiste Boric 	 * File Name shall not contain more than 8 d or d1 characters
16279f988b79SJean-Baptiste Boric 	 * File Name Extension shall not contain more than 3 d or d1 characters
16289f988b79SJean-Baptiste Boric 	 * Directory Identifier shall not contain more than 8 d or d1 characters
16299f988b79SJean-Baptiste Boric 	 */
16309f988b79SJean-Baptiste Boric 	int namelen = 0;
16319f988b79SJean-Baptiste Boric 	int extlen = 0;
16329f988b79SJean-Baptiste Boric 	int found_ext = 0;
16339f988b79SJean-Baptiste Boric 
16349f988b79SJean-Baptiste Boric 	while (*oldname != '\0' && extlen < 3) {
16359f988b79SJean-Baptiste Boric 		/* Handle period first, as it is special */
16369f988b79SJean-Baptiste Boric 		if (*oldname == '.') {
16379f988b79SJean-Baptiste Boric 			if (found_ext) {
16389f988b79SJean-Baptiste Boric 				*newname++ = '_';
16399f988b79SJean-Baptiste Boric 				extlen ++;
16409f988b79SJean-Baptiste Boric 			}
16419f988b79SJean-Baptiste Boric 			else {
16429f988b79SJean-Baptiste Boric 				*newname++ = '.';
16439f988b79SJean-Baptiste Boric 				found_ext = 1;
16449f988b79SJean-Baptiste Boric 			}
16459f988b79SJean-Baptiste Boric 		} else {
16469f988b79SJean-Baptiste Boric 			/* cut RISC OS file type off ISO name */
16479f988b79SJean-Baptiste Boric 			if (diskStructure->archimedes_enabled &&
16489f988b79SJean-Baptiste Boric 			    *oldname == ',' && strlen(oldname) == 4)
16499f988b79SJean-Baptiste Boric 				break;
16509f988b79SJean-Baptiste Boric 
16519f988b79SJean-Baptiste Boric 			/* Enforce 12.3 / 8 */
16529f988b79SJean-Baptiste Boric 			if (namelen == 8 && !found_ext)
16539f988b79SJean-Baptiste Boric 				break;
16549f988b79SJean-Baptiste Boric 
16559f988b79SJean-Baptiste Boric 			if (islower((unsigned char)*oldname))
16569f988b79SJean-Baptiste Boric 				*newname++ = toupper((unsigned char)*oldname);
16579f988b79SJean-Baptiste Boric 			else if (isupper((unsigned char)*oldname)
16589f988b79SJean-Baptiste Boric 			    || isdigit((unsigned char)*oldname))
16599f988b79SJean-Baptiste Boric 				*newname++ = *oldname;
16609f988b79SJean-Baptiste Boric 			else
16619f988b79SJean-Baptiste Boric 				*newname++ = '_';
16629f988b79SJean-Baptiste Boric 
16639f988b79SJean-Baptiste Boric 			if (found_ext)
16649f988b79SJean-Baptiste Boric 				extlen++;
16659f988b79SJean-Baptiste Boric 			else
16669f988b79SJean-Baptiste Boric 				namelen++;
16679f988b79SJean-Baptiste Boric 		}
16689f988b79SJean-Baptiste Boric 		oldname++;
16699f988b79SJean-Baptiste Boric 	}
16709f988b79SJean-Baptiste Boric 	if (is_file) {
16719f988b79SJean-Baptiste Boric 		if (!found_ext && !diskStructure->omit_trailing_period)
16729f988b79SJean-Baptiste Boric 			*newname++ = '.';
16739f988b79SJean-Baptiste Boric 		/* Add version */
16749f988b79SJean-Baptiste Boric 		sprintf(newname, ";%i", 1);
16759f988b79SJean-Baptiste Boric 	}
16769f988b79SJean-Baptiste Boric 	return namelen + extlen + found_ext;
16779f988b79SJean-Baptiste Boric }
16789f988b79SJean-Baptiste Boric 
16799f988b79SJean-Baptiste Boric /* XXX bounds checking! */
16809f988b79SJean-Baptiste Boric static int
cd9660_level2_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)16819f988b79SJean-Baptiste Boric cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname,
16829f988b79SJean-Baptiste Boric     char *newname, int is_file)
16839f988b79SJean-Baptiste Boric {
16849f988b79SJean-Baptiste Boric 	/*
16859f988b79SJean-Baptiste Boric 	 * ISO 9660 : 7.5.1
16869f988b79SJean-Baptiste Boric 	 * File name : 0+ d or d1 characters
16879f988b79SJean-Baptiste Boric 	 * separator 1 (.)
16889f988b79SJean-Baptiste Boric 	 * File name extension : 0+ d or d1 characters
16899f988b79SJean-Baptiste Boric 	 * separator 2 (;)
16909f988b79SJean-Baptiste Boric 	 * File version number (5 characters, 1-32767)
16919f988b79SJean-Baptiste Boric 	 * 1 <= Sum of File name and File name extension <= 30
16929f988b79SJean-Baptiste Boric 	 */
16939f988b79SJean-Baptiste Boric 	int namelen = 0;
16949f988b79SJean-Baptiste Boric 	int extlen = 0;
16959f988b79SJean-Baptiste Boric 	int found_ext = 0;
16969f988b79SJean-Baptiste Boric 
16979f988b79SJean-Baptiste Boric 	while (*oldname != '\0' && namelen + extlen < 30) {
16989f988b79SJean-Baptiste Boric 		/* Handle period first, as it is special */
16999f988b79SJean-Baptiste Boric 		if (*oldname == '.') {
17009f988b79SJean-Baptiste Boric 			if (found_ext) {
17019f988b79SJean-Baptiste Boric 				if (diskStructure->allow_multidot) {
17029f988b79SJean-Baptiste Boric 					*newname++ = '.';
17039f988b79SJean-Baptiste Boric 				} else {
17049f988b79SJean-Baptiste Boric 					*newname++ = '_';
17059f988b79SJean-Baptiste Boric 				}
17069f988b79SJean-Baptiste Boric 				extlen ++;
17079f988b79SJean-Baptiste Boric 			}
17089f988b79SJean-Baptiste Boric 			else {
17099f988b79SJean-Baptiste Boric 				*newname++ = '.';
17109f988b79SJean-Baptiste Boric 				found_ext = 1;
17119f988b79SJean-Baptiste Boric 			}
17129f988b79SJean-Baptiste Boric 		} else {
17139f988b79SJean-Baptiste Boric 			/* cut RISC OS file type off ISO name */
17149f988b79SJean-Baptiste Boric 			if (diskStructure->archimedes_enabled &&
17159f988b79SJean-Baptiste Boric 			    *oldname == ',' && strlen(oldname) == 4)
17169f988b79SJean-Baptiste Boric 				break;
17179f988b79SJean-Baptiste Boric 
17189f988b79SJean-Baptiste Boric 			 if (islower((unsigned char)*oldname))
17199f988b79SJean-Baptiste Boric 				*newname++ = toupper((unsigned char)*oldname);
17209f988b79SJean-Baptiste Boric 			else if (isupper((unsigned char)*oldname) ||
17219f988b79SJean-Baptiste Boric 			    isdigit((unsigned char)*oldname))
17229f988b79SJean-Baptiste Boric 				*newname++ = *oldname;
17239f988b79SJean-Baptiste Boric 			else if (diskStructure->allow_multidot &&
17249f988b79SJean-Baptiste Boric 			    *oldname == '.') {
17259f988b79SJean-Baptiste Boric 			    	*newname++ = '.';
17269f988b79SJean-Baptiste Boric 			} else {
17279f988b79SJean-Baptiste Boric 				*newname++ = '_';
17289f988b79SJean-Baptiste Boric 			}
17299f988b79SJean-Baptiste Boric 
17309f988b79SJean-Baptiste Boric 			if (found_ext)
17319f988b79SJean-Baptiste Boric 				extlen++;
17329f988b79SJean-Baptiste Boric 			else
17339f988b79SJean-Baptiste Boric 				namelen++;
17349f988b79SJean-Baptiste Boric 		}
17359f988b79SJean-Baptiste Boric 		oldname ++;
17369f988b79SJean-Baptiste Boric 	}
17379f988b79SJean-Baptiste Boric 	if (is_file) {
17389f988b79SJean-Baptiste Boric 		if (!found_ext && !diskStructure->omit_trailing_period)
17399f988b79SJean-Baptiste Boric 			*newname++ = '.';
17409f988b79SJean-Baptiste Boric 		/* Add version */
17419f988b79SJean-Baptiste Boric 		sprintf(newname, ";%i", 1);
17429f988b79SJean-Baptiste Boric 	}
17439f988b79SJean-Baptiste Boric 	return namelen + extlen + found_ext;
17449f988b79SJean-Baptiste Boric }
17459f988b79SJean-Baptiste Boric 
17469f988b79SJean-Baptiste Boric #if 0
17479f988b79SJean-Baptiste Boric static int
17489f988b79SJean-Baptiste Boric cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname,
17499f988b79SJean-Baptiste Boric     char *newname, int is_file)
17509f988b79SJean-Baptiste Boric {
17519f988b79SJean-Baptiste Boric 	/* TODO: implement later, move to cd9660_joliet.c ?? */
17529f988b79SJean-Baptiste Boric }
17539f988b79SJean-Baptiste Boric #endif
17549f988b79SJean-Baptiste Boric 
17559f988b79SJean-Baptiste Boric 
17569f988b79SJean-Baptiste Boric /*
17579f988b79SJean-Baptiste Boric  * Convert a file name to ISO compliant file name
17589f988b79SJean-Baptiste Boric  * @param char * oldname The original filename
17599f988b79SJean-Baptiste Boric  * @param char ** newname The new file name, in the appropriate character
17609f988b79SJean-Baptiste Boric  *                        set and of appropriate length
17619f988b79SJean-Baptiste Boric  * @param int 1 if file, 0 if directory
17629f988b79SJean-Baptiste Boric  * @returns int The length of the new string
17639f988b79SJean-Baptiste Boric  */
17649f988b79SJean-Baptiste Boric static int
cd9660_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)17659f988b79SJean-Baptiste Boric cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname,
17669f988b79SJean-Baptiste Boric     char *newname, int is_file)
17679f988b79SJean-Baptiste Boric {
17689f988b79SJean-Baptiste Boric 	/* NEW */
17699f988b79SJean-Baptiste Boric 	cd9660_filename_conversion_functor conversion_function = 0;
17709f988b79SJean-Baptiste Boric 	if (diskStructure->isoLevel == 1)
17719f988b79SJean-Baptiste Boric 		conversion_function = &cd9660_level1_convert_filename;
17729f988b79SJean-Baptiste Boric 	else if (diskStructure->isoLevel == 2)
17739f988b79SJean-Baptiste Boric 		conversion_function = &cd9660_level2_convert_filename;
17749f988b79SJean-Baptiste Boric 	return (*conversion_function)(diskStructure, oldname, newname, is_file);
17759f988b79SJean-Baptiste Boric }
17769f988b79SJean-Baptiste Boric 
17779f988b79SJean-Baptiste Boric int
cd9660_compute_record_size(iso9660_disk * diskStructure,cd9660node * node)17789f988b79SJean-Baptiste Boric cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node)
17799f988b79SJean-Baptiste Boric {
17809f988b79SJean-Baptiste Boric 	int size = node->isoDirRecord->length[0];
17819f988b79SJean-Baptiste Boric 
17829f988b79SJean-Baptiste Boric 	if (diskStructure->rock_ridge_enabled)
17839f988b79SJean-Baptiste Boric 		size += node->susp_entry_size;
17849f988b79SJean-Baptiste Boric 	size += node->su_tail_size;
17859f988b79SJean-Baptiste Boric 	size += size & 1; /* Ensure length of record is even. */
17869f988b79SJean-Baptiste Boric 	assert(size <= 254);
17879f988b79SJean-Baptiste Boric 	return size;
17889f988b79SJean-Baptiste Boric }
17899f988b79SJean-Baptiste Boric 
17909f988b79SJean-Baptiste Boric static void
cd9660_populate_dot_records(iso9660_disk * diskStructure,cd9660node * node)17919f988b79SJean-Baptiste Boric cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node)
17929f988b79SJean-Baptiste Boric {
17939f988b79SJean-Baptiste Boric 	node->dot_record->fileDataSector = node->fileDataSector;
17949f988b79SJean-Baptiste Boric 	memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
17959f988b79SJean-Baptiste Boric 	node->dot_record->isoDirRecord->name_len[0] = 1;
17969f988b79SJean-Baptiste Boric 	node->dot_record->isoDirRecord->name[0] = 0;
17979f988b79SJean-Baptiste Boric 	node->dot_record->isoDirRecord->name[1] = 0;
17989f988b79SJean-Baptiste Boric 	node->dot_record->isoDirRecord->length[0] = 34;
17999f988b79SJean-Baptiste Boric 	node->dot_record->fileRecordSize =
18009f988b79SJean-Baptiste Boric 	    cd9660_compute_record_size(diskStructure, node->dot_record);
18019f988b79SJean-Baptiste Boric 
18029f988b79SJean-Baptiste Boric 	if (node == diskStructure->rootNode) {
18039f988b79SJean-Baptiste Boric 		node->dot_dot_record->fileDataSector = node->fileDataSector;
18049f988b79SJean-Baptiste Boric 		memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
18059f988b79SJean-Baptiste Boric 		    34);
18069f988b79SJean-Baptiste Boric 	} else {
18079f988b79SJean-Baptiste Boric 		node->dot_dot_record->fileDataSector =
18089f988b79SJean-Baptiste Boric 		    node->parent->fileDataSector;
18099f988b79SJean-Baptiste Boric 		memcpy(node->dot_dot_record->isoDirRecord,
18109f988b79SJean-Baptiste Boric 		    node->parent->isoDirRecord,34);
18119f988b79SJean-Baptiste Boric 	}
18129f988b79SJean-Baptiste Boric 	node->dot_dot_record->isoDirRecord->name_len[0] = 1;
18139f988b79SJean-Baptiste Boric 	node->dot_dot_record->isoDirRecord->name[0] = 1;
18149f988b79SJean-Baptiste Boric 	node->dot_dot_record->isoDirRecord->name[1] = 0;
18159f988b79SJean-Baptiste Boric 	node->dot_dot_record->isoDirRecord->length[0] = 34;
18169f988b79SJean-Baptiste Boric 	node->dot_dot_record->fileRecordSize =
18179f988b79SJean-Baptiste Boric 	    cd9660_compute_record_size(diskStructure, node->dot_dot_record);
18189f988b79SJean-Baptiste Boric }
18199f988b79SJean-Baptiste Boric 
18209f988b79SJean-Baptiste Boric /*
18219f988b79SJean-Baptiste Boric  * @param struct cd9660node *node The node
18229f988b79SJean-Baptiste Boric  * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
18239f988b79SJean-Baptiste Boric  * @returns int The total size of files and directory entries (should be
18249f988b79SJean-Baptiste Boric  *              a multiple of sector size)
18259f988b79SJean-Baptiste Boric */
18269f988b79SJean-Baptiste Boric static int64_t
cd9660_compute_offsets(iso9660_disk * diskStructure,cd9660node * node,int64_t startOffset)18279f988b79SJean-Baptiste Boric cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node,
18289f988b79SJean-Baptiste Boric     int64_t startOffset)
18299f988b79SJean-Baptiste Boric {
18309f988b79SJean-Baptiste Boric 	/*
18319f988b79SJean-Baptiste Boric 	 * This function needs to compute the size of directory records and
18329f988b79SJean-Baptiste Boric 	 * runs, file lengths, and set the appropriate variables both in
18339f988b79SJean-Baptiste Boric 	 * cd9660node and isoDirEntry
18349f988b79SJean-Baptiste Boric 	 */
18359f988b79SJean-Baptiste Boric 	int64_t used_bytes = 0;
18369f988b79SJean-Baptiste Boric 	int64_t current_sector_usage = 0;
18379f988b79SJean-Baptiste Boric 	cd9660node *child;
18389f988b79SJean-Baptiste Boric 	fsinode *inode;
18399f988b79SJean-Baptiste Boric 	int64_t r;
18409f988b79SJean-Baptiste Boric 
18419f988b79SJean-Baptiste Boric 	assert(node != NULL);
18429f988b79SJean-Baptiste Boric 
18439f988b79SJean-Baptiste Boric 
18449f988b79SJean-Baptiste Boric 	/*
18459f988b79SJean-Baptiste Boric 	 * NOTE : There needs to be some special case detection for
18469f988b79SJean-Baptiste Boric 	 * the "real root" node, since for it, node->node is undefined
18479f988b79SJean-Baptiste Boric 	 */
18489f988b79SJean-Baptiste Boric 
18499f988b79SJean-Baptiste Boric 	node->fileDataSector = -1;
18509f988b79SJean-Baptiste Boric 
18519f988b79SJean-Baptiste Boric 	if (node->type & CD9660_TYPE_DIR) {
18529f988b79SJean-Baptiste Boric 		node->fileRecordSize = cd9660_compute_record_size(
18539f988b79SJean-Baptiste Boric 		    diskStructure, node);
18549f988b79SJean-Baptiste Boric 		/*Set what sector this directory starts in*/
18559f988b79SJean-Baptiste Boric 		node->fileDataSector =
18569f988b79SJean-Baptiste Boric 		    CD9660_BLOCKS(diskStructure->sectorSize,startOffset);
18579f988b79SJean-Baptiste Boric 
18589f988b79SJean-Baptiste Boric 		cd9660_bothendian_dword(node->fileDataSector,
18599f988b79SJean-Baptiste Boric 		    node->isoDirRecord->extent);
18609f988b79SJean-Baptiste Boric 
18619f988b79SJean-Baptiste Boric 		/*
18629f988b79SJean-Baptiste Boric 		 * First loop over children, need to know the size of
18639f988b79SJean-Baptiste Boric 		 * their directory records
18649f988b79SJean-Baptiste Boric 		 */
18659f988b79SJean-Baptiste Boric 		node->fileSectorsUsed = 1;
18669f988b79SJean-Baptiste Boric 		TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
18679f988b79SJean-Baptiste Boric 			node->fileDataLength +=
18689f988b79SJean-Baptiste Boric 			    cd9660_compute_record_size(diskStructure, child);
18699f988b79SJean-Baptiste Boric 			if ((cd9660_compute_record_size(diskStructure, child) +
18709f988b79SJean-Baptiste Boric 			    current_sector_usage) >=
18719f988b79SJean-Baptiste Boric 		 	    diskStructure->sectorSize) {
18729f988b79SJean-Baptiste Boric 				current_sector_usage = 0;
18739f988b79SJean-Baptiste Boric 				node->fileSectorsUsed++;
18749f988b79SJean-Baptiste Boric 			}
18759f988b79SJean-Baptiste Boric 
18769f988b79SJean-Baptiste Boric 			current_sector_usage +=
18779f988b79SJean-Baptiste Boric 			    cd9660_compute_record_size(diskStructure, child);
18789f988b79SJean-Baptiste Boric 		}
18799f988b79SJean-Baptiste Boric 
18809f988b79SJean-Baptiste Boric 		cd9660_bothendian_dword(node->fileSectorsUsed *
18819f988b79SJean-Baptiste Boric 			diskStructure->sectorSize,node->isoDirRecord->size);
18829f988b79SJean-Baptiste Boric 
18839f988b79SJean-Baptiste Boric 		/*
18849f988b79SJean-Baptiste Boric 		 * This should point to the sector after the directory
18859f988b79SJean-Baptiste Boric 		 * record (or, the first byte in that sector)
18869f988b79SJean-Baptiste Boric 		 */
18879f988b79SJean-Baptiste Boric 		used_bytes += node->fileSectorsUsed * diskStructure->sectorSize;
18889f988b79SJean-Baptiste Boric 
18899f988b79SJean-Baptiste Boric 		for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
18909f988b79SJean-Baptiste Boric 		     child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
18919f988b79SJean-Baptiste Boric 			/* Directories need recursive call */
18929f988b79SJean-Baptiste Boric 			if (S_ISDIR(child->node->type)) {
18939f988b79SJean-Baptiste Boric 				r = cd9660_compute_offsets(diskStructure, child,
18949f988b79SJean-Baptiste Boric 				    used_bytes + startOffset);
18959f988b79SJean-Baptiste Boric 
18969f988b79SJean-Baptiste Boric 				if (r != -1)
18979f988b79SJean-Baptiste Boric 					used_bytes += r;
18989f988b79SJean-Baptiste Boric 				else
18999f988b79SJean-Baptiste Boric 					return -1;
19009f988b79SJean-Baptiste Boric 			}
19019f988b79SJean-Baptiste Boric 		}
19029f988b79SJean-Baptiste Boric 
19039f988b79SJean-Baptiste Boric 		/* Explicitly set the . and .. records */
19049f988b79SJean-Baptiste Boric 		cd9660_populate_dot_records(diskStructure, node);
19059f988b79SJean-Baptiste Boric 
19069f988b79SJean-Baptiste Boric 		/* Finally, do another iteration to write the file data*/
19079f988b79SJean-Baptiste Boric 		for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
19089f988b79SJean-Baptiste Boric 		     child != NULL;
19099f988b79SJean-Baptiste Boric 		     child = TAILQ_NEXT(child, cn_next_child)) {
19109f988b79SJean-Baptiste Boric 			/* Files need extent set */
19119f988b79SJean-Baptiste Boric 			if (S_ISDIR(child->node->type))
19129f988b79SJean-Baptiste Boric 				continue;
19139f988b79SJean-Baptiste Boric 			child->fileRecordSize =
19149f988b79SJean-Baptiste Boric 			    cd9660_compute_record_size(diskStructure, child);
19159f988b79SJean-Baptiste Boric 
19169f988b79SJean-Baptiste Boric 			child->fileSectorsUsed =
19179f988b79SJean-Baptiste Boric 			    CD9660_BLOCKS(diskStructure->sectorSize,
19189f988b79SJean-Baptiste Boric 				child->fileDataLength);
19199f988b79SJean-Baptiste Boric 
19209f988b79SJean-Baptiste Boric 			inode = child->node->inode;
19219f988b79SJean-Baptiste Boric 			if ((inode->flags & FI_ALLOCATED) == 0) {
19229f988b79SJean-Baptiste Boric 				inode->ino =
19239f988b79SJean-Baptiste Boric 				    CD9660_BLOCKS(diskStructure->sectorSize,
19249f988b79SJean-Baptiste Boric 				        used_bytes + startOffset);
19259f988b79SJean-Baptiste Boric 				inode->flags |= FI_ALLOCATED;
19269f988b79SJean-Baptiste Boric 				used_bytes += child->fileSectorsUsed *
19279f988b79SJean-Baptiste Boric 				    diskStructure->sectorSize;
19289f988b79SJean-Baptiste Boric 			} else {
19299f988b79SJean-Baptiste Boric 				INODE_WARNX(("%s: already allocated inode %d "
19309f988b79SJean-Baptiste Boric 				      "data sectors at %" PRIu32, __func__,
19319f988b79SJean-Baptiste Boric 				      (int)inode->st.st_ino, inode->ino));
19329f988b79SJean-Baptiste Boric 			}
19339f988b79SJean-Baptiste Boric 			child->fileDataSector = inode->ino;
19349f988b79SJean-Baptiste Boric 			cd9660_bothendian_dword(child->fileDataSector,
19359f988b79SJean-Baptiste Boric 				child->isoDirRecord->extent);
19369f988b79SJean-Baptiste Boric 		}
19379f988b79SJean-Baptiste Boric 	}
19389f988b79SJean-Baptiste Boric 
19399f988b79SJean-Baptiste Boric 	return used_bytes;
19409f988b79SJean-Baptiste Boric }
19419f988b79SJean-Baptiste Boric 
19429f988b79SJean-Baptiste Boric #if 0
19439f988b79SJean-Baptiste Boric /* Might get rid of this func */
19449f988b79SJean-Baptiste Boric static int
19459f988b79SJean-Baptiste Boric cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
19469f988b79SJean-Baptiste Boric {
19479f988b79SJean-Baptiste Boric 	to->node->inode->st.st_dev = 0;
19489f988b79SJean-Baptiste Boric 	to->node->inode->st.st_ino = 0;
19499f988b79SJean-Baptiste Boric 	to->node->inode->st.st_size = 0;
19509f988b79SJean-Baptiste Boric 	to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
19519f988b79SJean-Baptiste Boric 	to->node->inode->st.st_atime = from->node->inode->st.st_atime;
19529f988b79SJean-Baptiste Boric 	to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
19539f988b79SJean-Baptiste Boric 	to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
19549f988b79SJean-Baptiste Boric 	to->node->inode->st.st_uid = from->node->inode->st.st_uid;
19559f988b79SJean-Baptiste Boric 	to->node->inode->st.st_gid = from->node->inode->st.st_gid;
19569f988b79SJean-Baptiste Boric 	to->node->inode->st.st_mode = from->node->inode->st.st_mode;
19579f988b79SJean-Baptiste Boric 	/* Clear out type */
19589f988b79SJean-Baptiste Boric 	to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
19599f988b79SJean-Baptiste Boric 	if (file)
19609f988b79SJean-Baptiste Boric 		to->node->inode->st.st_mode |= S_IFREG;
19619f988b79SJean-Baptiste Boric 	else
19629f988b79SJean-Baptiste Boric 		to->node->inode->st.st_mode |= S_IFDIR;
19639f988b79SJean-Baptiste Boric 	return 1;
19649f988b79SJean-Baptiste Boric }
19659f988b79SJean-Baptiste Boric #endif
19669f988b79SJean-Baptiste Boric 
19679f988b79SJean-Baptiste Boric static cd9660node *
cd9660_create_virtual_entry(iso9660_disk * diskStructure,const char * name,cd9660node * parent,int file,int insert)19689f988b79SJean-Baptiste Boric cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name,
19699f988b79SJean-Baptiste Boric     cd9660node *parent, int file, int insert)
19709f988b79SJean-Baptiste Boric {
19719f988b79SJean-Baptiste Boric 	cd9660node *temp;
19729f988b79SJean-Baptiste Boric 	fsnode * tfsnode;
19739f988b79SJean-Baptiste Boric 
19749f988b79SJean-Baptiste Boric 	assert(parent != NULL);
19759f988b79SJean-Baptiste Boric 
19769f988b79SJean-Baptiste Boric 	temp = cd9660_allocate_cd9660node();
19779f988b79SJean-Baptiste Boric 	if (temp == NULL)
19789f988b79SJean-Baptiste Boric 		return NULL;
19799f988b79SJean-Baptiste Boric 
19809f988b79SJean-Baptiste Boric 	tfsnode = emalloc(sizeof(*tfsnode));
19819f988b79SJean-Baptiste Boric 	tfsnode->name = estrdup(name);
19829f988b79SJean-Baptiste Boric 	temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord));
19839f988b79SJean-Baptiste Boric 
19849f988b79SJean-Baptiste Boric 	cd9660_convert_filename(diskStructure, tfsnode->name,
19859f988b79SJean-Baptiste Boric 	    temp->isoDirRecord->name, file);
19869f988b79SJean-Baptiste Boric 
19879f988b79SJean-Baptiste Boric 	temp->node = tfsnode;
19889f988b79SJean-Baptiste Boric 	temp->parent = parent;
19899f988b79SJean-Baptiste Boric 
19909f988b79SJean-Baptiste Boric 	if (insert) {
19919f988b79SJean-Baptiste Boric 		if (temp->parent != NULL) {
19929f988b79SJean-Baptiste Boric 			temp->level = temp->parent->level + 1;
19939f988b79SJean-Baptiste Boric 			if (!TAILQ_EMPTY(&temp->parent->cn_children))
19949f988b79SJean-Baptiste Boric 				cd9660_sorted_child_insert(temp->parent, temp);
19959f988b79SJean-Baptiste Boric 			else
19969f988b79SJean-Baptiste Boric 				TAILQ_INSERT_HEAD(&temp->parent->cn_children,
19979f988b79SJean-Baptiste Boric 				    temp, cn_next_child);
19989f988b79SJean-Baptiste Boric 		}
19999f988b79SJean-Baptiste Boric 	}
20009f988b79SJean-Baptiste Boric 
20019f988b79SJean-Baptiste Boric 	if (parent->node != NULL) {
20029f988b79SJean-Baptiste Boric 		tfsnode->type = parent->node->type;
20039f988b79SJean-Baptiste Boric 	}
20049f988b79SJean-Baptiste Boric 
20059f988b79SJean-Baptiste Boric 	/* Clear out file type bits */
20069f988b79SJean-Baptiste Boric 	tfsnode->type &= ~(S_IFMT);
20079f988b79SJean-Baptiste Boric 	if (file)
20089f988b79SJean-Baptiste Boric 		tfsnode->type |= S_IFREG;
20099f988b79SJean-Baptiste Boric 	else
20109f988b79SJean-Baptiste Boric 		tfsnode->type |= S_IFDIR;
20119f988b79SJean-Baptiste Boric 
20129f988b79SJean-Baptiste Boric 	/* Indicate that there is no spec entry (inode) */
20139f988b79SJean-Baptiste Boric 	tfsnode->flags &= ~(FSNODE_F_HASSPEC);
20149f988b79SJean-Baptiste Boric #if 0
20159f988b79SJean-Baptiste Boric 	cd9660_copy_stat_info(parent, temp, file);
20169f988b79SJean-Baptiste Boric #endif
20179f988b79SJean-Baptiste Boric 	return temp;
20189f988b79SJean-Baptiste Boric }
20199f988b79SJean-Baptiste Boric 
20209f988b79SJean-Baptiste Boric static cd9660node *
cd9660_create_file(iso9660_disk * diskStructure,const char * name,cd9660node * parent,cd9660node * me)20219f988b79SJean-Baptiste Boric cd9660_create_file(iso9660_disk *diskStructure, const char *name,
20229f988b79SJean-Baptiste Boric     cd9660node *parent, cd9660node *me)
20239f988b79SJean-Baptiste Boric {
20249f988b79SJean-Baptiste Boric 	cd9660node *temp;
20259f988b79SJean-Baptiste Boric 
20269f988b79SJean-Baptiste Boric 	temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1);
20279f988b79SJean-Baptiste Boric 	if (temp == NULL)
20289f988b79SJean-Baptiste Boric 		return NULL;
20299f988b79SJean-Baptiste Boric 
20309f988b79SJean-Baptiste Boric 	temp->fileDataLength = 0;
20319f988b79SJean-Baptiste Boric 
20329f988b79SJean-Baptiste Boric 	temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
20339f988b79SJean-Baptiste Boric 
20349f988b79SJean-Baptiste Boric 	temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
20359f988b79SJean-Baptiste Boric 	*temp->node->inode = *me->node->inode;
20369f988b79SJean-Baptiste Boric 
20379f988b79SJean-Baptiste Boric 	if (cd9660_translate_node_common(diskStructure, temp) == 0)
20389f988b79SJean-Baptiste Boric 		return NULL;
20399f988b79SJean-Baptiste Boric 	return temp;
20409f988b79SJean-Baptiste Boric }
20419f988b79SJean-Baptiste Boric 
20429f988b79SJean-Baptiste Boric /*
20439f988b79SJean-Baptiste Boric  * Create a new directory which does not exist on disk
20449f988b79SJean-Baptiste Boric  * @param const char * name The name to assign to the directory
20459f988b79SJean-Baptiste Boric  * @param const char * parent Pointer to the parent directory
20469f988b79SJean-Baptiste Boric  * @returns cd9660node * Pointer to the new directory
20479f988b79SJean-Baptiste Boric  */
20489f988b79SJean-Baptiste Boric static cd9660node *
cd9660_create_directory(iso9660_disk * diskStructure,const char * name,cd9660node * parent,cd9660node * me)20499f988b79SJean-Baptiste Boric cd9660_create_directory(iso9660_disk *diskStructure, const char *name,
20509f988b79SJean-Baptiste Boric     cd9660node *parent, cd9660node *me)
20519f988b79SJean-Baptiste Boric {
20529f988b79SJean-Baptiste Boric 	cd9660node *temp;
20539f988b79SJean-Baptiste Boric 
20549f988b79SJean-Baptiste Boric 	temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1);
20559f988b79SJean-Baptiste Boric 	if (temp == NULL)
20569f988b79SJean-Baptiste Boric 		return NULL;
20579f988b79SJean-Baptiste Boric 	temp->node->type |= S_IFDIR;
20589f988b79SJean-Baptiste Boric 
20599f988b79SJean-Baptiste Boric 	temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
20609f988b79SJean-Baptiste Boric 
20619f988b79SJean-Baptiste Boric 	temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
20629f988b79SJean-Baptiste Boric 	*temp->node->inode = *me->node->inode;
20639f988b79SJean-Baptiste Boric 
20649f988b79SJean-Baptiste Boric 	if (cd9660_translate_node_common(diskStructure, temp) == 0)
20659f988b79SJean-Baptiste Boric 		return NULL;
20669f988b79SJean-Baptiste Boric 	return temp;
20679f988b79SJean-Baptiste Boric }
20689f988b79SJean-Baptiste Boric 
20699f988b79SJean-Baptiste Boric static cd9660node *
cd9660_create_special_directory(iso9660_disk * diskStructure,u_char type,cd9660node * parent)20709f988b79SJean-Baptiste Boric cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type,
20719f988b79SJean-Baptiste Boric     cd9660node *parent)
20729f988b79SJean-Baptiste Boric {
20739f988b79SJean-Baptiste Boric 	cd9660node *temp, *first;
20749f988b79SJean-Baptiste Boric 	char na[2];
20759f988b79SJean-Baptiste Boric 
20769f988b79SJean-Baptiste Boric 	assert(parent != NULL);
20779f988b79SJean-Baptiste Boric 
20789f988b79SJean-Baptiste Boric 	if (type == CD9660_TYPE_DOT)
20799f988b79SJean-Baptiste Boric 		na[0] = 0;
20809f988b79SJean-Baptiste Boric 	else if (type == CD9660_TYPE_DOTDOT)
20819f988b79SJean-Baptiste Boric 		na[0] = 1;
20829f988b79SJean-Baptiste Boric 	else
20839f988b79SJean-Baptiste Boric 		return 0;
20849f988b79SJean-Baptiste Boric 
20859f988b79SJean-Baptiste Boric 	na[1] = 0;
20869f988b79SJean-Baptiste Boric 	if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent,
20879f988b79SJean-Baptiste Boric 	    0, 0)) == NULL)
20889f988b79SJean-Baptiste Boric 		return NULL;
20899f988b79SJean-Baptiste Boric 
20909f988b79SJean-Baptiste Boric 	temp->parent = parent;
20919f988b79SJean-Baptiste Boric 	temp->type = type;
20929f988b79SJean-Baptiste Boric 	temp->isoDirRecord->length[0] = 34;
20939f988b79SJean-Baptiste Boric 	/* Dot record is always first */
20949f988b79SJean-Baptiste Boric 	if (type == CD9660_TYPE_DOT) {
20959f988b79SJean-Baptiste Boric 		parent->dot_record = temp;
20969f988b79SJean-Baptiste Boric 		TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
20979f988b79SJean-Baptiste Boric 	/* DotDot should be second */
20989f988b79SJean-Baptiste Boric 	} else if (type == CD9660_TYPE_DOTDOT) {
20999f988b79SJean-Baptiste Boric 		parent->dot_dot_record = temp;
21009f988b79SJean-Baptiste Boric 		/*
21019f988b79SJean-Baptiste Boric                  * If the first child is the dot record, insert
21029f988b79SJean-Baptiste Boric                  * this second.  Otherwise, insert it at the head.
21039f988b79SJean-Baptiste Boric 		 */
21049f988b79SJean-Baptiste Boric 		if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
21059f988b79SJean-Baptiste Boric 		    (first->type & CD9660_TYPE_DOT) == 0) {
21069f988b79SJean-Baptiste Boric 			TAILQ_INSERT_HEAD(&parent->cn_children, temp,
21079f988b79SJean-Baptiste Boric 			    cn_next_child);
21089f988b79SJean-Baptiste Boric 		} else {
21099f988b79SJean-Baptiste Boric 			TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
21109f988b79SJean-Baptiste Boric 			    cn_next_child);
21119f988b79SJean-Baptiste Boric 		}
21129f988b79SJean-Baptiste Boric 	}
21139f988b79SJean-Baptiste Boric 
21149f988b79SJean-Baptiste Boric 	return temp;
21159f988b79SJean-Baptiste Boric }
21169f988b79SJean-Baptiste Boric 
21179f988b79SJean-Baptiste Boric static int
cd9660_add_generic_bootimage(iso9660_disk * diskStructure,const char * bootimage)21189f988b79SJean-Baptiste Boric cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage)
21199f988b79SJean-Baptiste Boric {
21209f988b79SJean-Baptiste Boric 	struct stat stbuf;
21219f988b79SJean-Baptiste Boric 
21229f988b79SJean-Baptiste Boric 	assert(bootimage != NULL);
21239f988b79SJean-Baptiste Boric 
21249f988b79SJean-Baptiste Boric 	if (*bootimage == '\0') {
21259f988b79SJean-Baptiste Boric 		warnx("Error: Boot image must be a filename");
21269f988b79SJean-Baptiste Boric 		return 0;
21279f988b79SJean-Baptiste Boric 	}
21289f988b79SJean-Baptiste Boric 
21299f988b79SJean-Baptiste Boric 	diskStructure->generic_bootimage = estrdup(bootimage);
21309f988b79SJean-Baptiste Boric 
21319f988b79SJean-Baptiste Boric 	/* Get information about the file */
21329f988b79SJean-Baptiste Boric 	if (lstat(diskStructure->generic_bootimage, &stbuf) == -1)
21339f988b79SJean-Baptiste Boric 		err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
21349f988b79SJean-Baptiste Boric 		    diskStructure->generic_bootimage);
21359f988b79SJean-Baptiste Boric 
21369f988b79SJean-Baptiste Boric 	if (stbuf.st_size > 32768) {
21379f988b79SJean-Baptiste Boric 		warnx("Error: Boot image must be no greater than 32768 bytes");
21389f988b79SJean-Baptiste Boric 		return 0;
21399f988b79SJean-Baptiste Boric 	}
21409f988b79SJean-Baptiste Boric 
21419f988b79SJean-Baptiste Boric 	if (diskStructure->verbose_level > 0) {
21429f988b79SJean-Baptiste Boric 		printf("Generic boot image image has size %lld\n",
21439f988b79SJean-Baptiste Boric 		    (long long)stbuf.st_size);
21449f988b79SJean-Baptiste Boric 	}
21459f988b79SJean-Baptiste Boric 
21469f988b79SJean-Baptiste Boric 	diskStructure->has_generic_bootimage = 1;
21479f988b79SJean-Baptiste Boric 
21489f988b79SJean-Baptiste Boric 	return 1;
21499f988b79SJean-Baptiste Boric }
2150