From: Peter Stamfest Date: Thu, 4 Sep 2014 06:46:29 +0000 (+0200) Subject: Fix rrd create via cached - file name sanitation (absolutation?) X-Git-Tag: v1.5.0-rc1~44^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed4915b531e7e15dcb5c1409c6a8b05db48b4ba6;p=thirdparty%2Frrdtool-1.x.git Fix rrd create via cached - file name sanitation (absolutation?) did quietly assume that files would have to exists. Which ist not quite right for many "create" usecases. --- diff --git a/src/rrd_client.c b/src/rrd_client.c index dc59d389..5f2366df 100644 --- a/src/rrd_client.c +++ b/src/rrd_client.c @@ -101,9 +101,38 @@ static const char *get_path (const char *path, char *resolved_path) /* {{{ */ if (is_unix) { + if (path == NULL || strlen(path) == 0) return NULL; ret = realpath(path, resolved_path); - if (ret == NULL) - rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno)); + if (ret == NULL) { + /* this may happen, because the file DOES NOT YET EXIST (as would be + * the case for rrdcreate) - retry by stripping the last path element, + * resolving the directory and re-concatenate them.... */ + char buffer[PATH_MAX]; + char *lastslash = strrchr(path, '/'); + + char *dir = (lastslash == NULL || lastslash == path) ? strdup(".") + : strndup(path, lastslash - path); + + if (dir != NULL) { + ret = realpath(dir, buffer); + free(dir); + if (ret == NULL) { + rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno)); + } else { + strcat(buffer, lastslash); + if (resolved_path == NULL) { + ret = strdup(buffer); + } else { + strcpy(resolved_path, buffer); + ret = resolved_path; + } + } + } else { + // out of memory + rrd_set_error("cannot allocate memory"); + ret = NULL; // redundant, but make intention clear + } + } return ret; } else