]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
mkfs: add initial ini format config file parsing support
authorDave Chinner <dchinner@redhat.com>
Fri, 20 Nov 2020 22:03:29 +0000 (17:03 -0500)
committerEric Sandeen <sandeen@sandeen.net>
Fri, 20 Nov 2020 22:03:29 +0000 (17:03 -0500)
Add the framework that will allow the config file to be supplied on
the CLI and passed to the library that will parse it. This does not
yet do any option parsing from the config file.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
mkfs/Makefile
mkfs/xfs_mkfs.c

index 31482b08d559b49349658a3c12c7e581c2252c90..b8805f7e1ea157ccda6f4ec171bb9d8cb5d6e5d4 100644 (file)
@@ -11,7 +11,7 @@ HFILES =
 CFILES = proto.c xfs_mkfs.c
 
 LLDLIBS += $(LIBXFS) $(LIBXCMD) $(LIBFROG) $(LIBRT) $(LIBPTHREAD) $(LIBBLKID) \
-       $(LIBUUID)
+       $(LIBUUID) $(LIBINIH)
 LTDEPENDENCIES += $(LIBXFS) $(LIBXCMD) $(LIBFROG)
 LLDFLAGS = -static-libtool-libs
 
index bad92f2ba3c2fd7c90d3208b3de918dcd7999243..1d054a3fb7186b58334d1edae2be60e0741b40ef 100644 (file)
@@ -11,6 +11,7 @@
 #include "libfrog/fsgeom.h"
 #include "libfrog/topology.h"
 #include "libfrog/convert.h"
+#include <ini.h>
 
 #define TERABYTES(count, blog) ((uint64_t)(count) << (40 - (blog)))
 #define GIGABYTES(count, blog) ((uint64_t)(count) << (30 - (blog)))
@@ -44,6 +45,11 @@ enum {
        B_MAX_OPTS,
 };
 
+enum {
+       C_OPTFILE = 0,
+       C_MAX_OPTS,
+};
+
 enum {
        D_AGCOUNT = 0,
        D_FILE,
@@ -239,6 +245,28 @@ static struct opt_params bopts = {
        },
 };
 
+/*
+ * Config file specification. Usage is:
+ *
+ * mkfs.xfs -c options=<name>
+ *
+ * A subopt is used for the filename so in future we can extend the behaviour
+ * of the config file (e.g. specified defaults rather than options) if we ever
+ * have a need to do that sort of thing.
+ */
+static struct opt_params copts = {
+       .name = 'c',
+       .subopts = {
+               [C_OPTFILE] = "options",
+       },
+       .subopt_params = {
+               { .index = C_OPTFILE,
+                 .conflicts = { { NULL, LAST_CONFLICT } },
+                 .defaultval = SUBOPT_NEEDS_VAL,
+               },
+       },
+};
+
 static struct opt_params dopts = {
        .name = 'd',
        .subopts = {
@@ -766,6 +794,8 @@ struct cli_params {
        int     sectorsize;
        int     blocksize;
 
+       char    *cfgfile;
+
        /* parameters that depend on sector/block size being validated. */
        char    *dsize;
        char    *agsize;
@@ -880,6 +910,7 @@ usage( void )
 {
        fprintf(stderr, _("Usage: %s\n\
 /* blocksize */                [-b size=num]\n\
+/* config file */      [-c options=xxx]\n\
 /* metadata */         [-m crc=0|1,finobt=0|1,uuid=xxx,rmapbt=0|1,reflink=0|1,\n\
                            inobtcnt=0|1,bigtime=0|1]\n\
 /* data subvol */      [-d agcount=n,agsize=n,file,name=xxx,size=num,\n\
@@ -1404,6 +1435,23 @@ block_opts_parser(
        return 0;
 }
 
+static int
+cfgfile_opts_parser(
+       struct opt_params       *opts,
+       int                     subopt,
+       char                    *value,
+       struct cli_params       *cli)
+{
+       switch (subopt) {
+       case C_OPTFILE:
+               cli->cfgfile = getstr(value, opts, subopt);
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
 static int
 data_opts_parser(
        struct opt_params       *opts,
@@ -1689,6 +1737,7 @@ static struct subopts {
                                  struct cli_params     *cli);
 } subopt_tab[] = {
        { 'b', &bopts, block_opts_parser },
+       { 'c', &copts, cfgfile_opts_parser },
        { 'd', &dopts, data_opts_parser },
        { 'i', &iopts, inode_opts_parser },
        { 'l', &lopts, log_opts_parser },
@@ -3622,6 +3671,65 @@ check_root_ino(
        }
 }
 
+/*
+ * INI file format option parser.
+ *
+ * This is called by the file parser library for every valid option it finds in
+ * the config file. The option is already broken down into a
+ * {section,name,value} tuple, so all we need to do is feed it to the correct
+ * suboption parser function and translate the return value.
+ *
+ * Returns 0 on failure, 1 for success.
+ */
+static int
+cfgfile_parse_ini(
+       void                    *user,
+       const char              *section,
+       const char              *name,
+       const char              *value)
+{
+       struct cli_params       *cli = user;
+
+       fprintf(stderr, "Ini debug: file %s, section %s, name %s, value %s\n",
+               cli->cfgfile, section, name, value);
+
+       return 1;
+}
+
+void
+cfgfile_parse(
+       struct cli_params       *cli)
+{
+       int                     error;
+
+       if (!cli->cfgfile)
+               return;
+
+       error = ini_parse(cli->cfgfile, cfgfile_parse_ini, cli);
+       if (error) {
+               if (error > 0) {
+                       fprintf(stderr,
+               _("%s: Unrecognised input on line %d. Aborting.\n"),
+                               cli->cfgfile, error);
+               } else if (error == -1) {
+                       fprintf(stderr,
+               _("Unable to open config file %s. Aborting.\n"),
+                               cli->cfgfile);
+               } else if (error == -2) {
+                       fprintf(stderr,
+               _("Memory allocation failure parsing %s. Aborting.\n"),
+                               cli->cfgfile);
+               } else {
+                       fprintf(stderr,
+               _("Unknown error %d opening config file %s. Aborting.\n"),
+                               error, cli->cfgfile);
+               }
+               exit(1);
+       }
+       printf(_("Parameters parsed from config file %s successfully\n"),
+               cli->cfgfile);
+}
+
 int
 main(
        int                     argc,
@@ -3710,13 +3818,14 @@ main(
        memcpy(&cli.sb_feat, &dft.sb_feat, sizeof(cli.sb_feat));
        memcpy(&cli.fsx, &dft.fsx, sizeof(cli.fsx));
 
-       while ((c = getopt(argc, argv, "b:d:i:l:L:m:n:KNp:qr:s:CfV")) != EOF) {
+       while ((c = getopt(argc, argv, "b:c:d:i:l:L:m:n:KNp:qr:s:CfV")) != EOF) {
                switch (c) {
                case 'C':
                case 'f':
                        force_overwrite = 1;
                        break;
                case 'b':
+               case 'c':
                case 'd':
                case 'i':
                case 'l':
@@ -3760,6 +3869,14 @@ main(
        } else
                dfile = xi.dname;
 
+       /*
+        * Now we have all the options parsed, we can read in the option file
+        * specified on the command line via "-c options=xxx". Once we have all
+        * the options from this file parsed, we can then proceed with parameter
+        * and bounds checking and making the filesystem.
+        */
+       cfgfile_parse(&cli);
+
        protostring = setup_proto(protofile);
 
        /*