+ assert (buffer_free < sizeof (buffer));
+ buffer_size = sizeof (buffer) - buffer_free;
+ assert (buffer[buffer_size - 1] == ' ');
+ buffer[buffer_size - 1] = '\n';
+
+ res = NULL;
+ status = request (buffer, buffer_size, &res);
+ if (status != 0)
+ return (status);
+
+ status = res->status;
+ if (status < 0)
+ {
+ rrd_set_error ("rrdcached: %s", res->message);
+ response_free (res);
+ return (status);
+ }
+ /* }}} Send request */
+
+ ds_names = NULL;
+ ds_num = 0;
+ data = NULL;
+ current_line = 0;
+
+ /* Macros to make error handling a little easier (i. e. less to type and
+ * read. `BAIL_OUT' sets the error message, frees all dynamically allocated
+ * variables and returns the provided status code. */
+#define BAIL_OUT(status, ...) do { \
+ rrd_set_error ("rrdc_fetch: " __VA_ARGS__); \
+ free (data); \
+ if (ds_names != 0) { size_t k; for (k = 0; k < ds_num; k++) free (ds_names[k]); } \
+ free (ds_names); \
+ response_free (res); \
+ return (status); \
+ } while (0)
+
+#define READ_NUMERIC_FIELD(name,type,var) do { \
+ char *key; \
+ unsigned long value; \
+ assert (current_line < res->lines_num); \
+ status = parse_ulong_header (res->lines[current_line], &key, &value); \
+ if (status != 0) \
+ BAIL_OUT (-1, "Unable to parse header `%s'", name); \
+ if (strcasecmp (key, name) != 0) \
+ BAIL_OUT (-1, "Unexpected header line: Expected `%s', got `%s'", name, key); \
+ var = (type) value; \
+ current_line++; \
+ } while (0)
+
+ if (res->lines_num < 1)
+ BAIL_OUT (-1, "Premature end of response packet");
+
+ /* We're making some very strong assumptions about the fields below. We
+ * therefore check the version of the `flush' command first, so that later
+ * versions can change the order of fields and it's easier to implement
+ * backwards compatibility. */
+ READ_NUMERIC_FIELD ("FlushVersion", unsigned long, flush_version);
+ if (flush_version != 1)
+ BAIL_OUT (-1, "Don't know how to handle flush format version %lu.",
+ flush_version);
+
+ if (res->lines_num < 5)
+ BAIL_OUT (-1, "Premature end of response packet");
+
+ READ_NUMERIC_FIELD ("Start", time_t, start);
+ READ_NUMERIC_FIELD ("End", time_t, end);
+ if (start >= end)
+ BAIL_OUT (-1, "Malformed start and end times: start = %lu; end = %lu;",
+ (unsigned long) start,
+ (unsigned long) end);
+
+ READ_NUMERIC_FIELD ("Step", unsigned long, step);
+ if (step < 1)
+ BAIL_OUT (-1, "Invalid number for Step: %lu", step);
+
+ READ_NUMERIC_FIELD ("DSCount", unsigned long, ds_num);
+ if (ds_num < 1)
+ BAIL_OUT (-1, "Invalid number for DSCount: %lu", ds_num);
+
+ /* It's time to allocate some memory */
+ ds_names = calloc ((size_t) ds_num, sizeof (*ds_names));
+ if (ds_names == NULL)
+ BAIL_OUT (-1, "Out of memory");
+
+ status = parse_char_array_header (res->lines[current_line],
+ &str_tmp, ds_names, (size_t) ds_num, /* alloc = */ 1);
+ if (status != 0)
+ BAIL_OUT (-1, "Unable to parse header `DSName'");
+ if (strcasecmp ("DSName", str_tmp) != 0)
+ BAIL_OUT (-1, "Unexpected header line: Expected `DSName', got `%s'", str_tmp);
+ current_line++;
+
+ data_size = ds_num * (end - start) / step;
+ if (data_size < 1)
+ BAIL_OUT (-1, "No data returned or headers invalid.");
+
+ if (res->lines_num != (6 + (data_size / ds_num)))
+ BAIL_OUT (-1, "Got %zu lines, expected %zu",
+ res->lines_num, (6 + (data_size / ds_num)));
+
+ data = calloc (data_size, sizeof (*data));
+ if (data == NULL)
+ BAIL_OUT (-1, "Out of memory");
+
+
+ data_fill = 0;
+ for (t = start + step; t <= end; t += step, current_line++)
+ {
+ time_t tmp;