diff options
author | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-05-24 03:29:05 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2012-06-05 00:54:47 -0300 |
commit | 44e54667950faa9faa4e64ebe3037c3d59d6ed41 (patch) | |
tree | 8689072fc5020d9edd14f4e3cea5b8967f95971f | |
parent | 113d0084d4c44e219a055b358b166c32663863d3 (diff) | |
download | kmod-44e54667950faa9faa4e64ebe3037c3d59d6ed41.tar.gz |
testsuite: add mkdir_p implementation
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | testsuite/mkdir.c | 70 | ||||
-rw-r--r-- | testsuite/mkdir.h | 26 |
3 files changed, 97 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 223e319..5751dbd 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 0000000..c334902 --- /dev/null +++ b/testsuite/mkdir.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> + +#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 0000000..6d48ca9 --- /dev/null +++ b/testsuite/mkdir.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef _LIBKMOD_TESTSUITE_MKDIR_ +#define _LIBKMOD_TESTSUITE_MKDIR_ + +#include <sys/stat.h> +#include <sys/types.h> + +int mkdir_p(const char *path, mode_t mode); + +#endif |