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