lib/fileutils: add mkdir_p() from libmount
This commit is contained in:
parent
ce6d69ddac
commit
934530c7e8
|
@ -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.
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
*/
|
||||
|
||||
#include "mountP.h"
|
||||
#include "fileutils.h"
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
|
|
|
@ -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[])
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue