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