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