| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 6 | */ |
| 7 | |
| 8 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 9 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 10 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META" |
| 11 | #include "logging/comp-logging.h" |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | #include "common/assert.h" |
| 17 | #include <glib.h> |
| 18 | #include "common/uuid.h" |
| 19 | #include "compat/memstream.h" |
| 20 | #include <babeltrace2/babeltrace.h> |
| 21 | |
| 22 | #include "fs.hpp" |
| 23 | #include "file.hpp" |
| 24 | #include "metadata.hpp" |
| 25 | #include "../common/metadata/decoder.hpp" |
| 26 | |
| 27 | FILE *ctf_fs_metadata_open_file(const char *trace_path, bt_logging_level log_level, |
| 28 | bt_self_component_class *comp_class) |
| 29 | { |
| 30 | GString *metadata_path; |
| 31 | FILE *fp = NULL; |
| 32 | |
| 33 | metadata_path = g_string_new(trace_path); |
| 34 | if (!metadata_path) { |
| 35 | goto end; |
| 36 | } |
| 37 | |
| 38 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
| 39 | fp = fopen(metadata_path->str, "rb"); |
| 40 | if (!fp) { |
| 41 | BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(comp_class, "Failed to open metadata file", |
| 42 | ": path=\"%s\"", metadata_path->str); |
| 43 | } |
| 44 | |
| 45 | g_string_free(metadata_path, TRUE); |
| 46 | |
| 47 | end: |
| 48 | return fp; |
| 49 | } |
| 50 | |
| 51 | static struct ctf_fs_file *get_file(const char *trace_path, bt_logging_level log_level, |
| 52 | bt_self_component *self_comp) |
| 53 | { |
| 54 | struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp); |
| 55 | |
| 56 | if (!file) { |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | g_string_append(file->path, trace_path); |
| 61 | g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
| 62 | |
| 63 | if (ctf_fs_file_open(file, "rb")) { |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | goto end; |
| 68 | |
| 69 | error: |
| 70 | if (file) { |
| 71 | ctf_fs_file_destroy(file); |
| 72 | file = NULL; |
| 73 | } |
| 74 | |
| 75 | end: |
| 76 | return file; |
| 77 | } |
| 78 | |
| 79 | int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace, |
| 80 | struct ctf_fs_metadata_config *config) |
| 81 | { |
| 82 | int ret = 0; |
| 83 | struct ctf_fs_file *file = NULL; |
| 84 | bt_logging_level log_level = ctf_fs_trace->log_level; |
| 85 | |
| 86 | ctf_metadata_decoder_config decoder_config {}; |
| 87 | decoder_config.log_level = ctf_fs_trace->log_level, decoder_config.self_comp = self_comp, |
| 88 | decoder_config.clock_class_offset_s = config ? config->clock_class_offset_s : 0, |
| 89 | decoder_config.clock_class_offset_ns = config ? config->clock_class_offset_ns : 0, |
| 90 | decoder_config.force_clock_class_origin_unix_epoch = |
| 91 | config ? config->force_clock_class_origin_unix_epoch : false, |
| 92 | decoder_config.create_trace_class = true, |
| 93 | |
| 94 | file = get_file(ctf_fs_trace->path->str, log_level, self_comp); |
| 95 | if (!file) { |
| 96 | BT_COMP_LOGE("Cannot create metadata file object."); |
| 97 | ret = -1; |
| 98 | goto end; |
| 99 | } |
| 100 | |
| 101 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(&decoder_config); |
| 102 | if (!ctf_fs_trace->metadata->decoder) { |
| 103 | BT_COMP_LOGE("Cannot create metadata decoder object."); |
| 104 | ret = -1; |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | ret = ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder, file->fp); |
| 109 | if (ret) { |
| 110 | BT_COMP_LOGE("Cannot update metadata decoder's content."); |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | ctf_fs_trace->metadata->trace_class = |
| 115 | ctf_metadata_decoder_get_ir_trace_class(ctf_fs_trace->metadata->decoder); |
| 116 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
| 117 | ctf_fs_trace->metadata->tc = |
| 118 | ctf_metadata_decoder_borrow_ctf_trace_class(ctf_fs_trace->metadata->decoder); |
| 119 | BT_ASSERT(ctf_fs_trace->metadata->tc); |
| 120 | |
| 121 | end: |
| 122 | ctf_fs_file_destroy(file); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | int ctf_fs_metadata_init(struct ctf_fs_metadata *) |
| 127 | { |
| 128 | /* Nothing to initialize for the moment. */ |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
| 133 | { |
| 134 | free(metadata->text); |
| 135 | |
| 136 | if (metadata->trace_class) { |
| 137 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class); |
| 138 | } |
| 139 | |
| 140 | if (metadata->decoder) { |
| 141 | ctf_metadata_decoder_destroy(metadata->decoder); |
| 142 | } |
| 143 | } |