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