| 1 | /* |
| 2 | * BabelTrace - Common Trace Format (CTF) |
| 3 | * |
| 4 | * Format registration. |
| 5 | * |
| 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/format.h> |
| 20 | #include <babeltrace/ctf/types.h> |
| 21 | #include <babeltrace/ctf/metadata.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <dirent.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <glib.h> |
| 28 | |
| 29 | struct trace_descriptor { |
| 30 | struct ctf_trace ctf_trace; |
| 31 | }; |
| 32 | |
| 33 | struct trace_descriptor *ctf_open_trace(const char *path, int flags); |
| 34 | void ctf_close_trace(struct trace_descriptor *descriptor); |
| 35 | |
| 36 | static struct format ctf_format = { |
| 37 | .uint_read = ctf_uint_read, |
| 38 | .int_read = ctf_int_read, |
| 39 | .uint_write = ctf_uint_write, |
| 40 | .int_write = ctf_int_write, |
| 41 | .double_read = ctf_double_read, |
| 42 | .double_write = ctf_double_write, |
| 43 | .float_copy = ctf_float_copy, |
| 44 | .string_copy = ctf_string_copy, |
| 45 | .string_read = ctf_string_read, |
| 46 | .string_write = ctf_string_write, |
| 47 | .string_free_temp = ctf_string_free_temp, |
| 48 | .enum_read = ctf_enum_read, |
| 49 | .enum_write = ctf_enum_write, |
| 50 | .struct_begin = ctf_struct_begin, |
| 51 | .struct_end = ctf_struct_end, |
| 52 | .variant_begin = ctf_variant_begin, |
| 53 | .variant_end = ctf_variant_end, |
| 54 | .array_begin = ctf_array_begin, |
| 55 | .array_end = ctf_array_end, |
| 56 | .sequence_begin = ctf_sequence_begin, |
| 57 | .sequence_end = ctf_sequence_end, |
| 58 | .open_trace = ctf_open_trace, |
| 59 | .close_trace = ctf_close_trace, |
| 60 | }; |
| 61 | |
| 62 | static |
| 63 | int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags) |
| 64 | { |
| 65 | int ret; |
| 66 | |
| 67 | td->ctf_trace.flags = flags; |
| 68 | |
| 69 | /* Open trace directory */ |
| 70 | td->ctf_trace.dir = opendir(path); |
| 71 | if (!td->ctf_trace.dir) { |
| 72 | fprintf(stdout, "Unable to open trace directory.\n"); |
| 73 | ret = -ENOENT; |
| 74 | goto error; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Open each stream: for each file, try to open, check magic |
| 81 | * number, and get the stream ID to add to the right location in |
| 82 | * the stream array. |
| 83 | * |
| 84 | * Keep the metadata file separate. |
| 85 | */ |
| 86 | |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Use the metadata file to populate the trace metadata. |
| 91 | */ |
| 92 | |
| 93 | |
| 94 | return 0; |
| 95 | error: |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static |
| 100 | int ctf_open_trace_write(struct trace_descriptor *td, const char *path, int flags) |
| 101 | { |
| 102 | int ret; |
| 103 | |
| 104 | ret = mkdir(path, S_IRWXU|S_IRWXG); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | struct trace_descriptor *ctf_open_trace(const char *path, int flags) |
| 112 | { |
| 113 | struct trace_descriptor *td; |
| 114 | int ret; |
| 115 | |
| 116 | td = g_new(struct trace_descriptor, 1); |
| 117 | |
| 118 | switch (flags) { |
| 119 | case O_RDONLY: |
| 120 | ret = ctf_open_trace_read(td, path, flags); |
| 121 | if (ret) |
| 122 | goto error; |
| 123 | break; |
| 124 | case O_WRONLY: |
| 125 | ret = ctf_open_trace_write(td, path, flags); |
| 126 | if (ret) |
| 127 | goto error; |
| 128 | break; |
| 129 | default: |
| 130 | fprintf(stdout, "Incorrect open flags.\n"); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | return td; |
| 135 | error: |
| 136 | g_free(td); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | void ctf_close_trace(struct trace_descriptor *td) |
| 141 | { |
| 142 | closedir(td->ctf_trace.dir); |
| 143 | g_free(td); |
| 144 | } |
| 145 | |
| 146 | void __attribute__((constructor)) ctf_init(void) |
| 147 | { |
| 148 | int ret; |
| 149 | |
| 150 | ctf_format.name = g_quark_from_static_string("ctf"); |
| 151 | ret = bt_register_format(&ctf_format); |
| 152 | assert(!ret); |
| 153 | } |
| 154 | |
| 155 | /* TODO: finalize */ |