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 |
98903a3e PP |
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 | |
56a1cced JG |
22 | #include "fs.h" |
23 | #include "file.h" | |
24 | #include "metadata.h" | |
1e649dff | 25 | #include "../common/metadata/decoder.h" |
e98a2d6e | 26 | |
33f93973 PP |
27 | BT_HIDDEN |
28 | FILE *ctf_fs_metadata_open_file(const char *trace_path) | |
29 | { | |
4dd8c9bf | 30 | GString *metadata_path; |
33f93973 PP |
31 | FILE *fp = NULL; |
32 | ||
4dd8c9bf | 33 | metadata_path = g_string_new(trace_path); |
33f93973 | 34 | if (!metadata_path) { |
4dd8c9bf | 35 | goto end; |
33f93973 PP |
36 | } |
37 | ||
3743a302 | 38 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
33f93973 | 39 | fp = fopen(metadata_path->str, "rb"); |
33f93973 | 40 | g_string_free(metadata_path, TRUE); |
4dd8c9bf | 41 | end: |
33f93973 PP |
42 | return fp; |
43 | } | |
44 | ||
98903a3e | 45 | static struct ctf_fs_file *get_file(const char *trace_path, |
4c65a157 | 46 | bt_logging_level log_level, bt_self_component *self_comp) |
e98a2d6e | 47 | { |
4c65a157 | 48 | struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp); |
e98a2d6e PP |
49 | |
50 | if (!file) { | |
51 | goto error; | |
52 | } | |
53 | ||
54 | g_string_append(file->path, trace_path); | |
3743a302 | 55 | g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
e98a2d6e | 56 | |
55314f2a | 57 | if (ctf_fs_file_open(file, "rb")) { |
e98a2d6e PP |
58 | goto error; |
59 | } | |
60 | ||
61 | goto end; | |
62 | ||
63 | error: | |
64 | if (file) { | |
65 | ctf_fs_file_destroy(file); | |
66 | file = NULL; | |
67 | } | |
68 | ||
69 | end: | |
70 | return file; | |
71 | } | |
72 | ||
41693723 PP |
73 | BT_HIDDEN |
74 | int ctf_fs_metadata_set_trace_class( | |
4c65a157 | 75 | bt_self_component *self_comp, |
41693723 | 76 | struct ctf_fs_trace *ctf_fs_trace, |
a2a54545 | 77 | struct ctf_fs_metadata_config *config) |
e98a2d6e PP |
78 | { |
79 | int ret = 0; | |
1e649dff | 80 | struct ctf_fs_file *file = NULL; |
a2a54545 | 81 | struct ctf_metadata_decoder_config decoder_config = { |
98903a3e | 82 | .log_level = ctf_fs_trace->log_level, |
4c65a157 | 83 | .self_comp = self_comp, |
297941e3 JG |
84 | .clock_class_offset_s = config ? config->clock_class_offset_s : 0, |
85 | .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0, | |
c0aa240b FD |
86 | .force_clock_class_origin_unix_epoch = |
87 | config ? config->force_clock_class_origin_unix_epoch : false, | |
06be9946 | 88 | .create_trace_class = true, |
a2a54545 | 89 | }; |
98903a3e | 90 | bt_logging_level log_level = ctf_fs_trace->log_level; |
e98a2d6e | 91 | |
4c65a157 | 92 | file = get_file(ctf_fs_trace->path->str, log_level, self_comp); |
e98a2d6e | 93 | if (!file) { |
06be9946 | 94 | BT_COMP_LOGE("Cannot create metadata file object."); |
8318bbaa | 95 | ret = -1; |
1e649dff | 96 | goto end; |
e98a2d6e PP |
97 | } |
98 | ||
f7b785ac PP |
99 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create( |
100 | &decoder_config); | |
44c440bc | 101 | if (!ctf_fs_trace->metadata->decoder) { |
06be9946 | 102 | BT_COMP_LOGE("Cannot create metadata decoder object."); |
8318bbaa | 103 | ret = -1; |
1e649dff | 104 | goto end; |
e98a2d6e PP |
105 | } |
106 | ||
06be9946 PP |
107 | ret = ctf_metadata_decoder_append_content( |
108 | ctf_fs_trace->metadata->decoder, file->fp); | |
1e649dff | 109 | if (ret) { |
06be9946 | 110 | BT_COMP_LOGE("Cannot update metadata decoder's content."); |
1e649dff | 111 | goto end; |
e98a2d6e PP |
112 | } |
113 | ||
862ca4ed PP |
114 | ctf_fs_trace->metadata->trace_class = |
115 | ctf_metadata_decoder_get_ir_trace_class( | |
116 | ctf_fs_trace->metadata->decoder); | |
41693723 | 117 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
44c440bc PP |
118 | ctf_fs_trace->metadata->tc = |
119 | ctf_metadata_decoder_borrow_ctf_trace_class( | |
120 | ctf_fs_trace->metadata->decoder); | |
121 | BT_ASSERT(ctf_fs_trace->metadata->tc); | |
4f1f88a6 | 122 | |
e98a2d6e | 123 | end: |
1e649dff | 124 | ctf_fs_file_destroy(file); |
4f1f88a6 | 125 | return ret; |
e98a2d6e PP |
126 | } |
127 | ||
41693723 | 128 | BT_HIDDEN |
e98a2d6e PP |
129 | int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata) |
130 | { | |
5b29e799 | 131 | /* Nothing to initialize for the moment. */ |
e98a2d6e PP |
132 | return 0; |
133 | } | |
134 | ||
41693723 | 135 | BT_HIDDEN |
413bc2c4 | 136 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
e98a2d6e | 137 | { |
6b934a94 | 138 | free(metadata->text); |
e98a2d6e | 139 | |
862ca4ed | 140 | if (metadata->trace_class) { |
c5b9b441 | 141 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class); |
e98a2d6e | 142 | } |
44c440bc PP |
143 | |
144 | if (metadata->decoder) { | |
145 | ctf_metadata_decoder_destroy(metadata->decoder); | |
146 | } | |
e98a2d6e | 147 | } |