From: Lucas De Marchi Date: Thu, 24 May 2012 06:29:05 +0000 (-0300) Subject: testsuite: add mkdir_p implementation X-Git-Tag: v9~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44e54667950faa9faa4e64ebe3037c3d59d6ed41;p=thirdparty%2Fkmod.git testsuite: add mkdir_p implementation --- diff --git a/Makefile.am b/Makefile.am index 223e3191..5751dbd5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -153,6 +153,7 @@ testsuite_path_la_LDFLAGS = $(TESTSUITE_OVERRIDE_LIBS_LDFLAGS) testsuite_delete_module_la_LDFLAGS = $(TESTSUITE_OVERRIDE_LIBS_LDFLAGS) testsuite_init_module_la_LDFLAGS = $(TESTSUITE_OVERRIDE_LIBS_LDFLAGS) testsuite_init_module_la_SOURCES = testsuite/init_module.c \ + testsuite/mkdir.c \ testsuite/stripped-module.h testsuite_init_module_la_LIBADD = libkmod/libkmod-private.la diff --git a/testsuite/mkdir.c b/testsuite/mkdir.c new file mode 100644 index 00000000..c334902b --- /dev/null +++ b/testsuite/mkdir.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2012 Lucas De Marchi . + */ + +#include +#include +#include +#include + +#include "mkdir.h" +#include "testsuite.h" + +TS_EXPORT int mkdir_p(const char *path, mode_t mode) +{ + char *start = strdupa(path); + int len = strlen(path); + char *end = start + len; + struct stat st; + + /* + * scan backwards, replacing '/' with '\0' while the component doesn't + * exist + */ + for (;;) { + if (stat(start, &st) >= 0) { + if (S_ISDIR(st.st_mode)) + break; + return -ENOTDIR; + } + + /* Find the next component, backwards, discarding extra '/'*/ + for (; end != start && *end != '/'; end--) + ; + + for (; end != start - 1 && *end == '/'; end--) + ; + + end++; + if (end == start) + break; + + *end = '\0'; + } + + if (end == start + len) + return 0; + + for (; end < start + len;) { + *end = '/'; + end += strlen(end); + + if (mkdir(start, mode) < 0) + return -errno; + } + + return 0; +} diff --git a/testsuite/mkdir.h b/testsuite/mkdir.h new file mode 100644 index 00000000..6d48ca98 --- /dev/null +++ b/testsuite/mkdir.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2012 Lucas De Marchi + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _LIBKMOD_TESTSUITE_MKDIR_ +#define _LIBKMOD_TESTSUITE_MKDIR_ + +#include +#include + +int mkdir_p(const char *path, mode_t mode); + +#endif