]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Fix rrd create via cached - file name sanitation (absolutation?)
authorPeter Stamfest <peter@stamfest.at>
Thu, 4 Sep 2014 06:46:29 +0000 (08:46 +0200)
committerPeter Stamfest <peter@stamfest.at>
Thu, 4 Sep 2014 08:18:22 +0000 (10:18 +0200)
did quietly assume that files would have to exists. Which ist not
quite right for many "create" usecases.

src/rrd_client.c

index dc59d3895d1d0266d977963db6a29076073aba4a..5f2366dfe1f4224010e965d86ed1181cadc2cf4e 100644 (file)
@@ -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