]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
mkfs: consolidate and streamline config opening function
authorEric Sandeen <sandeen@redhat.com>
Wed, 13 Jun 2018 03:26:31 +0000 (22:26 -0500)
committerEric Sandeen <sandeen@redhat.com>
Wed, 13 Jun 2018 03:26:31 +0000 (22:26 -0500)
There was a lot of duplication between open_cli_config and open_config_file
which was unnecessary.  Collapse all the logic into one function.

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

index fcc7035c7b8bc2c8053e8cbc6af483663dd5313d..eb5263874bd6186b8f8e77ef45d133342581957b 100644 (file)
@@ -523,60 +523,6 @@ config_stat_check(
        return 0;
 }
 
-/*
- * If the file is not found -1 is returned and errno set. Otherwise
- * the file descriptor is returned.
- */
-int
-open_cli_config(
-       int             dirfd,
-       const char      *cli_config_file,
-       char            **fpath)
-{
-       int             fd = -1, len, ret;
-       struct stat     st;
-
-       fd = openat(AT_FDCWD, cli_config_file, O_NOFOLLOW, O_RDONLY);
-       if (fd < 0) {
-               len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR,
-                              cli_config_file);
-               /* Indicates truncation */
-               if (len >= PATH_MAX) {
-                       errno = ENAMETOOLONG;
-                       goto out;
-               }
-
-               fd = openat(dirfd, cli_config_file, O_NOFOLLOW, O_RDONLY);
-               if (fd < 0)
-                       goto out;
-
-               ret = fstat(fd, &st);
-               if (ret != 0)
-                       goto err_out_close;
-
-               ret = config_stat_check(&st);
-               if (ret != 0)
-                       goto err_out_close;
-
-               goto out;
-       }
-
-       memcpy(*fpath, cli_config_file, strlen(cli_config_file));
-
-       ret = fstat(fd, &st);
-       if (ret != 0)
-               goto err_out_close;
-
-       ret = config_stat_check(&st);
-       if (ret != 0)
-               goto err_out_close;
-out:
-       return fd;
-err_out_close:
-       close(fd);
-       return -1;
-}
-
 #ifndef O_PATH
 #if defined __alpha__
 #define O_PATH         040000000
@@ -589,13 +535,27 @@ err_out_close:
 #endif
 #endif /* O_PATH */
 
