ctf plugin: add CTF metadata decoder API
[babeltrace.git] / plugins / ctf / fs / 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>
1e649dff 30#include <assert.h>
e98a2d6e
PP
31#include <glib.h>
32#include <babeltrace/compat/uuid.h>
33#include <babeltrace/compat/memstream.h>
34
35#define PRINT_ERR_STREAM ctf_fs->error_fp
36#define PRINT_PREFIX "ctf-fs-metadata"
37#include "print.h"
38
56a1cced
JG
39#include "fs.h"
40#include "file.h"
41#include "metadata.h"
1e649dff 42#include "../common/metadata/decoder.h"
e98a2d6e 43
92540773
JD
44#define NSEC_PER_SEC 1000000000LL
45
33f93973
PP
46BT_HIDDEN
47FILE *ctf_fs_metadata_open_file(const char *trace_path)
48{
49 GString *metadata_path = g_string_new(trace_path);
50 FILE *fp = NULL;
51
52 if (!metadata_path) {
53 goto error;
54 }
55
56 g_string_append(metadata_path, "/" CTF_FS_METADATA_FILENAME);
57 fp = fopen(metadata_path->str, "rb");
58 if (!fp) {
59 goto error;
60 }
61
62 goto end;
63
64error:
65 if (fp) {
66 fclose(fp);
67 fp = NULL;
68 }
69
70end:
71 g_string_free(metadata_path, TRUE);
72 return fp;
73}
74
56a1cced 75static struct ctf_fs_file *get_file(struct ctf_fs_component *ctf_fs,
e98a2d6e
PP
76 const char *trace_path)
77{
78 struct ctf_fs_file *file = ctf_fs_file_create(ctf_fs);
79
80 if (!file) {
81 goto error;
82 }
83
84 g_string_append(file->path, trace_path);
85 g_string_append(file->path, "/" CTF_FS_METADATA_FILENAME);
86
87 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
88 goto error;
89 }
90
91 goto end;
92
93error:
94 if (file) {
95 ctf_fs_file_destroy(file);
96 file = NULL;
97 }
98
99end:
100 return file;
101}
102
4f1f88a6 103int ctf_fs_metadata_set_trace(struct ctf_fs_component *ctf_fs)
e98a2d6e
PP
104{
105 int ret = 0;
1e649dff
PP
106 struct ctf_fs_file *file = NULL;
107 struct ctf_metadata_decoder *metadata_decoder = NULL;
e98a2d6e 108
1e649dff 109 file = get_file(ctf_fs, ctf_fs->trace_path->str);
e98a2d6e
PP
110 if (!file) {
111 PERR("Cannot create metadata file object\n");
1e649dff 112 goto end;
e98a2d6e
PP
113 }
114
1e649dff 115 metadata_decoder = ctf_metadata_decoder_create(ctf_fs->error_fp,
92540773
JD
116 ctf_fs->options.clock_offset * NSEC_PER_SEC +
117 ctf_fs->options.clock_offset_ns);
1e649dff
PP
118 if (!metadata_decoder) {
119 PERR("Cannot create metadata decoder object\n");
120 goto end;
e98a2d6e
PP
121 }
122
1e649dff
PP
123 ret = ctf_metadata_decoder_decode(metadata_decoder, file->fp);
124 if (ret) {
125 PERR("Cannot decode metadata file\n");
126 goto end;
e98a2d6e
PP
127 }
128
1e649dff
PP
129 ctf_fs->metadata->trace = ctf_metadata_decoder_get_trace(
130 metadata_decoder);
131 assert(ctf_fs->metadata->trace);
4f1f88a6
PP
132 ret = -1;
133
e98a2d6e 134end:
1e649dff
PP
135 ctf_fs_file_destroy(file);
136 ctf_metadata_decoder_destroy(metadata_decoder);
4f1f88a6 137 return ret;
e98a2d6e
PP
138}
139
140int ctf_fs_metadata_init(struct ctf_fs_metadata *metadata)
141{
5b29e799 142 /* Nothing to initialize for the moment. */
e98a2d6e
PP
143 return 0;
144}
145
413bc2c4 146void ctf_fs_metadata_fini(struct ctf_fs_metadata *metadata)
e98a2d6e
PP
147{
148 if (metadata->text) {
149 free(metadata->text);
150 }
151
152 if (metadata->trace) {
153 BT_PUT(metadata->trace);
154 }
155}
This page took 0.032349 seconds and 4 git commands to generate.