Explorar o código

Implement starpu_mkpath in order to mimic the behaviour of "mkdir -p".

Cédric Augonnet %!s(int64=15) %!d(string=hai) anos
pai
achega
05abf577fb
Modificáronse 3 ficheiros con 90 adicións e 0 borrados
  1. 2 0
      src/Makefile.am
  2. 62 0
      src/common/utils.c
  3. 26 0
      src/common/utils.h

+ 2 - 0
src/Makefile.am

@@ -69,6 +69,7 @@ noinst_HEADERS = 						\
 	common/rwlock.h						\
 	common/starpu-spinlock.h				\
 	common/fxt.h						\
+	common/utils.h						\
 	drivers/core/driver_core.h				\
 	drivers/gordon/driver_gordon.h				\
 	drivers/gordon/gordon_interface.h			\
@@ -81,6 +82,7 @@ libstarpu_la_SOURCES = 						\
 	common/starpu-spinlock.c				\
 	common/timing.c						\
 	common/fxt.c						\
+	common/utils.c						\
 	core/jobs.c						\
 	core/task.c						\
 	core/workers.c						\

+ 62 - 0
src/common/utils.c

@@ -0,0 +1,62 @@
+/*
+ * StarPU
+ * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+#include <starpu.h>
+#include <common/config.h>
+#include <common/utils.h>
+#include <libgen.h>
+
+/* Function with behaviour like `mkdir -p'. This function was adapted from
+ * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
+
+int starpu_mkpath(const char *s, mode_t mode)
+{
+	char *q, *r = NULL, *path = NULL, *up = NULL;
+	int rv;
+
+	rv = -1;
+	if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
+		return 0;
+
+	if ((path = strdup(s)) == NULL)
+		STARPU_ABORT();
+
+	if ((q = strdup(s)) == NULL)
+		STARPU_ABORT();
+
+	if ((r = dirname(q)) == NULL)
+		goto out;
+
+	if ((up = strdup(r)) == NULL)
+		STARPU_ABORT();
+
+	if ((starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
+		goto out;
+
+	if ((mkdir(path, mode) == -1) && (errno != EEXIST))
+		rv = -1;
+	else 
+		rv = 0;
+	
+out:
+	if (up)
+		free(up);
+
+	free(q);
+	free(path);
+	return rv;
+}
+

+ 26 - 0
src/common/utils.h

@@ -0,0 +1,26 @@
+/*
+ * StarPU
+ * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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 Lesser General Public License in COPYING.LGPL for more details.
+ */
+
+#ifndef __COMMON_UTILS_H__
+#define __COMMON_UTILS_H__
+
+#include <starpu.h>
+#include <common/config.h>
+#include <sys/stat.h>
+
+int starpu_mkpath(const char *s, mode_t mode);
+
+#endif // __COMMON_UTILS_H__