X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_utils.c;h=6853c66dacef29fd655d9c60d90d80597c1313ba;hp=9ac3e8ab7536e00771bf13afcc9fccbe88290689;hb=9e2eb0abd095264f584490ede57ae5afe8beb748;hpb=4445ea8dfeb4f30fd181c922141303fa49e01c5a diff --git a/src/rrd_utils.c b/src/rrd_utils.c index 9ac3e8a..6853c66 100644 --- a/src/rrd_utils.c +++ b/src/rrd_utils.c @@ -1,4 +1,8 @@ /** + * 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. @@ -11,12 +15,22 @@ * 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 + * Sebastian Harl **/ #include "rrd_tool.h" -#include #include +#include +#include +#include +#include +#include +#include +#include #ifdef WIN32 # define random() rand() @@ -36,29 +50,53 @@ long rrd_random(void) return random(); } -/* rrd_add_ptr: add a pointer to a dynamically sized array of pointers, - * realloc as necessary. returns 1 on success, 0 on failure. +/* rrd_add_ptr_chunk: add a pointer to a dynamically sized array of + * pointers, realloc as necessary in multiples of "chunk". + * + * "alloc" is the number of pointers allocated + * "dest_size" is the number of valid pointers + * + * returns 1 on success, 0 on failure. */ -int rrd_add_ptr(void ***dest, size_t *dest_size, void *src) +int rrd_add_ptr_chunk(void ***dest, size_t *dest_size, void *src, + size_t *alloc, size_t chunk) { void **temp; assert(dest != NULL); + assert(alloc != NULL); + assert(*alloc >= *dest_size); - temp = (void **) rrd_realloc(*dest, (*dest_size+1) * sizeof(*dest)); - if (!temp) - return 0; + if (*alloc == *dest_size) + { + temp = (void **) rrd_realloc(*dest, (*alloc+chunk) * sizeof(*dest)); + if (!temp) + return 0; - *dest = temp; - temp[*dest_size] = src; + *dest = temp; + *alloc += chunk; + } + + (*dest)[*dest_size] = src; (*dest_size)++; return 1; } -/* like rrd_add_ptr, but calls strdup() on a string first. */ -int rrd_add_strdup(char ***dest, size_t *dest_size, char *src) +/* rrd_add_ptr: add a pointer to a dynamically sized array of pointers, + * realloc as necessary. returns 1 on success, 0 on failure. + */ +int rrd_add_ptr(void ***dest, size_t *dest_size, void *src) +{ + size_t alloc = *dest_size; + + return rrd_add_ptr_chunk(dest, dest_size, src, &alloc, 1); +} + +/* like rrd_add_ptr_chunk, but calls strdup() on a string first. */ +int rrd_add_strdup_chunk(char ***dest, size_t *dest_size, char *src, + size_t *alloc, size_t chunk) { char *dup_src; int add_ok; @@ -70,13 +108,20 @@ int rrd_add_strdup(char ***dest, size_t *dest_size, char *src) if (!dup_src) return 0; - add_ok = rrd_add_ptr((void ***)dest, dest_size, (void *)dup_src); + add_ok = rrd_add_ptr_chunk((void ***)dest, dest_size, (void *)dup_src, alloc, chunk); if (!add_ok) free(dup_src); return add_ok; } +int rrd_add_strdup(char ***dest, size_t *dest_size, char *src) +{ + size_t alloc = *dest_size; + + return rrd_add_strdup_chunk(dest, dest_size, src, &alloc, 1); +} + void rrd_free_ptrs(void ***src, size_t *cnt) { void **sp; @@ -95,3 +140,51 @@ void rrd_free_ptrs(void ***src, size_t *cnt) 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 */ +