diff options
-rw-r--r-- | disk-utils/fsck.c | 2 | ||||
-rw-r--r-- | include/fileutils.h | 2 | ||||
-rw-r--r-- | lib/fileutils.c | 36 | ||||
-rw-r--r-- | libmount/src/context.c | 1 | ||||
-rw-r--r-- | libmount/src/utils.c | 38 |
5 files changed, 41 insertions, 38 deletions
diff --git a/disk-utils/fsck.c b/disk-utils/fsck.c index caeb6381c..7c7043ac1 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 <kzak@redhat.com> + * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com> * * This file may be redistributed under the terms of the GNU Public * License. diff --git a/include/fileutils.h b/include/fileutils.h index d0d4505cd..99a71edbc 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 92b474cef..3d4553179 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 b7278a42d..1a04c1a29 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -32,6 +32,7 @@ */ #include "mountP.h" +#include "fileutils.h" #include <sys/wait.h> diff --git a/libmount/src/utils.c b/libmount/src/utils.c index d82920905..7700d805a 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[]) { |