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