Move ctf-fs source query implementations to their own file
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 5 Jun 2017 18:30:16 +0000 (14:30 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Jun 2017 21:03:27 +0000 (17:03 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
plugins/ctf/fs-src/Makefile.am
plugins/ctf/fs-src/fs.c
plugins/ctf/fs-src/query.c [new file with mode: 0644]
plugins/ctf/fs-src/query.h [new file with mode: 0644]

index 65691d5744b5441bb92f058c58817255819788cf..9704c6b2c0a930fedc1e5c7cbfa1048168099455 100644 (file)
@@ -12,4 +12,6 @@ libbabeltrace_plugin_ctf_fs_la_SOURCES = \
        lttng-index.h \
        metadata.c \
        metadata.h \
-       print.h
+       print.h \
+       query.h \
+       query.c
index 55321b2fd4785d8b94f0617ad0a4e577c090c412..3f49e00cba439d621116f1ed7e87392884d099d5 100644 (file)
 #include "data-stream-file.h"
 #include "file.h"
 #include "../common/metadata/decoder.h"
+#include "query.h"
 
 #define PRINT_ERR_STREAM       ctf_fs->error_fp
 #define PRINT_PREFIX           "ctf-fs"
 #define PRINT_DBG_CHECK                ctf_fs_debug
 #include "../print.h"
-#define METADATA_TEXT_SIG      "/* CTF 1.8"
 
 BT_HIDDEN
 bool ctf_fs_debug;
@@ -1323,125 +1323,14 @@ BT_HIDDEN
 struct bt_value *ctf_fs_query(struct bt_component_class *comp_class,
                const char *object, struct bt_value *params)
 {
-       struct bt_value *results = NULL;
-       struct bt_value *path_value = NULL;
-       char *metadata_text = NULL;
-       FILE *metadata_fp = NULL;
-       GString *g_metadata_text = NULL;
-
-       if (strcmp(object, "metadata-info") == 0) {
-               int ret;
-               int bo;
-               const char *path;
-               bool is_packetized;
-
-               results = bt_value_map_create();
-               if (!results) {
-                       goto error;
-               }
-
-               if (!bt_value_is_map(params)) {
-                       fprintf(stderr,
-                               "Query parameters is not a map value object\n");
-                       goto error;
-               }
-
-               path_value = bt_value_map_get(params, "path");
-               ret = bt_value_string_get(path_value, &path);
-               if (ret) {
-                       fprintf(stderr,
-                               "Cannot get `path` string parameter\n");
-                       goto error;
-               }
+       struct bt_value *result = NULL;
 
-               assert(path);
-               metadata_fp = ctf_fs_metadata_open_file(path);
-               if (!metadata_fp) {
-                       fprintf(stderr,
-                               "Cannot open trace at path `%s`\n", path);
-                       goto error;
-               }
-
-               is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
-                       &bo);
-
-               if (is_packetized) {
-                       ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
-                               metadata_fp, &metadata_text, bo);
-                       if (ret) {
-                               fprintf(stderr,
-                                       "Cannot decode packetized metadata file\n");
-                               goto error;
-                       }
-               } else {
-                       long filesize;
-
-                       fseek(metadata_fp, 0, SEEK_END);
-                       filesize = ftell(metadata_fp);
-                       rewind(metadata_fp);
-                       metadata_text = malloc(filesize + 1);
-                       if (!metadata_text) {
-                               fprintf(stderr,
-                                       "Cannot allocate buffer for metadata text\n");
-                               goto error;
-                       }
-
-                       if (fread(metadata_text, filesize, 1, metadata_fp) !=
-                                       1) {
-                               fprintf(stderr,
-                                       "Cannot read metadata file\n");
-                               goto error;
-                       }
-
-                       metadata_text[filesize] = '\0';
-               }
-
-               g_metadata_text = g_string_new(NULL);
-               if (!g_metadata_text) {
-                       goto error;
-               }
-
-               if (strncmp(metadata_text, METADATA_TEXT_SIG,
-                               sizeof(METADATA_TEXT_SIG) - 1) != 0) {
-                       g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
-                       g_string_append(g_metadata_text, " */\n\n");
-               }
-
-               g_string_append(g_metadata_text, metadata_text);
-
-               ret = bt_value_map_insert_string(results, "text",
-                       g_metadata_text->str);
-               if (ret) {
-                       fprintf(stderr, "Cannot insert metadata text into results\n");
-                       goto error;
-               }
-
-               ret = bt_value_map_insert_bool(results, "is-packetized",
-                       is_packetized);
-               if (ret) {
-                       fprintf(stderr, "Cannot insert is packetized into results\n");
-                       goto error;
-               }
+       if (!strcmp(object, "metadata-info")) {
+               result = metadata_info_query(comp_class, params);
        } else {
                fprintf(stderr, "Unknown query object `%s`\n", object);
-               goto error;
+               goto end;
        }
-
-       goto end;
-
-error:
-       BT_PUT(results);
-
 end:
-       bt_put(path_value);
-       free(metadata_text);
-
-       if (g_metadata_text) {
-               g_string_free(g_metadata_text, TRUE);
-       }
-
-       if (metadata_fp) {
-               fclose(metadata_fp);
-       }
-       return results;
+       return result;
 }
diff --git a/plugins/ctf/fs-src/query.c b/plugins/ctf/fs-src/query.c
new file mode 100644 (file)
index 0000000..60ec42e
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * query.c
+ *
+ * Babeltrace CTF file system Reader Component queries
+ *
+ * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "query.h"
+#include <stdbool.h>
+#include <assert.h>
+#include "metadata.h"
+#include "../common/metadata/decoder.h"
+
+#define METADATA_TEXT_SIG      "/* CTF 1.8"
+
+BT_HIDDEN
+struct bt_value *metadata_info_query(struct bt_component_class *comp_class,
+               struct bt_value *params)
+{
+       struct bt_value *results = NULL;
+       struct bt_value *path_value = NULL;
+       char *metadata_text = NULL;
+       FILE *metadata_fp = NULL;
+       GString *g_metadata_text = NULL;
+       int ret;
+       int bo;
+       const char *path;
+       bool is_packetized;
+
+       results = bt_value_map_create();
+       if (!results) {
+               goto error;
+       }
+
+       if (!bt_value_is_map(params)) {
+               fprintf(stderr,
+                       "Query parameters is not a map value object\n");
+               goto error;
+       }
+
+       path_value = bt_value_map_get(params, "path");
+       ret = bt_value_string_get(path_value, &path);
+       if (ret) {
+               fprintf(stderr,
+                       "Cannot get `path` string parameter\n");
+               goto error;
+       }
+
+       assert(path);
+       metadata_fp = ctf_fs_metadata_open_file(path);
+       if (!metadata_fp) {
+               fprintf(stderr,
+                       "Cannot open trace at path `%s`\n", path);
+               goto error;
+       }
+
+       is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
+               &bo);
+
+       if (is_packetized) {
+               ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
+                       metadata_fp, &metadata_text, bo);
+               if (ret) {
+                       fprintf(stderr,
+                               "Cannot decode packetized metadata file\n");
+                       goto error;
+               }
+       } else {
+               long filesize;
+
+               fseek(metadata_fp, 0, SEEK_END);
+               filesize = ftell(metadata_fp);
+               rewind(metadata_fp);
+               metadata_text = malloc(filesize + 1);
+               if (!metadata_text) {
+                       fprintf(stderr,
+                               "Cannot allocate buffer for metadata text\n");
+                       goto error;
+               }
+
+               if (fread(metadata_text, filesize, 1, metadata_fp) != 1) {
+                       fprintf(stderr,
+                               "Cannot read metadata file\n");
+                       goto error;
+               }
+
+               metadata_text[filesize] = '\0';
+       }
+
+       g_metadata_text = g_string_new(NULL);
+       if (!g_metadata_text) {
+               goto error;
+       }
+
+       if (strncmp(metadata_text, METADATA_TEXT_SIG,
+                       sizeof(METADATA_TEXT_SIG) - 1) != 0) {
+               g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
+               g_string_append(g_metadata_text, " */\n\n");
+       }
+
+       g_string_append(g_metadata_text, metadata_text);
+
+       ret = bt_value_map_insert_string(results, "text",
+               g_metadata_text->str);
+       if (ret) {
+               fprintf(stderr, "Cannot insert metadata text into results\n");
+               goto error;
+       }
+
+       ret = bt_value_map_insert_bool(results, "is-packetized",
+               is_packetized);
+       if (ret) {
+               fprintf(stderr, "Cannot insert is packetized into results\n");
+               goto error;
+       }
+
+       goto end;
+
+error:
+       BT_PUT(results);
+
+end:
+       bt_put(path_value);
+       free(metadata_text);
+
+       if (g_metadata_text) {
+               g_string_free(g_metadata_text, TRUE);
+       }
+
+       if (metadata_fp) {
+               fclose(metadata_fp);
+       }
+       return results;
+}
diff --git a/plugins/ctf/fs-src/query.h b/plugins/ctf/fs-src/query.h
new file mode 100644 (file)
index 0000000..01b67c2
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef BABELTRACE_PLUGIN_CTF_FS_QUERY_H
+#define BABELTRACE_PLUGIN_CTF_FS_QUERY_H
+
+/*
+ * BabelTrace - CTF on File System Component
+ *
+ * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/values.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/graph/component-class.h>
+
+BT_HIDDEN
+struct bt_value *metadata_info_query(struct bt_component_class *comp_class,
+               struct bt_value *params);
+
+#endif /* BABELTRACE_PLUGIN_CTF_FS_QUERY_H */
This page took 0.029101 seconds and 4 git commands to generate.