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