Update comment to match context API code
[babeltrace.git] / lib / context.c
CommitLineData
b469d2dd
JD
1/*
2 * context.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
22#include <babeltrace/babeltrace.h>
23#include <babeltrace/context.h>
6cba487f
MD
24#include <babeltrace/trace-handle.h>
25#include <babeltrace/trace-handle-internal.h>
26#include <babeltrace/trace-collection.h>
27#include <babeltrace/format.h>
b469d2dd
JD
28#include <stdlib.h>
29
6cba487f
MD
30#include <fts.h>
31#include <fcntl.h> /* For O_RDONLY */
32
33/* TODO ybrosseau: should be hidden in the CTF format */
34#include <babeltrace/ctf/types.h> /* for ctf_move_pos_slow */
35
36#include <glib.h>
37
38struct bt_context *bt_context_create(void)
b469d2dd
JD
39{
40 struct bt_context *ctx;
41
6cba487f 42 ctx = g_new0(struct bt_context, 1);
b469d2dd 43 ctx->refcount = 1;
6cba487f 44 /* Negative handle id are errors. */
842c2b97 45 ctx->last_trace_handle_id = 0;
b469d2dd 46
6cba487f
MD
47 /* Instanciate the trace handle container */
48 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
49 g_direct_equal, NULL,
50 (GDestroyNotify) bt_trace_handle_destroy);
51
52 ctx->tc = g_new0(struct trace_collection, 1);
53 init_trace_collection(ctx->tc);
54
b469d2dd 55 return ctx;
6cba487f 56}
b469d2dd 57
6cba487f
MD
58int bt_context_add_trace(struct bt_context *ctx, const char *path,
59 const char *format_str)
60{
61 struct trace_descriptor *td;
62 struct format *fmt;
63 struct bt_trace_handle *handle;
64
65 fmt = bt_lookup_format(g_quark_from_string(format_str));
66 td = fmt->open_trace(path, O_RDONLY,
67 ctf_move_pos_slow, NULL);
68 if (!td) {
69 fprintf(stdout, "[error] [Context] Cannot Open_trace of the format %s .\n\n",
70 path);
71 return 0;
72 }
73
74 /* Create an handle for the trace */
75 handle = bt_trace_handle_create(ctx);
76 if (handle < 0) {
77 fprintf(stdout, "[error] [Context] Creating trace handle %s .\n\n",
78 path);
79 return 0;
80 }
81 handle->format = fmt;
82 handle->td = td;
83 strncpy(handle->path, path, PATH_MAX);
84 handle->path[PATH_MAX - 1] = '\0';
85
86 /* Add new handle to container */
87 g_hash_table_insert(ctx->trace_handles,
88 (gpointer) (unsigned long) handle->id,
89 handle);
90 trace_collection_add(ctx->tc, td);
91 return handle->id;
b469d2dd
JD
92}
93
6cba487f
MD
94/*
95 * Unable to open toplevel: failure.
96 * Unable to open some subdirectory or file: warn and continue;
97 */
98int bt_context_add_traces(struct bt_context *ctx, const char *path,
99 const char *format_str)
b469d2dd 100{
6cba487f
MD
101 FTS *tree;
102 FTSENT *node;
103 GArray *trace_ids;
104 char lpath[PATH_MAX];
105 char * const paths[2] = { lpath, NULL };
106 int ret;
107
108 /*
109 * Need to copy path, because fts_open can change it.
110 * It is the pointer array, not the strings, that are constant.
111 */
112 strncpy(lpath, path, PATH_MAX);
113 lpath[PATH_MAX - 1] = '\0';
114
115 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
116 if (tree == NULL) {
117 fprintf(stdout, "[error] Cannot traverse \"%s\" for reading.\n\n",
118 path);
119 return -EINVAL;
120 }
121
122 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
b469d2dd 123
6cba487f
MD
124 while ((node = fts_read(tree))) {
125 int dirfd, metafd;
126
127 if (!(node->fts_info & FTS_D))
128 continue;
129
130 dirfd = open(node->fts_accpath, 0);
131 if (dirfd < 0) {
132 fprintf(stdout, "[warning] unable to open trace "
133 "directory file descriptor.\n");
134 continue;
135 }
136 metafd = openat(dirfd, "metadata", O_RDONLY);
137 if (metafd < 0) {
138 ret = close(dirfd);
139 if (ret < 0) {
140 perror("close");
141 goto error;
142 }
143 } else {
144 int trace_id;
145
146 ret = close(metafd);
147 if (ret < 0) {
148 perror("close");
149 goto error;
150 }
151 ret = close(dirfd);
152 if (ret < 0) {
153 perror("close");
154 goto error;
155 }
156
157 trace_id = bt_context_add_trace(ctx,
158 node->fts_accpath, format_str);
159 if (trace_id < 0) {
160 fprintf(stdout, "[warning] CTX opening trace \"%s\"from %s "
161 "for reading.\n\n", node->fts_accpath, path);
162 continue;
163 }
164 g_array_append_val(trace_ids, trace_id);
165 }
b469d2dd 166 }
6cba487f
MD
167
168 g_array_free(trace_ids, TRUE);
b469d2dd
JD
169 return 0;
170
6cba487f
MD
171error:
172 return ret;
b469d2dd
JD
173}
174
6cba487f 175void bt_context_remove_trace(struct bt_context *ctx, int handle_id)
b469d2dd 176{
6cba487f
MD
177 struct bt_trace_handle *handle;
178
179 handle = g_hash_table_lookup(ctx->trace_handles,
180 (gpointer) (unsigned long) handle_id);
181 assert(handle != NULL);
182
183 /* Remove from containers */
184 trace_collection_remove(ctx->tc, handle->td);
185 g_hash_table_remove(ctx->trace_handles,
186 (gpointer) (unsigned long) handle_id);
187
188 /* Close the trace */
189 handle->format->close_trace(handle->td);
190
191 /* Destory the handle */
192 bt_trace_handle_destroy(handle);
193}
194
195static
196void bt_context_destroy(struct bt_context *ctx)
197{
198 finalize_trace_collection(ctx->tc);
199
200 /* Remote all traces. The g_hash_table_destroy will call
201 * bt_trace_handle_destroy on each elements.
202 */
203 g_hash_table_destroy(ctx->trace_handles);
204
205 /* ctx->tc should always be valid */
206 assert(ctx->tc != NULL);
207 g_free(ctx->tc);
208 g_free(ctx);
b469d2dd
JD
209}
210
6cba487f 211void bt_context_get(struct bt_context *ctx)
b469d2dd 212{
6cba487f
MD
213 ctx->refcount++;
214}
b469d2dd 215
6cba487f
MD
216void bt_context_put(struct bt_context *ctx)
217{
b469d2dd
JD
218 ctx->refcount--;
219 if (ctx->refcount == 0)
6cba487f 220 bt_context_destroy(ctx);
b469d2dd 221}
This page took 0.046575 seconds and 4 git commands to generate.