Rename LTTng index in CTF index
[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 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <sys/stat.h>
22
23 #include <common/common.h>
24 #include <common/defaults.h>
25 #include <common/utils.h>
26
27 #include "index.h"
28
29 /*
30 * Create the index file associated with a trace file.
31 *
32 * Return fd on success, a negative value on error.
33 */
34 int index_create_file(char *path_name, char *stream_name, int uid, int gid,
35 uint64_t size, uint64_t count)
36 {
37 int ret, fd = -1;
38 ssize_t size_ret;
39 struct ctf_packet_index_file_hdr hdr;
40 char fullpath[PATH_MAX];
41
42 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR,
43 path_name);
44 if (ret < 0) {
45 PERROR("snprintf index path");
46 goto error;
47 }
48
49 /* Create index directory if necessary. */
50 ret = run_as_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
51 if (ret < 0) {
52 if (ret != -EEXIST) {
53 ERR("Index trace directory creation error");
54 goto error;
55 }
56 }
57
58 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
59 gid, DEFAULT_INDEX_FILE_SUFFIX);
60 if (ret < 0) {
61 goto error;
62 }
63 fd = ret;
64
65 hdr.magic = htobe32(CTF_INDEX_MAGIC);
66 hdr.index_major = htobe32(CTF_INDEX_MAJOR);
67 hdr.index_minor = htobe32(CTF_INDEX_MINOR);
68 hdr.packet_index_len = sizeof(struct ctf_packet_index);
69
70 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
71 if (size_ret < sizeof(hdr)) {
72 PERROR("write index header");
73 ret = -1;
74 goto error;
75 }
76
77 return fd;
78
79 error:
80 if (fd >= 0) {
81 int close_ret;
82
83 close_ret = close(fd);
84 if (close_ret < 0) {
85 PERROR("close index fd");
86 }
87 }
88 return ret;
89 }
90
91 /*
92 * Write index values to the given fd of size len.
93 *
94 * Return "len" on success or else < len on error. errno contains error
95 * details.
96 */
97 ssize_t index_write(int fd, struct ctf_packet_index *index, size_t len)
98 {
99 ssize_t ret;
100
101 assert(fd >= 0);
102 assert(index);
103
104 ret = lttng_write(fd, index, len);
105 if (ret < len) {
106 PERROR("writing index file");
107 }
108
109 return ret;
110 }
This page took 0.030539 seconds and 4 git commands to generate.