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