+/*
+ * Try to open a config file, either cli-specified, or default.
+ *
+ * If specified on commandline, search relative to pwd or absolute path.
+ * If not specified or if above fails, try either cli-spec'd file or "default"
+ * in MKFS_XFS_CONF_DIR.
+ *
+ * If any config file is successfully opened, dft->type is set to reflect the
+ * source, an fd is returned, and the absolute path is returned in **fpath,
+ * which must be free()'d by the caller.
+ *
+ * If a cli-specified file is not found -1 is returned and errno set. Otherwise
+ * the file descriptor is returned.
+ */
 int
 open_config_file(
-       const char                      *cli_config_file,
+       const char                      *config_file,
        struct mkfs_default_params      *dft,
        char                            **fpath)
 {
-       int                             dirfd, fd = -1, len, ret;
+       int                             dirfd = -1, fd = -1, len, ret = 0;
        struct stat                     st;
 
        *fpath = malloc(PATH_MAX);
@@ -604,57 +564,62 @@ open_config_file(
 
        memset(*fpath, 0, PATH_MAX);
 
-       dirfd = open(MKFS_XFS_CONF_DIR, O_PATH|O_NOFOLLOW|O_DIRECTORY);
-
-       if (cli_config_file) {
-               if (strlen(cli_config_file) > PATH_MAX) {
+       /* first try relative to pwd or absolute path to cli configfile */
+       if (config_file) {
+               dft->type = DEFAULTS_CLI_CONFIG;
+               if (strlen(config_file) > PATH_MAX) {
                        errno = ENAMETOOLONG;
                        goto out;
                }
-               fd = open_cli_config(dirfd, cli_config_file, fpath);
-               goto out;
+               memcpy(*fpath, config_file, strlen(config_file));
+               fd = openat(AT_FDCWD, config_file, O_NOFOLLOW, O_RDONLY);
        }
 
-       fd = openat(dirfd, "default", O_NOFOLLOW, O_RDONLY);
-       if (fd < 0)
-               goto out;
-
-       dft->type = DEFAULTS_CONFIG;
-
-       len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR, "default");
-       /* Indicates truncation */
-       if (len >= PATH_MAX) {
-               errno = ENAMETOOLONG;
-               goto err_out_close;
+       /* on failure search for cli config or default file in sysconfdir */
+       if (fd < 0) {
+               if (!config_file)
+                       config_file = MKFS_XFS_DEFAULT_CONFIG;
+               len = snprintf(*fpath, PATH_MAX, "%s/%s", MKFS_XFS_CONF_DIR,
+                               config_file);
+               /* Indicates truncation */
+               if (len >= PATH_MAX) {
+                       errno = ENAMETOOLONG;
+                       goto out;
+               }
+               dirfd = open(MKFS_XFS_CONF_DIR, O_PATH|O_NOFOLLOW|O_DIRECTORY);
+               if (dirfd < 0)
+                       goto out;
+               fd = openat(dirfd, config_file, O_NOFOLLOW, O_RDONLY);
+               if (fd < 0)
+                       goto out;
+               if (!strcmp(config_file, MKFS_XFS_DEFAULT_CONFIG))
+                       dft->type = DEFAULTS_CONFIG;
        }
 
        ret = fstat(fd, &st);
        if (ret != 0)
-               goto err_out_close;
-
+               goto out;
        ret = config_stat_check(&st);
        if (ret != 0)
-               goto err_out_close;
-
+               goto out;
+       
 out:
-       if (fd < 0) {
-               if (dft->type != DEFAULTS_BUILTIN) {
-                       fprintf(stderr,
+       /* stat check is always fatal; missing is fatal only if cli-specified */
+       if (ret ||
+           (fd < 0 && dft->type == DEFAULTS_CLI_CONFIG)) {
+               fprintf(stderr,
 _("Unable to open %s config file: %s : %s\n"),
-                               default_type_str(dft->type), *fpath,
-                               strerror(errno));
-                       free(*fpath);
-                       exit(1);
-               }
+                       default_type_str(dft->type), *fpath,
+                       strerror(errno));
+               free(*fpath);
+               exit(1);
        }
+
+       if (ret && fd >= 0)
+               close(fd);
        if (dirfd >= 0)
                close(dirfd);
-       return fd;
-
-err_out_close:
-       close(fd);
-       fd = -1;
-       goto out;
+       return ret ? ret : fd;
 }
 
 /*
index 163d1e893eab486c530f9d83ae786e3fabe681ef..db22adec12a4576746bebe2b4ec236b0c68bf111 100644 (file)
@@ -19,7 +19,8 @@
 #ifndef _XFS_MKFS_CONFIG_H
 #define _XFS_MKFS_CONFIG_H
 
-#define MKFS_XFS_CONF_DIR      ROOT_SYSCONFDIR "/xfs/mkfs"
+#define MKFS_XFS_CONF_DIR              ROOT_SYSCONFDIR "/xfs/mkfs"
+#define MKFS_XFS_DEFAULT_CONFIG        "default"
 
 struct fsxattr;
 
index 25e8d9c9cc2a29652dc5344da86debbaa1999135..d32c2c8e45afc161e758b48f15601e035cac2868 100644 (file)
@@ -3785,7 +3785,6 @@ _("respecification of configuration not allowed\n"));
                                exit(1);
                        }
                        cli_config_file = optarg;
-                       dft.type = DEFAULTS_CLI_CONFIG;
                        break;
                default:
                        continue;