clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / common / index / index.cpp
CommitLineData
309167d2 1/*
ab5be9fa
MJ
2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
309167d2 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
309167d2 7 *
309167d2
JD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
28ab034a 11#include "index.hpp"
309167d2 12
c9e313bc 13#include <common/common.hpp>
c9e313bc 14#include <common/compat/endian.hpp>
28ab034a 15#include <common/defaults.hpp>
c9e313bc 16#include <common/utils.hpp>
309167d2 17
28ab034a 18#include <lttng/constant.h>
309167d2 19
28ab034a
JG
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#define WRITE_FILE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC)
25#define READ_ONLY_FILE_FLAGS O_RDONLY
ebb29c10 26
28ab034a
JG
27static enum lttng_trace_chunk_status
28_lttng_index_file_create_from_trace_chunk(struct lttng_trace_chunk *chunk,
29 const char *channel_path,
30 const char *stream_name,
31 uint64_t stream_file_size,
32 uint64_t stream_file_index,
33 uint32_t index_major,
34 uint32_t index_minor,
35 bool unlink_existing_file,
36 int flags,
37 bool expect_no_file,
38 struct lttng_index_file **file)
d2956687
JG
39{
40 struct lttng_index_file *index_file;
41 enum lttng_trace_chunk_status chunk_status;
8bb66c3c 42 int ret;
cd9adb8b 43 struct fs_handle *fs_handle = nullptr;
d2956687
JG
44 ssize_t size_ret;
45 struct ctf_packet_index_file_hdr hdr;
46 char index_directory_path[LTTNG_PATH_MAX];
47 char index_file_path[LTTNG_PATH_MAX];
d2956687 48 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
ebb29c10 49 const bool acquired_reference = lttng_trace_chunk_get(chunk);
cc280027 50 const char *separator;
c35f9726 51
a0377dfe 52 LTTNG_ASSERT(acquired_reference);
d2956687 53
64803277 54 index_file = zmalloc<lttng_index_file>();
d2956687
JG
55 if (!index_file) {
56 PERROR("Failed to allocate lttng_index_file");
3ff5c5db 57 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
58 goto error;
59 }
60
c35f9726 61 index_file->trace_chunk = chunk;
cc280027
MD
62 if (channel_path[0] == '\0') {
63 separator = "";
64 } else {
65 separator = "/";
66 }
28ab034a
JG
67 ret = snprintf(index_directory_path,
68 sizeof(index_directory_path),
69 "%s%s" DEFAULT_INDEX_DIR,
70 channel_path,
71 separator);
d2956687
JG
72 if (ret < 0 || ret >= sizeof(index_directory_path)) {
73 ERR("Failed to format index directory path");
3ff5c5db 74 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
75 goto error;
76 }
77
28ab034a
JG
78 ret = utils_stream_file_path(index_directory_path,
79 stream_name,
80 stream_file_size,
81 stream_file_index,
82 DEFAULT_INDEX_FILE_SUFFIX,
83 index_file_path,
84 sizeof(index_file_path));
d2956687 85 if (ret) {
3ff5c5db 86 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
d2956687
JG
87 goto error;
88 }
89
90 if (unlink_existing_file) {
91 /*
92 * For tracefile rotation. We need to unlink the old
93 * file if present to synchronize with the tail of the
94 * live viewer which could be working on this same file.
95 * By doing so, any reference to the old index file
96 * stays valid even if we re-create a new file with the
97 * same name afterwards.
98 */
7fe0498a 99 chunk_status = (lttng_trace_chunk_status) lttng_trace_chunk_unlink_file(
28ab034a 100 chunk, index_file_path);
348a81dc 101 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK &&
28ab034a 102 !(chunk_status == LTTNG_TRACE_CHUNK_STATUS_ERROR && errno == ENOENT)) {
d2956687
JG
103 goto error;
104 }
348a81dc 105 }
d2956687 106
28ab034a
JG
107 chunk_status = lttng_trace_chunk_open_fs_handle(
108 chunk, index_file_path, flags, mode, &fs_handle, expect_no_file);
d2956687
JG
109 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
110 goto error;
111 }
112
84546278
MD
113 if (flags == WRITE_FILE_FLAGS) {
114 ctf_packet_index_file_hdr_init(&hdr, index_major, index_minor);
8bb66c3c 115 size_ret = fs_handle_write(fs_handle, &hdr, sizeof(hdr));
84546278
MD
116 if (size_ret < sizeof(hdr)) {
117 PERROR("Failed to write index header");
3ff5c5db 118 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
119 goto error;
120 }
121 index_file->element_len = ctf_packet_index_len(index_major, index_minor);
122 } else {
123 uint32_t element_len;
124
8bb66c3c 125 size_ret = fs_handle_read(fs_handle, &hdr, sizeof(hdr));
84546278
MD
126 if (size_ret < 0) {
127 PERROR("Failed to read index header");
3ff5c5db 128 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
129 goto error;
130 }
131 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
132 ERR("Invalid header magic");
3ff5c5db 133 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
134 goto error;
135 }
136 if (index_major != be32toh(hdr.index_major)) {
137 ERR("Index major number mismatch: %u, expect %u",
28ab034a
JG
138 be32toh(hdr.index_major),
139 index_major);
3ff5c5db 140 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
141 goto error;
142 }
143 if (index_minor != be32toh(hdr.index_minor)) {
144 ERR("Index minor number mismatch: %u, expect %u",
28ab034a
JG
145 be32toh(hdr.index_minor),
146 index_minor);
3ff5c5db 147 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
148 goto error;
149 }
150 element_len = be32toh(hdr.packet_index_len);
151 if (element_len > sizeof(struct ctf_packet_index)) {
152 ERR("Index element length too long");
3ff5c5db 153 chunk_status = LTTNG_TRACE_CHUNK_STATUS_ERROR;
84546278
MD
154 goto error;
155 }
156 index_file->element_len = element_len;
d2956687 157 }
8bb66c3c 158 index_file->file = fs_handle;
d2956687
JG
159 index_file->major = index_major;
160 index_file->minor = index_minor;
d2956687
JG
161 urcu_ref_init(&index_file->ref);
162
3ff5c5db
MD
163 *file = index_file;
164 return LTTNG_TRACE_CHUNK_STATUS_OK;
d2956687
JG
165
166error:
8bb66c3c
JG
167 if (fs_handle) {
168 ret = fs_handle_close(fs_handle);
d2956687
JG
169 if (ret < 0) {
170 PERROR("Failed to close file descriptor of index file");
171 }
172 }
307db950 173 lttng_trace_chunk_put(chunk);
d2956687 174 free(index_file);
3ff5c5db 175 return chunk_status;
d2956687
JG
176}
177
28ab034a
JG
178enum lttng_trace_chunk_status
179lttng_index_file_create_from_trace_chunk(struct lttng_trace_chunk *chunk,
180 const char *channel_path,
181 const char *stream_name,
182 uint64_t stream_file_size,
183 uint64_t stream_file_index,
184 uint32_t index_major,
185 uint32_t index_minor,
186 bool unlink_existing_file,
187 struct lttng_index_file **file)
ebb29c10 188{
28ab034a
JG
189 return _lttng_index_file_create_from_trace_chunk(chunk,
190 channel_path,
191 stream_name,
192 stream_file_size,
193 stream_file_index,
194 index_major,
195 index_minor,
196 unlink_existing_file,
197 WRITE_FILE_FLAGS,
198 false,
199 file);
ebb29c10
JG
200}
201
28ab034a
JG
202enum lttng_trace_chunk_status
203lttng_index_file_create_from_trace_chunk_read_only(struct lttng_trace_chunk *chunk,
204 const char *channel_path,
205 const char *stream_name,
206 uint64_t stream_file_size,
207 uint64_t stream_file_index,
208 uint32_t index_major,
209 uint32_t index_minor,
210 bool expect_no_file,
211 struct lttng_index_file **file)
ebb29c10 212{
28ab034a
JG
213 return _lttng_index_file_create_from_trace_chunk(chunk,
214 channel_path,
215 stream_name,
216 stream_file_size,
217 stream_file_index,
218 index_major,
219 index_minor,
220 false,
221 READ_ONLY_FILE_FLAGS,
222 expect_no_file,
223 file);
ebb29c10
JG
224}
225
309167d2 226/*
f8f3885c 227 * Write index values to the given index file.
309167d2 228 *
f8f3885c 229 * Return 0 on success, -1 on error.
309167d2 230 */
f8f3885c 231int lttng_index_file_write(const struct lttng_index_file *index_file,
28ab034a 232 const struct ctf_packet_index *element)
309167d2 233{
6cd525e8 234 ssize_t ret;
28ab034a
JG
235 const size_t len = index_file->element_len;
236 ;
309167d2 237
a0377dfe
FD
238 LTTNG_ASSERT(index_file);
239 LTTNG_ASSERT(element);
309167d2 240
8bb66c3c 241 if (!index_file->file) {
183f6fa2
DG
242 goto error;
243 }
244
8bb66c3c 245 ret = fs_handle_write(index_file->file, element, len);
6cd525e8 246 if (ret < len) {
309167d2 247 PERROR("writing index file");
f8f3885c
MD
248 goto error;
249 }
250 return 0;
251
252error:
253 return -1;
254}
255
256/*
257 * Read index values from the given index file.
258 *
259 * Return 0 on success, -1 on error.
260 */
261int lttng_index_file_read(const struct lttng_index_file *index_file,
28ab034a 262 struct ctf_packet_index *element)
f8f3885c
MD
263{
264 ssize_t ret;
8bb66c3c 265 const size_t len = index_file->element_len;
f8f3885c 266
a0377dfe 267 LTTNG_ASSERT(element);
f8f3885c 268
8bb66c3c 269 if (!index_file->file) {
f8f3885c
MD
270 goto error;
271 }
272
8bb66c3c 273 ret = fs_handle_read(index_file->file, element, len);
2239b15a 274 if (ret < 0) {
f8f3885c
MD
275 PERROR("read index file");
276 goto error;
309167d2 277 }
2239b15a
MD
278 if (ret < len) {
279 ERR("lttng_read expected %zu, returned %zd", len, ret);
280 goto error;
281 }
f8f3885c 282 return 0;
309167d2 283
183f6fa2 284error:
f8f3885c 285 return -1;
309167d2 286}
2f8f53af 287
f8f3885c
MD
288void lttng_index_file_get(struct lttng_index_file *index_file)
289{
290 urcu_ref_get(&index_file->ref);
291}
292
293static void lttng_index_file_release(struct urcu_ref *ref)
294{
28ab034a 295 struct lttng_index_file *index_file = caa_container_of(ref, struct lttng_index_file, ref);
f8f3885c 296
8bb66c3c 297 if (fs_handle_close(index_file->file)) {
f8f3885c
MD
298 PERROR("close index fd");
299 }
c35f9726 300 lttng_trace_chunk_put(index_file->trace_chunk);
f8f3885c
MD
301 free(index_file);
302}
303
304void lttng_index_file_put(struct lttng_index_file *index_file)
305{
306 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 307}
This page took 0.074247 seconds and 4 git commands to generate.