From: Karel Zak Date: Mon, 9 Jun 2014 08:59:18 +0000 (+0200) Subject: lib/fileutils: add mkdir_p() from libmount X-Git-Tag: v2.25-rc1~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=934530c7e831e6265142df14e08246dcb4952872;p=thirdparty%2Futil-linux.git lib/fileutils: add mkdir_p() from libmount --- diff --git a/disk-utils/fsck.c b/disk-utils/fsck.c index caeb6381c2..7c7043ac16 100644 --- a/disk-utils/fsck.c +++ b/disk-utils/fsck.c @@ -19,7 +19,7 @@ * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o. * - * Copyright (C) 2009, 2012 Karel Zak + * Copyright (C) 2009-2014 Karel Zak * * This file may be redistributed under the terms of the GNU Public * License. diff --git a/include/fileutils.h b/include/fileutils.h index d0d4505cdb..99a71edbcd 100644 --- a/include/fileutils.h +++ b/include/fileutils.h @@ -20,4 +20,6 @@ static inline FILE *xfmkstemp(char **tmpname, char *dir) extern int get_fd_tabsize(void); +extern int mkdir_p(const char *path, mode_t mode); + #endif /* UTIL_LINUX_FILEUTILS */ diff --git a/lib/fileutils.c b/lib/fileutils.c index 92b474cef4..3d45531792 100644 --- a/lib/fileutils.c +++ b/lib/fileutils.c @@ -81,3 +81,39 @@ int main(void) return EXIT_FAILURE; } #endif + + +int mkdir_p(const char *path, mode_t mode) +{ + char *p, *dir; + int rc = 0; + + if (!path || !*path) + return -EINVAL; + + dir = p = strdup(path); + if (!dir) + return -ENOMEM; + + if (*p == '/') + p++; + + while (p && *p) { + char *e = strchr(p, '/'); + if (e) + *e = '\0'; + if (*p) { + rc = mkdir(dir, mode); + if (rc && errno != EEXIST) + break; + rc = 0; + } + if (!e) + break; + *e = '/'; + p = e + 1; + } + + free(dir); + return rc; +} diff --git a/libmount/src/context.c b/libmount/src/context.c index b7278a42dd..1a04c1a29e 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -32,6 +32,7 @@ */ #include "mountP.h" +#include "fileutils.h" #include diff --git a/libmount/src/utils.c b/libmount/src/utils.c index d82920905e..7700d805a5 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -23,6 +23,7 @@ #include "canonicalize.h" #include "env.h" #include "match.h" +#include "fileutils.h" #include "statfs_magic.h" int append_string(char **a, const char *b) @@ -1110,43 +1111,6 @@ char *mnt_get_kernel_cmdline_option(const char *name) return res; } -int mkdir_p(const char *path, mode_t mode) -{ - char *p, *dir; - int rc = 0; - - if (!path || !*path) - return -EINVAL; - - dir = p = strdup(path); - if (!dir) - return -ENOMEM; - - if (*p == '/') - p++; - - while (p && *p) { - char *e = strchr(p, '/'); - if (e) - *e = '\0'; - if (*p) { - rc = mkdir(dir, mode); - if (rc && errno != EEXIST) - break; - rc = 0; - } - if (!e) - break; - *e = '/'; - p = e + 1; - } - - DBG(UTILS, ul_debug("%s mkdir %s", path, rc ? "FAILED" : "SUCCESS")); - - free(dir); - return rc; -} - #ifdef TEST_PROGRAM int test_match_fstype(struct libmnt_test *ts, int argc, char *argv[]) {