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