cd9660.c revision 1.23 1 /* $NetBSD: cd9660.c,v 1.23 2008/11/21 10:02:12 ad 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.23 2008/11/21 10:02:12 ad 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 rv = set_option(cd9660_options, var, val);
426
427 if (var)
428 free(var);
429 return (rv);
430 }
431
432 /*
433 * Main function for cd9660_makefs
434 * Builds the ISO image file
435 * @param const char *image The image filename to create
436 * @param const char *dir The directory that is being read
437 * @param struct fsnode *root The root node of the filesystem tree
438 * @param struct fsinfo_t *fsopts Any options
439 */
440 void
441 cd9660_makefs(const char *image, const char *dir, fsnode *root,
442 fsinfo_t *fsopts)
443 {
444 int startoffset;
445 int numDirectories;
446 int pathTableSectors;
447 int firstAvailableSector;
448 int totalSpace;
449 int error;
450 cd9660node *real_root;
451
452 if (diskStructure.verbose_level > 0)
453 printf("cd9660_makefs: ISO level is %i\n",
454 diskStructure.isoLevel);
455 if (diskStructure.isoLevel < 2 &&
456 diskStructure.allow_multidot)
457 errx(1, "allow-multidot requires iso level of 2\n");
458
459 assert(image != NULL);
460 assert(dir != NULL);
461 assert(root != NULL);
462 assert(fsopts != NULL);
463
464 if (diskStructure.displayHelp) {
465 /*
466 * Display help here - probably want to put it in
467 * a separate function
468 */
469 return;
470 }
471
472 diskStructure.rootFilesystemPath = dir;
473
474 if (diskStructure.verbose_level > 0)
475 printf("cd9660_makefs: image %s directory %s root %p\n",
476 image, dir, root);
477
478 /* Set up some constants. Later, these will be defined with options */
479
480 /* Counter needed for path tables */
481 numDirectories = 0;
482
483 /* Convert tree to our own format */
484 /* Actually, we now need to add the REAL root node, at level 0 */
485
486 real_root = cd9660_allocate_cd9660node();
487 if ((real_root->isoDirRecord =
488 malloc( sizeof(iso_directory_record_cd9660) )) == NULL) {
489 CD9660_MEM_ALLOC_ERROR("cd9660_makefs");
490 exit(1);
491 }
492
493 /* Leave filename blank for root */
494 memset(real_root->isoDirRecord->name, 0,
495 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
496
497 real_root->level = 0;
498 diskStructure.rootNode = real_root;
499 real_root->type = CD9660_TYPE_DIR;
500 error = 0;
501 real_root->node = root;
502 cd9660_convert_structure(root, real_root, 1, &numDirectories, &error);
503
504 if (TAILQ_EMPTY(&real_root->cn_children)) {
505 errx(1, "cd9660_makefs: converted directory is empty. "
506 "Tree conversion failed\n");
507 } else if (error != 0) {
508 errx(1, "cd9660_makefs: tree conversion failed\n");
509 } else {
510 if (diskStructure.verbose_level > 0)
511 printf("cd9660_makefs: tree converted\n");
512 }
513
514 /* Add the dot and dot dot records */
515 cd9660_add_dot_records(real_root);
516
517 cd9660_setup_root_node();
518
519 if (diskStructure.verbose_level > 0)
520 printf("cd9660_makefs: done converting tree\n");
521
522 /* Rock ridge / SUSP init pass */
523 if (diskStructure.rock_ridge_enabled) {
524 cd9660_susp_initialize(diskStructure.rootNode,
525 diskStructure.rootNode, NULL);
526 }
527
528 /* Build path table structure */
529 diskStructure.pathTableLength = cd9660_generate_path_table();
530
531 pathTableSectors = CD9660_BLOCKS(diskStructure.sectorSize,
532 diskStructure.pathTableLength);
533
534 firstAvailableSector = cd9660_setup_volume_descriptors();
535 if (diskStructure.is_bootable) {
536 firstAvailableSector = cd9660_setup_boot(firstAvailableSector);
537 if (firstAvailableSector < 0)
538 errx(1, "setup_boot failed");
539 }
540 /* LE first, then BE */
541 diskStructure.primaryLittleEndianTableSector = firstAvailableSector;
542 diskStructure.primaryBigEndianTableSector =
543 diskStructure.primaryLittleEndianTableSector + pathTableSectors;
544
545 /* Set the secondary ones to -1, not going to use them for now */
546 diskStructure.secondaryBigEndianTableSector = -1;
547 diskStructure.secondaryLittleEndianTableSector = -1;
548
549 diskStructure.dataFirstSector =
550 diskStructure.primaryBigEndianTableSector + pathTableSectors;
551 if (diskStructure.verbose_level > 0)
552 printf("cd9660_makefs: Path table conversion complete. "
553 "Each table is %i bytes, or %i sectors.\n",
554 diskStructure.pathTableLength, pathTableSectors);
555
556 startoffset = diskStructure.sectorSize*diskStructure.dataFirstSector;
557
558 totalSpace = cd9660_compute_offsets(real_root, startoffset);
559
560 diskStructure.totalSectors = diskStructure.dataFirstSector +
561 CD9660_BLOCKS(diskStructure.sectorSize, totalSpace);
562
563 /* Disabled until pass 1 is done */
564 if (diskStructure.rock_ridge_enabled) {
565 diskStructure.susp_continuation_area_start_sector =
566 diskStructure.totalSectors;
567 diskStructure.totalSectors +=
568 CD9660_BLOCKS(diskStructure.sectorSize,
569 diskStructure.susp_continuation_area_size);
570 cd9660_susp_finalize(diskStructure.rootNode);
571 }
572
573
574 cd9660_finalize_PVD();
575
576 /* Add padding sectors, just for testing purposes right now */
577 /* diskStructure.totalSectors+=150; */
578
579 /* Debugging output */
580 if (diskStructure.verbose_level > 0) {
581 printf("cd9660_makefs: Sectors 0-15 reserved\n");
582 printf("cd9660_makefs: Primary path tables starts in sector %i\n",
583 diskStructure.primaryLittleEndianTableSector);
584 printf("cd9660_makefs: File data starts in sector %i\n",
585 diskStructure.dataFirstSector);
586 printf("cd9660_makefs: Total sectors: %i\n",diskStructure.totalSectors);
587 }
588
589 /*
590 * Add padding sectors at the end
591 * TODO: Clean this up and separate padding
592 */
593 if (diskStructure.include_padding_areas)
594 diskStructure.totalSectors += 150;
595
596 cd9660_write_image(image);
597
598 if (diskStructure.verbose_level > 1) {
599 debug_print_volume_descriptor_information();
600 debug_print_tree(real_root,0);
601 debug_print_path_tree(real_root);
602 }
603
604 /* Clean up data structures */
605 cd9660_free_structure(real_root);
606
607 if (diskStructure.verbose_level > 0)
608 printf("cd9660_makefs: done\n");
609 }
610
611 /* Generic function pointer - implement later */
612 typedef int (*cd9660node_func)(cd9660node *);
613
614 static void
615 cd9660_finalize_PVD(void)
616 {
617 time_t tim;
618 unsigned char *temp;
619
620 /* Copy the root directory record */
621 temp = (unsigned char *) &diskStructure.primaryDescriptor;
622
623 /* root should be a fixed size of 34 bytes since it has no name */
624 memcpy(diskStructure.primaryDescriptor.root_directory_record,
625 diskStructure.rootNode->dot_record->isoDirRecord, 34);
626
627 /* In RRIP, this might be longer than 34 */
628 diskStructure.primaryDescriptor.root_directory_record[0] = 34;
629
630 /* Set up all the important numbers in the PVD */
631 cd9660_bothendian_dword(diskStructure.totalSectors,
632 (unsigned char *)diskStructure.primaryDescriptor.volume_space_size);
633 cd9660_bothendian_word(1,
634 (unsigned char *)diskStructure.primaryDescriptor.volume_set_size);
635 cd9660_bothendian_word(1,
636 (unsigned char *)
637 diskStructure.primaryDescriptor.volume_sequence_number);
638 cd9660_bothendian_word(diskStructure.sectorSize,
639 (unsigned char *)
640 diskStructure.primaryDescriptor.logical_block_size);
641 cd9660_bothendian_dword(diskStructure.pathTableLength,
642 (unsigned char *)diskStructure.primaryDescriptor.path_table_size);
643
644 cd9660_731(diskStructure.primaryLittleEndianTableSector,
645 (u_char *)diskStructure.primaryDescriptor.type_l_path_table);
646 cd9660_732(diskStructure.primaryBigEndianTableSector,
647 (u_char *)diskStructure.primaryDescriptor.type_m_path_table);
648
649 diskStructure.primaryDescriptor.file_structure_version[0] = 1;
650
651 /* Pad all strings with spaces instead of nulls */
652 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_id, 32);
653 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.system_id, 32);
654 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.volume_set_id,
655 128);
656 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.publisher_id,
657 128);
658 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.preparer_id,
659 128);
660 cd9660_pad_string_spaces(diskStructure.primaryDescriptor.application_id,
661 128);
662 cd9660_pad_string_spaces(
663 diskStructure.primaryDescriptor.copyright_file_id, 128);
664 cd9660_pad_string_spaces(
665 diskStructure.primaryDescriptor.abstract_file_id, 128);
666 cd9660_pad_string_spaces(
667 diskStructure.primaryDescriptor.bibliographic_file_id, 128);
668
669 /* Setup dates */
670 time(&tim);
671 cd9660_time_8426(
672 (unsigned char *)diskStructure.primaryDescriptor.creation_date,
673 tim);
674 cd9660_time_8426(
675 (unsigned char *)diskStructure.primaryDescriptor.modification_date,
676 tim);
677
678 /*
679 cd9660_set_date(diskStructure.primaryDescriptor.expiration_date, now);
680 */
681
682 memset(diskStructure.primaryDescriptor.expiration_date, '0' ,17);
683 cd9660_time_8426(
684 (unsigned char *)diskStructure.primaryDescriptor.effective_date,
685 tim);
686 }
687
688 static void
689 cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record,
690 u_char ext_attr_length, u_char flags,
691 u_char name_len, const char * name)
692 {
693 record->ext_attr_length[0] = ext_attr_length;
694 record->flags[0] = ISO_FLAG_CLEAR | flags;
695 record->file_unit_size[0] = 0;
696 record->interleave[0] = 0;
697 cd9660_bothendian_word(1, record->volume_sequence_number);
698 record->name_len[0] = name_len;
699 memset(record->name, '\0', sizeof (record->name));
700 memcpy(record->name, name, name_len);
701 record->length[0] = 33 + name_len;
702
703 /* Todo : better rounding */
704 record->length[0] += (record->length[0] & 1) ? 1 : 0;
705 }
706
707 static void
708 cd9660_setup_root_node(void)
709 {
710 cd9660_populate_iso_dir_record(diskStructure.rootNode->isoDirRecord,
711 0, ISO_FLAG_DIRECTORY, 1, "\0");
712
713 }
714
715 /*********** SUPPORT FUNCTIONS ***********/
716 static int
717 cd9660_setup_volume_descriptors(void)
718 {
719 /* Boot volume descriptor should come second */
720 int sector = 16;
721 /* For now, a fixed 2 : PVD and terminator */
722 volume_descriptor *temp, *t;
723
724 /* Set up the PVD */
725 if ((temp = malloc(sizeof(volume_descriptor))) == NULL) {
726 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
727 exit(1);
728 }
729
730 temp->volumeDescriptorData =
731 (unsigned char *)&diskStructure.primaryDescriptor;
732 temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD;
733 temp->volumeDescriptorData[6] = 1;
734 temp->sector = sector;
735 memcpy(temp->volumeDescriptorData + 1,
736 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
737 diskStructure.firstVolumeDescriptor = temp;
738
739 sector++;
740 /* Set up boot support if enabled. BVD must reside in sector 17 */
741 if (diskStructure.is_bootable) {
742 if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
743 CD9660_MEM_ALLOC_ERROR(
744 "cd9660_setup_volume_descriptors");
745 exit(1);
746 }
747 if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
748 CD9660_MEM_ALLOC_ERROR(
749 "cd9660_setup_volume_descriptors");
750 exit(1);
751 }
752 temp->next = t;
753 temp = t;
754 memset(t->volumeDescriptorData, 0, 2048);
755 t->sector = 17;
756 if (diskStructure.verbose_level > 0)
757 printf("Setting up boot volume descriptor\n");
758 cd9660_setup_boot_volume_descriptor(t);
759 sector++;
760 }
761
762 /* Set up the terminator */
763 if ((t = malloc(sizeof(volume_descriptor))) == NULL) {
764 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
765 exit(1);
766 }
767 if ((t->volumeDescriptorData = malloc(2048)) == NULL) {
768 CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors");
769 exit(1);
770 }
771
772 temp->next = t;
773 memset(t->volumeDescriptorData, 0, 2048);
774 t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR;
775 t->next = 0;
776 t->volumeDescriptorData[6] = 1;
777 t->sector = sector;
778 memcpy(t->volumeDescriptorData + 1,
779 ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
780
781 sector++;
782 return sector;
783 }
784
785 #if 0
786 /*
787 * Populate EAR at some point. Not required, but is used by NetBSD's
788 * cd9660 support
789 */
790 static int
791 cd9660_fill_extended_attribute_record(cd9660node *node)
792 {
793 if ((node->isoExtAttributes =
794 malloc(sizeof(struct iso_extended_attributes))) == NULL) {
795 CD9660_MEM_ALLOC_ERROR("cd9660_fill_extended_attribute_record");
796 exit(1);
797 };
798
799 return 1;
800 }
801 #endif
802
803 static int
804 cd9960_translate_node_common(cd9660node *newnode)
805 {
806 time_t tim;
807 int test;
808 u_char flag;
809 char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
810
811 /* Now populate the isoDirRecord structure */
812 memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
813
814 test = cd9660_convert_filename(newnode->node->name,
815 temp, !(S_ISDIR(newnode->node->type)));
816
817 flag = ISO_FLAG_CLEAR;
818 if (S_ISDIR(newnode->node->type))
819 flag |= ISO_FLAG_DIRECTORY;
820
821 cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0,
822 flag, strlen(temp), temp);
823
824 /* Set the various dates */
825
826 /* If we want to use the current date and time */
827 time(&tim);
828
829 cd9660_time_915(newnode->isoDirRecord->date, tim);
830
831 cd9660_bothendian_dword(newnode->fileDataLength,
832 newnode->isoDirRecord->size);
833 /* If the file is a link, we want to set the size to 0 */
834 if (S_ISLNK(newnode->node->type))
835 newnode->fileDataLength = 0;
836
837 return 1;
838 }
839
840 /*
841 * Translate fsnode to cd9960node
842 * Translate filenames and other metadata, including dates, sizes,
843 * permissions, etc
844 * @param struct fsnode * The node generated by makefs
845 * @param struct cd9660node * The intermediate node to be written to
846 * @returns int 0 on failure, 1 on success
847 */
848 static int
849 cd9660_translate_node(fsnode *node, cd9660node *newnode)
850 {
851 if (node == NULL) {
852 if (diskStructure.verbose_level > 0)
853 printf("cd9660_translate_node: NULL node passed, "
854 "returning\n");
855 return 0;
856 }
857 if ((newnode->isoDirRecord =
858 malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
859 CD9660_MEM_ALLOC_ERROR("cd9660_translate_node");
860 return 0;
861 }
862
863 /* Set the node pointer */
864 newnode->node = node;
865
866 /* Set the size */
867 if (!(S_ISDIR(node->type)))
868 newnode->fileDataLength = node->inode->st.st_size;
869
870 if (cd9960_translate_node_common(newnode) == 0)
871 return 0;
872
873 /* Finally, overwrite some of the values that are set by default */
874 cd9660_time_915(newnode->isoDirRecord->date, node->inode->st.st_mtime);
875
876 return 1;
877 }
878
879 /*
880 * Compares two ISO filenames
881 * @param const char * The first file name
882 * @param const char * The second file name
883 * @returns : -1 if first is less than second, 0 if they are the same, 1 if
884 * the second is greater than the first
885 */
886 static int
887 cd9660_compare_filename(const char *first, const char *second)
888 {
889 /*
890 * This can be made more optimal once it has been tested
891 * (the extra character, for example, is for testing)
892 */
893
894 int p1 = 0;
895 int p2 = 0;
896 char c1, c2;
897 /* First, on the filename */
898
899 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1
900 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) {
901 c1 = first[p1];
902 c2 = second[p2];
903 if (c1 == '.' && c2 =='.')
904 break;
905 else if (c1 == '.') {
906 p2++;
907 c1 = ' ';
908 } else if (c2 == '.') {
909 p1++;
910 c2 = ' ';
911 } else {
912 p1++;
913 p2++;
914 }
915
916 if (c1 < c2)
917 return -1;
918 else if (c1 > c2) {
919 return 1;
920 }
921 }
922
923 if (first[p1] == '.' && second[p2] == '.') {
924 p1++;
925 p2++;
926 while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1
927 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) {
928 c1 = first[p1];
929 c2 = second[p2];
930 if (c1 == ';' && c2 == ';')
931 break;
932 else if (c1 == ';') {
933 p2++;
934 c1 = ' ';
935 } else if (c2 == ';') {
936 p1++;
937 c2 = ' ';
938 } else {
939 p1++;
940 p2++;
941 }
942
943 if (c1 < c2)
944 return -1;
945 else if (c1 > c2)
946 return 1;
947 }
948 }
949 return 0;
950 }
951
952 /*
953 * Insert a node into list with ISO sorting rules
954 * @param cd9660node * The head node of the list
955 * @param cd9660node * The node to be inserted
956 */
957 static void
958 cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new)
959 {
960 int compare;
961 cd9660node *cn;
962 struct cd9660_children_head *head = &parent->cn_children;
963
964 /* TODO: Optimize? */
965 cn_new->parent = parent;
966
967 /*
968 * first will either be 0, the . or the ..
969 * if . or .., this means no other entry may be written before first
970 * if 0, the new node may be inserted at the head
971 */
972
973 TAILQ_FOREACH(cn, head, cn_next_child) {
974 /*
975 * Dont insert a node twice -
976 * that would cause an infinite loop
977 */
978 if (cn_new == cn)
979 return;
980
981 compare = cd9660_compare_filename(cn_new->isoDirRecord->name,
982 cn->isoDirRecord->name);
983
984 if (compare == 0)
985 compare = cd9660_compare_filename(cn_new->node->name,
986 cn->node->name);
987
988 if (compare < 0)
989 break;
990 }
991 if (cn == NULL)
992 TAILQ_INSERT_TAIL(head, cn_new, cn_next_child);
993 else
994 TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child);
995 }
996
997 /*
998 * Called After cd9660_sorted_child_insert
999 * handles file collisions by suffixing each filname with ~n
1000 * where n represents the files respective place in the ordering
1001 */
1002 static int
1003 cd9660_handle_collisions(cd9660node *colliding, int past)
1004 {
1005 cd9660node *iter, *next, *prev;
1006 int skip;
1007 int delete_chars = 0;
1008 int temp_past = past;
1009 int temp_skip;
1010 int flag = 0;
1011 cd9660node *end_of_range;
1012
1013 for (iter = TAILQ_FIRST(&colliding->cn_children);
1014 iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) {
1015 if (strcmp(iter->isoDirRecord->name,
1016 next->isoDirRecord->name) != 0) {
1017 iter = TAILQ_NEXT(iter, cn_next_child);
1018 continue;
1019 }
1020 flag = 1;
1021 temp_skip = skip = cd9660_count_collisions(iter);
1022 end_of_range = iter;
1023 while (temp_skip > 0) {
1024 temp_skip--;
1025 end_of_range = TAILQ_NEXT(end_of_range, cn_next_child);
1026 }
1027 temp_past = past;
1028 while (temp_past > 0) {
1029 if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL)
1030 end_of_range = next;
1031 else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL)
1032 iter = prev;
1033 else
1034 delete_chars++;
1035 temp_past--;
1036 }
1037 skip += past;
1038 iter = cd9660_rename_filename(iter, skip, delete_chars);
1039 }
1040 return flag;
1041 }
1042
1043
1044 static cd9660node *
1045 cd9660_rename_filename(cd9660node *iter, int num, int delete_chars)
1046 {
1047 int i = 0;
1048 int numbts, dot, semi, digit, digits, temp, powers, multiplier, count;
1049 char *naming;
1050 int maxlength;
1051 char *tmp;
1052
1053 if (diskStructure.verbose_level > 0)
1054 printf("Rename_filename called\n");
1055
1056 /* TODO : A LOT of chanes regarding 8.3 filenames */
1057 if (diskStructure.isoLevel == 1)
1058 maxlength = 8;
1059 else if (diskStructure.isoLevel == 2)
1060 maxlength = 31;
1061 else
1062 maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION;
1063
1064 tmp = malloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1065
1066 while (i < num) {
1067 powers = 1;
1068 count = 0;
1069 digits = 1;
1070 multiplier = 1;
1071 while (((int)(i / powers) ) >= 10) {
1072 digits++;
1073 powers = powers * 10;
1074 }
1075
1076 naming = iter->o_name;
1077
1078 /*
1079 while ((*naming != '.') && (*naming != ';')) {
1080 naming++;
1081 count++;
1082 }
1083 */
1084
1085 dot = -1;
1086 semi = -1;
1087 while (count < maxlength) {
1088 if (*naming == '.')
1089 dot = count;
1090 else if (*naming == ';') {
1091 semi = count;
1092 break;
1093 }
1094 naming++;
1095 count++;
1096 }
1097
1098 if ((count + digits) < maxlength)
1099 numbts = count;
1100 else
1101 numbts = maxlength - (digits);
1102 numbts -= delete_chars;
1103
1104 /* 8.3 rules - keep the extension, add before the dot */
1105
1106 /*
1107 * This code makes a bunch of assumptions.
1108 * See if you can spot them all :)
1109 */
1110
1111 /*
1112 if (diskStructure.isoLevel == 1) {
1113 numbts = 8 - digits - delete_chars;
1114 if (dot < 0) {
1115
1116 } else {
1117 if (dot < 8) {
1118 memmove(&tmp[numbts],&tmp[dot],4);
1119 }
1120 }
1121 }
1122 */
1123
1124 /* (copying just the filename before the '.' */
1125 memcpy(tmp, (iter->o_name), numbts);
1126
1127 /* adding the appropriate number following the name */
1128 temp = i;
1129 while (digits > 0) {
1130 digit = (int)(temp / powers);
1131 temp = temp - digit * powers;
1132 sprintf(&tmp[numbts] , "%d", digit);
1133 digits--;
1134 numbts++;
1135 powers = powers / 10;
1136 }
1137
1138 while ((*naming != ';') && (numbts < maxlength)) {
1139 tmp[numbts] = (*naming);
1140 naming++;
1141 numbts++;
1142 }
1143
1144 tmp[numbts] = ';';
1145 tmp[numbts+1] = '1';
1146 tmp[numbts+2] = '\0';
1147
1148 /*
1149 * now tmp has exactly the identifier
1150 * we want so we'll copy it back to record
1151 */
1152 memcpy((iter->isoDirRecord->name), tmp, numbts + 3);
1153
1154 iter = TAILQ_NEXT(iter, cn_next_child);
1155 i++;
1156 }
1157
1158 free(tmp);
1159 return iter;
1160 }
1161
1162 /* Todo: Figure out why these functions are nec. */
1163 static void
1164 cd9660_copy_filenames(cd9660node *node)
1165 {
1166 cd9660node *cn;
1167
1168 if (TAILQ_EMPTY(&node->cn_children))
1169 return;
1170
1171 if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) {
1172 debug_print_tree(diskStructure.rootNode, 0);
1173 exit(1);
1174 }
1175
1176 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1177 cd9660_copy_filenames(cn);
1178 memcpy(cn->o_name, cn->isoDirRecord->name,
1179 ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1180 }
1181 }
1182
1183 static void
1184 cd9660_sorting_nodes(cd9660node *node)
1185 {
1186 cd9660node *cn;
1187
1188 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
1189 cd9660_sorting_nodes(cn);
1190 cd9660_sort_nodes(node);
1191 }
1192
1193 /* XXX Bubble sort. */
1194 static void
1195 cd9660_sort_nodes(cd9660node *node)
1196 {
1197 cd9660node *cn, *next;
1198
1199 do {
1200 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
1201 if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL)
1202 return;
1203 else if (strcmp(next->isoDirRecord->name,
1204 cn->isoDirRecord->name) >= 0)
1205 continue;
1206 TAILQ_REMOVE(&node->cn_children, next, cn_next_child);
1207 TAILQ_INSERT_BEFORE(cn, next, cn_next_child);
1208 break;
1209 }
1210 } while (cn != NULL);
1211 }
1212
1213 static int
1214 cd9660_count_collisions(cd9660node *copy)
1215 {
1216 int count = 0;
1217 cd9660node *iter, *next;
1218
1219 for (iter = copy;
1220 (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;
1221 iter = next) {
1222 if (cd9660_compare_filename(iter->isoDirRecord->name,
1223 next->isoDirRecord->name) == 0)
1224 count++;
1225 else
1226 return count;
1227 }
1228 #if 0
1229 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) {
1230 printf("cd9660_recurse_on_collision: count is %i \n", count);
1231 compare = cd9660_compare_filename(iter->isoDirRecord->name,
1232 next->isoDirRecord->name);
1233 if (compare == 0) {
1234 count++;
1235 return cd9660_recurse_on_collision(next, count);
1236 } else
1237 return count;
1238 }
1239 #endif
1240 return count;
1241 }
1242
1243 static cd9660node *
1244 cd9660_rrip_move_directory(cd9660node *dir)
1245 {
1246 char newname[9];
1247 cd9660node *tfile;
1248
1249 /*
1250 * This function needs to:
1251 * 1) Create an empty virtual file in place of the old directory
1252 * 2) Point the virtual file to the new directory
1253 * 3) Point the relocated directory to its old parent
1254 * 4) Move the directory specified by dir into rr_moved_dir,
1255 * and rename it to "diskStructure.rock_ridge_move_count" (as a string)
1256 */
1257
1258 /* First see if the moved directory even exists */
1259 if (diskStructure.rr_moved_dir == NULL) {
1260 diskStructure.rr_moved_dir =
1261 cd9660_create_directory(ISO_RRIP_DEFAULT_MOVE_DIR_NAME,
1262 diskStructure.rootNode, dir);
1263 if (diskStructure.rr_moved_dir == NULL)
1264 return 0;
1265 }
1266
1267 /* Create a file with the same ORIGINAL name */
1268 tfile = cd9660_create_file(dir->node->name, dir->parent, dir);
1269 if (tfile == NULL)
1270 return NULL;
1271
1272 diskStructure.rock_ridge_move_count++;
1273 snprintf(newname, sizeof(newname), "%08i",
1274 diskStructure.rock_ridge_move_count);
1275
1276 /* Point to old parent */
1277 dir->rr_real_parent = dir->parent;
1278
1279 /* Place the placeholder file */
1280 if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) {
1281 TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile,
1282 cn_next_child);
1283 } else {
1284 cd9660_sorted_child_insert(dir->rr_real_parent, tfile);
1285 }
1286
1287 /* Point to new parent */
1288 dir->parent = diskStructure.rr_moved_dir;
1289
1290 /* Point the file to the moved directory */
1291 tfile->rr_relocated = dir;
1292
1293 /* Actually move the directory */
1294 cd9660_sorted_child_insert(diskStructure.rr_moved_dir, dir);
1295
1296 /* TODO: Inherit permissions / ownership (basically the entire inode) */
1297
1298 /* Set the new name */
1299 memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING);
1300 strncpy(dir->isoDirRecord->name, newname, 8);
1301
1302 return dir;
1303 }
1304
1305 static int
1306 cd9660_add_dot_records(cd9660node *root)
1307 {
1308 struct cd9660_children_head *head = &root->cn_children;
1309 cd9660node *cn;
1310
1311 TAILQ_FOREACH(cn, head, cn_next_child) {
1312 if ((cn->type & CD9660_TYPE_DIR) == 0)
1313 continue;
1314 /* Recursion first */
1315 cd9660_add_dot_records(cn);
1316 }
1317 cd9660_create_special_directory(CD9660_TYPE_DOT, root);
1318 cd9660_create_special_directory(CD9660_TYPE_DOTDOT, root);
1319 return 1;
1320 }
1321
1322 /*
1323 * Convert node to cd9660 structure
1324 * This function is designed to be called recursively on the root node of
1325 * the filesystem
1326 * Lots of recursion going on here, want to make sure it is efficient
1327 * @param struct fsnode * The root node to be converted
1328 * @param struct cd9660* The parent node (should not be NULL)
1329 * @param int Current directory depth
1330 * @param int* Running count of the number of directories that are being created
1331 */
1332 static void
1333 cd9660_convert_structure(fsnode *root, cd9660node *parent_node, int level,
1334 int *numDirectories, int *error)
1335 {
1336 fsnode *iterator = root;
1337 cd9660node *this_node;
1338 int working_level;
1339 int add;
1340 int flag = 0;
1341 int counter = 0;
1342
1343 /*
1344 * Newer, more efficient method, reduces recursion depth
1345 */
1346 if (root == NULL) {
1347 warnx("%s: root is null\n", __func__);
1348 return;
1349 }
1350
1351 /* Test for an empty directory - makefs still gives us the . record */
1352 if ((S_ISDIR(root->type)) && (root->name[0] == '.')
1353 && (root->name[1] == '\0')) {
1354 root = root->next;
1355 if (root == NULL)
1356 return;
1357 }
1358 if ((this_node = cd9660_allocate_cd9660node()) == NULL) {
1359 CD9660_MEM_ALLOC_ERROR(__func__);
1360 }
1361
1362 /*
1363 * To reduce the number of recursive calls, we will iterate over
1364 * the next pointers to the right.
1365 */
1366 while (iterator != NULL) {
1367 add = 1;
1368 /*
1369 * Increment the directory count if this is a directory
1370 * Ignore "." entries. We will generate them later
1371 */
1372 if (!S_ISDIR(iterator->type) ||
1373 strcmp(iterator->name, ".") != 0) {
1374
1375 /* Translate the node, including its filename */
1376 this_node->parent = parent_node;
1377 cd9660_translate_node(iterator, this_node);
1378 this_node->level = level;
1379
1380 if (S_ISDIR(iterator->type)) {
1381 (*numDirectories)++;
1382 this_node->type = CD9660_TYPE_DIR;
1383 working_level = level + 1;
1384
1385 /*
1386 * If at level 8, directory would be at 8
1387 * and have children at 9 which is not
1388 * allowed as per ISO spec
1389 */
1390 if (level == 8) {
1391 if ((!diskStructure.allow_deep_trees) &&
1392 (!diskStructure.rock_ridge_enabled)) {
1393 warnx("error: found entry "
1394 "with depth greater "
1395 "than 8.");
1396 (*error) = 1;
1397 return;
1398 } else if (diskStructure.
1399 rock_ridge_enabled) {
1400 working_level = 3;
1401 /*
1402 * Moved directory is actually
1403 * at level 2.
1404 */
1405 this_node->level =
1406 working_level - 1;
1407 if (cd9660_rrip_move_directory(
1408 this_node) == 0) {
1409 warnx("Failure in "
1410 "cd9660_rrip_"
1411 "move_directory"
1412 );
1413 (*error) = 1;
1414 return;
1415 }
1416 add = 0;
1417 }
1418 }
1419
1420 /* Do the recursive call on the children */
1421 if (iterator->child != 0) {
1422 cd9660_convert_structure(
1423 iterator->child, this_node,
1424 working_level,
1425 numDirectories, error);
1426
1427 if ((*error) == 1) {
1428 warnx("%s: Error on recursive "
1429 "call", __func__);
1430 return;
1431 }
1432 }
1433
1434 } else {
1435 /* Only directories should have children */
1436 assert(iterator->child == NULL);
1437
1438 this_node->type = CD9660_TYPE_FILE;
1439 }
1440
1441 /*
1442 * Finally, do a sorted insert
1443 */
1444 if (add) {
1445 cd9660_sorted_child_insert(
1446 parent_node, this_node);
1447 }
1448
1449 /*Allocate new temp_node */
1450 if (iterator->next != 0) {
1451 this_node = cd9660_allocate_cd9660node();
1452 if (this_node == NULL)
1453 CD9660_MEM_ALLOC_ERROR(__func__);
1454 }
1455 }
1456 iterator = iterator->next;
1457 }
1458
1459 /* cd9660_handle_collisions(first_node); */
1460
1461 /* TODO: need cleanup */
1462 cd9660_copy_filenames(parent_node);
1463
1464 do {
1465 flag = cd9660_handle_collisions(parent_node, counter);
1466 counter++;
1467 cd9660_sorting_nodes(parent_node);
1468 } while ((flag == 1) && (counter < 100));
1469 }
1470
1471 /*
1472 * Clean up the cd9660node tree
1473 * This is designed to be called recursively on the root node
1474 * @param struct cd9660node *root The node to free
1475 * @returns void
1476 */
1477 static void
1478 cd9660_free_structure(cd9660node *root)
1479 {
1480 cd9660node *cn;
1481
1482 while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) {
1483 TAILQ_REMOVE(&root->cn_children, cn, cn_next_child);
1484 cd9660_free_structure(cn);
1485 }
1486 free(root);
1487 }
1488
1489 /*
1490 * Be a little more memory conservative:
1491 * instead of having the TAILQ_ENTRY as part of the cd9660node,
1492 * just create a temporary structure
1493 */
1494 struct ptq_entry
1495 {
1496 TAILQ_ENTRY(ptq_entry) ptq;
1497 cd9660node *node;
1498 } *n;
1499
1500 #define PTQUEUE_NEW(n,s,r,t){\
1501 n = malloc(sizeof(struct s)); \
1502 if (n == NULL) \
1503 return r; \
1504 n->node = t;\
1505 }
1506
1507 /*
1508 * Generate the path tables
1509 * The specific implementation of this function is left as an exercise to the
1510 * programmer. It could be done recursively. Make sure you read how the path
1511 * table has to be laid out, it has levels.
1512 * @param struct iso9660_disk *disk The disk image
1513 * @returns int The number of built path tables (between 1 and 4), 0 on failure
1514 */
1515 static int
1516 cd9660_generate_path_table(void)
1517 {
1518 cd9660node *cn, *dirNode = diskStructure.rootNode;
1519 cd9660node *last = dirNode;
1520 int pathTableSize = 0; /* computed as we go */
1521 int counter = 1; /* root gets a count of 0 */
1522 int parentRecNum = 0; /* root's parent is '0' */
1523
1524 TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head;
1525 TAILQ_INIT(&pt_head);
1526
1527 PTQUEUE_NEW(n, ptq_entry, -1, diskStructure.rootNode);
1528
1529 /* Push the root node */
1530 TAILQ_INSERT_HEAD(&pt_head, n, ptq);
1531
1532 /* Breadth-first traversal of file structure */
1533 while (pt_head.tqh_first != 0) {
1534 n = pt_head.tqh_first;
1535 dirNode = n->node;
1536 TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq);
1537 free(n);
1538
1539 /* Update the size */
1540 pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE
1541 + dirNode->isoDirRecord->name_len[0]+
1542 (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1);
1543 /* includes the padding bit */
1544
1545 dirNode->ptnumber=counter;
1546 if (dirNode != last) {
1547 last->ptnext = dirNode;
1548 dirNode->ptprev = last;
1549 }
1550 last = dirNode;
1551
1552 parentRecNum = 1;
1553 if (dirNode->parent != 0)
1554 parentRecNum = dirNode->parent->ptnumber;
1555
1556 /* Push children onto queue */
1557 TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) {
1558 /*
1559 * Dont add the DOT and DOTDOT types to the path
1560 * table.
1561 */
1562 if ((cn->type != CD9660_TYPE_DOT)
1563 && (cn->type != CD9660_TYPE_DOTDOT)) {
1564
1565 if (S_ISDIR(cn->node->type)) {
1566 PTQUEUE_NEW(n, ptq_entry, -1, cn);
1567 TAILQ_INSERT_TAIL(&pt_head, n, ptq);
1568 }
1569 }
1570 }
1571 counter++;
1572 }
1573 return pathTableSize;
1574 }
1575
1576 void
1577 cd9660_compute_full_filename(cd9660node *node, char *buf, int level)
1578 {
1579 cd9660node *parent;
1580
1581 parent = (node->rr_real_parent == NULL ?
1582 node->parent : node->rr_real_parent);
1583 if (parent != NULL) {
1584 cd9660_compute_full_filename(parent, buf, level + 1);
1585 strcat(buf, node->node->name);
1586 } else {
1587 /* We are at the root */
1588 strcat(buf, diskStructure.rootFilesystemPath);
1589 if (buf[strlen(buf) - 1] == '/')
1590 buf[strlen(buf) - 1] = '\0';
1591 }
1592
1593 if (level != 0)
1594 strcat(buf, "/");
1595 }
1596
1597 /* NEW filename conversion method */
1598 typedef int(*cd9660_filename_conversion_functor)(const char *, char *, int);
1599
1600
1601 /*
1602 * TODO: These two functions are almost identical.
1603 * Some code cleanup is possible here
1604 *
1605 * XXX bounds checking!
1606 */
1607 static int
1608 cd9660_level1_convert_filename(const char *oldname, char *newname, int is_file)
1609 {
1610 /*
1611 * ISO 9660 : 10.1
1612 * File Name shall not contain more than 8 d or d1 characters
1613 * File Name Extension shall not contain more than 3 d or d1 characters
1614 * Directory Identifier shall not contain more than 8 d or d1 characters
1615 */
1616 int namelen = 0;
1617 int extlen = 0;
1618 int found_ext = 0;
1619
1620 while (*oldname != '\0') {
1621 /* Handle period first, as it is special */
1622 if (*oldname == '.') {
1623 if (found_ext) {
1624 *newname++ = '_';
1625 extlen ++;
1626 }
1627 else {
1628 *newname++ = '.';
1629 found_ext = 1;
1630 }
1631 } else {
1632 /* Enforce 12.3 / 8 */
1633 if (((namelen == 8) && !found_ext) ||
1634 (found_ext && extlen == 3)) {
1635 break;
1636 }
1637
1638 if (islower((unsigned char)*oldname))
1639 *newname++ = toupper((unsigned char)*oldname);
1640 else if (isupper((unsigned char)*oldname)
1641 || isdigit((unsigned char)*oldname))
1642 *newname++ = *oldname;
1643 else
1644 *newname++ = '_';
1645
1646 if (found_ext)
1647 extlen++;
1648 else
1649 namelen++;
1650 }
1651 oldname ++;
1652 }
1653 if (is_file) {
1654 if (!found_ext && !diskStructure.omit_trailing_period)
1655 *newname++ = '.';
1656 /* Add version */
1657 sprintf(newname, ";%i", 1);
1658 }
1659 return namelen + extlen + found_ext;
1660 }
1661
1662 /* XXX bounds checking! */
1663 static int
1664 cd9660_level2_convert_filename(const char *oldname, char *newname, int is_file)
1665 {
1666 /*
1667 * ISO 9660 : 7.5.1
1668 * File name : 0+ d or d1 characters
1669 * separator 1 (.)
1670 * File name extension : 0+ d or d1 characters
1671 * separator 2 (;)
1672 * File version number (5 characters, 1-32767)
1673 * 1 <= Sum of File name and File name extension <= 30
1674 */
1675 int namelen = 0;
1676 int extlen = 0;
1677 int found_ext = 0;
1678
1679 while (*oldname != '\0') {
1680 /* Handle period first, as it is special */
1681 if (*oldname == '.') {
1682 if (found_ext) {
1683 if (diskStructure.allow_multidot) {
1684 *newname++ = '.';
1685 } else {
1686 *newname++ = '_';
1687 }
1688 extlen ++;
1689 }
1690 else {
1691 *newname++ = '.';
1692 found_ext = 1;
1693 }
1694 } else {
1695 if ((namelen + extlen) == 30)
1696 break;
1697
1698 if (islower((unsigned char)*oldname))
1699 *newname++ = toupper((unsigned char)*oldname);
1700 else if (isupper((unsigned char)*oldname) ||
1701 isdigit((unsigned char)*oldname))
1702 *newname++ = *oldname;
1703 else if (diskStructure.allow_multidot &&
1704 *oldname == '.') {
1705 *newname++ = '.';
1706 } else {
1707 *newname++ = '_';
1708 }
1709
1710 if (found_ext)
1711 extlen++;
1712 else
1713 namelen++;
1714 }
1715 oldname ++;
1716 }
1717 if (is_file) {
1718 if (!found_ext && !diskStructure.omit_trailing_period)
1719 *newname++ = '.';
1720 /* Add version */
1721 sprintf(newname, ";%i", 1);
1722 }
1723 return namelen + extlen + found_ext;
1724 }
1725
1726 #if 0
1727 static int
1728 cd9660_joliet_convert_filename(const char *oldname, char *newname, int is_file)
1729 {
1730 /* TODO: implement later, move to cd9660_joliet.c ?? */
1731 }
1732 #endif
1733
1734
1735 /*
1736 * Convert a file name to ISO compliant file name
1737 * @param char * oldname The original filename
1738 * @param char ** newname The new file name, in the appropriate character
1739 * set and of appropriate length
1740 * @param int 1 if file, 0 if directory
1741 * @returns int The length of the new string
1742 */
1743 static int
1744 cd9660_convert_filename(const char *oldname, char *newname, int is_file)
1745 {
1746 /* NEW */
1747 cd9660_filename_conversion_functor conversion_function = 0;
1748 if (diskStructure.isoLevel == 1)
1749 conversion_function = &cd9660_level1_convert_filename;
1750 else if (diskStructure.isoLevel == 2)
1751 conversion_function = &cd9660_level2_convert_filename;
1752 return (*conversion_function)(oldname, newname, is_file);
1753 }
1754
1755 int
1756 cd9660_compute_record_size(cd9660node *node)
1757 {
1758 int size = node->isoDirRecord->length[0];
1759
1760 if (diskStructure.rock_ridge_enabled)
1761 size += node->susp_entry_size;
1762 return size;
1763 }
1764
1765 static void
1766 cd9660_populate_dot_records(cd9660node *node)
1767 {
1768 node->dot_record->fileDataSector = node->fileDataSector;
1769 memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34);
1770 node->dot_record->isoDirRecord->name_len[0] = 1;
1771 node->dot_record->isoDirRecord->name[0] = 0;
1772 node->dot_record->isoDirRecord->name[1] = 0;
1773 node->dot_record->isoDirRecord->length[0] = 34;
1774 node->dot_record->fileRecordSize =
1775 cd9660_compute_record_size(node->dot_record);
1776
1777 if (node == diskStructure.rootNode) {
1778 node->dot_dot_record->fileDataSector = node->fileDataSector;
1779 memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord,
1780 34);
1781 } else {
1782 node->dot_dot_record->fileDataSector =
1783 node->parent->fileDataSector;
1784 memcpy(node->dot_dot_record->isoDirRecord,
1785 node->parent->isoDirRecord,34);
1786 }
1787 node->dot_dot_record->isoDirRecord->name_len[0] = 1;
1788 node->dot_dot_record->isoDirRecord->name[0] = 1;
1789 node->dot_dot_record->isoDirRecord->name[1] = 0;
1790 node->dot_dot_record->isoDirRecord->length[0] = 34;
1791 node->dot_dot_record->fileRecordSize =
1792 cd9660_compute_record_size(node->dot_dot_record);
1793 }
1794
1795 /*
1796 * @param struct cd9660node *node The node
1797 * @param int The offset (in bytes) - SHOULD align to the beginning of a sector
1798 * @returns int The total size of files and directory entries (should be
1799 * a multiple of sector size)
1800 */
1801 static int
1802 cd9660_compute_offsets(cd9660node *node, int startOffset)
1803 {
1804 /*
1805 * This function needs to compute the size of directory records and
1806 * runs, file lengths, and set the appropriate variables both in
1807 * cd9660node and isoDirEntry
1808 */
1809 int used_bytes = 0;
1810 int current_sector_usage = 0;
1811 cd9660node *child;
1812 fsinode *inode;
1813 int r;
1814
1815 assert(node != NULL);
1816
1817
1818 /*
1819 * NOTE : There needs to be some special case detection for
1820 * the "real root" node, since for it, node->node is undefined
1821 */
1822
1823 node->fileDataSector = -1;
1824
1825 if (node->type & CD9660_TYPE_DIR) {
1826 node->fileRecordSize = cd9660_compute_record_size(node);
1827 /*Set what sector this directory starts in*/
1828 node->fileDataSector =
1829 CD9660_BLOCKS(diskStructure.sectorSize,startOffset);
1830
1831 cd9660_bothendian_dword(node->fileDataSector,
1832 node->isoDirRecord->extent);
1833
1834 /*
1835 * First loop over children, need to know the size of
1836 * their directory records
1837 */
1838 node->fileSectorsUsed = 1;
1839 TAILQ_FOREACH(child, &node->cn_children, cn_next_child) {
1840 node->fileDataLength +=
1841 cd9660_compute_record_size(child);
1842 if ((cd9660_compute_record_size(child) +
1843 current_sector_usage) >=
1844 diskStructure.sectorSize) {
1845 current_sector_usage = 0;
1846 node->fileSectorsUsed++;
1847 }
1848
1849 current_sector_usage +=
1850 cd9660_compute_record_size(child);
1851 }
1852
1853 cd9660_bothendian_dword(node->fileSectorsUsed *
1854 diskStructure.sectorSize,node->isoDirRecord->size);
1855
1856 /*
1857 * This should point to the sector after the directory
1858 * record (or, the first byte in that sector)
1859 */
1860 used_bytes += node->fileSectorsUsed * diskStructure.sectorSize;
1861
1862 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1863 child != NULL; child = TAILQ_NEXT(child, cn_next_child)) {
1864 /* Directories need recursive call */
1865 if (S_ISDIR(child->node->type)) {
1866 r = cd9660_compute_offsets(child,
1867 used_bytes + startOffset);
1868
1869 if (r != -1)
1870 used_bytes += r;
1871 else
1872 return -1;
1873 }
1874 }
1875
1876 /* Explicitly set the . and .. records */
1877 cd9660_populate_dot_records(node);
1878
1879 /* Finally, do another iteration to write the file data*/
1880 for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child);
1881 child != NULL;
1882 child = TAILQ_NEXT(child, cn_next_child)) {
1883 /* Files need extent set */
1884 if (S_ISDIR(child->node->type))
1885 continue;
1886 child->fileRecordSize =
1887 cd9660_compute_record_size(child);
1888
1889 child->fileSectorsUsed =
1890 CD9660_BLOCKS(diskStructure.sectorSize,
1891 child->fileDataLength);
1892
1893 inode = child->node->inode;
1894 if ((inode->flags & FI_ALLOCATED) == 0) {
1895 inode->ino =
1896 CD9660_BLOCKS(diskStructure.sectorSize,
1897 used_bytes + startOffset);
1898 inode->flags |= FI_ALLOCATED;
1899 used_bytes += child->fileSectorsUsed *
1900 diskStructure.sectorSize;
1901 } else {
1902 INODE_WARNX(("%s: already allocated inode %d "
1903 "data sectors at %" PRIu32, __func__,
1904 (int)inode->st.st_ino, inode->ino));
1905 }
1906 child->fileDataSector = inode->ino;
1907 cd9660_bothendian_dword(child->fileDataSector,
1908 child->isoDirRecord->extent);
1909 }
1910 }
1911
1912 return used_bytes;
1913 }
1914
1915 #if 0
1916 /* Might get rid of this func */
1917 static int
1918 cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file)
1919 {
1920 to->node->inode->st.st_dev = 0;
1921 to->node->inode->st.st_ino = 0;
1922 to->node->inode->st.st_size = 0;
1923 to->node->inode->st.st_blksize = from->node->inode->st.st_blksize;
1924 to->node->inode->st.st_atime = from->node->inode->st.st_atime;
1925 to->node->inode->st.st_mtime = from->node->inode->st.st_mtime;
1926 to->node->inode->st.st_ctime = from->node->inode->st.st_ctime;
1927 to->node->inode->st.st_uid = from->node->inode->st.st_uid;
1928 to->node->inode->st.st_gid = from->node->inode->st.st_gid;
1929 to->node->inode->st.st_mode = from->node->inode->st.st_mode;
1930 /* Clear out type */
1931 to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT);
1932 if (file)
1933 to->node->inode->st.st_mode |= S_IFREG;
1934 else
1935 to->node->inode->st.st_mode |= S_IFDIR;
1936 return 1;
1937 }
1938 #endif
1939
1940 static cd9660node *
1941 cd9660_create_virtual_entry(const char *name, cd9660node *parent, int file,
1942 int insert)
1943 {
1944 cd9660node *temp;
1945 fsnode * tfsnode;
1946
1947 assert(parent != NULL);
1948
1949 temp = cd9660_allocate_cd9660node();
1950 if (temp == NULL)
1951 return NULL;
1952
1953 if ((tfsnode = malloc(sizeof(fsnode))) == NULL) {
1954 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1955 return NULL;
1956 }
1957
1958 /* Assume for now name is a valid length */
1959 if ((tfsnode->name = malloc(strlen(name) + 1)) == NULL) {
1960 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1961 return NULL;
1962 }
1963
1964 if ((temp->isoDirRecord =
1965 malloc(sizeof(iso_directory_record_cd9660))) == NULL) {
1966 CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry");
1967 return NULL;
1968 }
1969
1970 strcpy(tfsnode->name, name);
1971
1972 cd9660_convert_filename(tfsnode->name, temp->isoDirRecord->name, file);
1973
1974 temp->node = tfsnode;
1975 temp->parent = parent;
1976
1977 if (insert) {
1978 if (temp->parent != NULL) {
1979 temp->level = temp->parent->level + 1;
1980 if (!TAILQ_EMPTY(&temp->parent->cn_children))
1981 cd9660_sorted_child_insert(temp->parent, temp);
1982 else
1983 TAILQ_INSERT_HEAD(&temp->parent->cn_children,
1984 temp, cn_next_child);
1985 }
1986 }
1987
1988 if (parent->node != NULL) {
1989 tfsnode->type = parent->node->type;
1990 }
1991
1992 /* Clear out file type bits */
1993 tfsnode->type &= ~(S_IFMT);
1994 if (file)
1995 tfsnode->type |= S_IFREG;
1996 else
1997 tfsnode->type |= S_IFDIR;
1998
1999 /* Indicate that there is no spec entry (inode) */
2000 tfsnode->flags &= ~(FSNODE_F_HASSPEC);
2001 #if 0
2002 cd9660_copy_stat_info(parent, temp, file);
2003 #endif
2004 return temp;
2005 }
2006
2007 static cd9660node *
2008 cd9660_create_file(const char * name, cd9660node *parent, cd9660node *me)
2009 {
2010 cd9660node *temp;
2011
2012 temp = cd9660_create_virtual_entry(name,parent,1,1);
2013 if (temp == NULL)
2014 return NULL;
2015
2016 temp->fileDataLength = 0;
2017
2018 temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL;
2019
2020 if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2021 return NULL;
2022 *temp->node->inode = *me->node->inode;
2023
2024 if (cd9960_translate_node_common(temp) == 0)
2025 return NULL;
2026 return temp;
2027 }
2028
2029 /*
2030 * Create a new directory which does not exist on disk
2031 * @param const char * name The name to assign to the directory
2032 * @param const char * parent Pointer to the parent directory
2033 * @returns cd9660node * Pointer to the new directory
2034 */
2035 static cd9660node *
2036 cd9660_create_directory(const char *name, cd9660node *parent, cd9660node *me)
2037 {
2038 cd9660node *temp;
2039
2040 temp = cd9660_create_virtual_entry(name,parent,0,1);
2041 if (temp == NULL)
2042 return NULL;
2043 temp->node->type |= S_IFDIR;
2044
2045 temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL;
2046
2047 if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL)
2048 return NULL;
2049 *temp->node->inode = *me->node->inode;
2050
2051 if (cd9960_translate_node_common(temp) == 0)
2052 return NULL;
2053 return temp;
2054 }
2055
2056 static cd9660node *
2057 cd9660_create_special_directory(u_char type, cd9660node *parent)
2058 {
2059 cd9660node *temp, *first;
2060 char na[2];
2061
2062 assert(parent != NULL);
2063
2064 if (type == CD9660_TYPE_DOT)
2065 na[0] = 0;
2066 else if (type == CD9660_TYPE_DOTDOT)
2067 na[0] = 1;
2068 else
2069 return 0;
2070
2071 na[1] = 0;
2072 if ((temp = cd9660_create_virtual_entry(na, parent, 0, 0)) == NULL)
2073 return NULL;
2074
2075 temp->parent = parent;
2076 temp->type = type;
2077 temp->isoDirRecord->length[0] = 34;
2078 /* Dot record is always first */
2079 if (type == CD9660_TYPE_DOT) {
2080 parent->dot_record = temp;
2081 TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child);
2082 /* DotDot should be second */
2083 } else if (type == CD9660_TYPE_DOTDOT) {
2084 parent->dot_dot_record = temp;
2085 /*
2086 * If the first child is the dot record, insert
2087 * this second. Otherwise, insert it at the head.
2088 */
2089 if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL ||
2090 (first->type & CD9660_TYPE_DOT) == 0) {
2091 TAILQ_INSERT_HEAD(&parent->cn_children, temp,
2092 cn_next_child);
2093 } else {
2094 TAILQ_INSERT_AFTER(&parent->cn_children, first, temp,
2095 cn_next_child);
2096 }
2097 }
2098
2099 return temp;
2100 }
2101
2102 int
2103 cd9660_add_generic_bootimage(const char *bootimage)
2104 {
2105 struct stat stbuf;
2106
2107 assert(bootimage != NULL);
2108
2109 if (*bootimage == '\0') {
2110 warnx("Error: Boot image must be a filename");
2111 return 0;
2112 }
2113
2114 if ((diskStructure.generic_bootimage = strdup(bootimage)) == NULL) {
2115 warn("%s: strdup", __func__);
2116 return 0;
2117 }
2118
2119 /* Get information about the file */
2120 if (lstat(diskStructure.generic_bootimage, &stbuf) == -1)
2121 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
2122 diskStructure.generic_bootimage);
2123
2124 if (stbuf.st_size > 32768) {
2125 warnx("Error: Boot image must be no greater than 32768 bytes");
2126 return 0;
2127 }
2128
2129 if (diskStructure.verbose_level > 0) {
2130 printf("Generic boot image image has size %lld\n",
2131 (long long)stbuf.st_size);
2132 }
2133
2134 diskStructure.has_generic_bootimage = 1;
2135
2136 return 1;
2137 }
2138