common: index: remove redundant separator for empty channel string
[lttng-tools.git] / src / common / index / index.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25
26 #include <lttng/constant.h>
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/compat/endian.h>
30 #include <common/utils.h>
31
32 #include "index.h"
33
34 #define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
35 #define READ_ONLY_FILE_FLAGS O_RDONLY
36
37 static struct lttng_index_file *_lttng_index_file_create_from_trace_chunk(
38 struct lttng_trace_chunk *chunk,
39 const char *channel_path, const char *stream_name,
40 uint64_t stream_file_size, uint64_t stream_file_index,
41 uint32_t index_major, uint32_t index_minor,
42 bool unlink_existing_file,
43 int flags)
44 {
45 struct lttng_index_file *index_file;
46 enum lttng_trace_chunk_status chunk_status;
47 int ret, fd = -1;
48 ssize_t size_ret;
49 struct ctf_packet_index_file_hdr hdr;
50 char index_directory_path[LTTNG_PATH_MAX];
51 char index_file_path[LTTNG_PATH_MAX];
52 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
53 const bool acquired_reference = lttng_trace_chunk_get(chunk);
54 const char *separator;
55
56 assert(acquired_reference);
57
58 index_file = zmalloc(sizeof(*index_file));
59 if (!index_file) {
60 PERROR("Failed to allocate lttng_index_file");
61 goto error;
62 }
63
64 index_file->trace_chunk = chunk;
65 if (channel_path[0] == '\0') {
66 separator = "";
67 } else {
68 separator = "/";
69 }
70 ret = snprintf(index_directory_path, sizeof(index_directory_path),
71 "%s%s" DEFAULT_INDEX_DIR, channel_path, separator);
72 if (ret < 0 || ret >= sizeof(index_directory_path)) {
73 ERR("Failed to format index directory path");
74 goto error;
75 }
76
77 ret = utils_stream_file_path(index_directory_path, stream_name,
78 stream_file_size, stream_file_index,
79 DEFAULT_INDEX_FILE_SUFFIX,
80 index_file_path, sizeof(index_file_path));
81 if (ret) {
82 goto error;
83 }
84
85 if (unlink_existing_file) {
86 /*
87 * For tracefile rotation. We need to unlink the old
88 * file if present to synchronize with the tail of the
89 * live viewer which could be working on this same file.
90 * By doing so, any reference to the old index file
91 * stays valid even if we re-create a new file with the
92 * same name afterwards.
93 */
94 chunk_status = lttng_trace_chunk_unlink_file(
95 chunk, index_file_path);
96 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
97 !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR &&
98 errno == ENOENT)) {
99 goto error;
100 }
101 }
102
103 chunk_status = lttng_trace_chunk_open_file(chunk, index_file_path,
104 flags, mode, &fd);
105 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
106 goto error;
107 }
108
109 if (flags == WRITE_FILE_FLAGS) {
110 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
111 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
112 if (size_ret < sizeof(hdr)) {
113 PERROR("Failed to write index header");
114 goto error;
115 }
116 index_file->element_len = ctf_packet_index_len(index_major, index_minor);
117 } else {
118 uint32_t element_len;
119
120 size_ret = lttng_read(fd, &hdr, sizeof(hdr));
121 if (size_ret < 0) {
122 PERROR("Failed to read index header");
123 goto error;
124 }
125 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
126 ERR("Invalid header magic");
127 goto error;
128 }
129 if (index_major != be32toh(hdr.index_major)) {
130 ERR("Index major number mismatch: %u, expect %u",
131 be32toh(hdr.index_major), index_major);
132 goto error;
133 }
134 if (index_minor != be32toh(hdr.index_minor)) {
135 ERR("Index minor number mismatch: %u, expect %u",
136 be32toh(hdr.index_minor), index_minor);
137 goto error;
138 }
139 element_len = be32toh(hdr.packet_index_len);
140 if (element_len > sizeof(struct ctf_packet_index)) {
141 ERR("Index element length too long");
142 goto error;
143 }
144 index_file->element_len = element_len;
145 }
146 index_file->fd = fd;
147 index_file->major = index_major;
148 index_file->minor = index_minor;
149 urcu_ref_init(&index_file->ref);
150
151 return index_file;
152
153 error:
154 if (fd >= 0) {
155 ret = close(fd);
156 if (ret < 0) {
157 PERROR("Failed to close file descriptor of index file");
158 }
159 }
160 lttng_trace_chunk_put(chunk);
161 free(index_file);
162 return NULL;
163 }
164
165 struct lttng_index_file *lttng_index_file_create_from_trace_chunk(
166 struct lttng_trace_chunk *chunk,
167 const char *channel_path, const char *stream_name,
168 uint64_t stream_file_size, uint64_t stream_file_index,
169 uint32_t index_major, uint32_t index_minor,
170 bool unlink_existing_file)
171 {
172 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
173 stream_name, stream_file_size, stream_file_index,
174 index_major, index_minor, unlink_existing_file,
175 WRITE_FILE_FLAGS);
176 }
177
178 struct lttng_index_file *lttng_index_file_create_from_trace_chunk_read_only(
179 struct lttng_trace_chunk *chunk,
180 const char *channel_path, const char *stream_name,
181 uint64_t stream_file_size, uint64_t stream_file_index,
182 uint32_t index_major, uint32_t index_minor)
183 {
184 return _lttng_index_file_create_from_trace_chunk(chunk, channel_path,
185 stream_name, stream_file_size, stream_file_index,
186 index_major, index_minor, false,
187 READ_ONLY_FILE_FLAGS);
188 }
189
190 /*
191 * Write index values to the given index file.
192 *
193 * Return 0 on success, -1 on error.
194 */
195 int lttng_index_file_write(const struct lttng_index_file *index_file,
196 const struct ctf_packet_index *element)
197 {
198 int fd;
199 size_t len;
200 ssize_t ret;
201
202 assert(index_file);
203 assert(element);
204
205 fd = index_file->fd;
206 len = index_file->element_len;
207
208 if (fd < 0) {
209 goto error;
210 }
211
212 ret = lttng_write(fd, element, len);
213 if (ret < len) {
214 PERROR("writing index file");
215 goto error;
216 }
217 return 0;
218
219 error:
220 return -1;
221 }
222
223 /*
224 * Read index values from the given index file.
225 *
226 * Return 0 on success, -1 on error.
227 */
228 int lttng_index_file_read(const struct lttng_index_file *index_file,
229 struct ctf_packet_index *element)
230 {
231 ssize_t ret;
232 int fd = index_file->fd;
233 size_t len = index_file->element_len;
234
235 assert(element);
236
237 if (fd < 0) {
238 goto error;
239 }
240
241 ret = lttng_read(fd, element, len);
242 if (ret < 0) {
243 PERROR("read index file");
244 goto error;
245 }
246 if (ret < len) {
247 ERR("lttng_read expected %zu, returned %zd", len, ret);
248 goto error;
249 }
250 return 0;
251
252 error:
253 return -1;
254 }
255
256 void lttng_index_file_get(struct lttng_index_file *index_file)
257 {
258 urcu_ref_get(&index_file->ref);
259 }
260
261 static void lttng_index_file_release(struct urcu_ref *ref)
262 {
263 struct lttng_index_file *index_file = caa_container_of(ref,
264 struct lttng_index_file, ref);
265
266 if (close(index_file->fd)) {
267 PERROR("close index fd");
268 }
269 lttng_trace_chunk_put(index_file->trace_chunk);
270 free(index_file);
271 }
272
273 void lttng_index_file_put(struct lttng_index_file *index_file)
274 {
275 urcu_ref_put(&index_file->ref, lttng_index_file_release);
276 }
This page took 0.051529 seconds and 4 git commands to generate.