Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db MJ |
4 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
5 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation | |
e98a2d6e PP |
6 | */ |
7 | ||
578e048b | 8 | #include "common/assert.h" |
4726b1ee | 9 | #include "cpp-common/bt2s/make-unique.hpp" |
e98a2d6e | 10 | |
5656cea5 | 11 | #include "../common/src/metadata/tsdl/decoder.hpp" |
087cd0f5 | 12 | #include "file.hpp" |
c802cacb | 13 | #include "fs.hpp" |
087cd0f5 | 14 | #include "metadata.hpp" |
e98a2d6e | 15 | |
0f5c5d5c | 16 | FILE *ctf_fs_metadata_open_file(const char *trace_path, const bt2c::Logger& logger) |
33f93973 | 17 | { |
4164020e SM |
18 | GString *metadata_path; |
19 | FILE *fp = NULL; | |
33f93973 | 20 | |
4164020e SM |
21 | metadata_path = g_string_new(trace_path); |
22 | if (!metadata_path) { | |
23 | goto end; | |
24 | } | |
33f93973 | 25 | |
4164020e SM |
26 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
27 | fp = fopen(metadata_path->str, "rb"); | |
0ac40cd4 | 28 | if (!fp) { |
0f5c5d5c SM |
29 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(logger, "Failed to open metadata file", ": path=\"{}\"", |
30 | metadata_path->str); | |
0ac40cd4 SM |
31 | } |
32 | ||
4164020e | 33 | g_string_free(metadata_path, TRUE); |
0ac40cd4 | 34 | |
4dd8c9bf | 35 | end: |
4164020e | 36 | return fp; |
33f93973 PP |
37 | } |
38 | ||
cee8a466 | 39 | static ctf_fs_file::UP get_file(const bt2c::CStringView trace_path, const bt2c::Logger& logger) |
e98a2d6e | 40 | { |
4726b1ee | 41 | auto file = bt2s::make_unique<ctf_fs_file>(logger); |
e98a2d6e | 42 | |
4164020e SM |
43 | if (!file) { |
44 | goto error; | |
45 | } | |
e98a2d6e | 46 | |
a39d9817 | 47 | file->path = fmt::format("{}" G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME, trace_path); |
e98a2d6e | 48 | |
b4dec6b2 | 49 | if (ctf_fs_file_open(file.get(), "rb")) { |
4164020e SM |
50 | goto error; |
51 | } | |
e98a2d6e | 52 | |
4164020e | 53 | goto end; |
e98a2d6e PP |
54 | |
55 | error: | |
b4dec6b2 | 56 | file.reset(); |
e98a2d6e PP |
57 | |
58 | end: | |
4164020e | 59 | return file; |
e98a2d6e PP |
60 | } |
61 | ||
4164020e | 62 | int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace, |
c942e7a2 | 63 | const ctf::src::ClkClsCfg& clkClsCfg) |
e98a2d6e | 64 | { |
4164020e | 65 | int ret = 0; |
0f5c5d5c | 66 | ctf_metadata_decoder_config decoder_config {ctf_fs_trace->logger}; |
4164020e | 67 | |
afb0f12b | 68 | decoder_config.self_comp = self_comp; |
c942e7a2 | 69 | decoder_config.clkClsCfg = clkClsCfg; |
afb0f12b | 70 | decoder_config.create_trace_class = true; |
4164020e | 71 | |
cee8a466 | 72 | const auto file = get_file(ctf_fs_trace->path, ctf_fs_trace->logger); |
4164020e | 73 | if (!file) { |
0f5c5d5c | 74 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata file object."); |
4164020e SM |
75 | ret = -1; |
76 | goto end; | |
77 | } | |
78 | ||
79 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(&decoder_config); | |
80 | if (!ctf_fs_trace->metadata->decoder) { | |
0f5c5d5c | 81 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata decoder object."); |
4164020e SM |
82 | ret = -1; |
83 | goto end; | |
84 | } | |
85 | ||
85a25425 SM |
86 | ret = |
87 | ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder.get(), file->fp.get()); | |
4164020e | 88 | if (ret) { |
0f5c5d5c | 89 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot update metadata decoder's content."); |
4164020e SM |
90 | goto end; |
91 | } | |
92 | ||
ded1f3d6 SM |
93 | ctf_fs_trace->metadata->trace_class = |
94 | ctf_metadata_decoder_get_ir_trace_class(ctf_fs_trace->metadata->decoder.get()); | |
4164020e | 95 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
70923576 | 96 | |
4164020e | 97 | ctf_fs_trace->metadata->tc = |
1fa280c9 | 98 | ctf_metadata_decoder_borrow_ctf_trace_class(ctf_fs_trace->metadata->decoder.get()); |
4164020e | 99 | BT_ASSERT(ctf_fs_trace->metadata->tc); |
4f1f88a6 | 100 | |
e98a2d6e | 101 | end: |
4164020e | 102 | return ret; |
e98a2d6e | 103 | } |