1*ec5d1d82Stsutsui /* $NetBSD: cd9660.c,v 1.60 2023/12/28 12:13:55 tsutsui Exp $ */
23550dc98Sfvdl
33550dc98Sfvdl /*
43550dc98Sfvdl * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
53550dc98Sfvdl * Perez-Rathke and Ram Vedam. All rights reserved.
63550dc98Sfvdl *
73550dc98Sfvdl * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
83550dc98Sfvdl * Alan Perez-Rathke and Ram Vedam.
93550dc98Sfvdl *
103550dc98Sfvdl * Redistribution and use in source and binary forms, with or
113550dc98Sfvdl * without modification, are permitted provided that the following
123550dc98Sfvdl * conditions are met:
133550dc98Sfvdl * 1. Redistributions of source code must retain the above copyright
143550dc98Sfvdl * notice, this list of conditions and the following disclaimer.
153550dc98Sfvdl * 2. Redistributions in binary form must reproduce the above
163550dc98Sfvdl * copyright notice, this list of conditions and the following
173550dc98Sfvdl * disclaimer in the documentation and/or other materials provided
183550dc98Sfvdl * with the distribution.
193550dc98Sfvdl *
203550dc98Sfvdl * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
213550dc98Sfvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
223550dc98Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
233550dc98Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
243550dc98Sfvdl * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
253550dc98Sfvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
263550dc98Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
273550dc98Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
283550dc98Sfvdl * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
293550dc98Sfvdl * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
303550dc98Sfvdl * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313550dc98Sfvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
323550dc98Sfvdl * OF SUCH DAMAGE.
333550dc98Sfvdl */
343550dc98Sfvdl /*
353550dc98Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc.
363550dc98Sfvdl * All rights reserved.
373550dc98Sfvdl *
383550dc98Sfvdl * Written by Luke Mewburn for Wasabi Systems, Inc.
393550dc98Sfvdl *
403550dc98Sfvdl * Redistribution and use in source and binary forms, with or without
413550dc98Sfvdl * modification, are permitted provided that the following conditions
423550dc98Sfvdl * are met:
433550dc98Sfvdl * 1. Redistributions of source code must retain the above copyright
443550dc98Sfvdl * notice, this list of conditions and the following disclaimer.
453550dc98Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
463550dc98Sfvdl * notice, this list of conditions and the following disclaimer in the
473550dc98Sfvdl * documentation and/or other materials provided with the distribution.
483550dc98Sfvdl * 3. All advertising materials mentioning features or use of this software
493550dc98Sfvdl * must display the following acknowledgement:
503550dc98Sfvdl * This product includes software developed for the NetBSD Project by
513550dc98Sfvdl * Wasabi Systems, Inc.
523550dc98Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
533550dc98Sfvdl * or promote products derived from this software without specific prior
543550dc98Sfvdl * written permission.
553550dc98Sfvdl *
563550dc98Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
573550dc98Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
583550dc98Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
593550dc98Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
603550dc98Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
613550dc98Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
623550dc98Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
633550dc98Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
643550dc98Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
653550dc98Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
663550dc98Sfvdl * POSSIBILITY OF SUCH DAMAGE.
673550dc98Sfvdl */
683550dc98Sfvdl /*
693550dc98Sfvdl * Copyright (c) 1982, 1986, 1989, 1993
703550dc98Sfvdl * The Regents of the University of California. All rights reserved.
713550dc98Sfvdl *
723550dc98Sfvdl * Redistribution and use in source and binary forms, with or without
733550dc98Sfvdl * modification, are permitted provided that the following conditions
743550dc98Sfvdl * are met:
753550dc98Sfvdl * 1. Redistributions of source code must retain the above copyright
763550dc98Sfvdl * notice, this list of conditions and the following disclaimer.
773550dc98Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
783550dc98Sfvdl * notice, this list of conditions and the following disclaimer in the
793550dc98Sfvdl * documentation and/or other materials provided with the distribution.
803550dc98Sfvdl * 3. Neither the name of the University nor the names of its contributors
813550dc98Sfvdl * may be used to endorse or promote products derived from this software
823550dc98Sfvdl * without specific prior written permission.
833550dc98Sfvdl *
843550dc98Sfvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
853550dc98Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
863550dc98Sfvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
873550dc98Sfvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
883550dc98Sfvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
893550dc98Sfvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
903550dc98Sfvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
913550dc98Sfvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
923550dc98Sfvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
933550dc98Sfvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
943550dc98Sfvdl * SUCH DAMAGE.
953550dc98Sfvdl *
963550dc98Sfvdl */
973550dc98Sfvdl
983550dc98Sfvdl #if HAVE_NBTOOL_CONFIG_H
993550dc98Sfvdl #include "nbtool_config.h"
100889da177Shubertf #else
101889da177Shubertf #include <sys/mount.h>
1023550dc98Sfvdl #endif
1033550dc98Sfvdl
1043550dc98Sfvdl #include <sys/cdefs.h>
1053550dc98Sfvdl #if defined(__RCSID) && !defined(__lint)
106*ec5d1d82Stsutsui __RCSID("$NetBSD: cd9660.c,v 1.60 2023/12/28 12:13:55 tsutsui Exp $");
1073550dc98Sfvdl #endif /* !__lint */
1083550dc98Sfvdl
1093550dc98Sfvdl #include <string.h>
1103550dc98Sfvdl #include <ctype.h>
1113550dc98Sfvdl #include <sys/param.h>
1123550dc98Sfvdl #include <sys/queue.h>
113e4989541Schristos #include <util.h>
1143550dc98Sfvdl
1153550dc98Sfvdl #include "makefs.h"
1163550dc98Sfvdl #include "cd9660.h"
1173550dc98Sfvdl #include "cd9660/iso9660_rrip.h"
118f8474b32Sbjh21 #include "cd9660/cd9660_archimedes.h"
1193550dc98Sfvdl
1203550dc98Sfvdl /*
1213550dc98Sfvdl * Global variables
1223550dc98Sfvdl */
1233550dc98Sfvdl
124e4989541Schristos static void cd9660_finalize_PVD(iso9660_disk *);
1253550dc98Sfvdl static cd9660node *cd9660_allocate_cd9660node(void);
126e4989541Schristos static void cd9660_set_defaults(iso9660_disk *);
127c316d7b2Schristos static int cd9660_arguments_set_string(const char *, const char *, size_t,
1283550dc98Sfvdl char, char *);
1293550dc98Sfvdl static void cd9660_populate_iso_dir_record(
1303550dc98Sfvdl struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
1313550dc98Sfvdl const char *);
132e4989541Schristos static void cd9660_setup_root_node(iso9660_disk *);
133e4989541Schristos static int cd9660_setup_volume_descriptors(iso9660_disk *);
1343550dc98Sfvdl #if 0
1353550dc98Sfvdl static int cd9660_fill_extended_attribute_record(cd9660node *);
1363550dc98Sfvdl #endif
137689c61f6Sdyoung static void cd9660_sort_nodes(cd9660node *);
138e4989541Schristos static int cd9660_translate_node_common(iso9660_disk *, cd9660node *);
139e4989541Schristos static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *);
1403550dc98Sfvdl static int cd9660_compare_filename(const char *, const char *);
141689c61f6Sdyoung static void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
142e4989541Schristos static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int);
143e4989541Schristos static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int,
144e4989541Schristos int);
145e4989541Schristos static void cd9660_copy_filenames(iso9660_disk *, cd9660node *);
1463550dc98Sfvdl static void cd9660_sorting_nodes(cd9660node *);
1473550dc98Sfvdl static int cd9660_count_collisions(cd9660node *);
148e4989541Schristos static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *);
149e4989541Schristos static int cd9660_add_dot_records(iso9660_disk *, cd9660node *);
1503550dc98Sfvdl
151e4989541Schristos static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int,
152d0c4ff45Sdbj int *, int *);
1533550dc98Sfvdl static void cd9660_free_structure(cd9660node *);
154e4989541Schristos static int cd9660_generate_path_table(iso9660_disk *);
155e4989541Schristos static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *,
156e4989541Schristos int);
157e4989541Schristos static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *,
158e4989541Schristos int);
1593550dc98Sfvdl #if 0
160e4989541Schristos static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *,
161e4989541Schristos int);
1623550dc98Sfvdl #endif
163e4989541Schristos static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int);
164e4989541Schristos static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *);
165e4989541Schristos static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t);
1663550dc98Sfvdl #if 0
1673550dc98Sfvdl static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
1683550dc98Sfvdl #endif
169e4989541Schristos static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *,
170e4989541Schristos cd9660node *, int, int);
171e4989541Schristos static cd9660node *cd9660_create_file(iso9660_disk *, const char *,
172e4989541Schristos cd9660node *, cd9660node *);
173e4989541Schristos static cd9660node *cd9660_create_directory(iso9660_disk *, const char *,
174e4989541Schristos cd9660node *, cd9660node *);
175e4989541Schristos static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char,
176535dee52Schristos cd9660node *);
177e4989541Schristos static int cd9660_add_generic_bootimage(iso9660_disk *, const char *);
1783550dc98Sfvdl
1793550dc98Sfvdl
1803550dc98Sfvdl /*
181c56890eeSmsaitoh * Allocate and initialize a cd9660node
1823550dc98Sfvdl * @returns struct cd9660node * Pointer to new node, or NULL on error
1833550dc98Sfvdl */
1843550dc98Sfvdl static cd9660node *
cd9660_allocate_cd9660node(void)1853550dc98Sfvdl cd9660_allocate_cd9660node(void)
1863550dc98Sfvdl {
187e4989541Schristos cd9660node *temp = ecalloc(1, sizeof(*temp));
188689c61f6Sdyoung TAILQ_INIT(&temp->cn_children);
189689c61f6Sdyoung temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
190689c61f6Sdyoung temp->ptnext = temp->ptprev = temp->ptlast = NULL;
191689c61f6Sdyoung temp->node = NULL;
192689c61f6Sdyoung temp->isoDirRecord = NULL;
193689c61f6Sdyoung temp->isoExtAttributes = NULL;
194689c61f6Sdyoung temp->rr_real_parent = temp->rr_relocated = NULL;
195f8474b32Sbjh21 temp->su_tail_data = NULL;
1963550dc98Sfvdl return temp;
1973550dc98Sfvdl }
1983550dc98Sfvdl
1993550dc98Sfvdl int cd9660_defaults_set = 0;
2003550dc98Sfvdl
2013550dc98Sfvdl /**
2023550dc98Sfvdl * Set default values for cd9660 extension to makefs
2033550dc98Sfvdl */
2043550dc98Sfvdl static void
cd9660_set_defaults(iso9660_disk * diskStructure)205e4989541Schristos cd9660_set_defaults(iso9660_disk *diskStructure)
2063550dc98Sfvdl {
2073550dc98Sfvdl /*Fix the sector size for now, though the spec allows for other sizes*/
208e4989541Schristos diskStructure->sectorSize = 2048;
2093550dc98Sfvdl
2103550dc98Sfvdl /* Set up defaults in our own structure */
211e4989541Schristos diskStructure->verbose_level = 0;
212e4989541Schristos diskStructure->keep_bad_images = 0;
213e4989541Schristos diskStructure->follow_sym_links = 0;
214e4989541Schristos diskStructure->isoLevel = 2;
2153550dc98Sfvdl
216e4989541Schristos diskStructure->rock_ridge_enabled = 0;
217e4989541Schristos diskStructure->rock_ridge_renamed_dir_name = 0;
218e4989541Schristos diskStructure->rock_ridge_move_count = 0;
219e4989541Schristos diskStructure->rr_moved_dir = 0;
2203550dc98Sfvdl
221e4989541Schristos diskStructure->archimedes_enabled = 0;
222e4989541Schristos diskStructure->chrp_boot = 0;
223f8474b32Sbjh21
224e4989541Schristos diskStructure->include_padding_areas = 1;
2253550dc98Sfvdl
2263550dc98Sfvdl /* Spec breaking functionality */
227e4989541Schristos diskStructure->allow_deep_trees =
228e4989541Schristos diskStructure->allow_start_dot =
229e4989541Schristos diskStructure->allow_max_name =
230e4989541Schristos diskStructure->allow_illegal_chars =
231e4989541Schristos diskStructure->allow_lowercase =
232e4989541Schristos diskStructure->allow_multidot =
233e4989541Schristos diskStructure->omit_trailing_period = 0;
2343550dc98Sfvdl
2353550dc98Sfvdl /* Make sure the PVD is clear */
236e4989541Schristos memset(&diskStructure->primaryDescriptor, 0, 2048);
2373550dc98Sfvdl
238e4989541Schristos memset(diskStructure->primaryDescriptor.publisher_id, 0x20,128);
239e4989541Schristos memset(diskStructure->primaryDescriptor.preparer_id, 0x20,128);
240e4989541Schristos memset(diskStructure->primaryDescriptor.application_id, 0x20,128);
241e4989541Schristos memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37);
242e4989541Schristos memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37);
243e4989541Schristos memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37);
2443550dc98Sfvdl
245e4989541Schristos strcpy(diskStructure->primaryDescriptor.system_id,"NetBSD");
2463550dc98Sfvdl
2473550dc98Sfvdl cd9660_defaults_set = 1;
2483550dc98Sfvdl
2493550dc98Sfvdl /* Boot support: Initially disabled */
250e4989541Schristos diskStructure->has_generic_bootimage = 0;
251e4989541Schristos diskStructure->generic_bootimage = NULL;
252989d6357Sskrll
253e4989541Schristos diskStructure->boot_image_directory = 0;
254e4989541Schristos /*memset(diskStructure->boot_descriptor, 0, 2048);*/
2553550dc98Sfvdl
256e4989541Schristos diskStructure->is_bootable = 0;
257e4989541Schristos TAILQ_INIT(&diskStructure->boot_images);
258e4989541Schristos LIST_INIT(&diskStructure->boot_entries);
2593550dc98Sfvdl }
2603550dc98Sfvdl
2613550dc98Sfvdl void
cd9660_prep_opts(fsinfo_t * fsopts)262e4989541Schristos cd9660_prep_opts(fsinfo_t *fsopts)
2633550dc98Sfvdl {
264e4989541Schristos iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
265e4989541Schristos
2663cfc4347Schristos #define OPT_STR(letter, name, desc) \
2673cfc4347Schristos { letter, name, NULL, OPT_STRBUF, 0, 0, desc }
2683cfc4347Schristos
2693cfc4347Schristos #define OPT_NUM(letter, name, field, min, max, desc) \
2703cfc4347Schristos { letter, name, &diskStructure->field, \
2713cfc4347Schristos sizeof(diskStructure->field) == 8 ? OPT_INT64 : \
2723cfc4347Schristos (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \
2733cfc4347Schristos (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \
2743cfc4347Schristos min, max, desc }
2753cfc4347Schristos
2763cfc4347Schristos #define OPT_BOOL(letter, name, field, desc) \
2773cfc4347Schristos OPT_NUM(letter, name, field, 0, 1, desc)
2783cfc4347Schristos
279e4989541Schristos const option_t cd9660_options[] = {
2803cfc4347Schristos OPT_NUM('l', "isolevel", isoLevel,
2813cfc4347Schristos 1, 3, "ISO Level"),
2823cfc4347Schristos OPT_NUM('v', "verbose", verbose_level,
2833cfc4347Schristos 0, 2, "Turns on verbose output"),
28450d02345Schristos
2853cfc4347Schristos OPT_BOOL('h', "help", displayHelp,
2863cfc4347Schristos "Show help message"),
2873cfc4347Schristos OPT_BOOL('S', "follow-symlinks", follow_sym_links,
2883cfc4347Schristos "Resolve symlinks in pathnames"),
2893cfc4347Schristos OPT_BOOL('R', "rockridge", rock_ridge_enabled,
2903cfc4347Schristos "Enable Rock-Ridge extensions"),
2913cfc4347Schristos OPT_BOOL('C', "chrp-boot", chrp_boot,
2923cfc4347Schristos "Enable CHRP boot"),
2933cfc4347Schristos OPT_BOOL('K', "keep-bad-images", keep_bad_images,
2943cfc4347Schristos "Keep bad images"),
2953cfc4347Schristos OPT_BOOL('D', "allow-deep-trees", allow_deep_trees,
2963cfc4347Schristos "Allow trees more than 8 levels"),
2973cfc4347Schristos OPT_BOOL('a', "allow-max-name", allow_max_name,
2983cfc4347Schristos "Allow 37 char filenames (unimplemented)"),
2993cfc4347Schristos OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars,
3003cfc4347Schristos "Allow illegal characters in filenames"),
301c8429e33Schristos OPT_BOOL('m', "allow-multidot", allow_multidot,
3023cfc4347Schristos "Allow multiple periods in filenames"),
3033cfc4347Schristos OPT_BOOL('o', "omit-trailing-period", omit_trailing_period,
3043cfc4347Schristos "Omit trailing periods in filenames"),
3053cfc4347Schristos OPT_BOOL('\0', "allow-lowercase", allow_lowercase,
3063cfc4347Schristos "Allow lowercase characters in filenames"),
3073cfc4347Schristos OPT_BOOL('\0', "archimedes", archimedes_enabled,
3083cfc4347Schristos "Enable Archimedes structure"),
3093cfc4347Schristos OPT_BOOL('\0', "no-trailing-padding", include_padding_areas,
3103cfc4347Schristos "Include padding areas"),
31150d02345Schristos
3123cfc4347Schristos OPT_STR('A', "applicationid", "Application Identifier"),
3133cfc4347Schristos OPT_STR('P', "publisher", "Publisher Identifier"),
3143cfc4347Schristos OPT_STR('p', "preparer", "Preparer Identifier"),
3153cfc4347Schristos OPT_STR('L', "label", "Disk Label"),
3163cfc4347Schristos OPT_STR('V', "volumeid", "Volume Set Identifier"),
3173cfc4347Schristos OPT_STR('B', "bootimage", "Boot image parameter"),
3183cfc4347Schristos OPT_STR('G', "generic-bootimage", "Generic boot image param"),
3193cfc4347Schristos OPT_STR('\0', "bootimagedir", "Boot image directory"),
3203cfc4347Schristos OPT_STR('\0', "no-emul-boot", "No boot emulation"),
3213cfc4347Schristos OPT_STR('\0', "no-boot", "No boot support"),
3223cfc4347Schristos OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"),
3233cfc4347Schristos OPT_STR('\0', "boot-load-segment", "Boot load segment"),
324d01ec609Snonaka OPT_STR('\0', "platformid", "Section Header Platform ID"),
3253cfc4347Schristos
326e4989541Schristos { .name = NULL }
327e4989541Schristos };
328e4989541Schristos
329e4989541Schristos fsopts->fs_specific = diskStructure;
330e4989541Schristos fsopts->fs_options = copy_opts(cd9660_options);
331e4989541Schristos
332e4989541Schristos cd9660_set_defaults(diskStructure);
3333550dc98Sfvdl }
3343550dc98Sfvdl
3353550dc98Sfvdl void
cd9660_cleanup_opts(fsinfo_t * fsopts)336e4989541Schristos cd9660_cleanup_opts(fsinfo_t *fsopts)
3373550dc98Sfvdl {
338e4989541Schristos free(fsopts->fs_specific);
339e4989541Schristos free(fsopts->fs_options);
3403550dc98Sfvdl }
3413550dc98Sfvdl
3423550dc98Sfvdl static int
cd9660_arguments_set_string(const char * val,const char * fieldtitle,size_t length,char testmode,char * dest)343c316d7b2Schristos cd9660_arguments_set_string(const char *val, const char *fieldtitle,
344c316d7b2Schristos size_t length, char testmode, char * dest)
3453550dc98Sfvdl {
346c316d7b2Schristos size_t len;
347c316d7b2Schristos int test;
3483550dc98Sfvdl
3493550dc98Sfvdl if (val == NULL)
3503550dc98Sfvdl warnx("error: The %s requires a string argument", fieldtitle);
3513550dc98Sfvdl else if ((len = strlen(val)) <= length) {
3523550dc98Sfvdl if (testmode == 'd')
3533550dc98Sfvdl test = cd9660_valid_d_chars(val);
3543550dc98Sfvdl else
3553550dc98Sfvdl test = cd9660_valid_a_chars(val);
3563550dc98Sfvdl if (test) {
3573550dc98Sfvdl memcpy(dest, val, len);
3583550dc98Sfvdl if (test == 2)
3593550dc98Sfvdl cd9660_uppercase_characters(dest, len);
3603550dc98Sfvdl return 1;
3613550dc98Sfvdl } else
3623550dc98Sfvdl warnx("error: The %s must be composed of "
3633550dc98Sfvdl "%c-characters", fieldtitle, testmode);
3643550dc98Sfvdl } else
3653550dc98Sfvdl warnx("error: The %s must be at most 32 characters long",
3663550dc98Sfvdl fieldtitle);
3673550dc98Sfvdl return 0;
3683550dc98Sfvdl }
3693550dc98Sfvdl
3703550dc98Sfvdl /*
3713550dc98Sfvdl * Command-line parsing function
3723550dc98Sfvdl */
3733550dc98Sfvdl
3743550dc98Sfvdl int
cd9660_parse_opts(const char * option,fsinfo_t * fsopts)3753550dc98Sfvdl cd9660_parse_opts(const char *option, fsinfo_t *fsopts)
3763550dc98Sfvdl {
37750d02345Schristos int rv, i;
378e4989541Schristos iso9660_disk *diskStructure = fsopts->fs_specific;
37950d02345Schristos option_t *cd9660_options = fsopts->fs_options;
38050d02345Schristos char buf[1024];
38150d02345Schristos const char *name, *desc;
3825d94f47eShe
3833550dc98Sfvdl assert(option != NULL);
3843550dc98Sfvdl
3853550dc98Sfvdl if (debug & DEBUG_FS_PARSE_OPTS)
3868ee626c9Schristos printf("%s: got `%s'\n", __func__, option);
3873550dc98Sfvdl
38850d02345Schristos i = set_option(cd9660_options, option, buf, sizeof(buf));
38950d02345Schristos if (i == -1)
39050d02345Schristos return 0;
3913550dc98Sfvdl
39250d02345Schristos if (cd9660_options[i].name == NULL)
39350d02345Schristos abort();
3943550dc98Sfvdl
39550d02345Schristos
39650d02345Schristos name = cd9660_options[i].name;
39750d02345Schristos desc = cd9660_options[i].desc;
39850d02345Schristos switch (cd9660_options[i].letter) {
39950d02345Schristos case 'h':
40050d02345Schristos case 'S':
40150d02345Schristos rv = 0; /* this is not handled yet */
40250d02345Schristos break;
40350d02345Schristos case 'L':
40450d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 32, 'd',
405e4989541Schristos diskStructure->primaryDescriptor.volume_id);
40650d02345Schristos break;
40750d02345Schristos case 'A':
40850d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
409e4989541Schristos diskStructure->primaryDescriptor.application_id);
41050d02345Schristos break;
41150d02345Schristos case 'P':
41250d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
41350d02345Schristos diskStructure->primaryDescriptor.publisher_id);
41450d02345Schristos break;
41550d02345Schristos case 'p':
41650d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
41750d02345Schristos diskStructure->primaryDescriptor.preparer_id);
41850d02345Schristos break;
41950d02345Schristos case 'V':
42050d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 128, 'a',
42150d02345Schristos diskStructure->primaryDescriptor.volume_set_id);
42250d02345Schristos break;
4233550dc98Sfvdl /* Boot options */
42450d02345Schristos case 'B':
42550d02345Schristos if (buf[0] == '\0') {
42650d02345Schristos warnx("The Boot Image parameter requires a valid boot"
42750d02345Schristos " information string");
42850d02345Schristos rv = 0;
42950d02345Schristos } else
43050d02345Schristos rv = cd9660_add_boot_disk(diskStructure, buf);
43150d02345Schristos break;
43250d02345Schristos case 'G':
43350d02345Schristos if (buf[0] == '\0') {
43450d02345Schristos warnx("The Generic Boot Image parameter requires a"
43550d02345Schristos " valid boot information string");
43650d02345Schristos rv = 0;
43750d02345Schristos } else
43850d02345Schristos rv = cd9660_add_generic_bootimage(diskStructure, buf);
43950d02345Schristos break;
44050d02345Schristos default:
44150d02345Schristos if (strcmp(name, "bootimagedir") == 0) {
4423550dc98Sfvdl /*
4433550dc98Sfvdl * XXXfvdl this is unused.
4443550dc98Sfvdl */
44550d02345Schristos if (buf[0] == '\0') {
44650d02345Schristos warnx("The Boot Image Directory parameter"
4478ee626c9Schristos " requires a directory name");
44850d02345Schristos rv = 0;
44950d02345Schristos } else {
450e4989541Schristos diskStructure->boot_image_directory =
45150d02345Schristos emalloc(strlen(buf) + 1);
4523550dc98Sfvdl /* BIG TODO: Add the max length function here */
45350d02345Schristos rv = cd9660_arguments_set_string(buf, desc, 12,
45450d02345Schristos 'd', diskStructure->boot_image_directory);
4553550dc98Sfvdl }
45650d02345Schristos } else if (strcmp(name, "no-emul-boot") == 0 ||
45750d02345Schristos strcmp(name, "no-boot") == 0 ||
45850d02345Schristos strcmp(name, "hard-disk-boot") == 0) {
4593550dc98Sfvdl /* RRIP */
46050d02345Schristos cd9660_eltorito_add_boot_option(diskStructure, name, 0);
46150d02345Schristos rv = 1;
462d01ec609Snonaka } else if (strcmp(name, "boot-load-segment") == 0 ||
463d01ec609Snonaka strcmp(name, "platformid") == 0) {
46450d02345Schristos if (buf[0] == '\0') {
46550d02345Schristos warnx("Option `%s' doesn't contain a value",
46650d02345Schristos name);
467323f38bdSchristos rv = 0;
468924dbbdbShubertf } else {
46950d02345Schristos cd9660_eltorito_add_boot_option(diskStructure,
47050d02345Schristos name, buf);
47150d02345Schristos rv = 1;
472924dbbdbShubertf }
4733550dc98Sfvdl } else
47450d02345Schristos rv = 1;
47572846fc1Sbjh21 }
47650d02345Schristos return rv;
4773550dc98Sfvdl }
4783550dc98Sfvdl
4793550dc98Sfvdl /*
4803550dc98Sfvdl * Main function for cd9660_makefs
4813550dc98Sfvdl * Builds the ISO image file
4823550dc98Sfvdl * @param const char *image The image filename to create
4833550dc98Sfvdl * @param const char *dir The directory that is being read
4843550dc98Sfvdl * @param struct fsnode *root The root node of the filesystem tree
4853550dc98Sfvdl * @param struct fsinfo_t *fsopts Any options
4863550dc98Sfvdl */
4873550dc98Sfvdl void
cd9660_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)4883550dc98Sfvdl cd9660_makefs(const char *image, const char *dir, fsnode *root,
4893550dc98Sfvdl fsinfo_t *fsopts)
4903550dc98Sfvdl {
4912ec6e0f1Schristos int64_t startoffset;
4923550dc98Sfvdl int numDirectories;
4932ec6e0f1Schristos uint64_t pathTableSectors;
4942ec6e0f1Schristos int64_t firstAvailableSector;
4952ec6e0f1Schristos int64_t totalSpace;
4963550dc98Sfvdl int error;
4973550dc98Sfvdl cd9660node *real_root;
498e4989541Schristos iso9660_disk *diskStructure = fsopts->fs_specific;
4993550dc98Sfvdl
500e4989541Schristos if (diskStructure->verbose_level > 0)
5018ee626c9Schristos printf("%s: ISO level is %i\n", __func__,
502e4989541Schristos diskStructure->isoLevel);
503e4989541Schristos if (diskStructure->isoLevel < 2 &&
504e4989541Schristos diskStructure->allow_multidot)
5057408c4ccSchristos errx(EXIT_FAILURE, "allow-multidot requires iso level of 2");
5063550dc98Sfvdl
5073550dc98Sfvdl assert(image != NULL);
5083550dc98Sfvdl assert(dir != NULL);
5093550dc98Sfvdl assert(root != NULL);
5103550dc98Sfvdl
511e4989541Schristos if (diskStructure->displayHelp) {
5123550dc98Sfvdl /*
5133550dc98Sfvdl * Display help here - probably want to put it in
5143550dc98Sfvdl * a separate function
5153550dc98Sfvdl */
5163550dc98Sfvdl return;
5173550dc98Sfvdl }
5183550dc98Sfvdl
519e4989541Schristos if (diskStructure->verbose_level > 0)
5208ee626c9Schristos printf("%s: image %s directory %s root %p\n", __func__,
5213550dc98Sfvdl image, dir, root);
5223550dc98Sfvdl
5233550dc98Sfvdl /* Set up some constants. Later, these will be defined with options */
5243550dc98Sfvdl
5253550dc98Sfvdl /* Counter needed for path tables */
5263550dc98Sfvdl numDirectories = 0;
5273550dc98Sfvdl
5283550dc98Sfvdl /* Convert tree to our own format */
5293550dc98Sfvdl /* Actually, we now need to add the REAL root node, at level 0 */
5303550dc98Sfvdl
5313550dc98Sfvdl real_root = cd9660_allocate_cd9660node();
532e4989541Schristos real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord));
5333550dc98Sfvdl /* Leave filename blank for root */
5343550dc98Sfvdl memset(real_root->isoDirRecord->name, 0,
5353550dc98Sfvdl ISO_FILENAME_MAXLENGTH_WITH_PADDING);
5363550dc98Sfvdl
5373550dc98Sfvdl real_root->level = 0;
538e4989541Schristos diskStructure->rootNode = real_root;
5393550dc98Sfvdl real_root->type = CD9660_TYPE_DIR;
5403550dc98Sfvdl error = 0;
541689c61f6Sdyoung real_root->node = root;
542e4989541Schristos cd9660_convert_structure(diskStructure, root, real_root, 1,
543e4989541Schristos &numDirectories, &error);
5443550dc98Sfvdl
545689c61f6Sdyoung if (TAILQ_EMPTY(&real_root->cn_children)) {
5467408c4ccSchristos errx(EXIT_FAILURE, "%s: converted directory is empty. "
5477408c4ccSchristos "Tree conversion failed", __func__);
5483550dc98Sfvdl } else if (error != 0) {
5497408c4ccSchristos errx(EXIT_FAILURE, "%s: tree conversion failed", __func__);
5503550dc98Sfvdl } else {
551e4989541Schristos if (diskStructure->verbose_level > 0)
5528ee626c9Schristos printf("%s: tree converted\n", __func__);
5533550dc98Sfvdl }
5543550dc98Sfvdl
5553550dc98Sfvdl /* Add the dot and dot dot records */
556e4989541Schristos cd9660_add_dot_records(diskStructure, real_root);
5573550dc98Sfvdl
558e4989541Schristos cd9660_setup_root_node(diskStructure);
5593550dc98Sfvdl
560e4989541Schristos if (diskStructure->verbose_level > 0)
5618ee626c9Schristos printf("%s: done converting tree\n", __func__);
5623550dc98Sfvdl
563f8474b32Sbjh21 /* non-SUSP extensions */
564e4989541Schristos if (diskStructure->archimedes_enabled)
565e4989541Schristos archimedes_convert_tree(diskStructure->rootNode);
566f8474b32Sbjh21
5673550dc98Sfvdl /* Rock ridge / SUSP init pass */
568e4989541Schristos if (diskStructure->rock_ridge_enabled) {
569e4989541Schristos cd9660_susp_initialize(diskStructure, diskStructure->rootNode,
570e4989541Schristos diskStructure->rootNode, NULL);
571689c61f6Sdyoung }
5723550dc98Sfvdl
5733550dc98Sfvdl /* Build path table structure */
574e4989541Schristos diskStructure->pathTableLength = cd9660_generate_path_table(
575e4989541Schristos diskStructure);
5763550dc98Sfvdl
577e4989541Schristos pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize,
578e4989541Schristos diskStructure->pathTableLength);
5793550dc98Sfvdl
580e4989541Schristos firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure);
581e4989541Schristos if (diskStructure->is_bootable) {
582e4989541Schristos firstAvailableSector = cd9660_setup_boot(diskStructure,
583e4989541Schristos firstAvailableSector);
5843550dc98Sfvdl if (firstAvailableSector < 0)
5857408c4ccSchristos errx(EXIT_FAILURE, "setup_boot failed");
5863550dc98Sfvdl }
5873550dc98Sfvdl /* LE first, then BE */
588e4989541Schristos diskStructure->primaryLittleEndianTableSector = firstAvailableSector;
589e4989541Schristos diskStructure->primaryBigEndianTableSector =
590e4989541Schristos diskStructure->primaryLittleEndianTableSector + pathTableSectors;
5913550dc98Sfvdl
5923550dc98Sfvdl /* Set the secondary ones to -1, not going to use them for now */
593e4989541Schristos diskStructure->secondaryBigEndianTableSector = -1;
594e4989541Schristos diskStructure->secondaryLittleEndianTableSector = -1;
5953550dc98Sfvdl
596e4989541Schristos diskStructure->dataFirstSector =
597e4989541Schristos diskStructure->primaryBigEndianTableSector + pathTableSectors;
598e4989541Schristos if (diskStructure->verbose_level > 0)
5998ee626c9Schristos printf("%s: Path table conversion complete. "
6002ec6e0f1Schristos "Each table is %i bytes, or %" PRIu64 " sectors.\n",
6018ee626c9Schristos __func__,
602e4989541Schristos diskStructure->pathTableLength, pathTableSectors);
6033550dc98Sfvdl
604e4989541Schristos startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector;
6053550dc98Sfvdl
606e4989541Schristos totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset);
6073550dc98Sfvdl
608e4989541Schristos diskStructure->totalSectors = diskStructure->dataFirstSector +
609e4989541Schristos CD9660_BLOCKS(diskStructure->sectorSize, totalSpace);
6103550dc98Sfvdl
6113550dc98Sfvdl /* Disabled until pass 1 is done */
612e4989541Schristos if (diskStructure->rock_ridge_enabled) {
613e4989541Schristos diskStructure->susp_continuation_area_start_sector =
614e4989541Schristos diskStructure->totalSectors;
615e4989541Schristos diskStructure->totalSectors +=
616e4989541Schristos CD9660_BLOCKS(diskStructure->sectorSize,
617e4989541Schristos diskStructure->susp_continuation_area_size);
618e4989541Schristos cd9660_susp_finalize(diskStructure, diskStructure->rootNode);
6193550dc98Sfvdl }
6203550dc98Sfvdl
6213550dc98Sfvdl
622e4989541Schristos cd9660_finalize_PVD(diskStructure);
6233550dc98Sfvdl
6243550dc98Sfvdl /* Add padding sectors, just for testing purposes right now */
625e4989541Schristos /* diskStructure->totalSectors+=150; */
6263550dc98Sfvdl
6273550dc98Sfvdl /* Debugging output */
628e4989541Schristos if (diskStructure->verbose_level > 0) {
6298ee626c9Schristos printf("%s: Sectors 0-15 reserved\n", __func__);
6308ee626c9Schristos printf("%s: Primary path tables starts in sector %"
6318ee626c9Schristos PRId64 "\n", __func__,
6328ee626c9Schristos diskStructure->primaryLittleEndianTableSector);
6338ee626c9Schristos printf("%s: File data starts in sector %"
6348ee626c9Schristos PRId64 "\n", __func__, diskStructure->dataFirstSector);
6358ee626c9Schristos printf("%s: Total sectors: %"
6368ee626c9Schristos PRId64 "\n", __func__, diskStructure->totalSectors);
6373550dc98Sfvdl }
6383550dc98Sfvdl
6393550dc98Sfvdl /*
6403550dc98Sfvdl * Add padding sectors at the end
6413550dc98Sfvdl * TODO: Clean this up and separate padding
6423550dc98Sfvdl */
643e4989541Schristos if (diskStructure->include_padding_areas)
644e4989541Schristos diskStructure->totalSectors += 150;
6453550dc98Sfvdl
646e4989541Schristos cd9660_write_image(diskStructure, image);
6473550dc98Sfvdl
648e4989541Schristos if (diskStructure->verbose_level > 1) {
649e4989541Schristos debug_print_volume_descriptor_information(diskStructure);
650e4989541Schristos debug_print_tree(diskStructure, real_root, 0);
6513550dc98Sfvdl debug_print_path_tree(real_root);
6523550dc98Sfvdl }
6533550dc98Sfvdl
6543550dc98Sfvdl /* Clean up data structures */
6553550dc98Sfvdl cd9660_free_structure(real_root);
6563550dc98Sfvdl
657e4989541Schristos if (diskStructure->verbose_level > 0)
6588ee626c9Schristos printf("%s: done\n", __func__);
6593550dc98Sfvdl }
6603550dc98Sfvdl
6613550dc98Sfvdl /* Generic function pointer - implement later */
6623550dc98Sfvdl typedef int (*cd9660node_func)(cd9660node *);
6633550dc98Sfvdl
6643550dc98Sfvdl static void
cd9660_finalize_PVD(iso9660_disk * diskStructure)665e4989541Schristos cd9660_finalize_PVD(iso9660_disk *diskStructure)
6663550dc98Sfvdl {
667204cbe51Schristos time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL);
6683550dc98Sfvdl
6693550dc98Sfvdl /* root should be a fixed size of 34 bytes since it has no name */
670e4989541Schristos memcpy(diskStructure->primaryDescriptor.root_directory_record,
671e4989541Schristos diskStructure->rootNode->dot_record->isoDirRecord, 34);
6723550dc98Sfvdl
6733550dc98Sfvdl /* In RRIP, this might be longer than 34 */
674e4989541Schristos diskStructure->primaryDescriptor.root_directory_record[0] = 34;
6753550dc98Sfvdl
6763550dc98Sfvdl /* Set up all the important numbers in the PVD */
677e4989541Schristos cd9660_bothendian_dword(diskStructure->totalSectors,
678e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.volume_space_size);
6793550dc98Sfvdl cd9660_bothendian_word(1,
680e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.volume_set_size);
6813550dc98Sfvdl cd9660_bothendian_word(1,
6823550dc98Sfvdl (unsigned char *)
683e4989541Schristos diskStructure->primaryDescriptor.volume_sequence_number);
684e4989541Schristos cd9660_bothendian_word(diskStructure->sectorSize,
6853550dc98Sfvdl (unsigned char *)
686e4989541Schristos diskStructure->primaryDescriptor.logical_block_size);
687e4989541Schristos cd9660_bothendian_dword(diskStructure->pathTableLength,
688e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.path_table_size);
6893550dc98Sfvdl
690e4989541Schristos cd9660_731(diskStructure->primaryLittleEndianTableSector,
691e4989541Schristos (u_char *)diskStructure->primaryDescriptor.type_l_path_table);
692e4989541Schristos cd9660_732(diskStructure->primaryBigEndianTableSector,
693e4989541Schristos (u_char *)diskStructure->primaryDescriptor.type_m_path_table);
6943550dc98Sfvdl
695e4989541Schristos diskStructure->primaryDescriptor.file_structure_version[0] = 1;
6963550dc98Sfvdl
6973550dc98Sfvdl /* Pad all strings with spaces instead of nulls */
698e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32);
699e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32);
700e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id,
7013550dc98Sfvdl 128);
702e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id,
7033550dc98Sfvdl 128);
704e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id,
7053550dc98Sfvdl 128);
706e4989541Schristos cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id,
7073550dc98Sfvdl 128);
7083550dc98Sfvdl cd9660_pad_string_spaces(
709e4989541Schristos diskStructure->primaryDescriptor.copyright_file_id, 37);
7103550dc98Sfvdl cd9660_pad_string_spaces(
711e4989541Schristos diskStructure->primaryDescriptor.abstract_file_id, 37);
7123550dc98Sfvdl cd9660_pad_string_spaces(
713e4989541Schristos diskStructure->primaryDescriptor.bibliographic_file_id, 37);
7143550dc98Sfvdl
7153550dc98Sfvdl /* Setup dates */
7163550dc98Sfvdl cd9660_time_8426(
717e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.creation_date,
718204cbe51Schristos tstamp);
7193550dc98Sfvdl cd9660_time_8426(
720e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.modification_date,
721204cbe51Schristos tstamp);
7223550dc98Sfvdl
723204cbe51Schristos #if 0
724204cbe51Schristos cd9660_set_date(diskStructure->primaryDescriptor.expiration_date,
725204cbe51Schristos tstamp);
726204cbe51Schristos #endif
7277da88b25Sreinoud memset(diskStructure->primaryDescriptor.expiration_date, '0' ,16);
7287da88b25Sreinoud diskStructure->primaryDescriptor.expiration_date[16] = 0;
7293550dc98Sfvdl
7303550dc98Sfvdl cd9660_time_8426(
731e4989541Schristos (unsigned char *)diskStructure->primaryDescriptor.effective_date,
732204cbe51Schristos tstamp);
7333550dc98Sfvdl }
7343550dc98Sfvdl
7353550dc98Sfvdl static void
cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 * record,u_char ext_attr_length,u_char flags,u_char name_len,const char * name)7363550dc98Sfvdl cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
7373550dc98Sfvdl u_char ext_attr_length, u_char flags,
7383550dc98Sfvdl u_char name_len, const char * name)
7393550dc98Sfvdl {
7407e2bdad6Schristos time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL);
7417e2bdad6Schristos
7423550dc98Sfvdl record->ext_attr_length[0] = ext_attr_length;
7437e2bdad6Schristos cd9660_time_915(record->date, tstamp);
7443550dc98Sfvdl record->flags[0] = ISO_FLAG_CLEAR | flags;
7453550dc98Sfvdl record->file_unit_size[0] = 0;
7463550dc98Sfvdl record->interleave[0] = 0;
7473550dc98Sfvdl cd9660_bothendian_word(1, record->volume_sequence_number);
7483550dc98Sfvdl record->name_len[0] = name_len;
74940ccd11cSahoka memset(record->name, '\0', sizeof (record->name));
7503550dc98Sfvdl memcpy(record->name, name, name_len);
7513550dc98Sfvdl record->length[0] = 33 + name_len;
7523550dc98Sfvdl
7533550dc98Sfvdl /* Todo : better rounding */
7543550dc98Sfvdl record->length[0] += (record->length[0] & 1) ? 1 : 0;
7553550dc98Sfvdl }
7563550dc98Sfvdl
7573550dc98Sfvdl static void
cd9660_setup_root_node(iso9660_disk * diskStructure)758e4989541Schristos cd9660_setup_root_node(iso9660_disk *diskStructure)
7593550dc98Sfvdl {
760e4989541Schristos cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord,
7613550dc98Sfvdl 0, ISO_FLAG_DIRECTORY, 1, "\0");
7623550dc98Sfvdl
7633550dc98Sfvdl }
7643550dc98Sfvdl
7653550dc98Sfvdl /*********** SUPPORT FUNCTIONS ***********/
7663550dc98Sfvdl static int
cd9660_setup_volume_descriptors(iso9660_disk * diskStructure)767e4989541Schristos cd9660_setup_volume_descriptors(iso9660_disk *diskStructure)
7683550dc98Sfvdl {
7693550dc98Sfvdl /* Boot volume descriptor should come second */
7703550dc98Sfvdl int sector = 16;
7713550dc98Sfvdl /* For now, a fixed 2 : PVD and terminator */
7723550dc98Sfvdl volume_descriptor *temp, *t;
7733550dc98Sfvdl
7743550dc98Sfvdl /* Set up the PVD */
775e4989541Schristos temp = emalloc(sizeof(*temp));
7763550dc98Sfvdl temp->volumeDescriptorData =
777e4989541Schristos (unsigned char *)&diskStructure->primaryDescriptor;
7783550dc98Sfvdl temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
7793550dc98Sfvdl temp->volumeDescriptorData[6] = 1;
7803550dc98Sfvdl temp->sector = sector;
7813550dc98Sfvdl memcpy(temp->volumeDescriptorData + 1,
7823550dc98Sfvdl ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
783e4989541Schristos diskStructure->firstVolumeDescriptor = temp;
7843550dc98Sfvdl
7853550dc98Sfvdl sector++;
7863550dc98Sfvdl /* Set up boot support if enabled. BVD must reside in sector 17 */
787e4989541Schristos if (diskStructure->is_bootable) {
788e4989541Schristos t = emalloc(sizeof(*t));
789e4989541Schristos t->volumeDescriptorData = ecalloc(1, 2048);
7903550dc98Sfvdl temp->next = t;
7913550dc98Sfvdl temp = t;
7923550dc98Sfvdl t->sector = 17;
793e4989541Schristos if (diskStructure->verbose_level > 0)
7943550dc98Sfvdl printf("Setting up boot volume descriptor\n");
795e4989541Schristos cd9660_setup_boot_volume_descriptor(diskStructure, t);
7963550dc98Sfvdl sector++;
7973550dc98Sfvdl }
7983550dc98Sfvdl
7993550dc98Sfvdl /* Set up the terminator */
800e4989541Schristos t = emalloc(sizeof(*t));
801e4989541Schristos t->volumeDescriptorData = ecalloc(1, 2048);
8023550dc98Sfvdl temp->next = t;
8033550dc98Sfvdl t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
8043550dc98Sfvdl t->next = 0;
8053550dc98Sfvdl t->volumeDescriptorData[6] = 1;
8063550dc98Sfvdl t->sector = sector;
8073550dc98Sfvdl memcpy(t->volumeDescriptorData + 1,
8083550dc98Sfvdl ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
8093550dc98Sfvdl
8103550dc98Sfvdl sector++;
8113550dc98Sfvdl return sector;
8123550dc98Sfvdl }
8133550dc98Sfvdl
8143550dc98Sfvdl #if 0
8153550dc98Sfvdl /*
8163550dc98Sfvdl * Populate EAR at some point. Not required, but is used by NetBSD's
8173550dc98Sfvdl * cd9660 support
8183550dc98Sfvdl */
8193550dc98Sfvdl static int
8203550dc98Sfvdl cd9660_fill_extended_attribute_record(cd9660node *node)
8213550dc98Sfvdl {
822e4989541Schristos node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes));
8233550dc98Sfvdl return 1;
8243550dc98Sfvdl }
8253550dc98Sfvdl #endif
8263550dc98Sfvdl
8273550dc98Sfvdl static int
cd9660_translate_node_common(iso9660_disk * diskStructure,cd9660node * newnode)828e4989541Schristos cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode)
8293550dc98Sfvdl {
8303550dc98Sfvdl u_char flag;
8313550dc98Sfvdl char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
8323550dc98Sfvdl
8333550dc98Sfvdl /* Now populate the isoDirRecord structure */
8343550dc98Sfvdl memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
8353550dc98Sfvdl
8360a77b69aSchristos (void)cd9660_convert_filename(diskStructure, newnode->node->name,
8373550dc98Sfvdl temp, !(S_ISDIR(newnode->node->type)));
8383550dc98Sfvdl
8393550dc98Sfvdl flag = ISO_FLAG_CLEAR;
8403550dc98Sfvdl if (S_ISDIR(newnode->node->type))
8413550dc98Sfvdl flag |= ISO_FLAG_DIRECTORY;
8423550dc98Sfvdl
8433550dc98Sfvdl cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
8443550dc98Sfvdl flag, strlen(temp), temp);
8453550dc98Sfvdl
8463550dc98Sfvdl cd9660_bothendian_dword(newnode->fileDataLength,
8473550dc98Sfvdl newnode->isoDirRecord->size);
8483550dc98Sfvdl /* If the file is a link, we want to set the size to 0 */
8493550dc98Sfvdl if (S_ISLNK(newnode->node->type))
8503550dc98Sfvdl newnode->fileDataLength = 0;
8513550dc98Sfvdl
8523550dc98Sfvdl return 1;
8533550dc98Sfvdl }
8543550dc98Sfvdl
8553550dc98Sfvdl /*
85641610f52Stsutsui * Translate fsnode to cd9660node
8573550dc98Sfvdl * Translate filenames and other metadata, including dates, sizes,
8583550dc98Sfvdl * permissions, etc
8593550dc98Sfvdl * @param struct fsnode * The node generated by makefs
8603550dc98Sfvdl * @param struct cd9660node * The intermediate node to be written to
8613550dc98Sfvdl * @returns int 0 on failure, 1 on success
8623550dc98Sfvdl */
8633550dc98Sfvdl static int
cd9660_translate_node(iso9660_disk * diskStructure,fsnode * node,cd9660node * newnode)864e4989541Schristos cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node,
865e4989541Schristos cd9660node *newnode)
8663550dc98Sfvdl {
8673550dc98Sfvdl if (node == NULL) {
868e4989541Schristos if (diskStructure->verbose_level > 0)
8698ee626c9Schristos printf("%s: NULL node passed, returning\n", __func__);
8703550dc98Sfvdl return 0;
8713550dc98Sfvdl }
872e4989541Schristos newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord));
8733550dc98Sfvdl /* Set the node pointer */
8743550dc98Sfvdl newnode->node = node;
8753550dc98Sfvdl
8763550dc98Sfvdl /* Set the size */
8773550dc98Sfvdl if (!(S_ISDIR(node->type)))
8783550dc98Sfvdl newnode->fileDataLength = node->inode->st.st_size;
8793550dc98Sfvdl
880e4989541Schristos if (cd9660_translate_node_common(diskStructure, newnode) == 0)
8813550dc98Sfvdl return 0;
8823550dc98Sfvdl
8833550dc98Sfvdl /* Finally, overwrite some of the values that are set by default */
884204cbe51Schristos cd9660_time_915(newnode->isoDirRecord->date,
885204cbe51Schristos stampst.st_ino ? stampst.st_mtime : node->inode->st.st_mtime);
8863550dc98Sfvdl
8873550dc98Sfvdl return 1;
8883550dc98Sfvdl }
8893550dc98Sfvdl
8903550dc98Sfvdl /*
8913550dc98Sfvdl * Compares two ISO filenames
8923550dc98Sfvdl * @param const char * The first file name
8933550dc98Sfvdl * @param const char * The second file name
8943550dc98Sfvdl * @returns : -1 if first is less than second, 0 if they are the same, 1 if
8953550dc98Sfvdl * the second is greater than the first
8963550dc98Sfvdl */
8973550dc98Sfvdl static int
cd9660_compare_filename(const char * first,const char * second)8983550dc98Sfvdl cd9660_compare_filename(const char *first, const char *second)
8993550dc98Sfvdl {
9003550dc98Sfvdl /*
9013550dc98Sfvdl * This can be made more optimal once it has been tested
9023550dc98Sfvdl * (the extra character, for example, is for testing)
9033550dc98Sfvdl */
9043550dc98Sfvdl
9053550dc98Sfvdl int p1 = 0;
9063550dc98Sfvdl int p2 = 0;
9073550dc98Sfvdl char c1, c2;
9083550dc98Sfvdl /* First, on the filename */
9093550dc98Sfvdl
9103550dc98Sfvdl while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
9113550dc98Sfvdl && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
9123550dc98Sfvdl c1 = first[p1];
9133550dc98Sfvdl c2 = second[p2];
9143550dc98Sfvdl if (c1 == '.' && c2 =='.')
9153550dc98Sfvdl break;
9163550dc98Sfvdl else if (c1 == '.') {
9173550dc98Sfvdl p2++;
9183550dc98Sfvdl c1 = ' ';
9193550dc98Sfvdl } else if (c2 == '.') {
9203550dc98Sfvdl p1++;
9213550dc98Sfvdl c2 = ' ';
9223550dc98Sfvdl } else {
9233550dc98Sfvdl p1++;
9243550dc98Sfvdl p2++;
9253550dc98Sfvdl }
9263550dc98Sfvdl
9273550dc98Sfvdl if (c1 < c2)
9283550dc98Sfvdl return -1;
9293550dc98Sfvdl else if (c1 > c2) {
9303550dc98Sfvdl return 1;
9313550dc98Sfvdl }
9323550dc98Sfvdl }
9333550dc98Sfvdl
9343550dc98Sfvdl if (first[p1] == '.' && second[p2] == '.') {
9353550dc98Sfvdl p1++;
9363550dc98Sfvdl p2++;
9373550dc98Sfvdl while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
9383550dc98Sfvdl && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
9393550dc98Sfvdl c1 = first[p1];
9403550dc98Sfvdl c2 = second[p2];
9413550dc98Sfvdl if (c1 == ';' && c2 == ';')
9423550dc98Sfvdl break;
9433550dc98Sfvdl else if (c1 == ';') {
9443550dc98Sfvdl p2++;
9453550dc98Sfvdl c1 = ' ';
9463550dc98Sfvdl } else if (c2 == ';') {
9473550dc98Sfvdl p1++;
9483550dc98Sfvdl c2 = ' ';
9493550dc98Sfvdl } else {
9503550dc98Sfvdl p1++;
9513550dc98Sfvdl p2++;
9523550dc98Sfvdl }
9533550dc98Sfvdl
9543550dc98Sfvdl if (c1 < c2)
9553550dc98Sfvdl return -1;
9563550dc98Sfvdl else if (c1 > c2)
9573550dc98Sfvdl return 1;
9583550dc98Sfvdl }
9593550dc98Sfvdl }
9603550dc98Sfvdl return 0;
9613550dc98Sfvdl }
9623550dc98Sfvdl
9633550dc98Sfvdl /*
9643550dc98Sfvdl * Insert a node into list with ISO sorting rules
9653550dc98Sfvdl * @param cd9660node * The head node of the list
9663550dc98Sfvdl * @param cd9660node * The node to be inserted
9673550dc98Sfvdl */
968689c61f6Sdyoung static void
cd9660_sorted_child_insert(cd9660node * parent,cd9660node * cn_new)969689c61f6Sdyoung cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
9703550dc98Sfvdl {
9713550dc98Sfvdl int compare;
972689c61f6Sdyoung cd9660node *cn;
973689c61f6Sdyoung struct cd9660_children_head *head = &parent->cn_children;
9743550dc98Sfvdl
9753550dc98Sfvdl /* TODO: Optimize? */
976689c61f6Sdyoung cn_new->parent = parent;
9773550dc98Sfvdl
9783550dc98Sfvdl /*
9793550dc98Sfvdl * first will either be 0, the . or the ..
9803550dc98Sfvdl * if . or .., this means no other entry may be written before first
9813550dc98Sfvdl * if 0, the new node may be inserted at the head
9823550dc98Sfvdl */
9833550dc98Sfvdl
984689c61f6Sdyoung TAILQ_FOREACH(cn, head, cn_next_child) {
9853550dc98Sfvdl /*
9863550dc98Sfvdl * Dont insert a node twice -
9873550dc98Sfvdl * that would cause an infinite loop
9883550dc98Sfvdl */
989535dee52Schristos if (cn_new == cn)
990535dee52Schristos return;
9913550dc98Sfvdl
992689c61f6Sdyoung compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
993689c61f6Sdyoung cn->isoDirRecord->name);
9943550dc98Sfvdl
9953550dc98Sfvdl if (compare == 0)
996689c61f6Sdyoung compare = cd9660_compare_filename(cn_new->node->name,
997689c61f6Sdyoung cn->node->name);
9983550dc98Sfvdl
999689c61f6Sdyoung if (compare < 0)
1000689c61f6Sdyoung break;
10013550dc98Sfvdl }
1002689c61f6Sdyoung if (cn == NULL)
1003689c61f6Sdyoung TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
1004689c61f6Sdyoung else
1005689c61f6Sdyoung TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
10063550dc98Sfvdl }
10073550dc98Sfvdl
10083550dc98Sfvdl /*
1009689c61f6Sdyoung * Called After cd9660_sorted_child_insert
1010427b92d9Sandvar * handles file collisions by suffixing each filename with ~n
10113550dc98Sfvdl * where n represents the files respective place in the ordering
10123550dc98Sfvdl */
10133550dc98Sfvdl static int
cd9660_handle_collisions(iso9660_disk * diskStructure,cd9660node * colliding,int past)1014e4989541Schristos cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding,
1015e4989541Schristos int past)
10163550dc98Sfvdl {
1017689c61f6Sdyoung cd9660node *iter, *next, *prev;
10183550dc98Sfvdl int skip;
10193550dc98Sfvdl int delete_chars = 0;
10203550dc98Sfvdl int temp_past = past;
10213550dc98Sfvdl int temp_skip;
10223550dc98Sfvdl int flag = 0;
10233550dc98Sfvdl cd9660node *end_of_range;
10243550dc98Sfvdl
1025689c61f6Sdyoung for (iter = TAILQ_FIRST(&colliding->cn_children);
1026689c61f6Sdyoung iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
10273550dc98Sfvdl if (strcmp(iter->isoDirRecord->name,
1028689c61f6Sdyoung next->isoDirRecord->name) != 0) {
1029689c61f6Sdyoung iter = TAILQ_NEXT(iter, cn_next_child);
1030689c61f6Sdyoung continue;
1031689c61f6Sdyoung }
10323550dc98Sfvdl flag = 1;
10333550dc98Sfvdl temp_skip = skip = cd9660_count_collisions(iter);
10343550dc98Sfvdl end_of_range = iter;
10353550dc98Sfvdl while (temp_skip > 0) {
10363550dc98Sfvdl temp_skip--;
1037689c61f6Sdyoung end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
10383550dc98Sfvdl }
10393550dc98Sfvdl temp_past = past;
10403550dc98Sfvdl while (temp_past > 0) {
1041689c61f6Sdyoung if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
1042689c61f6Sdyoung end_of_range = next;
1043689c61f6Sdyoung else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
1044689c61f6Sdyoung iter = prev;
10453550dc98Sfvdl else
10463550dc98Sfvdl delete_chars++;
10473550dc98Sfvdl temp_past--;
10483550dc98Sfvdl }
10493550dc98Sfvdl skip += past;
1050e4989541Schristos iter = cd9660_rename_filename(diskStructure, iter, skip,
1051e4989541Schristos delete_chars);
10523550dc98Sfvdl }
10533550dc98Sfvdl return flag;
10543550dc98Sfvdl }
10553550dc98Sfvdl
10563550dc98Sfvdl
10573550dc98Sfvdl static cd9660node *
cd9660_rename_filename(iso9660_disk * diskStructure,cd9660node * iter,int num,int delete_chars)1058e4989541Schristos cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num,
1059e4989541Schristos int delete_chars)
10603550dc98Sfvdl {
10613550dc98Sfvdl int i = 0;
10623550dc98Sfvdl int numbts, dot, semi, digit, digits, temp, powers, multiplier, count;
10633550dc98Sfvdl char *naming;
10643550dc98Sfvdl int maxlength;
10653550dc98Sfvdl char *tmp;
10663550dc98Sfvdl
1067e4989541Schristos if (diskStructure->verbose_level > 0)
10683550dc98Sfvdl printf("Rename_filename called\n");
10693550dc98Sfvdl
10703550dc98Sfvdl /* TODO : A LOT of chanes regarding 8.3 filenames */
1071e4989541Schristos if (diskStructure->isoLevel == 1)
10723550dc98Sfvdl maxlength = 8;
1073e4989541Schristos else if (diskStructure->isoLevel == 2)
10743550dc98Sfvdl maxlength = 31;
10753550dc98Sfvdl else
10763550dc98Sfvdl maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
10773550dc98Sfvdl
1078e4989541Schristos tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
10793550dc98Sfvdl
108052352066Schristos while (i < num && iter) {
10813550dc98Sfvdl powers = 1;
10823550dc98Sfvdl count = 0;
10833550dc98Sfvdl digits = 1;
10843550dc98Sfvdl multiplier = 1;
10853550dc98Sfvdl while (((int)(i / powers) ) >= 10) {
10863550dc98Sfvdl digits++;
10873550dc98Sfvdl powers = powers * 10;
10883550dc98Sfvdl }
10893550dc98Sfvdl
10903550dc98Sfvdl naming = iter->o_name;
10913550dc98Sfvdl
10923550dc98Sfvdl /*
10933550dc98Sfvdl while ((*naming != '.') && (*naming != ';')) {
10943550dc98Sfvdl naming++;
10953550dc98Sfvdl count++;
10963550dc98Sfvdl }
10973550dc98Sfvdl */
10983550dc98Sfvdl
10993550dc98Sfvdl dot = -1;
11003550dc98Sfvdl semi = -1;
11013550dc98Sfvdl while (count < maxlength) {
11023550dc98Sfvdl if (*naming == '.')
11033550dc98Sfvdl dot = count;
11043550dc98Sfvdl else if (*naming == ';') {
11053550dc98Sfvdl semi = count;
11063550dc98Sfvdl break;
11073550dc98Sfvdl }
11083550dc98Sfvdl naming++;
11093550dc98Sfvdl count++;
11103550dc98Sfvdl }
11113550dc98Sfvdl
11123550dc98Sfvdl if ((count + digits) < maxlength)
11133550dc98Sfvdl numbts = count;
11143550dc98Sfvdl else
11153550dc98Sfvdl numbts = maxlength - (digits);
11163550dc98Sfvdl numbts -= delete_chars;
11173550dc98Sfvdl
11183550dc98Sfvdl /* 8.3 rules - keep the extension, add before the dot */
11193550dc98Sfvdl
11203550dc98Sfvdl /*
11213550dc98Sfvdl * This code makes a bunch of assumptions.
11223550dc98Sfvdl * See if you can spot them all :)
11233550dc98Sfvdl */
11243550dc98Sfvdl
11250a77b69aSchristos #if 0
1126e4989541Schristos if (diskStructure->isoLevel == 1) {
11273550dc98Sfvdl numbts = 8 - digits - delete_chars;
11283550dc98Sfvdl if (dot < 0) {
11293550dc98Sfvdl
11303550dc98Sfvdl } else {
11313550dc98Sfvdl if (dot < 8) {
11323550dc98Sfvdl memmove(&tmp[numbts],&tmp[dot],4);
11333550dc98Sfvdl }
11343550dc98Sfvdl }
11353550dc98Sfvdl }
11363d4c34e6Sapb #else
11370a77b69aSchristos __USE(dot);
11380a77b69aSchristos __USE(semi);
11390a77b69aSchristos __USE(multiplier);
11400a77b69aSchristos #endif
11413550dc98Sfvdl
11423550dc98Sfvdl /* (copying just the filename before the '.' */
11433550dc98Sfvdl memcpy(tmp, (iter->o_name), numbts);
11443550dc98Sfvdl
11453550dc98Sfvdl /* adding the appropriate number following the name */
11463550dc98Sfvdl temp = i;
11473550dc98Sfvdl while (digits > 0) {
11483550dc98Sfvdl digit = (int)(temp / powers);
11493550dc98Sfvdl temp = temp - digit * powers;
11503550dc98Sfvdl sprintf(&tmp[numbts] , "%d", digit);
11513550dc98Sfvdl digits--;
11523550dc98Sfvdl numbts++;
11533550dc98Sfvdl powers = powers / 10;
11543550dc98Sfvdl }
11553550dc98Sfvdl
11563550dc98Sfvdl while ((*naming != ';') && (numbts < maxlength)) {
11573550dc98Sfvdl tmp[numbts] = (*naming);
11583550dc98Sfvdl naming++;
11593550dc98Sfvdl numbts++;
11603550dc98Sfvdl }
11613550dc98Sfvdl
11623550dc98Sfvdl tmp[numbts] = ';';
11633550dc98Sfvdl tmp[numbts+1] = '1';
116440ccd11cSahoka tmp[numbts+2] = '\0';
11653550dc98Sfvdl
11663550dc98Sfvdl /*
11673550dc98Sfvdl * now tmp has exactly the identifier
11683550dc98Sfvdl * we want so we'll copy it back to record
11693550dc98Sfvdl */
117040ccd11cSahoka memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
11713550dc98Sfvdl
1172689c61f6Sdyoung iter = TAILQ_NEXT(iter, cn_next_child);
11733550dc98Sfvdl i++;
11743550dc98Sfvdl }
11753550dc98Sfvdl
11763550dc98Sfvdl free(tmp);
11773550dc98Sfvdl return iter;
11783550dc98Sfvdl }
11793550dc98Sfvdl
11803550dc98Sfvdl /* Todo: Figure out why these functions are nec. */
11813550dc98Sfvdl static void
cd9660_copy_filenames(iso9660_disk * diskStructure,cd9660node * node)1182e4989541Schristos cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node)
11833550dc98Sfvdl {
1184689c61f6Sdyoung cd9660node *cn;
11853550dc98Sfvdl
1186689c61f6Sdyoung if (TAILQ_EMPTY(&node->cn_children))
11873550dc98Sfvdl return;
11883550dc98Sfvdl
1189689c61f6Sdyoung if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
1190e4989541Schristos debug_print_tree(diskStructure, diskStructure->rootNode, 0);
1191*ec5d1d82Stsutsui exit(EXIT_FAILURE);
11923550dc98Sfvdl }
11933550dc98Sfvdl
1194689c61f6Sdyoung TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1195e4989541Schristos cd9660_copy_filenames(diskStructure, cn);
1196689c61f6Sdyoung memcpy(cn->o_name, cn->isoDirRecord->name,
11973550dc98Sfvdl ISO_FILENAME_MAXLENGTH_WITH_PADDING);
11983550dc98Sfvdl }
11993550dc98Sfvdl }
12003550dc98Sfvdl
12013550dc98Sfvdl static void
cd9660_sorting_nodes(cd9660node * node)12023550dc98Sfvdl cd9660_sorting_nodes(cd9660node *node)
12033550dc98Sfvdl {
1204689c61f6Sdyoung cd9660node *cn;
12053550dc98Sfvdl
1206689c61f6Sdyoung TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
1207689c61f6Sdyoung cd9660_sorting_nodes(cn);
1208689c61f6Sdyoung cd9660_sort_nodes(node);
12093550dc98Sfvdl }
12103550dc98Sfvdl
1211689c61f6Sdyoung /* XXX Bubble sort. */
12123550dc98Sfvdl static void
cd9660_sort_nodes(cd9660node * node)1213689c61f6Sdyoung cd9660_sort_nodes(cd9660node *node)
12143550dc98Sfvdl {
1215689c61f6Sdyoung cd9660node *cn, *next;
12163550dc98Sfvdl
1217689c61f6Sdyoung do {
1218689c61f6Sdyoung TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1219689c61f6Sdyoung if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
12203550dc98Sfvdl return;
1221689c61f6Sdyoung else if (strcmp(next->isoDirRecord->name,
1222689c61f6Sdyoung cn->isoDirRecord->name) >= 0)
1223689c61f6Sdyoung continue;
1224689c61f6Sdyoung TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
12255138e749Sdyoung TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
1226689c61f6Sdyoung break;
12273550dc98Sfvdl }
1228689c61f6Sdyoung } while (cn != NULL);
12293550dc98Sfvdl }
12303550dc98Sfvdl
12313550dc98Sfvdl static int
cd9660_count_collisions(cd9660node * copy)12323550dc98Sfvdl cd9660_count_collisions(cd9660node *copy)
12333550dc98Sfvdl {
12343550dc98Sfvdl int count = 0;
1235689c61f6Sdyoung cd9660node *iter, *next;
12363550dc98Sfvdl
1237689c61f6Sdyoung for (iter = copy;
1238689c61f6Sdyoung (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
1239689c61f6Sdyoung iter = next) {
1240689c61f6Sdyoung if (cd9660_compare_filename(iter->isoDirRecord->name,
1241689c61f6Sdyoung next->isoDirRecord->name) == 0)
12423550dc98Sfvdl count++;
12433550dc98Sfvdl else
12443550dc98Sfvdl return count;
12453550dc98Sfvdl }
12463550dc98Sfvdl #if 0
1247689c61f6Sdyoung if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
12488ee626c9Schristos printf("%s: count is %i \n", __func__, count);
1249689c61f6Sdyoung compare = cd9660_compare_filename(iter->isoDirRecord->name,
1250689c61f6Sdyoung next->isoDirRecord->name);
12513550dc98Sfvdl if (compare == 0) {
12523550dc98Sfvdl count++;
1253689c61f6Sdyoung return cd9660_recurse_on_collision(next, count);
12543550dc98Sfvdl } else
12553550dc98Sfvdl return count;
12563550dc98Sfvdl }
12573550dc98Sfvdl #endif
12583550dc98Sfvdl return count;
12593550dc98Sfvdl }
12603550dc98Sfvdl
12613550dc98Sfvdl static cd9660node *
cd9660_rrip_move_directory(iso9660_disk * diskStructure,cd9660node * dir)1262e4989541Schristos cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir)
12633550dc98Sfvdl {
12643550dc98Sfvdl char newname[9];
12653550dc98Sfvdl cd9660node *tfile;
12663550dc98Sfvdl
12673550dc98Sfvdl /*
12683550dc98Sfvdl * This function needs to:
12693550dc98Sfvdl * 1) Create an empty virtual file in place of the old directory
12703550dc98Sfvdl * 2) Point the virtual file to the new directory
12713550dc98Sfvdl * 3) Point the relocated directory to its old parent
12723550dc98Sfvdl * 4) Move the directory specified by dir into rr_moved_dir,
1273e4989541Schristos * and rename it to "diskStructure->rock_ridge_move_count" (as a string)
12743550dc98Sfvdl */
12753550dc98Sfvdl
12763550dc98Sfvdl /* First see if the moved directory even exists */
1277e4989541Schristos if (diskStructure->rr_moved_dir == NULL) {
1278e4989541Schristos diskStructure->rr_moved_dir = cd9660_create_directory(
1279e4989541Schristos diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
1280e4989541Schristos diskStructure->rootNode, dir);
1281e4989541Schristos if (diskStructure->rr_moved_dir == NULL)
12823550dc98Sfvdl return 0;
1283799916c0Schristos cd9660_time_915(diskStructure->rr_moved_dir->isoDirRecord->date,
1284799916c0Schristos stampst.st_ino ? stampst.st_mtime : start_time.tv_sec);
12853550dc98Sfvdl }
12863550dc98Sfvdl
12873550dc98Sfvdl /* Create a file with the same ORIGINAL name */
1288e4989541Schristos tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent,
1289e4989541Schristos dir);
12903550dc98Sfvdl if (tfile == NULL)
12913550dc98Sfvdl return NULL;
12923550dc98Sfvdl
1293e4989541Schristos diskStructure->rock_ridge_move_count++;
1294551fa9b1Sreinoud snprintf(newname, sizeof(newname), "%08u",
1295e4989541Schristos diskStructure->rock_ridge_move_count);
12963550dc98Sfvdl
12973550dc98Sfvdl /* Point to old parent */
12983550dc98Sfvdl dir->rr_real_parent = dir->parent;
12993550dc98Sfvdl
13003550dc98Sfvdl /* Place the placeholder file */
1301689c61f6Sdyoung if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
1302689c61f6Sdyoung TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
1303689c61f6Sdyoung cn_next_child);
13043550dc98Sfvdl } else {
1305689c61f6Sdyoung cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
13063550dc98Sfvdl }
13073550dc98Sfvdl
13083550dc98Sfvdl /* Point to new parent */
1309e4989541Schristos dir->parent = diskStructure->rr_moved_dir;
13103550dc98Sfvdl
13113550dc98Sfvdl /* Point the file to the moved directory */
13123550dc98Sfvdl tfile->rr_relocated = dir;
13133550dc98Sfvdl
13143550dc98Sfvdl /* Actually move the directory */
1315e4989541Schristos cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir);
13163550dc98Sfvdl
13173550dc98Sfvdl /* TODO: Inherit permissions / ownership (basically the entire inode) */
13183550dc98Sfvdl
13193550dc98Sfvdl /* Set the new name */
13203550dc98Sfvdl memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
13213550dc98Sfvdl strncpy(dir->isoDirRecord->name, newname, 8);
132279fcae9eStsutsui dir->isoDirRecord->length[0] = 34 + 8;
132379fcae9eStsutsui dir->isoDirRecord->name_len[0] = 8;
13243550dc98Sfvdl
13253550dc98Sfvdl return dir;
13263550dc98Sfvdl }
13273550dc98Sfvdl
13283550dc98Sfvdl static int
cd9660_add_dot_records(iso9660_disk * diskStructure,cd9660node * root)1329e4989541Schristos cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root)
13303550dc98Sfvdl {
1331689c61f6Sdyoung struct cd9660_children_head *head = &root->cn_children;
1332689c61f6Sdyoung cd9660node *cn;
13333550dc98Sfvdl
1334689c61f6Sdyoung TAILQ_FOREACH(cn, head, cn_next_child) {
1335689c61f6Sdyoung if ((cn->type & CD9660_TYPE_DIR) == 0)
1336689c61f6Sdyoung continue;
13373550dc98Sfvdl /* Recursion first */
1338e4989541Schristos cd9660_add_dot_records(diskStructure, cn);
13393550dc98Sfvdl }
1340e4989541Schristos cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root);
1341e4989541Schristos cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT,
1342e4989541Schristos root);
13433550dc98Sfvdl return 1;
13443550dc98Sfvdl }
13453550dc98Sfvdl
13463550dc98Sfvdl /*
13473550dc98Sfvdl * Convert node to cd9660 structure
13483550dc98Sfvdl * This function is designed to be called recursively on the root node of
13493550dc98Sfvdl * the filesystem
13503550dc98Sfvdl * Lots of recursion going on here, want to make sure it is efficient
13513550dc98Sfvdl * @param struct fsnode * The root node to be converted
13523550dc98Sfvdl * @param struct cd9660* The parent node (should not be NULL)
13533550dc98Sfvdl * @param int Current directory depth
13543550dc98Sfvdl * @param int* Running count of the number of directories that are being created
13553550dc98Sfvdl */
1356689c61f6Sdyoung static void
cd9660_convert_structure(iso9660_disk * diskStructure,fsnode * root,cd9660node * parent_node,int level,int * numDirectories,int * error)1357e4989541Schristos cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root,
1358e4989541Schristos cd9660node *parent_node, int level, int *numDirectories, int *error)
13593550dc98Sfvdl {
13603550dc98Sfvdl fsnode *iterator = root;
1361689c61f6Sdyoung cd9660node *this_node;
13623550dc98Sfvdl int working_level;
13633550dc98Sfvdl int add;
13643550dc98Sfvdl int flag = 0;
13653550dc98Sfvdl int counter = 0;
13663550dc98Sfvdl
13673550dc98Sfvdl /*
13683550dc98Sfvdl * Newer, more efficient method, reduces recursion depth
13693550dc98Sfvdl */
13703550dc98Sfvdl if (root == NULL) {
13718ee626c9Schristos warnx("%s: root is null", __func__);
1372689c61f6Sdyoung return;
13733550dc98Sfvdl }
13743550dc98Sfvdl
13753550dc98Sfvdl /* Test for an empty directory - makefs still gives us the . record */
13763550dc98Sfvdl if ((S_ISDIR(root->type)) && (root->name[0] == '.')
13773550dc98Sfvdl && (root->name[1] == '\0')) {
13783550dc98Sfvdl root = root->next;
1379689c61f6Sdyoung if (root == NULL)
1380689c61f6Sdyoung return;
13813550dc98Sfvdl }
1382689c61f6Sdyoung if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
1383689c61f6Sdyoung CD9660_MEM_ALLOC_ERROR(__func__);
13843550dc98Sfvdl }
13853550dc98Sfvdl
13863550dc98Sfvdl /*
13873550dc98Sfvdl * To reduce the number of recursive calls, we will iterate over
13883550dc98Sfvdl * the next pointers to the right.
13893550dc98Sfvdl */
1390689c61f6Sdyoung while (iterator != NULL) {
13913550dc98Sfvdl add = 1;
13923550dc98Sfvdl /*
13933550dc98Sfvdl * Increment the directory count if this is a directory
13943550dc98Sfvdl * Ignore "." entries. We will generate them later
13953550dc98Sfvdl */
1396689c61f6Sdyoung if (!S_ISDIR(iterator->type) ||
1397689c61f6Sdyoung strcmp(iterator->name, ".") != 0) {
13983550dc98Sfvdl
13993550dc98Sfvdl /* Translate the node, including its filename */
1400689c61f6Sdyoung this_node->parent = parent_node;
1401e4989541Schristos cd9660_translate_node(diskStructure, iterator,
1402e4989541Schristos this_node);
1403689c61f6Sdyoung this_node->level = level;
14043550dc98Sfvdl
14053550dc98Sfvdl if (S_ISDIR(iterator->type)) {
1406689c61f6Sdyoung (*numDirectories)++;
1407689c61f6Sdyoung this_node->type = CD9660_TYPE_DIR;
14083550dc98Sfvdl working_level = level + 1;
14093550dc98Sfvdl
14103550dc98Sfvdl /*
14113550dc98Sfvdl * If at level 8, directory would be at 8
14123550dc98Sfvdl * and have children at 9 which is not
14133550dc98Sfvdl * allowed as per ISO spec
14143550dc98Sfvdl */
14153550dc98Sfvdl if (level == 8) {
1416e4989541Schristos if ((!diskStructure->allow_deep_trees) &&
1417e4989541Schristos (!diskStructure->rock_ridge_enabled)) {
14183550dc98Sfvdl warnx("error: found entry "
14193550dc98Sfvdl "with depth greater "
14203550dc98Sfvdl "than 8.");
14213550dc98Sfvdl (*error) = 1;
1422689c61f6Sdyoung return;
1423e4989541Schristos } else if (diskStructure->
14243550dc98Sfvdl rock_ridge_enabled) {
14253550dc98Sfvdl working_level = 3;
14263550dc98Sfvdl /*
14273550dc98Sfvdl * Moved directory is actually
14283550dc98Sfvdl * at level 2.
14293550dc98Sfvdl */
1430689c61f6Sdyoung this_node->level =
14313550dc98Sfvdl working_level - 1;
14323550dc98Sfvdl if (cd9660_rrip_move_directory(
1433e4989541Schristos diskStructure,
1434689c61f6Sdyoung this_node) == 0) {
14353550dc98Sfvdl warnx("Failure in "
14363550dc98Sfvdl "cd9660_rrip_"
14373550dc98Sfvdl "move_directory"
14383550dc98Sfvdl );
14393550dc98Sfvdl (*error) = 1;
1440689c61f6Sdyoung return;
14413550dc98Sfvdl }
14423550dc98Sfvdl add = 0;
14433550dc98Sfvdl }
14443550dc98Sfvdl }
14453550dc98Sfvdl
14463550dc98Sfvdl /* Do the recursive call on the children */
14473550dc98Sfvdl if (iterator->child != 0) {
1448e4989541Schristos cd9660_convert_structure(diskStructure,
1449689c61f6Sdyoung iterator->child, this_node,
14503550dc98Sfvdl working_level,
1451d0c4ff45Sdbj numDirectories, error);
14523550dc98Sfvdl
14533550dc98Sfvdl if ((*error) == 1) {
1454689c61f6Sdyoung warnx("%s: Error on recursive "
1455689c61f6Sdyoung "call", __func__);
1456689c61f6Sdyoung return;
14573550dc98Sfvdl }
14583550dc98Sfvdl }
14593550dc98Sfvdl
14603550dc98Sfvdl } else {
14613550dc98Sfvdl /* Only directories should have children */
14623550dc98Sfvdl assert(iterator->child == NULL);
14633550dc98Sfvdl
1464689c61f6Sdyoung this_node->type = CD9660_TYPE_FILE;
14653550dc98Sfvdl }
14663550dc98Sfvdl
14673550dc98Sfvdl /*
14683550dc98Sfvdl * Finally, do a sorted insert
14693550dc98Sfvdl */
14703550dc98Sfvdl if (add) {
1471689c61f6Sdyoung cd9660_sorted_child_insert(
1472689c61f6Sdyoung parent_node, this_node);
14733550dc98Sfvdl }
14743550dc98Sfvdl
14753550dc98Sfvdl /*Allocate new temp_node */
14763550dc98Sfvdl if (iterator->next != 0) {
1477689c61f6Sdyoung this_node = cd9660_allocate_cd9660node();
1478689c61f6Sdyoung if (this_node == NULL)
1479689c61f6Sdyoung CD9660_MEM_ALLOC_ERROR(__func__);
14803550dc98Sfvdl }
14813550dc98Sfvdl }
14823550dc98Sfvdl iterator = iterator->next;
14833550dc98Sfvdl }
14843550dc98Sfvdl
14853550dc98Sfvdl /* cd9660_handle_collisions(first_node); */
14863550dc98Sfvdl
14873550dc98Sfvdl /* TODO: need cleanup */
1488e4989541Schristos cd9660_copy_filenames(diskStructure, parent_node);
14893550dc98Sfvdl
14903550dc98Sfvdl do {
1491e4989541Schristos flag = cd9660_handle_collisions(diskStructure, parent_node,
1492e4989541Schristos counter);
14933550dc98Sfvdl counter++;
1494689c61f6Sdyoung cd9660_sorting_nodes(parent_node);
14953550dc98Sfvdl } while ((flag == 1) && (counter < 100));
14963550dc98Sfvdl }
14973550dc98Sfvdl
14983550dc98Sfvdl /*
14993550dc98Sfvdl * Clean up the cd9660node tree
15003550dc98Sfvdl * This is designed to be called recursively on the root node
15013550dc98Sfvdl * @param struct cd9660node *root The node to free
15023550dc98Sfvdl * @returns void
15033550dc98Sfvdl */
15043550dc98Sfvdl static void
cd9660_free_structure(cd9660node * root)15053550dc98Sfvdl cd9660_free_structure(cd9660node *root)
15063550dc98Sfvdl {
1507689c61f6Sdyoung cd9660node *cn;
15083550dc98Sfvdl
1509689c61f6Sdyoung while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
1510689c61f6Sdyoung TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
1511689c61f6Sdyoung cd9660_free_structure(cn);
15123550dc98Sfvdl }
1513689c61f6Sdyoung free(root);
15143550dc98Sfvdl }
15153550dc98Sfvdl
15163550dc98Sfvdl /*
15173550dc98Sfvdl * Be a little more memory conservative:
15183550dc98Sfvdl * instead of having the TAILQ_ENTRY as part of the cd9660node,
15193550dc98Sfvdl * just create a temporary structure
15203550dc98Sfvdl */
15213550dc98Sfvdl struct ptq_entry
15223550dc98Sfvdl {
15233550dc98Sfvdl TAILQ_ENTRY(ptq_entry) ptq;
15243550dc98Sfvdl cd9660node *node;
15253550dc98Sfvdl } *n;
15263550dc98Sfvdl
15273550dc98Sfvdl #define PTQUEUE_NEW(n,s,r,t){\
1528e4989541Schristos n = emalloc(sizeof(struct s)); \
15293550dc98Sfvdl if (n == NULL) \
15303550dc98Sfvdl return r; \
15313550dc98Sfvdl n->node = t;\
15323550dc98Sfvdl }
15333550dc98Sfvdl
15343550dc98Sfvdl /*
15353550dc98Sfvdl * Generate the path tables
15363550dc98Sfvdl * The specific implementation of this function is left as an exercise to the
15373550dc98Sfvdl * programmer. It could be done recursively. Make sure you read how the path
15383550dc98Sfvdl * table has to be laid out, it has levels.
15393550dc98Sfvdl * @param struct iso9660_disk *disk The disk image
15403550dc98Sfvdl * @returns int The number of built path tables (between 1 and 4), 0 on failure
15413550dc98Sfvdl */
15423550dc98Sfvdl static int
cd9660_generate_path_table(iso9660_disk * diskStructure)1543e4989541Schristos cd9660_generate_path_table(iso9660_disk *diskStructure)
15443550dc98Sfvdl {
1545e4989541Schristos cd9660node *cn, *dirNode = diskStructure->rootNode;
15463550dc98Sfvdl cd9660node *last = dirNode;
15473550dc98Sfvdl int pathTableSize = 0; /* computed as we go */
15483550dc98Sfvdl int counter = 1; /* root gets a count of 0 */
15493550dc98Sfvdl
15503550dc98Sfvdl TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
15513550dc98Sfvdl TAILQ_INIT(&pt_head);
15523550dc98Sfvdl
1553e4989541Schristos PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode);
15543550dc98Sfvdl
15553550dc98Sfvdl /* Push the root node */
15563550dc98Sfvdl TAILQ_INSERT_HEAD(&pt_head, n, ptq);
15573550dc98Sfvdl
15583550dc98Sfvdl /* Breadth-first traversal of file structure */
15593550dc98Sfvdl while (pt_head.tqh_first != 0) {
15603550dc98Sfvdl n = pt_head.tqh_first;
15613550dc98Sfvdl dirNode = n->node;
15623550dc98Sfvdl TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
15633550dc98Sfvdl free(n);
15643550dc98Sfvdl
15653550dc98Sfvdl /* Update the size */
15663550dc98Sfvdl pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
15673550dc98Sfvdl + dirNode->isoDirRecord->name_len[0]+
15683550dc98Sfvdl (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
15693550dc98Sfvdl /* includes the padding bit */
15703550dc98Sfvdl
15713550dc98Sfvdl dirNode->ptnumber=counter;
15723550dc98Sfvdl if (dirNode != last) {
15733550dc98Sfvdl last->ptnext = dirNode;
15743550dc98Sfvdl dirNode->ptprev = last;
15753550dc98Sfvdl }
15763550dc98Sfvdl last = dirNode;
15773550dc98Sfvdl
15783550dc98Sfvdl /* Push children onto queue */
1579689c61f6Sdyoung TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
15803550dc98Sfvdl /*
15813550dc98Sfvdl * Dont add the DOT and DOTDOT types to the path
15823550dc98Sfvdl * table.
15833550dc98Sfvdl */
1584689c61f6Sdyoung if ((cn->type != CD9660_TYPE_DOT)
1585689c61f6Sdyoung && (cn->type != CD9660_TYPE_DOTDOT)) {
15863550dc98Sfvdl
1587689c61f6Sdyoung if (S_ISDIR(cn->node->type)) {
1588689c61f6Sdyoung PTQUEUE_NEW(n, ptq_entry, -1, cn);
15893550dc98Sfvdl TAILQ_INSERT_TAIL(&pt_head, n, ptq);
15903550dc98Sfvdl }
15913550dc98Sfvdl }
15923550dc98Sfvdl }
15933550dc98Sfvdl counter++;
15943550dc98Sfvdl }
15953550dc98Sfvdl return pathTableSize;
15963550dc98Sfvdl }
15973550dc98Sfvdl
15983550dc98Sfvdl void
cd9660_compute_full_filename(cd9660node * node,char * buf)1599f1cc0951Schristos cd9660_compute_full_filename(cd9660node *node, char *buf)
16003550dc98Sfvdl {
1601f1cc0951Schristos int len;
16023550dc98Sfvdl
1603f1cc0951Schristos len = CD9660MAXPATH + 1;
1604f1cc0951Schristos len = snprintf(buf, len, "%s/%s/%s", node->node->root,
1605f1cc0951Schristos node->node->path, node->node->name);
1606f1cc0951Schristos if (len > CD9660MAXPATH)
16077408c4ccSchristos errx(EXIT_FAILURE, "Pathname too long.");
16083550dc98Sfvdl }
16093550dc98Sfvdl
16103550dc98Sfvdl /* NEW filename conversion method */
1611e4989541Schristos typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *,
1612e4989541Schristos char *, int);
16133550dc98Sfvdl
16143550dc98Sfvdl
16153550dc98Sfvdl /*
16163550dc98Sfvdl * TODO: These two functions are almost identical.
16173550dc98Sfvdl * Some code cleanup is possible here
16183b0eb509Sdyoung *
16193b0eb509Sdyoung * XXX bounds checking!
16203550dc98Sfvdl */
16213550dc98Sfvdl static int
cd9660_level1_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)1622e4989541Schristos cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1623e4989541Schristos char *newname, int is_file)
16243550dc98Sfvdl {
16253550dc98Sfvdl /*
16263550dc98Sfvdl * ISO 9660 : 10.1
16273550dc98Sfvdl * File Name shall not contain more than 8 d or d1 characters
16283550dc98Sfvdl * File Name Extension shall not contain more than 3 d or d1 characters
16293550dc98Sfvdl * Directory Identifier shall not contain more than 8 d or d1 characters
16303550dc98Sfvdl */
16313550dc98Sfvdl int namelen = 0;
16323550dc98Sfvdl int extlen = 0;
16333550dc98Sfvdl int found_ext = 0;
16343550dc98Sfvdl
163591cc0e56Schristos while (*oldname != '\0' && extlen < 3) {
16363550dc98Sfvdl /* Handle period first, as it is special */
163791cc0e56Schristos if (*oldname == '.') {
16383550dc98Sfvdl if (found_ext) {
16393b0eb509Sdyoung *newname++ = '_';
16403550dc98Sfvdl extlen ++;
16413550dc98Sfvdl }
16423550dc98Sfvdl else {
16433b0eb509Sdyoung *newname++ = '.';
16443550dc98Sfvdl found_ext = 1;
16453550dc98Sfvdl }
16463550dc98Sfvdl } else {
1647f8474b32Sbjh21 /* cut RISC OS file type off ISO name */
1648e4989541Schristos if (diskStructure->archimedes_enabled &&
1649f8474b32Sbjh21 *oldname == ',' && strlen(oldname) == 4)
1650f8474b32Sbjh21 break;
16513e74546bSchristos
16523550dc98Sfvdl /* Enforce 12.3 / 8 */
16531beaa2ceSchristos if (namelen == 8 && !found_ext)
16543550dc98Sfvdl break;
16553550dc98Sfvdl
16563550dc98Sfvdl if (islower((unsigned char)*oldname))
16573b0eb509Sdyoung *newname++ = toupper((unsigned char)*oldname);
16583550dc98Sfvdl else if (isupper((unsigned char)*oldname)
16593550dc98Sfvdl || isdigit((unsigned char)*oldname))
16603b0eb509Sdyoung *newname++ = *oldname;
16613550dc98Sfvdl else
16623b0eb509Sdyoung *newname++ = '_';
16633550dc98Sfvdl
16643550dc98Sfvdl if (found_ext)
16653550dc98Sfvdl extlen++;
16663550dc98Sfvdl else
16673550dc98Sfvdl namelen++;
16683550dc98Sfvdl }
16693550dc98Sfvdl oldname++;
16703550dc98Sfvdl }
1671689c61f6Sdyoung if (is_file) {
1672e4989541Schristos if (!found_ext && !diskStructure->omit_trailing_period)
167362e9fd65Sdyoung *newname++ = '.';
16743550dc98Sfvdl /* Add version */
1675689c61f6Sdyoung sprintf(newname, ";%i", 1);
1676689c61f6Sdyoung }
16773550dc98Sfvdl return namelen + extlen + found_ext;
16783550dc98Sfvdl }
16793550dc98Sfvdl
16803b0eb509Sdyoung /* XXX bounds checking! */
16813550dc98Sfvdl static int
cd9660_level2_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)1682e4989541Schristos cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1683e4989541Schristos char *newname, int is_file)
16843550dc98Sfvdl {
16853550dc98Sfvdl /*
16863550dc98Sfvdl * ISO 9660 : 7.5.1
16873550dc98Sfvdl * File name : 0+ d or d1 characters
16883550dc98Sfvdl * separator 1 (.)
16893550dc98Sfvdl * File name extension : 0+ d or d1 characters
16903550dc98Sfvdl * separator 2 (;)
16913550dc98Sfvdl * File version number (5 characters, 1-32767)
16923550dc98Sfvdl * 1 <= Sum of File name and File name extension <= 30
16933550dc98Sfvdl */
16943550dc98Sfvdl int namelen = 0;
16953550dc98Sfvdl int extlen = 0;
16963550dc98Sfvdl int found_ext = 0;
16973550dc98Sfvdl
16981beaa2ceSchristos while (*oldname != '\0' && namelen + extlen < 30) {
16993550dc98Sfvdl /* Handle period first, as it is special */
17003550dc98Sfvdl if (*oldname == '.') {
17013550dc98Sfvdl if (found_ext) {
1702e4989541Schristos if (diskStructure->allow_multidot) {
1703577910fbSad *newname++ = '.';
1704577910fbSad } else {
17053b0eb509Sdyoung *newname++ = '_';
1706577910fbSad }
17073550dc98Sfvdl extlen ++;
17083550dc98Sfvdl }
17093550dc98Sfvdl else {
17103b0eb509Sdyoung *newname++ = '.';
17113550dc98Sfvdl found_ext = 1;
17123550dc98Sfvdl }
17133550dc98Sfvdl } else {
1714f8474b32Sbjh21 /* cut RISC OS file type off ISO name */
1715e4989541Schristos if (diskStructure->archimedes_enabled &&
1716f8474b32Sbjh21 *oldname == ',' && strlen(oldname) == 4)
1717f8474b32Sbjh21 break;
17183550dc98Sfvdl
17193550dc98Sfvdl if (islower((unsigned char)*oldname))
17203b0eb509Sdyoung *newname++ = toupper((unsigned char)*oldname);
17213550dc98Sfvdl else if (isupper((unsigned char)*oldname) ||
17223550dc98Sfvdl isdigit((unsigned char)*oldname))
17233b0eb509Sdyoung *newname++ = *oldname;
1724e4989541Schristos else if (diskStructure->allow_multidot &&
1725577910fbSad *oldname == '.') {
1726577910fbSad *newname++ = '.';
1727577910fbSad } else {
17283b0eb509Sdyoung *newname++ = '_';
1729577910fbSad }
17303550dc98Sfvdl
17313550dc98Sfvdl if (found_ext)
17323550dc98Sfvdl extlen++;
17333550dc98Sfvdl else
17343550dc98Sfvdl namelen++;
17353550dc98Sfvdl }
17363550dc98Sfvdl oldname ++;
17373550dc98Sfvdl }
1738689c61f6Sdyoung if (is_file) {
1739e4989541Schristos if (!found_ext && !diskStructure->omit_trailing_period)
174062e9fd65Sdyoung *newname++ = '.';
17413550dc98Sfvdl /* Add version */
1742689c61f6Sdyoung sprintf(newname, ";%i", 1);
1743689c61f6Sdyoung }
17443550dc98Sfvdl return namelen + extlen + found_ext;
17453550dc98Sfvdl }
17463550dc98Sfvdl
17473550dc98Sfvdl #if 0
17483550dc98Sfvdl static int
1749e4989541Schristos cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1750e4989541Schristos char *newname, int is_file)
17513550dc98Sfvdl {
17523550dc98Sfvdl /* TODO: implement later, move to cd9660_joliet.c ?? */
17533550dc98Sfvdl }
17543550dc98Sfvdl #endif
17553550dc98Sfvdl
17563550dc98Sfvdl
17573550dc98Sfvdl /*
17583550dc98Sfvdl * Convert a file name to ISO compliant file name
17593550dc98Sfvdl * @param char * oldname The original filename
17603550dc98Sfvdl * @param char ** newname The new file name, in the appropriate character
17613550dc98Sfvdl * set and of appropriate length
17623550dc98Sfvdl * @param int 1 if file, 0 if directory
17633550dc98Sfvdl * @returns int The length of the new string
17643550dc98Sfvdl */
17653550dc98Sfvdl static int
cd9660_convert_filename(iso9660_disk * diskStructure,const char * oldname,char * newname,int is_file)1766e4989541Schristos cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname,
1767e4989541Schristos char *newname, int is_file)
17683550dc98Sfvdl {
17693550dc98Sfvdl /* NEW */
17703550dc98Sfvdl cd9660_filename_conversion_functor conversion_function = 0;
1771e4989541Schristos if (diskStructure->isoLevel == 1)
17723550dc98Sfvdl conversion_function = &cd9660_level1_convert_filename;
1773e4989541Schristos else if (diskStructure->isoLevel == 2)
17743550dc98Sfvdl conversion_function = &cd9660_level2_convert_filename;
1775e4989541Schristos return (*conversion_function)(diskStructure, oldname, newname, is_file);
17763550dc98Sfvdl }
17773550dc98Sfvdl
17783550dc98Sfvdl int
cd9660_compute_record_size(iso9660_disk * diskStructure,cd9660node * node)1779e4989541Schristos cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node)
17803550dc98Sfvdl {
17813550dc98Sfvdl int size = node->isoDirRecord->length[0];
17823550dc98Sfvdl
1783e4989541Schristos if (diskStructure->rock_ridge_enabled)
17843550dc98Sfvdl size += node->susp_entry_size;
1785f8474b32Sbjh21 size += node->su_tail_size;
1786f8474b32Sbjh21 size += size & 1; /* Ensure length of record is even. */
1787f8474b32Sbjh21 assert(size <= 254);
17883550dc98Sfvdl return size;
17893550dc98Sfvdl }
17903550dc98Sfvdl
17913550dc98Sfvdl static void
cd9660_populate_dot_records(iso9660_disk * diskStructure,cd9660node * node)1792e4989541Schristos cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node)
17933550dc98Sfvdl {
17943550dc98Sfvdl node->dot_record->fileDataSector = node->fileDataSector;
17953550dc98Sfvdl memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
17963550dc98Sfvdl node->dot_record->isoDirRecord->name_len[0] = 1;
17973550dc98Sfvdl node->dot_record->isoDirRecord->name[0] = 0;
17983550dc98Sfvdl node->dot_record->isoDirRecord->name[1] = 0;
17993550dc98Sfvdl node->dot_record->isoDirRecord->length[0] = 34;
18003550dc98Sfvdl node->dot_record->fileRecordSize =
1801e4989541Schristos cd9660_compute_record_size(diskStructure, node->dot_record);
18023550dc98Sfvdl
1803e4989541Schristos if (node == diskStructure->rootNode) {
18043550dc98Sfvdl node->dot_dot_record->fileDataSector = node->fileDataSector;
18053550dc98Sfvdl memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
18063550dc98Sfvdl 34);
18073550dc98Sfvdl } else {
18083550dc98Sfvdl node->dot_dot_record->fileDataSector =
18093550dc98Sfvdl node->parent->fileDataSector;
18103550dc98Sfvdl memcpy(node->dot_dot_record->isoDirRecord,
18113550dc98Sfvdl node->parent->isoDirRecord,34);
18123550dc98Sfvdl }
18133550dc98Sfvdl node->dot_dot_record->isoDirRecord->name_len[0] = 1;
18143550dc98Sfvdl node->dot_dot_record->isoDirRecord->name[0] = 1;
18153550dc98Sfvdl node->dot_dot_record->isoDirRecord->name[1] = 0;
18163550dc98Sfvdl node->dot_dot_record->isoDirRecord->length[0] = 34;
18173550dc98Sfvdl node->dot_dot_record->fileRecordSize =
1818e4989541Schristos cd9660_compute_record_size(diskStructure, node->dot_dot_record);
18193550dc98Sfvdl }
18203550dc98Sfvdl
18213550dc98Sfvdl /*
18223550dc98Sfvdl * @param struct cd9660node *node The node
18233550dc98Sfvdl * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
18243550dc98Sfvdl * @returns int The total size of files and directory entries (should be
18253550dc98Sfvdl * a multiple of sector size)
18263550dc98Sfvdl */
18272ec6e0f1Schristos static int64_t
cd9660_compute_offsets(iso9660_disk * diskStructure,cd9660node * node,int64_t startOffset)1828e4989541Schristos cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node,
1829e4989541Schristos int64_t startOffset)
18303550dc98Sfvdl {
18313550dc98Sfvdl /*
18323550dc98Sfvdl * This function needs to compute the size of directory records and
18333550dc98Sfvdl * runs, file lengths, and set the appropriate variables both in
18343550dc98Sfvdl * cd9660node and isoDirEntry
18353550dc98Sfvdl */
18362ec6e0f1Schristos int64_t used_bytes = 0;
18372ec6e0f1Schristos int64_t current_sector_usage = 0;
18383550dc98Sfvdl cd9660node *child;
18392fa0e02bSdyoung fsinode *inode;
18402ec6e0f1Schristos int64_t r;
18413550dc98Sfvdl
18423550dc98Sfvdl assert(node != NULL);
18433550dc98Sfvdl
18443550dc98Sfvdl
18453550dc98Sfvdl /*
18463550dc98Sfvdl * NOTE : There needs to be some special case detection for
18473550dc98Sfvdl * the "real root" node, since for it, node->node is undefined
18483550dc98Sfvdl */
18493550dc98Sfvdl
18503550dc98Sfvdl node->fileDataSector = -1;
18513550dc98Sfvdl
18523550dc98Sfvdl if (node->type & CD9660_TYPE_DIR) {
1853e4989541Schristos node->fileRecordSize = cd9660_compute_record_size(
1854e4989541Schristos diskStructure, node);
18553550dc98Sfvdl /*Set what sector this directory starts in*/
18563550dc98Sfvdl node->fileDataSector =
1857e4989541Schristos CD9660_BLOCKS(diskStructure->sectorSize,startOffset);
18583550dc98Sfvdl
18593550dc98Sfvdl cd9660_bothendian_dword(node->fileDataSector,
18603550dc98Sfvdl node->isoDirRecord->extent);
18613550dc98Sfvdl
18623550dc98Sfvdl /*
18633550dc98Sfvdl * First loop over children, need to know the size of
18643550dc98Sfvdl * their directory records
18653550dc98Sfvdl */
18663550dc98Sfvdl node->fileSectorsUsed = 1;
1867689c61f6Sdyoung TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
18683550dc98Sfvdl node->fileDataLength +=
1869e4989541Schristos cd9660_compute_record_size(diskStructure, child);
1870e4989541Schristos if ((cd9660_compute_record_size(diskStructure, child) +
18713550dc98Sfvdl current_sector_usage) >=
1872e4989541Schristos diskStructure->sectorSize) {
18733550dc98Sfvdl current_sector_usage = 0;
18743550dc98Sfvdl node->fileSectorsUsed++;
18753550dc98Sfvdl }
18763550dc98Sfvdl
18773550dc98Sfvdl current_sector_usage +=
1878e4989541Schristos cd9660_compute_record_size(diskStructure, child);
18793550dc98Sfvdl }
18803550dc98Sfvdl
18813550dc98Sfvdl cd9660_bothendian_dword(node->fileSectorsUsed *
1882e4989541Schristos diskStructure->sectorSize,node->isoDirRecord->size);
18833550dc98Sfvdl
18843550dc98Sfvdl /*
18853550dc98Sfvdl * This should point to the sector after the directory
18863550dc98Sfvdl * record (or, the first byte in that sector)
18873550dc98Sfvdl */
1888e4989541Schristos used_bytes += node->fileSectorsUsed * diskStructure->sectorSize;
18893550dc98Sfvdl
1890689c61f6Sdyoung for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1891689c61f6Sdyoung child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
18923550dc98Sfvdl /* Directories need recursive call */
18933550dc98Sfvdl if (S_ISDIR(child->node->type)) {
1894e4989541Schristos r = cd9660_compute_offsets(diskStructure, child,
18953550dc98Sfvdl used_bytes + startOffset);
18963550dc98Sfvdl
18973550dc98Sfvdl if (r != -1)
18983550dc98Sfvdl used_bytes += r;
18993550dc98Sfvdl else
19003550dc98Sfvdl return -1;
19013550dc98Sfvdl }
19023550dc98Sfvdl }
19033550dc98Sfvdl
19043550dc98Sfvdl /* Explicitly set the . and .. records */
1905e4989541Schristos cd9660_populate_dot_records(diskStructure, node);
19063550dc98Sfvdl
19073550dc98Sfvdl /* Finally, do another iteration to write the file data*/
1908689c61f6Sdyoung for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1909689c61f6Sdyoung child != NULL;
1910689c61f6Sdyoung child = TAILQ_NEXT(child, cn_next_child)) {
19113550dc98Sfvdl /* Files need extent set */
1912689c61f6Sdyoung if (S_ISDIR(child->node->type))
1913689c61f6Sdyoung continue;
19143550dc98Sfvdl child->fileRecordSize =
1915e4989541Schristos cd9660_compute_record_size(diskStructure, child);
19163550dc98Sfvdl
19173550dc98Sfvdl child->fileSectorsUsed =
1918e4989541Schristos CD9660_BLOCKS(diskStructure->sectorSize,
19193550dc98Sfvdl child->fileDataLength);
19203550dc98Sfvdl
19212fa0e02bSdyoung inode = child->node->inode;
19222fa0e02bSdyoung if ((inode->flags & FI_ALLOCATED) == 0) {
19232fa0e02bSdyoung inode->ino =
1924e4989541Schristos CD9660_BLOCKS(diskStructure->sectorSize,
19253550dc98Sfvdl used_bytes + startOffset);
19262fa0e02bSdyoung inode->flags |= FI_ALLOCATED;
19273550dc98Sfvdl used_bytes += child->fileSectorsUsed *
1928e4989541Schristos diskStructure->sectorSize;
19292fa0e02bSdyoung } else {
19302fa0e02bSdyoung INODE_WARNX(("%s: already allocated inode %d "
19312fa0e02bSdyoung "data sectors at %" PRIu32, __func__,
19322fa0e02bSdyoung (int)inode->st.st_ino, inode->ino));
19333550dc98Sfvdl }
19342fa0e02bSdyoung child->fileDataSector = inode->ino;
19351da54664Sdyoung cd9660_bothendian_dword(child->fileDataSector,
19361da54664Sdyoung child->isoDirRecord->extent);
19371da54664Sdyoung }
19383550dc98Sfvdl }
19393550dc98Sfvdl
19403550dc98Sfvdl return used_bytes;
19413550dc98Sfvdl }
19423550dc98Sfvdl
19433550dc98Sfvdl #if 0
19443550dc98Sfvdl /* Might get rid of this func */
19453550dc98Sfvdl static int
19463550dc98Sfvdl cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
19473550dc98Sfvdl {
19483550dc98Sfvdl to->node->inode->st.st_dev = 0;
19493550dc98Sfvdl to->node->inode->st.st_ino = 0;
19503550dc98Sfvdl to->node->inode->st.st_size = 0;
19513550dc98Sfvdl to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
19523550dc98Sfvdl to->node->inode->st.st_atime = from->node->inode->st.st_atime;
19533550dc98Sfvdl to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
19543550dc98Sfvdl to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
19553550dc98Sfvdl to->node->inode->st.st_uid = from->node->inode->st.st_uid;
19563550dc98Sfvdl to->node->inode->st.st_gid = from->node->inode->st.st_gid;
19573550dc98Sfvdl to->node->inode->st.st_mode = from->node->inode->st.st_mode;
19583550dc98Sfvdl /* Clear out type */
19593550dc98Sfvdl to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
19603550dc98Sfvdl if (file)
19613550dc98Sfvdl to->node->inode->st.st_mode |= S_IFREG;
19623550dc98Sfvdl else
19633550dc98Sfvdl to->node->inode->st.st_mode |= S_IFDIR;
19643550dc98Sfvdl return 1;
19653550dc98Sfvdl }
19663550dc98Sfvdl #endif
19673550dc98Sfvdl
19683550dc98Sfvdl static cd9660node *
cd9660_create_virtual_entry(iso9660_disk * diskStructure,const char * name,cd9660node * parent,int file,int insert)1969e4989541Schristos cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name,
1970e4989541Schristos cd9660node *parent, int file, int insert)
19713550dc98Sfvdl {
19723550dc98Sfvdl cd9660node *temp;
19733550dc98Sfvdl fsnode * tfsnode;
19743550dc98Sfvdl
19753550dc98Sfvdl assert(parent != NULL);
19763550dc98Sfvdl
19773550dc98Sfvdl temp = cd9660_allocate_cd9660node();
19783550dc98Sfvdl if (temp == NULL)
19793550dc98Sfvdl return NULL;
19803550dc98Sfvdl
1981e4989541Schristos tfsnode = emalloc(sizeof(*tfsnode));
1982e4989541Schristos tfsnode->name = estrdup(name);
1983e4989541Schristos temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord));
19843550dc98Sfvdl
1985e4989541Schristos cd9660_convert_filename(diskStructure, tfsnode->name,
1986e4989541Schristos temp->isoDirRecord->name, file);
19873550dc98Sfvdl
19883550dc98Sfvdl temp->node = tfsnode;
19893550dc98Sfvdl temp->parent = parent;
19903550dc98Sfvdl
19913550dc98Sfvdl if (insert) {
19923550dc98Sfvdl if (temp->parent != NULL) {
19933550dc98Sfvdl temp->level = temp->parent->level + 1;
1994689c61f6Sdyoung if (!TAILQ_EMPTY(&temp->parent->cn_children))
1995689c61f6Sdyoung cd9660_sorted_child_insert(temp->parent, temp);
19963550dc98Sfvdl else
1997689c61f6Sdyoung TAILQ_INSERT_HEAD(&temp->parent->cn_children,
1998689c61f6Sdyoung temp, cn_next_child);
19993550dc98Sfvdl }
20003550dc98Sfvdl }
20013550dc98Sfvdl
20023550dc98Sfvdl if (parent->node != NULL) {
20033550dc98Sfvdl tfsnode->type = parent->node->type;
20043550dc98Sfvdl }
20053550dc98Sfvdl
20063550dc98Sfvdl /* Clear out file type bits */
20073550dc98Sfvdl tfsnode->type &= ~(S_IFMT);
20083550dc98Sfvdl if (file)
20093550dc98Sfvdl tfsnode->type |= S_IFREG;
20103550dc98Sfvdl else
20113550dc98Sfvdl tfsnode->type |= S_IFDIR;
20123550dc98Sfvdl
20133550dc98Sfvdl /* Indicate that there is no spec entry (inode) */
20143550dc98Sfvdl tfsnode->flags &= ~(FSNODE_F_HASSPEC);
20153550dc98Sfvdl #if 0
20163550dc98Sfvdl cd9660_copy_stat_info(parent, temp, file);
20173550dc98Sfvdl #endif
20183550dc98Sfvdl return temp;
20193550dc98Sfvdl }
20203550dc98Sfvdl
20213550dc98Sfvdl static cd9660node *
cd9660_create_file(iso9660_disk * diskStructure,const char * name,cd9660node * parent,cd9660node * me)2022e4989541Schristos cd9660_create_file(iso9660_disk *diskStructure, const char *name,
2023e4989541Schristos cd9660node *parent, cd9660node *me)
20243550dc98Sfvdl {
20253550dc98Sfvdl cd9660node *temp;
20263550dc98Sfvdl
2027e4989541Schristos temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1);
20283550dc98Sfvdl if (temp == NULL)
20293550dc98Sfvdl return NULL;
20303550dc98Sfvdl
20313550dc98Sfvdl temp->fileDataLength = 0;
20323550dc98Sfvdl
20333550dc98Sfvdl temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
20343550dc98Sfvdl
2035e4989541Schristos temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
2036535dee52Schristos *temp->node->inode = *me->node->inode;
2037535dee52Schristos
2038e4989541Schristos if (cd9660_translate_node_common(diskStructure, temp) == 0)
20393550dc98Sfvdl return NULL;
20403550dc98Sfvdl return temp;
20413550dc98Sfvdl }
20423550dc98Sfvdl
20433550dc98Sfvdl /*
20443550dc98Sfvdl * Create a new directory which does not exist on disk
20453550dc98Sfvdl * @param const char * name The name to assign to the directory
20463550dc98Sfvdl * @param const char * parent Pointer to the parent directory
20473550dc98Sfvdl * @returns cd9660node * Pointer to the new directory
20483550dc98Sfvdl */
20493550dc98Sfvdl static cd9660node *
cd9660_create_directory(iso9660_disk * diskStructure,const char * name,cd9660node * parent,cd9660node * me)2050e4989541Schristos cd9660_create_directory(iso9660_disk *diskStructure, const char *name,
2051e4989541Schristos cd9660node *parent, cd9660node *me)
20523550dc98Sfvdl {
20533550dc98Sfvdl cd9660node *temp;
20543550dc98Sfvdl
2055e4989541Schristos temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1);
20563550dc98Sfvdl if (temp == NULL)
20573550dc98Sfvdl return NULL;
20583550dc98Sfvdl temp->node->type |= S_IFDIR;
20593550dc98Sfvdl
20603550dc98Sfvdl temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
20613550dc98Sfvdl
2062e4989541Schristos temp->node->inode = ecalloc(1, sizeof(*temp->node->inode));
2063535dee52Schristos *temp->node->inode = *me->node->inode;
2064535dee52Schristos
2065e4989541Schristos if (cd9660_translate_node_common(diskStructure, temp) == 0)
20663550dc98Sfvdl return NULL;
20673550dc98Sfvdl return temp;
20683550dc98Sfvdl }
20693550dc98Sfvdl
20703550dc98Sfvdl static cd9660node *
cd9660_create_special_directory(iso9660_disk * diskStructure,u_char type,cd9660node * parent)2071e4989541Schristos cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type,
2072e4989541Schristos cd9660node *parent)
20733550dc98Sfvdl {
2074689c61f6Sdyoung cd9660node *temp, *first;
20753550dc98Sfvdl char na[2];
20763550dc98Sfvdl
20773550dc98Sfvdl assert(parent != NULL);
20783550dc98Sfvdl
20793550dc98Sfvdl if (type == CD9660_TYPE_DOT)
20803550dc98Sfvdl na[0] = 0;
20813550dc98Sfvdl else if (type == CD9660_TYPE_DOTDOT)
20823550dc98Sfvdl na[0] = 1;
20833550dc98Sfvdl else
20843550dc98Sfvdl return 0;
20853550dc98Sfvdl
20863550dc98Sfvdl na[1] = 0;
2087e4989541Schristos if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent,
2088e4989541Schristos 0, 0)) == NULL)
20893550dc98Sfvdl return NULL;
20903550dc98Sfvdl
20913550dc98Sfvdl temp->parent = parent;
20923550dc98Sfvdl temp->type = type;
20933550dc98Sfvdl temp->isoDirRecord->length[0] = 34;
20943550dc98Sfvdl /* Dot record is always first */
20953550dc98Sfvdl if (type == CD9660_TYPE_DOT) {
20963550dc98Sfvdl parent->dot_record = temp;
2097689c61f6Sdyoung TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
20983550dc98Sfvdl /* DotDot should be second */
20993550dc98Sfvdl } else if (type == CD9660_TYPE_DOTDOT) {
21003550dc98Sfvdl parent->dot_dot_record = temp;
21013550dc98Sfvdl /*
2102689c61f6Sdyoung * If the first child is the dot record, insert
2103689c61f6Sdyoung * this second. Otherwise, insert it at the head.
21043550dc98Sfvdl */
2105689c61f6Sdyoung if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
2106689c61f6Sdyoung (first->type & CD9660_TYPE_DOT) == 0) {
2107689c61f6Sdyoung TAILQ_INSERT_HEAD(&parent->cn_children, temp,
2108689c61f6Sdyoung cn_next_child);
21093550dc98Sfvdl } else {
2110689c61f6Sdyoung TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
2111689c61f6Sdyoung cn_next_child);
21123550dc98Sfvdl }
21133550dc98Sfvdl }
21143550dc98Sfvdl
21153550dc98Sfvdl return temp;
21163550dc98Sfvdl }
21173550dc98Sfvdl
2118e4989541Schristos static int
cd9660_add_generic_bootimage(iso9660_disk * diskStructure,const char * bootimage)2119e4989541Schristos cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage)
2120989d6357Sskrll {
2121989d6357Sskrll struct stat stbuf;
2122989d6357Sskrll
2123989d6357Sskrll assert(bootimage != NULL);
2124989d6357Sskrll
2125989d6357Sskrll if (*bootimage == '\0') {
2126989d6357Sskrll warnx("Error: Boot image must be a filename");
2127989d6357Sskrll return 0;
2128989d6357Sskrll }
2129989d6357Sskrll
2130e4989541Schristos diskStructure->generic_bootimage = estrdup(bootimage);
2131989d6357Sskrll
2132989d6357Sskrll /* Get information about the file */
2133e4989541Schristos if (lstat(diskStructure->generic_bootimage, &stbuf) == -1)
2134989d6357Sskrll err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
2135e4989541Schristos diskStructure->generic_bootimage);
2136989d6357Sskrll
2137989d6357Sskrll if (stbuf.st_size > 32768) {
2138989d6357Sskrll warnx("Error: Boot image must be no greater than 32768 bytes");
2139989d6357Sskrll return 0;
2140989d6357Sskrll }
2141989d6357Sskrll
2142e4989541Schristos if (diskStructure->verbose_level > 0) {
2143989d6357Sskrll printf("Generic boot image image has size %lld\n",
2144989d6357Sskrll (long long)stbuf.st_size);
2145989d6357Sskrll }
2146989d6357Sskrll
2147e4989541Schristos diskStructure->has_generic_bootimage = 1;
2148989d6357Sskrll
2149989d6357Sskrll return 1;
2150989d6357Sskrll }
2151