Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
3 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation | |
4 | * | |
5 | * Some functions are based on older functions written by Mathieu Desnoyers. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
f67894f9 | 26 | #define BT_COMP_LOG_SELF_COMP self_comp |
3d731ea9 PP |
27 | #define BT_LOG_OUTPUT_LEVEL log_level |
28 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META" | |
3fa1b6a3 | 29 | #include "logging/comp-logging.h" |
3d731ea9 | 30 | |
e98a2d6e PP |
31 | #include <stdio.h> |
32 | #include <stdint.h> | |
33 | #include <stdlib.h> | |
34 | #include <stdbool.h> | |
57952005 | 35 | #include "common/assert.h" |
e98a2d6e | 36 | #include <glib.h> |
d126826c | 37 | #include "common/uuid.h" |
57952005 | 38 | #include "compat/memstream.h" |
71c5da58 | 39 | #include <babeltrace2/babeltrace.h> |
e98a2d6e | 40 | |
56a1cced JG |
41 | #include "fs.h" |
42 | #include "file.h" | |
43 | #include "metadata.h" | |
1e649dff | 44 | #include "../common/metadata/decoder.h" |
e98a2d6e | 45 | |
33f93973 PP |
46 | BT_HIDDEN |
47 | FILE *ctf_fs_metadata_open_file(const char *trace_path) | |
48 | { | |
e9287513 | 49 | GString *metadata_path; |
33f93973 PP |
50 | FILE *fp = NULL; |
51 | ||
e9287513 | 52 | metadata_path = g_string_new(trace_path); |
33f93973 | 53 | if (!metadata_path) { |
e9287513 | 54 | goto end; |
33f93973 PP |
55 | } |
56 | ||
2c7bce29 | 57 | g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
33f93973 | 58 | fp = fopen(metadata_path->str, "rb"); |
33f93973 | 59 | g_string_free(metadata_path, TRUE); |
e9287513 | 60 | end: |
33f93973 PP |
61 | return fp; |
62 | } | |
63 | ||
3d731ea9 | 64 | static struct ctf_fs_file *get_file(const char *trace_path, |
f67894f9 | 65 | bt_logging_level log_level, bt_self_component *self_comp) |
e98a2d6e | 66 | { |
f67894f9 | 67 | struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp); |
e98a2d6e PP |
68 | |
69 | if (!file) { | |
70 | goto error; | |
71 | } | |
72 | ||
73 | g_string_append(file->path, trace_path); | |
2c7bce29 | 74 | g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME); |
e98a2d6e | 75 | |
55314f2a | 76 | if (ctf_fs_file_open(file, "rb")) { |
e98a2d6e PP |
77 | goto error; |
78 | } | |
79 | ||
80 | goto end; | |
81 | ||
82 | error: | |
83 | if (file) { | |
84 | ctf_fs_file_destroy(file); | |
85 | file = NULL; | |
86 | } | |
87 | ||
88 | end: | |
89 | return file; | |
90 | } | |
91 | ||
e5e71bf8 PP |
92 | BT_HIDDEN |
93 | int ctf_fs_metadata_set_trace_class( | |
f67894f9 | 94 | bt_self_component *self_comp, |
e5e71bf8 | 95 | struct ctf_fs_trace *ctf_fs_trace, |
53ac3428 | 96 | struct ctf_fs_metadata_config *config) |
e98a2d6e PP |
97 | { |
98 | int ret = 0; | |
1e649dff | 99 | struct ctf_fs_file *file = NULL; |
53ac3428 | 100 | struct ctf_metadata_decoder_config decoder_config = { |
3d731ea9 | 101 | .log_level = ctf_fs_trace->log_level, |
f67894f9 | 102 | .self_comp = self_comp, |
7d36c29a JG |
103 | .clock_class_offset_s = config ? config->clock_class_offset_s : 0, |
104 | .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0, | |
1ffc68e8 FD |
105 | .force_clock_class_origin_unix_epoch = |
106 | config ? config->force_clock_class_origin_unix_epoch : false, | |
526c3a49 | 107 | .create_trace_class = true, |
53ac3428 | 108 | }; |
3d731ea9 | 109 | bt_logging_level log_level = ctf_fs_trace->log_level; |
e98a2d6e | 110 | |
f67894f9 | 111 | file = get_file(ctf_fs_trace->path->str, log_level, self_comp); |
e98a2d6e | 112 | if (!file) { |
526c3a49 | 113 | BT_COMP_LOGE("Cannot create metadata file object."); |
8318bbaa | 114 | ret = -1; |
1e649dff | 115 | goto end; |
e98a2d6e PP |
116 | } |
117 | ||
16699f85 PP |
118 | ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create( |
119 | &decoder_config); | |
7b33a0e0 | 120 | if (!ctf_fs_trace->metadata->decoder) { |
526c3a49 | 121 | BT_COMP_LOGE("Cannot create metadata decoder object."); |
8318bbaa | 122 | ret = -1; |
1e649dff | 123 | goto end; |
e98a2d6e PP |
124 | } |
125 | ||
526c3a49 PP |
126 | ret = ctf_metadata_decoder_append_content( |
127 | ctf_fs_trace->metadata->decoder, file->fp); | |
1e649dff | 128 | if (ret) { |
526c3a49 | 129 | BT_COMP_LOGE("Cannot update metadata decoder's content."); |
1e649dff | 130 | goto end; |
e98a2d6e PP |
131 | } |
132 | ||
10b7a2e4 PP |
133 | ctf_fs_trace->metadata->trace_class = |
134 | ctf_metadata_decoder_get_ir_trace_class( | |
135 | ctf_fs_trace->metadata->decoder); | |
e5e71bf8 | 136 | BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class); |
7b33a0e0 PP |
137 | ctf_fs_trace->metadata->tc = |
138 | ctf_metadata_decoder_borrow_ctf_trace_class( | |
139 | ctf_fs_trace->metadata->decoder); | |
140 | BT_ASSERT(ctf_fs_trace->metadata->tc); | |
4f1f88a6 | 141 | |
e98a2d6e | 142 | end: |
1e649dff | 143 | ctf_fs_file_destroy(file); |
4f1f88a6 | 144 | return ret; |
e98a2d6e PP |
145 | } |
146 | ||
e5e71bf8 | 147 | BT_HIDDEN |
e98a2d6e PP |
148 | int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata) |
149 | { | |
5b29e799 | 150 | /* Nothing to initialize for the moment. */ |
e98a2d6e PP |
151 | return 0; |
152 | } | |
153 | ||
e5e71bf8 | 154 | BT_HIDDEN |
413bc2c4 | 155 | void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata) |
e98a2d6e | 156 | { |
b17fd38d | 157 | free(metadata->text); |
e98a2d6e | 158 | |
10b7a2e4 | 159 | if (metadata->trace_class) { |
8c6884d9 | 160 | BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class); |
e98a2d6e | 161 | } |
7b33a0e0 PP |
162 | |
163 | if (metadata->decoder) { | |
164 | ctf_metadata_decoder_destroy(metadata->decoder); | |
165 | } | |
e98a2d6e | 166 | } |