Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/makefs - use emalloc and friends



details:   https://anonhg.NetBSD.org/src/rev/aa0fa46b2b6b
branches:  trunk
changeset: 784456:aa0fa46b2b6b
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Jan 28 21:03:26 2013 +0000

description:
- use emalloc and friends
- kill a bunch of global variables, more work to be done here
- homogenize option parsing. more work for cd9660
- use the new options parsing code to print an fs-specific usage

diffstat:

 usr.sbin/makefs/cd9660.c                   |  734 ++++++++++++++--------------
 usr.sbin/makefs/cd9660.h                   |   28 +-
 usr.sbin/makefs/cd9660/cd9660_archimedes.c |   12 +-
 usr.sbin/makefs/cd9660/cd9660_debug.c      |   14 +-
 usr.sbin/makefs/cd9660/cd9660_eltorito.c   |  141 ++---
 usr.sbin/makefs/cd9660/cd9660_write.c      |  161 +++---
 usr.sbin/makefs/cd9660/iso9660_rrip.c      |   74 +-
 usr.sbin/makefs/cd9660/iso9660_rrip.h      |   16 +-
 usr.sbin/makefs/chfs.c                     |   54 +-
 usr.sbin/makefs/chfs/chfs_mkfs.c           |   30 +-
 usr.sbin/makefs/chfs_makefs.h              |    2 -
 usr.sbin/makefs/ffs.c                      |   96 +--
 usr.sbin/makefs/ffs/buf.c                  |   13 +-
 usr.sbin/makefs/ffs/buf.h                  |    4 +-
 usr.sbin/makefs/ffs/mkfs.c                 |   17 +-
 usr.sbin/makefs/makefs.c                   |   45 +-
 usr.sbin/makefs/makefs.h                   |   53 +-
 usr.sbin/makefs/msdos.c                    |   46 +-
 usr.sbin/makefs/msdos/msdosfs_denode.c     |   13 +-
 usr.sbin/makefs/msdos/msdosfs_vfsops.c     |   13 +-
 usr.sbin/makefs/v7fs.c                     |   34 +-
 usr.sbin/makefs/walk.c                     |   31 +-
 22 files changed, 789 insertions(+), 842 deletions(-)

diffs (truncated from 3871 to 300 lines):

diff -r 2f818d9b3988 -r aa0fa46b2b6b usr.sbin/makefs/cd9660.c
--- a/usr.sbin/makefs/cd9660.c  Mon Jan 28 20:32:04 2013 +0000
+++ b/usr.sbin/makefs/cd9660.c  Mon Jan 28 21:03:26 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cd9660.c,v 1.38 2013/01/24 01:10:47 christos Exp $     */
+/*     $NetBSD: cd9660.c,v 1.39 2013/01/28 21:03:26 christos Exp $     */
 
 /*
  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
@@ -103,13 +103,14 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: cd9660.c,v 1.38 2013/01/24 01:10:47 christos Exp $");
+__RCSID("$NetBSD: cd9660.c,v 1.39 2013/01/28 21:03:26 christos Exp $");
 #endif  /* !__lint */
 
 #include <string.h>
 #include <ctype.h>
 #include <sys/param.h>
 #include <sys/queue.h>
+#include <util.h>
 
 #include "makefs.h"
 #include "cd9660.h"
@@ -119,55 +120,61 @@
 /*
  * Global variables
  */
-iso9660_disk diskStructure;
 
-static void cd9660_finalize_PVD(void);
+static void cd9660_finalize_PVD(iso9660_disk *);
 static cd9660node *cd9660_allocate_cd9660node(void);
-static void cd9660_set_defaults(void);
+static void cd9660_set_defaults(iso9660_disk *);
 static int cd9660_arguments_set_string(const char *, const char *, int,
     char, char *);
 static void cd9660_populate_iso_dir_record(
     struct _iso_directory_record_cd9660 *, u_char, u_char, u_char,
     const char *);
-static void cd9660_setup_root_node(void);
-static int cd9660_setup_volume_descriptors(void);
+static void cd9660_setup_root_node(iso9660_disk *);
+static int cd9660_setup_volume_descriptors(iso9660_disk *);
 #if 0
 static int cd9660_fill_extended_attribute_record(cd9660node *);
 #endif
 static void cd9660_sort_nodes(cd9660node *);
-static int cd9660_translate_node_common(cd9660node *);
-static int cd9660_translate_node(fsnode *, cd9660node *);
+static int cd9660_translate_node_common(iso9660_disk *, cd9660node *);
+static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *);
 static int cd9660_compare_filename(const char *, const char *);
 static void cd9660_sorted_child_insert(cd9660node *, cd9660node *);
-static int cd9660_handle_collisions(cd9660node *, int);
-static cd9660node *cd9660_rename_filename(cd9660node *, int, int);
-static void cd9660_copy_filenames(cd9660node *);
+static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int);
+static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int,
+    int);
+static void cd9660_copy_filenames(iso9660_disk *, cd9660node *);
 static void cd9660_sorting_nodes(cd9660node *);
 static int cd9660_count_collisions(cd9660node *);
