summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1dc3aa2)
raw | patch | inline | side by side (parent: 1dc3aa2)
author | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Mon, 28 Sep 2009 16:13:58 +0000 (16:13 +0000) | ||
committer | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Mon, 28 Sep 2009 16:13:58 +0000 (16:13 +0000) |
"mkdir -p" on the command-line. -- Sebastian Harl
git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1913 a5681a0c-68f1-0310-ab6d-d61299d08faa
git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1913 a5681a0c-68f1-0310-ab6d-d61299d08faa
src/librrd.sym.in.in | patch | blob | history | |
src/rrd.h | patch | blob | history | |
src/rrd_utils.c | patch | blob | history |
diff --git a/src/librrd.sym.in.in b/src/librrd.sym.in.in
index 18b5e7295ea28d84e48d5b1e272950ccf5078e52..a4b9a08893d884914db27ab8689aa180e338bbac 100644 (file)
--- a/src/librrd.sym.in.in
+++ b/src/librrd.sym.in.in
rrd_lastupdate
rrd_lastupdate_r
rrd_lock
+rrd_mkdir_p
rrd_new_context
rrd_open
rrd_parsetime
diff --git a/src/rrd.h b/src/rrd.h
index a5d3129f806364df1caa7087567a10538d982d86..697e5525fa8dedb549d26115c0a9286393ddb1f6 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
int rrd_add_strdup(char ***dest, size_t *dest_size, char *src);
void rrd_free_ptrs(void ***src, size_t *cnt);
+ int rrd_mkdir_p(const char *pathname, mode_t mode);
+
/*
* The following functions are _internal_ functions needed to read the raw RRD
* files. Since they are _internal_ they may change with the file format and
diff --git a/src/rrd_utils.c b/src/rrd_utils.c
index 9ac3e8ab7536e00771bf13afcc9fccbe88290689..3936cffd1d2e6689bd4ac5117e6404f54c09b35e 100644 (file)
--- a/src/rrd_utils.c
+++ b/src/rrd_utils.c
/**
+ * RRDtool - src/rrd_utils.c
+ * Copyright (C) 2009 Kevin Brintnall
+ * Copyright (C) 2008 Sebastian Harl
+ *
* 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; only version 2 of the License is applicable.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors:
+ * kevin brintnall <kbrint@rufus.net>
+ * Sebastian Harl <sh@tokkee.org>
**/
#include "rrd_tool.h"
-#include <stdlib.h>
#include <assert.h>
+#include <errno.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#ifdef WIN32
# define random() rand()
free (sp);
*src = NULL;
}
+
+/* recursively create the directory named by 'pathname'
+ * (similar to "mkdir -p" on the command line) */
+int rrd_mkdir_p(const char *pathname, mode_t mode)
+{
+ struct stat sb;
+
+ char *pathname_copy;
+ char *base_dir;
+
+ if ((NULL == pathname) || ('\0' == *pathname)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (0 == stat(pathname, &sb)) {
+ if (! S_ISDIR(sb.st_mode)) {
+ errno = ENOTDIR;
+ return -1;
+ }
+ return 0;
+ }
+
+ /* keep errno as set by stat() */
+ if (ENOENT != errno)
+ return -1;
+
+ /* dirname might modify its first argument */
+ if (NULL == (pathname_copy = strdup(pathname)))
+ return -1;
+
+ base_dir = dirname(pathname_copy);
+
+ if (0 != rrd_mkdir_p(base_dir, mode)) {
+ int orig_errno = errno;
+ free(pathname_copy);
+ errno = orig_errno;
+ return -1;
+ }
+
+ free(pathname_copy);
+
+ /* keep errno as set by mkdir() */
+ if (0 != mkdir(pathname, mode))
+ return -1;
+ return 0;
+} /* rrd_mkdir_p */
+