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