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