-static cd9660node *cd9660_rrip_move_directory(cd9660node *);
-static int cd9660_add_dot_records(cd9660node *);
+static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *);
+static int cd9660_add_dot_records(iso9660_disk *, cd9660node *);
 
-static void cd9660_convert_structure(fsnode *, cd9660node *, int,
+static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int,
     int *, int *);
 static void cd9660_free_structure(cd9660node *);
-static int cd9660_generate_path_table(void);
-static int cd9660_level1_convert_filename(const char *, char *, int);
-static int cd9660_level2_convert_filename(const char *, char *, int);
+static int cd9660_generate_path_table(iso9660_disk *);
+static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *,
+    int);
+static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *,
+    int);
 #if 0
-static int cd9660_joliet_convert_filename(const char *, char *, int);
+static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *,
+    int);
 #endif
-static int cd9660_convert_filename(const char *, char *, int);
-static void cd9660_populate_dot_records(cd9660node *);
-static int64_t cd9660_compute_offsets(cd9660node *, int64_t);
+static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int);
+static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *);
+static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t);
 #if 0
 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int);
 #endif
-static cd9660node *cd9660_create_virtual_entry(const char *, cd9660node *, int,
-    int);
-static cd9660node *cd9660_create_file(const char *, cd9660node *, cd9660node *);
-static cd9660node *cd9660_create_directory(const char *, cd9660node *,
+static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *,
+    cd9660node *, int, int);
+static cd9660node *cd9660_create_file(iso9660_disk *, const char *,
+    cd9660node *, cd9660node *);
+static cd9660node *cd9660_create_directory(iso9660_disk *, const char *,
+    cd9660node *, cd9660node *);
+static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char,
     cd9660node *);
-static cd9660node *cd9660_create_special_directory(u_char, cd9660node *);
+static int  cd9660_add_generic_bootimage(iso9660_disk *, const char *);
 
 
 /*
@@ -177,10 +184,7 @@
 static cd9660node *
 cd9660_allocate_cd9660node(void)
 {
-       cd9660node *temp;
-
-       if ((temp = calloc(1, sizeof(cd9660node))) == NULL)
-               err(EXIT_FAILURE, "%s: calloc", __func__);
+       cd9660node *temp = ecalloc(1, sizeof(*temp));
        TAILQ_INIT(&temp->cn_children);
        temp->parent = temp->dot_record = temp->dot_dot_record = NULL;
        temp->ptnext = temp->ptprev = temp->ptlast = NULL;
@@ -198,72 +202,90 @@
 * Set default values for cd9660 extension to makefs
 */
 static void
-cd9660_set_defaults(void)
+cd9660_set_defaults(iso9660_disk *diskStructure)
 {
        /*Fix the sector size for now, though the spec allows for other sizes*/
-       diskStructure.sectorSize = 2048;
+       diskStructure->sectorSize = 2048;
 
        /* Set up defaults in our own structure */
-       diskStructure.verbose_level = 0;
-       diskStructure.keep_bad_images = 0;
-       diskStructure.follow_sym_links = 0;
-       diskStructure.isoLevel = 2;
+       diskStructure->verbose_level = 0;
+       diskStructure->keep_bad_images = 0;
+       diskStructure->follow_sym_links = 0;
+       diskStructure->isoLevel = 2;
 
-       diskStructure.rock_ridge_enabled = 0;
-       diskStructure.rock_ridge_renamed_dir_name = 0;
-       diskStructure.rock_ridge_move_count = 0;
-       diskStructure.rr_moved_dir = 0;
+       diskStructure->rock_ridge_enabled = 0;
+       diskStructure->rock_ridge_renamed_dir_name = 0;
+       diskStructure->rock_ridge_move_count = 0;
+       diskStructure->rr_moved_dir = 0;
 
-       diskStructure.archimedes_enabled = 0;
-       diskStructure.chrp_boot = 0;
+       diskStructure->archimedes_enabled = 0;
+       diskStructure->chrp_boot = 0;
 
-       diskStructure.include_padding_areas = 1;
+       diskStructure->include_padding_areas = 1;
 
        /* Spec breaking functionality */
-       diskStructure.allow_deep_trees =
-           diskStructure.allow_start_dot =
-           diskStructure.allow_max_name =
-           diskStructure.allow_illegal_chars =
-           diskStructure.allow_lowercase =
-           diskStructure.allow_multidot =
-           diskStructure.omit_trailing_period = 0;
+       diskStructure->allow_deep_trees =
+           diskStructure->allow_start_dot =
+           diskStructure->allow_max_name =
+           diskStructure->allow_illegal_chars =
+           diskStructure->allow_lowercase =
+           diskStructure->allow_multidot =
+           diskStructure->omit_trailing_period = 0;
 
        /* Make sure the PVD is clear */
-       memset(&diskStructure.primaryDescriptor, 0, 2048);
+       memset(&diskStructure->primaryDescriptor, 0, 2048);
 
-       memset(diskStructure.primaryDescriptor.publisher_id,    0x20,128);
-       memset(diskStructure.primaryDescriptor.preparer_id,     0x20,128);
-       memset(diskStructure.primaryDescriptor.application_id,  0x20,128);
-       memset(diskStructure.primaryDescriptor.copyright_file_id, 0x20,37);
-       memset(diskStructure.primaryDescriptor.abstract_file_id, 0x20,37);
-       memset(diskStructure.primaryDescriptor.bibliographic_file_id, 0x20,37);
+       memset(diskStructure->primaryDescriptor.publisher_id,   0x20,128);
+       memset(diskStructure->primaryDescriptor.preparer_id,    0x20,128);
+       memset(diskStructure->primaryDescriptor.application_id, 0x20,128);
+       memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37);
+       memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37);
+       memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37);
 
