kconfig: split out useful helpers in confdata.c
authorMasahiro Yamada <yamada.masahiro@socionext.com>
Fri, 20 Jul 2018 07:46:27 +0000 (16:46 +0900)
committerChristian Lamparter <chunkeey@gmail.com>
Sun, 10 Feb 2019 21:15:40 +0000 (22:15 +0100)
Split out helpers:
 is_present() - check if the given path exists
 is_dir() - check if the given path exists and it is a directory
 make_parent_dir() - create the parent directories of the given path

These helpers will be reused in later commits.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
config/confdata.c

index 45c462c9312c1176898dccc3e6c9e0c14d63e4e2..b70ef117a01e5284e6dd1296073ac4dd17de97ee 100644 (file)
 
 #include "lkc.h"
 
+/* return true if 'path' exists, false otherwise */
+static bool is_present(const char *path)
+{
+       struct stat st;
+
+       return !stat(path, &st);
+}
+
+/* return true if 'path' exists and it is a directory, false otherwise */
+static bool is_dir(const char *path)
+{
+       struct stat st;
+
+       if (stat(path, &st))
+               return 0;
+
+       return S_ISDIR(st.st_mode);
+}
+
+/*
+ * Create the parent directory of the given path.
+ *
+ * For example, if 'include/config/auto.conf' is given, create 'include/config'.
+ */
+static int make_parent_dir(const char *path)
+{
+       char tmp[PATH_MAX + 1];
+       char *p;
+
+       strncpy(tmp, path, sizeof(tmp));
+       tmp[sizeof(tmp) - 1] = 0;
+
+       /* Remove the base name. Just return if nothing is left */
+       p = strrchr(tmp, '/');
+       if (!p)
+               return 0;
+       *(p + 1) = 0;
+
+       /* Just in case it is an absolute path */
+       p = tmp;
+       while (*p == '/')
+               p++;
+
+       while ((p = strchr(p, '/'))) {
+               *p = 0;
+
+               /* skip if the directory exists */
+               if (!is_dir(tmp) && mkdir(tmp, 0755))
+                       return -1;
+
+               *p = '/';
+               while (*p == '/')
+                       p++;
+       }
+
+       return 0;
+}
+
 struct conf_printer {
        void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
        void (*print_comment)(FILE *, const char *, void *);
@@ -88,7 +146,6 @@ const char *conf_get_autoconfig_name(void)
 
 char *conf_get_default_confname(void)
 {
-       struct stat buf;
        static char fullname[PATH_MAX+1];
        char *env, *name;
 
@@ -96,7 +153,7 @@ char *conf_get_default_confname(void)
        env = getenv(SRCTREE);
        if (env) {
                sprintf(fullname, "%s/%s", env, name);
-               if (!stat(fullname, &buf))
+               if (is_present(fullname))
                        return fullname;
        }
        return name;
@@ -776,10 +833,9 @@ int conf_write(const char *name)
 
        dirname[0] = 0;
        if (name && name[0]) {
-               struct stat st;
                char *slash;
 
-               if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
+               if (is_dir(name)) {
                        strcpy(dirname, name);
                        strcat(dirname, "/");
                        basename = conf_get_configname();
@@ -899,7 +955,6 @@ static int conf_split_config(void)
        char path[PATH_MAX+1];
        char *s, *d, c;
        struct symbol *sym;
-       struct stat sb;
        int res, i, fd;
 
        name = conf_get_autoconfig_name();
@@ -977,18 +1032,10 @@ static int conf_split_config(void)
                                res = 1;
                                break;
                        }
-                       /*
-                        * Create directory components,
-                        * unless they exist already.
-                        */
-                       d = path;
-                       while ((d = strchr(d, '/'))) {
-                               *d = 0;
-                               if (stat(path, &sb) && mkdir(path, 0755)) {
-                                       res = 1;
-                                       goto out;
-                               }
-                               *d++ = '/';
+
+                       if (make_parent_dir(path)) {
+                               res = 1;
+                               goto out;
                        }
                        /* Try it again. */
                        fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);