a462a635e78dfff933efc4dcec1253ceca37f552
[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 #include <sys/types.h>
23 #include <fcntl.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/compat/endian.h>
28 #include <common/utils.h>
29
30 #include "index.h"
31
32 /*
33 * Create the index file associated with a trace file.
34 *
35 * Return fd on success, a negative value on error.
36 */
37 int index_create_file(char *path_name, char *stream_name, int uid, int gid,
38 uint64_t size, uint64_t count)
39 {
40 int ret, fd = -1;
41 ssize_t size_ret;
42 struct ctf_packet_index_file_hdr hdr;
43 char fullpath[PATH_MAX];
44
45 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR,
46 path_name);
47 if (ret < 0) {
48 PERROR("snprintf index path");
49 goto error;
50 }
51
52 /* Create index directory if necessary. */
53 ret = run_as_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
54 if (ret < 0) {
55 if (ret != -EEXIST) {
56 PERROR("Index trace directory creation error");
57 goto error;
58 }
59 }
60
61 ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
62 gid, DEFAULT_INDEX_FILE_SUFFIX);
63 if (ret < 0) {
64 goto error;
65 }
66 fd = ret;
67
68 hdr.magic = htobe32(CTF_INDEX_MAGIC);
69 hdr.index_major = htobe32(CTF_INDEX_MAJOR);
70 hdr.index_minor = htobe32(CTF_INDEX_MINOR);
71 hdr.packet_index_len = htobe32(sizeof(struct ctf_packet_index));
72
73 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
74 if (size_ret < sizeof(hdr)) {
75 PERROR("write index header");
76 ret = -1;
77 goto error;
78 }
79
80 return fd;
81
82 error:
83 if (fd >= 0) {
84 int close_ret;
85
86 close_ret = close(fd);
87 if (close_ret < 0) {
88 PERROR("close index fd");
89 }
90 }
91 return ret;
92 }
93
94 /*
95 * Write index values to the given fd of size len.
96 *
97 * Return "len" on success or else < len on error. errno contains error
98 * details.
99 */
100 ssize_t index_write(int fd, struct ctf_packet_index *index, size_t len)
101 {
102 ssize_t ret;
103
104 assert(index);
105
106 if (fd < 0) {
107 ret = -EINVAL;
108 goto error;
109 }
110
111 ret = lttng_write(fd, index, len);
112 if (ret < len) {
113 PERROR("writing index file");
114 }
115
116 error:
117 return ret;
118 }
119
120 /*
121 * Open index file using a given path, channel name and tracefile count.
122 *
123 * Return read only FD on success or else a negative value.
124 */
125 int index_open(const char *path_name, const char *channel_name,
126 uint64_t tracefile_count, uint64_t tracefile_count_current)
127 {
128 int ret, read_fd;
129 ssize_t read_len;
130 char fullpath[PATH_MAX];
131 struct ctf_packet_index_file_hdr hdr;
132
133 assert(path_name);
134 assert(channel_name);
135
136 if (tracefile_count > 0) {
137 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s_%"
138 PRIu64 DEFAULT_INDEX_FILE_SUFFIX, path_name,
139 channel_name, tracefile_count_current);
140 } else {
141 ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR "/%s"
142 DEFAULT_INDEX_FILE_SUFFIX, path_name, channel_name);
143 }
144 if (ret < 0) {
145 PERROR("snprintf index path");
146 goto error;
147 }
148
149 DBG("Index opening file %s in read only", fullpath);
150 read_fd = open(fullpath, O_RDONLY);
151 if (read_fd < 0) {
152 if (errno == ENOENT) {
153 ret = -ENOENT;
154 } else {
155 PERROR("opening index in read-only");
156 }
157 goto error;
158 }
159
160 read_len = lttng_read(read_fd, &hdr, sizeof(hdr));
161 if (read_len < 0) {
162 PERROR("Reading index header");
163 goto error_close;
164 }
165
166 if (be32toh(hdr.magic) != CTF_INDEX_MAGIC) {
167 ERR("Invalid header magic");
168 goto error_close;
169 }
170 if (be32toh(hdr.index_major) != CTF_INDEX_MAJOR ||
171 be32toh(hdr.index_minor) != CTF_INDEX_MINOR) {
172 ERR("Invalid header version");
173 goto error_close;
174 }
175
176 return read_fd;
177
178 error_close:
179 if (read_fd >= 0) {
180 int close_ret;
181
182 close_ret = close(read_fd);
183 if (close_ret < 0) {
184 PERROR("close read fd %d", read_fd);
185 }
186 }
187 ret = -1;
188
189 error:
190 return ret;
191 }
This page took 0.032472 seconds and 3 git commands to generate.