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" |
e98a2d6e | 9 | |
5656cea5 | 10 | #include "../common/src/metadata/tsdl/decoder.hpp" |
087cd0f5 | 11 | #include "file.hpp" |
c802cacb | 12 | #include "fs.hpp" |
087cd0f5 | 13 | #include "metadata.hpp" |
e98a2d6e | 14 | |
0f5c5d5c | 15 | FILE *ctf_fs_metadata_open_file(const char *trace_path, const bt2c::Logger& logger) |
33f93973 | 16 | { |
4164020e SM |
17 | GString *metadata_path; |
18 | FILE *fp = NULL; | |
33f93973 | 19 | |
4164020e SM |
20 | metadata_path = g_string_new(trace_path); |
21 | if (!metadata_path) { | |
22 | goto end; | |
23 | } | |
33f93973 | 24 | |
4164020e SM |
25 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
26 | fp = fopen(metadata_path->str, "rb"); | |
0ac40cd4 | 27 | if (!fp) { |
0f5c5d5c SM |
28 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(logger, "Failed to open metadata file", ": path=\"{}\"", |
29 | metadata_path->str); | |
0ac40cd4 SM |
30 | } |
31 | ||
4164020e | 32 | g_string_free(metadata_path, TRUE); |
0ac40cd4 | 33 | |
4dd8c9bf | 34 | end: |
4164020e | 35 | return fp; |
33f93973 PP |
36 | } |
37 | ||
0f5c5d5c | 38 | static struct ctf_fs_file *get_file(const char *trace_path, const bt2c::Logger& logger) |
e98a2d6e | 39 | { |
0f5c5d5c | 40 | struct ctf_fs_file *file = ctf_fs_file_create(logger); |
e98a2d6e | 41 | |
4164020e SM |
42 | if (!file) { |
43 | goto error; | |
44 | } | |
e98a2d6e | 45 | |
4164020e SM |
46 | g_string_append(file->path, trace_path); |
47 | g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); | |
e98a2d6e | 48 | |
4164020e SM |
49 | if (ctf_fs_file_open(file, "rb")) { |
50 | goto error; | |
51 | } | |
e98a2d6e | 52 | |
4164020e | 53 | goto end; |
e98a2d6e PP |
54 | |
55 | error: | |
4164020e SM |
56 | if (file) { |
57 | ctf_fs_file_destroy(file); | |
58 | file = NULL; | |
59 | } | |
e98a2d6e PP |
60 | |
61 | end: | |
4164020e | 62 | return file; |
e98a2d6e PP |
63 | } |
64 | ||
4164020e SM |
65 | int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_trace *ctf_fs_trace, |
66 | struct ctf_fs_metadata_config *config) | |
e98a2d6e | 67 | { |
4164020e SM |
68 | int ret = 0; |
69 | struct ctf_fs_file *file = NULL; | |
0f5c5d5c | 70 | ctf_metadata_decoder_config decoder_config {ctf_fs_trace->logger}; |
4164020e | 71 | |
afb0f12b SM |
72 | decoder_config.self_comp = self_comp; |
73 | decoder_config.clock_class_offset_s = config ? config->clock_class_offset_s : 0; | |
74 | decoder_config.clock_class_offset_ns = config ? config->clock_class_offset_ns : 0; | |
4164020e | 75 | decoder_config.force_clock_class_origin_unix_epoch = |
afb0f12b SM |
76 | config ? config->force_clock_class_origin_unix_epoch : false; |
77 | decoder_config.create_trace_class = true; | |
4164020e | 78 | |
0f5c5d5c | 79 | file = get_file(ctf_fs_trace->path->str, ctf_fs_trace->logger); |
4164020e | 80 | if (!file) { |
0f5c5d5c | 81 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata file object."); |
4164020e SM |
82 | ret = -1; |
83 | goto end; | |
84 | } | |
85 | ||
86 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(&decoder_config); | |
87 | if (!ctf_fs_trace->metadata->decoder) { | |
0f5c5d5c | 88 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot create metadata decoder object."); |
4164020e SM |
89 | ret = -1; |
90 | goto end; | |
91 | } | |
92 | ||
1fa280c9 | 93 | ret = ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder.get(), file->fp); |
4164020e | 94 | if (ret) { |
0f5c5d5c | 95 | BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot update metadata decoder's content."); |
4164020e SM |
96 | goto end; |
97 | } | |
98 | ||
99 | ctf_fs_trace->metadata->trace_class = | |
1fa280c9 | 100 | ctf_metadata_decoder_get_ir_trace_class(ctf_fs_trace->metadata->decoder.get()); |
4164020e SM |
101 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
102 | ctf_fs_trace->metadata->tc = | |
1fa280c9 | 103 | ctf_metadata_decoder_borrow_ctf_trace_class(ctf_fs_trace->metadata->decoder.get()); |
4164020e | 104 | BT_ASSERT(ctf_fs_trace->metadata->tc); |
4f1f88a6 | 105 | |
e98a2d6e | 106 | end: |
4164020e SM |
107 | ctf_fs_file_destroy(file); |
108 | return ret; | |
e98a2d6e PP |
109 | } |
110 | ||
ecd7492f | 111 | int ctf_fs_metadata_init(struct ctf_fs_metadata *) |
e98a2d6e | 112 | { |
4164020e SM |
113 | /* Nothing to initialize for the moment. */ |
114 | return 0; | |
e98a2d6e PP |
115 | } |
116 | ||
413bc2c4 | 117 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
e98a2d6e | 118 | { |
4164020e | 119 | free(metadata->text); |
e98a2d6e | 120 | |
4164020e SM |
121 | if (metadata->trace_class) { |
122 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class); | |
123 | } | |
44c440bc | 124 | |
1fa280c9 | 125 | metadata->decoder.reset(); |
e98a2d6e | 126 | } |