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