Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / fs-src / metadata.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
6 */
7
8 #define BT_COMP_LOG_SELF_COMP self_comp
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/META"
11 #include "logging/comp-logging.h"
12
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include "common/assert.h"
17 #include <glib.h>
18 #include "common/uuid.h"
19 #include "compat/memstream.h"
20 #include <babeltrace2/babeltrace.h>
21
22 #include "fs.h"
23 #include "file.h"
24 #include "metadata.h"
25 #include "../common/metadata/decoder.h"
26
27 BT_HIDDEN
28 FILE *ctf_fs_metadata_open_file(const char *trace_path)
29 {
30 GString *metadata_path;
31 FILE *fp = NULL;
32
33 metadata_path = g_string_new(trace_path);
34 if (!metadata_path) {
35 goto end;
36 }
37
38 g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
39 fp = fopen(metadata_path->str, "rb");
40 g_string_free(metadata_path, TRUE);
41 end:
42 return fp;
43 }
44
45 static struct ctf_fs_file *get_file(const char *trace_path,
46 bt_logging_level log_level, bt_self_component *self_comp)
47 {
48 struct ctf_fs_file *file = ctf_fs_file_create(log_level, self_comp);
49
50 if (!file) {
51 goto error;
52 }
53
54 g_string_append(file->path, trace_path);
55 g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
56
57 if (ctf_fs_file_open(file, "rb")) {
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
73 BT_HIDDEN
74 int ctf_fs_metadata_set_trace_class(
75 bt_self_component *self_comp,
76 struct ctf_fs_trace *ctf_fs_trace,
77 struct ctf_fs_metadata_config *config)
78 {
79 int ret = 0;
80 struct ctf_fs_file *file = NULL;
81 struct ctf_metadata_decoder_config decoder_config = {
82 .log_level = ctf_fs_trace->log_level,
83 .self_comp = self_comp,
84 .clock_class_offset_s = config ? config->clock_class_offset_s : 0,
85 .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
86 .force_clock_class_origin_unix_epoch =
87 config ? config->force_clock_class_origin_unix_epoch : false,
88 .create_trace_class = true,
89 };
90 bt_logging_level log_level = ctf_fs_trace->log_level;
91
92 file = get_file(ctf_fs_trace->path->str, log_level, self_comp);
93 if (!file) {
94 BT_COMP_LOGE("Cannot create metadata file object.");
95 ret = -1;
96 goto end;
97 }
98
99 ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(
100 &decoder_config);
101 if (!ctf_fs_trace->metadata->decoder) {
102 BT_COMP_LOGE("Cannot create metadata decoder object.");
103 ret = -1;
104 goto end;
105 }
106
107 ret = ctf_metadata_decoder_append_content(
108 ctf_fs_trace->metadata->decoder, file->fp);
109 if (ret) {
110 BT_COMP_LOGE("Cannot update metadata decoder's content.");
111 goto end;
112 }
113
114 ctf_fs_trace->metadata->trace_class =
115 ctf_metadata_decoder_get_ir_trace_class(
116 ctf_fs_trace->metadata->decoder);
117 BT_ASSERT(!self_comp || ctf_fs_trace->metadata->trace_class);
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);
122
123 end:
124 ctf_fs_file_destroy(file);
125 return ret;
126 }
127
128 BT_HIDDEN
129 int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
130 {
131 /* Nothing to initialize for the moment. */
132 return 0;
133 }
134
135 BT_HIDDEN
136 void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
137 {
138 free(metadata->text);
139
140 if (metadata->trace_class) {
141 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class);
142 }
143
144 if (metadata->decoder) {
145 ctf_metadata_decoder_destroy(metadata->decoder);
146 }
147 }
This page took 0.032231 seconds and 4 git commands to generate.