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