Cleanup: remove duplicated code in snapshot record command
[lttng-tools.git] / src / common / index / index.c
CommitLineData
309167d2
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
f8f3885c 4 * 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
309167d2
JD
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
6c1c0768 20#define _LGPL_SOURCE
309167d2 21#include <assert.h>
1c20f0e2 22#include <sys/stat.h>
2f8f53af
DG
23#include <sys/types.h>
24#include <fcntl.h>
309167d2
JD
25
26#include <common/common.h>
27#include <common/defaults.h>
f263b7fd 28#include <common/compat/endian.h>
309167d2
JD
29#include <common/utils.h>
30
31#include "index.h"
32
33/*
34 * Create the index file associated with a trace file.
35 *
f8f3885c 36 * Return allocated struct lttng_index_file, NULL on error.
309167d2 37 */
cb523e02 38struct lttng_index_file *lttng_index_file_create(const char *path_name,
f8f3885c
MD
39 char *stream_name, int uid, int gid,
40 uint64_t size, uint64_t count, uint32_t major, uint32_t minor)
309167d2 41{
f8f3885c 42 struct lttng_index_file *index_file;
309167d2 43 int ret, fd = -1;
6cd525e8 44 ssize_t size_ret;
50adc264 45 struct ctf_packet_index_file_hdr hdr;
1c20f0e2 46 char fullpath[PATH_MAX];
f8f3885c
MD
47 uint32_t element_len = ctf_packet_index_len(major, minor);
48
49 index_file = zmalloc(sizeof(*index_file));
50 if (!index_file) {
51 PERROR("allocating lttng_index_file");
52 goto error;
53 }
309167d2 54
1c20f0e2
JD
55 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR,
56 path_name);
57 if (ret < 0) {
58 PERROR("snprintf index path");
59 goto error;
60 }
61
62 /* Create index directory if necessary. */
7591bab1 63 ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
1c20f0e2 64 if (ret < 0) {
df5b86c8 65 if (errno != EEXIST) {
6347a8b5 66 PERROR("Index trace directory creation error");
1c20f0e2
JD
67 goto error;
68 }
69 }
70
7591bab1
MD
71 /*
72 * For tracefile rotation. We need to unlink the old
73 * file if present to synchronize with the tail of the
74 * live viewer which could be working on this same file.
75 * By doing so, any reference to the old index file
76 * stays valid even if we re-create a new file with the
77 * same name afterwards.
78 */
79 ret = utils_unlink_stream_file(fullpath, stream_name, size, count, uid,
80 gid, DEFAULT_INDEX_FILE_SUFFIX);
81 if (ret < 0 && errno != ENOENT) {
82 goto error;
83 }
1c20f0e2 84 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
309167d2
JD
85 gid, DEFAULT_INDEX_FILE_SUFFIX);
86 if (ret < 0) {
87 goto error;
88 }
89 fd = ret;
90
71b4cdb4 91 ctf_packet_index_file_hdr_init(&hdr, major, minor);
6cd525e8
MD
92 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
93 if (size_ret < sizeof(hdr)) {
309167d2
JD
94 PERROR("write index header");
95 goto error;
96 }
f8f3885c
MD
97 index_file->fd = fd;
98 index_file->major = major;
99 index_file->minor = minor;
100 index_file->element_len = element_len;
101 urcu_ref_init(&index_file->ref);
309167d2 102
f8f3885c 103 return index_file;
309167d2
JD
104
105error:
106 if (fd >= 0) {
107 int close_ret;
108
109 close_ret = close(fd);
110 if (close_ret < 0) {
111 PERROR("close index fd");
112 }
113 }
f8f3885c
MD
114 free(index_file);
115 return NULL;
309167d2
JD
116}
117
118/*
f8f3885c 119 * Write index values to the given index file.
309167d2 120 *
f8f3885c 121 * Return 0 on success, -1 on error.
309167d2 122 */
f8f3885c
MD
123int lttng_index_file_write(const struct lttng_index_file *index_file,
124 const struct ctf_packet_index *element)
309167d2 125{
0b0ac4a9
JR
126 int fd;
127 size_t len;
6cd525e8 128 ssize_t ret;
309167d2 129
0b0ac4a9 130 assert(index_file);
f8f3885c 131 assert(element);
309167d2 132
0b0ac4a9
JR
133 fd = index_file->fd;
134 len = index_file->element_len;
135
183f6fa2 136 if (fd < 0) {
183f6fa2
DG
137 goto error;
138 }
139
f8f3885c 140 ret = lttng_write(fd, element, len);
6cd525e8 141 if (ret < len) {
309167d2 142 PERROR("writing index file");
f8f3885c
MD
143 goto error;
144 }
145 return 0;
146
147error:
148 return -1;
149}
150
151/*
152 * Read index values from the given index file.
153 *
154 * Return 0 on success, -1 on error.
155 */
156int lttng_index_file_read(const struct lttng_index_file *index_file,
157 struct ctf_packet_index *element)
158{
159 ssize_t ret;
160 int fd = index_file->fd;
161 size_t len = index_file->element_len;
162
163 assert(element);
164
165 if (fd < 0) {
166 goto error;
167 }
168
169 ret = lttng_read(fd, element, len);
2239b15a 170 if (ret < 0) {
f8f3885c
MD
171 PERROR("read index file");
172 goto error;
309167d2 173 }
2239b15a
MD
174 if (ret < len) {
175 ERR("lttng_read expected %zu, returned %zd", len, ret);
176 goto error;
177 }
f8f3885c 178 return 0;
309167d2 179
183f6fa2 180error:
f8f3885c 181 return -1;
309167d2 182}
2f8f53af
DG
183
184/*
185 * Open index file using a given path, channel name and tracefile count.
186 *
f8f3885c 187 * Return allocated struct lttng_index_file, NULL on error.
2f8f53af 188 */
f8f3885c
MD
189struct lttng_index_file *lttng_index_file_open(const char *path_name,
190 const char *channel_name, uint64_t tracefile_count,
191 uint64_t tracefile_count_current)
2f8f53af 192{
f8f3885c 193 struct lttng_index_file *index_file;
2f8f53af
DG
194 int ret, read_fd;
195 ssize_t read_len;
196 char fullpath[PATH_MAX];
197 struct ctf_packet_index_file_hdr hdr;
f8f3885c 198 uint32_t major, minor, element_len;
2f8f53af
DG
199
200 assert(path_name);
201 assert(channel_name);
202
f8f3885c
MD
203 index_file = zmalloc(sizeof(*index_file));
204 if (!index_file) {
205 PERROR("allocating lttng_index_file");
206 goto error;
207 }
208
2f8f53af
DG
209 if (tracefile_count > 0) {
210 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
211 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name,
212 channel_name, tracefile_count_current);
213 } else {
214 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
215 DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name);
216 }
217 if (ret < 0) {
218 PERROR("snprintf index path");
219 goto error;
220 }
221
222 DBG("Index opening file %s in read only", fullpath);
223 read_fd = open(fullpath, O_RDONLY);
224 if (read_fd < 0) {
4a3a61c1 225 PERROR("opening index in read-only");
2f8f53af
DG
226 goto error;
227 }
228
229 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
230 if (read_len < 0) {
231 PERROR("Reading index header");
232 goto error_close;
233 }
234
235 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
236 ERR("Invalid header magic");
237 goto error_close;
238 }
f8f3885c
MD
239 major = be32toh(hdr.index_major);
240 minor = be32toh(hdr.index_minor);
241 element_len = be32toh(hdr.packet_index_len);
242
243 if (major != CTF_INDEX_MAJOR) {
2f8f53af
DG
244 ERR("Invalid header version");
245 goto error_close;
246 }
a3461e94
MD
247 if (element_len > sizeof(struct ctf_packet_index)) {
248 ERR("Index element length too long");
249 goto error_close;
250 }
2f8f53af 251
f8f3885c
MD
252 index_file->fd = read_fd;
253 index_file->major = major;
254 index_file->minor = minor;
255 index_file->element_len = element_len;
256 urcu_ref_init(&index_file->ref);
257
258 return index_file;
2f8f53af
DG
259
260error_close:
261 if (read_fd >= 0) {
262 int close_ret;
263
264 close_ret = close(read_fd);
265 if (close_ret < 0) {
266 PERROR("close read fd %d", read_fd);
267 }
268 }
2f8f53af
DG
269
270error:
f8f3885c
MD
271 free(index_file);
272 return NULL;
273}
274
275void lttng_index_file_get(struct lttng_index_file *index_file)
276{
277 urcu_ref_get(&index_file->ref);
278}
279
280static void lttng_index_file_release(struct urcu_ref *ref)
281{
282 struct lttng_index_file *index_file = caa_container_of(ref,
283 struct lttng_index_file, ref);
284
285 if (close(index_file->fd)) {
286 PERROR("close index fd");
287 }
288 free(index_file);
289}
290
291void lttng_index_file_put(struct lttng_index_file *index_file)
292{
293 urcu_ref_put(&index_file->ref, lttng_index_file_release);
2f8f53af 294}
This page took 0.078682 seconds and 4 git commands to generate.