kconfig: split out useful helpers in confdata.c
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>
This commit is contained in:
parent
a2ff404015
commit
0608182ad5
|
@ -16,6 +16,64 @@
|
|||
|
||||
#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;
|
||||
|
@ -730,10 +787,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();
|
||||
|
@ -853,7 +909,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();
|
||||
|
@ -931,18 +986,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);
|
||||
|
|
Loading…
Reference in New Issue