-       strcpy(diskStructure.primaryDescriptor.system_id,"NetBSD");
+       strcpy(diskStructure->primaryDescriptor.system_id,"NetBSD");
 
        cd9660_defaults_set = 1;
 
        /* Boot support: Initially disabled */
-       diskStructure.has_generic_bootimage = 0;
-       diskStructure.generic_bootimage = NULL;
+       diskStructure->has_generic_bootimage = 0;
+       diskStructure->generic_bootimage = NULL;
 
-       diskStructure.boot_image_directory = 0;
-       /*memset(diskStructure.boot_descriptor, 0, 2048);*/
+       diskStructure->boot_image_directory = 0;
+       /*memset(diskStructure->boot_descriptor, 0, 2048);*/
 
-       diskStructure.is_bootable = 0;
-       TAILQ_INIT(&diskStructure.boot_images);
-       LIST_INIT(&diskStructure.boot_entries);
+       diskStructure->is_bootable = 0;
+       TAILQ_INIT(&diskStructure->boot_images);
+       LIST_INIT(&diskStructure->boot_entries);
 }
 
 void
-cd9660_prep_opts(fsinfo_t *fsopts __unused)
+cd9660_prep_opts(fsinfo_t *fsopts)
 {
-       cd9660_set_defaults();
+       iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure));
+
+       const option_t cd9660_options[] = {
+               { 'l', "isolevel", &diskStructure->isoLevel,
+                 OPT_INT32, 1, 3, "ISO Level" },
+               { 'v', "verbose",  &diskStructure->verbose_level,
+                 OPT_INT32, 0, 2, "Turns on verbose output" },
+               { 'L', "Label", diskStructure->primaryDescriptor.volume_id, 
+                 OPT_STRARRAY, 1,
+                 sizeof(diskStructure->primaryDescriptor.volume_id),
+                 "Disk Label" },
+               { .name = NULL }
+       };
+
+       fsopts->fs_specific = diskStructure;
+       fsopts->fs_options = copy_opts(cd9660_options);
+
+       cd9660_set_defaults(diskStructure);
 }
 
 void
-cd9660_cleanup_opts(fsinfo_t *fsopts __unused)
+cd9660_cleanup_opts(fsinfo_t *fsopts)
 {
-
+       free(fsopts->fs_specific);
+       free(fsopts->fs_options);
 }
 
 static int
@@ -302,29 +324,17 @@
 {
        char *var, *val;
        int     rv;
-       /* Set up allowed options - integer options ONLY */
-       option_t cd9660_options[] = {
-               { 'l', "isolevel", &diskStructure.isoLevel,
-                 OPT_INT32, 1, 3, "ISO Level" },
-               { 'v', "verbose",  &diskStructure.verbose_level,
-                 OPT_INT32, 0, 2, "Turns on verbose output" },
-               { 'L', "Label", diskStructure.primaryDescriptor.volume_id, 
-                 OPT_STRARRAY, 1,
-                 sizeof(diskStructure.primaryDescriptor.volume_id),
-                 "Disk Label" },
-               { .name = NULL }
-       };
+       iso9660_disk *diskStructure = fsopts->fs_specific;
 
        if (cd9660_defaults_set == 0)
-               cd9660_set_defaults();
+               cd9660_set_defaults(diskStructure);
 
        assert(option != NULL);
 
        if (debug & DEBUG_FS_PARSE_OPTS)
                printf("cd9660_parse_opts: got `%s'\n", option);
 
-       if ((var = strdup(option)) == NULL)
-               err(1, "allocating memory for copy of option string");
+       var = estrdup(option);
        rv = 1;
 
        val = strchr(var, '=');
@@ -333,33 +343,33 @@
 
        /* First handle options with no parameters */
        if (strcmp(var, "h") == 0) {
-               diskStructure.displayHelp = 1;
+               diskStructure->displayHelp = 1;
                rv = 1;
        } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "S", "follow-symlinks")) {
                /* this is not handled yet */
-               diskStructure.follow_sym_links = 1;
+               diskStructure->follow_sym_links = 1;
                rv = 1;
        } else if (CD9660_IS_COMMAND_ARG_DUAL(var, "L", "label")) {



Home | Main Index | Thread Index | Old Index