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