| 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 | #include "common/assert.h" |
| 9 | |
| 10 | #include "../common/src/metadata/tsdl/decoder.hpp" |
| 11 | #include "file.hpp" |
| 12 | #include "fs.hpp" |
| 13 | #include "metadata.hpp" |
| 14 | |
| 15 | FILE *ctf_fs_metadata_open_file(const char *trace_path, const bt2c::Logger& logger) |
| 16 | { |
| 17 | GString *metadata_path; |
| 18 | FILE *fp = NULL; |
| 19 | |
| 20 | metadata_path = g_string_new(trace_path); |
| 21 | if (!metadata_path) { |
| 22 | goto end; |
| 23 | } |
| 24 | |
| 25 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
| 26 | fp = fopen(metadata_path->str, "rb"); |
| 27 | if (!fp) { |
| 28 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(logger, "Failed to open metadata file", ": path=\"{}\"", |
| 29 | metadata_path->str); |
| 30 | } |
| 31 | |
| 32 | g_string_free(metadata_path, TRUE); |
| 33 | |
| 34 | end: |
| 35 | return fp; |
| 36 | } |
| 37 | |
| 38 | static ctf_fs_file::UP get_file(const bt2c::CStringView trace_path, const bt2c::Logger& logger) |
| 39 | { |
| 40 | auto file = ctf_fs_file_create(logger); |
| 41 | |
| 42 | if (!file) { |
| 43 | goto error; |
| 44 | } |
| 45 | |
| 46 | file->path = fmt::format("{}" G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME, trace_path); |
| 47 | |
| 48 | if (ctf_fs_file_open(file.get(), "rb")) { |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | goto end; |
| 53 | |
| 54 | error: |
| 55 | file.reset(); |
| 56 | |
| 57 | end: |
| 58 | return file; |
| 59 | } |
| 60 | |
| 61 | int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace, |
| 62 | const ctf::src::ClkClsCfg& clkClsCfg) |
| 63 | { |
| 64 | int ret = 0; |
| 65 | ctf_metadata_decoder_config decoder_config {ctf_fs_trace->logger}; |
| 66 | |
| 67 | decoder_config.self_comp = self_comp; |
| 68 | decoder_config.clkClsCfg = clkClsCfg; |
| 69 | decoder_config.create_trace_class = true; |
| 70 | |
| 71 | const auto file = get_file(ctf_fs_trace->path, ctf_fs_trace->logger); |
| 72 | if (!file) { |
| 73 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata file object."); |
| 74 | ret = -1; |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(&decoder_config); |
| 79 | if (!ctf_fs_trace->metadata->decoder) { |
| 80 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata decoder object."); |
| 81 | ret = -1; |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | ret = |
| 86 | ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder.get(), file->fp.get()); |
| 87 | if (ret) { |
| 88 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot update metadata decoder's content."); |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | ctf_fs_trace->metadata->trace_class = |
| 93 | ctf_metadata_decoder_get_ir_trace_class(ctf_fs_trace->metadata->decoder.get()); |
| 94 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
| 95 | |
| 96 | ctf_fs_trace->metadata->tc = |
| 97 | ctf_metadata_decoder_borrow_ctf_trace_class(ctf_fs_trace->metadata->decoder.get()); |
| 98 | BT_ASSERT(ctf_fs_trace->metadata->tc); |
| 99 | |
| 100 | end: |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int ctf_fs_metadata_init(struct ctf_fs_metadata *) |
| 105 | { |
| 106 | /* Nothing to initialize for the moment. */ |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
| 111 | { |
| 112 | metadata->decoder.reset(); |
| 113 | } |