testsuite: add mkdir_p implementation
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 24 May 2012 06:29:05 +0000 (03:29 -0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 5 Jun 2012 03:54:47 +0000 (00:54 -0300)
Makefile.am
testsuite/mkdir.c [new file with mode: 0644]
testsuite/mkdir.h [new file with mode: 0644]

index 223e319..5751dbd 100644 (file)
@@ -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 (file)
index 0000000..c334902
--- /dev/null
@@ -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 (file)
index 0000000..6d48ca9
--- /dev/null
@@ -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