Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp129055imm; Wed, 4 Jul 2018 19:46:05 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdmzOfBpKR2YdZMBaX96gua1O3s5NeG5JAwohiNSAAWzWXQW0bNGjPcAVMmKUEggfnw2QsM X-Received: by 2002:a17:902:64c2:: with SMTP id y2-v6mr4271320pli.53.1530758765085; Wed, 04 Jul 2018 19:46:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530758765; cv=none; d=google.com; s=arc-20160816; b=rdtrceakJuCspatM1rTDe6HQaR9ybolQVhQlaZ+ZEq9f8A9nkOQKMdLOfolx3KZprJ zAtPsH/dB4cgfkXniQYenJ/RkU6HT0LwmvIowUmZBQQDUXHGhUZZs3BPy3el4oHzIoJr SEIpF4rS0wFyIChJ1YOxAUgGHUIjGz/wlLoUhaFbRQ9pjpz7rplo2ORayhyEoIrDnmDX yvyU0p13cFiHLQt5KlEZlQTxGPjJEREhC8/Vnd+oMS8RNMjm/DkXydA9M8fwRKz6BJru 4auXO5SeBzIrYlWsv/MzDjlA+qomtKxXI4ZKVAyupBFBLEBoYvjfk6bFVWe/AjkIx2H7 VX8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=H9Y4ynGPta8AfBiCIIAsKjioyPf3qY185lk6pn4qKOM=; b=VLhtRe8u2lmIAAWHllWbfnwdyhD9HupqRA+rE5MBBI5YR/Zs0Qw0hDqXyDkraKefyi feao7gWhfGo3EfYkEbv7vjoq7pP92BIRW8DRyo86wd/LukftrkYJ9t3LdqdVWqXRR+ON WjWd0Av/UwGV07CJ78Y39Z6TdgsLyYVtdl8fnha9J5Q9tmXBJ0k0zcFVlVVzC06GcEyd eSEi+T9Ho8iGXOC/QQIv47taHhsVrP7c7A1jJsAGjxm8Jzc+1FDJs8o3Ti95SV5XZPLJ WPV0VaDafjXAkSJ91n+llK3xaA+3cTgq4C/EJTQzFC9vZ8lfYF+M/86LQ8GWhjvGdgWJ sSZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=HYlFQkks; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b11-v6si646543plr.42.2018.07.04.19.45.51; Wed, 04 Jul 2018 19:46:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=HYlFQkks; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753198AbeGEClb (ORCPT + 99 others); Wed, 4 Jul 2018 22:41:31 -0400 Received: from conuserg-07.nifty.com ([210.131.2.74]:43647 "EHLO conuserg-07.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753154AbeGECl3 (ORCPT ); Wed, 4 Jul 2018 22:41:29 -0400 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-07.nifty.com with ESMTP id w652duZY028145; Thu, 5 Jul 2018 11:39:58 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-07.nifty.com w652duZY028145 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1530758399; bh=H9Y4ynGPta8AfBiCIIAsKjioyPf3qY185lk6pn4qKOM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HYlFQkksOnYzNWDvz2mlO1g4n2mwDVaiPqjVs4Wiwsmr418N6yXZam1B7pmAfYEiV 6KLjZnp4fQXvvq2HoVT8WKtTSuqnCCgyshpEtKlidQtUZHul/cpOiiD93fXDftPLnt fJhzOW/FsFxGGhEj66n7hmLBJVWuHC6PIHexpTeyX2ukfcct8uGma0YCxweDTd4mCq l28bPAXUgkSYR4+4vYEfcGoNIDg1iSamlcXxktOrBuBsd9hO91dY8/dxyKDD8NjnSY X7ueo4YBhHvV3z5Ow5x3BfaEA2uNh8gEal0SPWFPPrzZ2HQQI/hqxfhHeWlRY6wtw1 Pnz1rNIQdhMuw== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Dirk Gouders , Ulf Magnusson , Linus Torvalds , Sam Ravnborg , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v3 02/12] kconfig: split out helpers to check file/directory, create directory Date: Thu, 5 Jul 2018 11:39:39 +0900 Message-Id: <1530758389-30862-3-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1530758389-30862-1-git-send-email-yamada.masahiro@socionext.com> References: <1530758389-30862-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Split out helpers: is_file() - check if the given path exists and it is a regular file is_dir() - check if the given path exists and it is a directory mkdir_p() - create the parent directories of the given path These helpers will be reused in later commits. Signed-off-by: Masahiro Yamada --- scripts/kconfig/confdata.c | 85 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index 4771820..881993e 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -16,6 +16,68 @@ #include "lkc.h" +/* return true if 'path' exists and it is a regular file, false otherwise */ +static bool is_file(const char *path) +{ + struct stat st; + + if (stat(path, &st)) + return 0; + + return S_ISREG(st.st_mode); +} + +/* 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'. + * If the path ends with '/' like 'include/config/', create 'include/config'. + */ +static int mkdir_p(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 *); @@ -83,7 +145,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; @@ -91,7 +152,7 @@ char *conf_get_default_confname(void) env = getenv(SRCTREE); if (env) { sprintf(fullname, "%s/%s", env, name); - if (!stat(fullname, &buf)) + if (is_file(fullname)) return fullname; } return name; @@ -725,10 +786,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(); @@ -848,7 +908,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(); @@ -926,18 +985,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 (mkdir_p(path)) { + res = 1; + goto out; } /* Try it again. */ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); -- 2.7.4