lib: make public reference count functions have strict types
[babeltrace.git] / plugins / ctf / fs-src / metadata.c
CommitLineData
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
26#include <stdio.h>
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdbool.h>
8b45963b 30#include <babeltrace/assert-internal.h>
e98a2d6e 31#include <glib.h>
3d9990ac
PP
32#include <babeltrace/compat/uuid-internal.h>
33#include <babeltrace/compat/memstream-internal.h>
e0831b38 34#include <babeltrace/babeltrace.h>
e98a2d6e 35
56a1cced
JG
36#include "fs.h"
37#include "file.h"
38#include "metadata.h"
1e649dff 39#include "../common/metadata/decoder.h"
e98a2d6e 40
55314f2a
JG
41#define BT_LOG_TAG "PLUGIN-CTF-FS-METADATA-SRC"
42#include "logging.h"
43
33f93973
PP
44BT_HIDDEN
45FILE *ctf_fs_metadata_open_file(const char *trace_path)
46{
e9287513 47 GString *metadata_path;
33f93973
PP
48 FILE *fp = NULL;
49
e9287513 50 metadata_path = g_string_new(trace_path);
33f93973 51 if (!metadata_path) {
e9287513 52 goto end;
33f93973
PP
53 }
54
2c7bce29 55 g_string_append(metadata_path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
33f93973 56 fp = fopen(metadata_path->str, "rb");
33f93973 57 g_string_free(metadata_path, TRUE);
e9287513 58end:
33f93973
PP
59 return fp;
60}
61
55314f2a 62static struct ctf_fs_file *get_file(const char *trace_path)
e98a2d6e 63{
55314f2a 64 struct ctf_fs_file *file = ctf_fs_file_create();
e98a2d6e
PP
65
66 if (!file) {
67 goto error;
68 }
69
70 g_string_append(file->path, trace_path);
2c7bce29 71 g_string_append(file->path, G_DIR_SEPARATOR_S CTF_FS_METADATA_FILENAME);
e98a2d6e 72
55314f2a 73 if (ctf_fs_file_open(file, "rb")) {
e98a2d6e
PP
74 goto error;
75 }
76
77 goto end;
78
79error:
80 if (file) {
81 ctf_fs_file_destroy(file);
82 file = NULL;
83 }
84
85end:
86 return file;
87}
88
10b7a2e4 89int ctf_fs_metadata_set_trace_class(struct ctf_fs_trace *ctf_fs_trace,
53ac3428 90 struct ctf_fs_metadata_config *config)
e98a2d6e
PP
91{
92 int ret = 0;
1e649dff 93 struct ctf_fs_file *file = NULL;
53ac3428 94 struct ctf_metadata_decoder_config decoder_config = {
7d36c29a
JG
95 .clock_class_offset_s = config ? config->clock_class_offset_s : 0,
96 .clock_class_offset_ns = config ? config->clock_class_offset_ns : 0,
53ac3428 97 };
e98a2d6e 98
55314f2a 99 file = get_file(ctf_fs_trace->path->str);
e98a2d6e 100 if (!file) {
55314f2a 101 BT_LOGE("Cannot create metadata file object");
8318bbaa 102 ret = -1;
1e649dff 103 goto end;
e98a2d6e
PP
104 }
105
7b33a0e0 106 ctf_fs_trace->metadata->decoder = ctf_metadata_decoder_create(
10b7a2e4 107 config ? &decoder_config : NULL);
7b33a0e0 108 if (!ctf_fs_trace->metadata->decoder) {
55314f2a 109 BT_LOGE("Cannot create metadata decoder object");
8318bbaa 110 ret = -1;
1e649dff 111 goto end;
e98a2d6e
PP
112 }
113
7b33a0e0
PP
114 ret = ctf_metadata_decoder_decode(ctf_fs_trace->metadata->decoder,
115 file->fp);
1e649dff 116 if (ret) {
55314f2a 117 BT_LOGE("Cannot decode metadata file");
1e649dff 118 goto end;
e98a2d6e
PP
119 }
120
10b7a2e4
PP
121 ctf_fs_trace->metadata->trace_class =
122 ctf_metadata_decoder_get_ir_trace_class(
123 ctf_fs_trace->metadata->decoder);
124 BT_ASSERT(ctf_fs_trace->metadata->trace_class);
7b33a0e0
PP
125 ctf_fs_trace->metadata->tc =
126 ctf_metadata_decoder_borrow_ctf_trace_class(
127 ctf_fs_trace->metadata->decoder);
128 BT_ASSERT(ctf_fs_trace->metadata->tc);
4f1f88a6 129
e98a2d6e 130end:
1e649dff 131 ctf_fs_file_destroy(file);
4f1f88a6 132 return ret;
e98a2d6e
PP
133}
134
135int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
136{
5b29e799 137 /* Nothing to initialize for the moment. */
e98a2d6e
PP
138 return 0;
139}
140
413bc2c4 141void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
e98a2d6e
PP
142{
143 if (metadata->text) {
144 free(metadata->text);
145 }
146
10b7a2e4 147 if (metadata->trace_class) {
8c6884d9 148 BT_TRACE_CLASS_PUT_REF_AND_RESET(metadata->trace_class);
e98a2d6e 149 }
7b33a0e0
PP
150
151 if (metadata->decoder) {
152 ctf_metadata_decoder_destroy(metadata->decoder);
153 }
e98a2d6e 154}
This page took 0.042568 seconds and 4 git commands to generate.