| 1 | /* |
| 2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation |
| 4 | * |
| 5 | * Some functions are based on older functions written by Mathieu Desnoyers. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 27 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 28 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META" |
| 29 | #include "logging/comp-logging.h" |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdint.h> |
| 33 | #include <stdlib.h> |
| 34 | #include "common/assert.h" |
| 35 | #include <glib.h> |
| 36 | #include "common/uuid.h" |
| 37 | #include "compat/memstream.h" |
| 38 | #include <babeltrace2/babeltrace.h> |
| 39 | |
| 40 | #include "fs.h" |
| 41 | #include "file.h" |
| 42 | #include "metadata.h" |
| 43 | #include "../common/metadata/decoder.h" |
| 44 | |
| 45 | BT_HIDDEN |
| 46 | FILE *ctf_fs_metadata_open_file(const char *trace_path) |
| 47 | { |
| 48 | GString *metadata_path; |
| 49 | FILE *fp = NULL; |
| 50 | |
| 51 | metadata_path = g_string_new(trace_path); |
| 52 | if (!metadata_path) { |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
| 57 | fp = fopen(metadata_path->str, "rb"); |
| 58 | g_string_free(metadata_path, TRUE); |
| 59 | end: |
| 60 | return fp; |
| 61 | } |
| 62 | |
| 63 | static struct ctf_fs_file *get_file(const char *trace_path, |
| 64 | bt_logging_level log_level, bt_self_component *self_comp) |
| 65 | { |
| 66 | struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp); |
| 67 | |
| 68 | if (!file) { |
| 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | g_string_append(file->path, trace_path); |
| 73 | g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
| 74 | |
| 75 | if (ctf_fs_file_open(file, "rb")) { |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | goto end; |
| 80 | |
| 81 | error: |
| 82 | if (file) { |
| 83 | ctf_fs_file_destroy(file); |
| 84 | file = NULL; |
| 85 | } |
| 86 | |
| 87 | end: |
| 88 | return file; |
| 89 | } |
| 90 | |
| 91 | BT_HIDDEN |
| 92 | int ctf_fs_metadata_set_trace_class( |
| 93 | bt_self_component *self_comp, |
| 94 | struct ctf_fs_trace *ctf_fs_trace, |
| 95 | struct ctf_fs_metadata_config *config) |
| 96 | { |
| 97 | int ret = 0; |
| 98 | struct ctf_fs_file *file = NULL; |
| 99 | struct ctf_metadata_decoder_config decoder_config = { |
| 100 | .log_level = ctf_fs_trace->log_level, |
| 101 | .self_comp = self_comp, |
| 102 | .clock_class_offset_s = config ? config->clock_class_offset_s : 0, |
| 103 | .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0, |
| 104 | .force_clock_class_origin_unix_epoch = |
| 105 | config ? config->force_clock_class_origin_unix_epoch : false, |
| 106 | .create_trace_class = true, |
| 107 | }; |
| 108 | bt_logging_level log_level = ctf_fs_trace->log_level; |
| 109 | |
| 110 | file = get_file(ctf_fs_trace->path->str, log_level, self_comp); |
| 111 | if (!file) { |
| 112 | BT_COMP_LOGE("Cannot create metadata file object."); |
| 113 | ret = -1; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create( |
| 118 | &decoder_config); |
| 119 | if (!ctf_fs_trace->metadata->decoder) { |
| 120 | BT_COMP_LOGE("Cannot create metadata decoder object."); |
| 121 | ret = -1; |
| 122 | goto end; |
| 123 | } |
| 124 | |
| 125 | ret = ctf_metadata_decoder_append_content( |
| 126 | ctf_fs_trace->metadata->decoder, file->fp); |
| 127 | if (ret) { |
| 128 | BT_COMP_LOGE("Cannot update metadata decoder's content."); |
| 129 | goto end; |
| 130 | } |
| 131 | |
| 132 | ctf_fs_trace->metadata->trace_class = |
| 133 | ctf_metadata_decoder_get_ir_trace_class( |
| 134 | ctf_fs_trace->metadata->decoder); |
| 135 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
| 136 | ctf_fs_trace->metadata->tc = |
| 137 | ctf_metadata_decoder_borrow_ctf_trace_class( |
| 138 | ctf_fs_trace->metadata->decoder); |
| 139 | BT_ASSERT(ctf_fs_trace->metadata->tc); |
| 140 | |
| 141 | end: |
| 142 | ctf_fs_file_destroy(file); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | BT_HIDDEN |
| 147 | int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata) |
| 148 | { |
| 149 | /* Nothing to initialize for the moment. */ |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | BT_HIDDEN |
| 154 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
| 155 | { |
| 156 | free(metadata->text); |
| 157 | |
| 158 | if (metadata->trace_class) { |
| 159 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class); |
| 160 | } |
| 161 | |
| 162 | if (metadata->decoder) { |
| 163 | ctf_metadata_decoder_destroy(metadata->decoder); |
| 164 | } |
| 165 | } |