2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
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.
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
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.
22 #include <sys/types.h>
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/compat/endian.h>
28 #include <common/utils.h>
33 * Create the index file associated with a trace file.
35 * Return fd on success, a negative value on error.
37 int index_create_file(char *path_name
, char *stream_name
, int uid
, int gid
,
38 uint64_t size
, uint64_t count
)
42 struct ctf_packet_index_file_hdr hdr
;
43 char fullpath
[PATH_MAX
];
45 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
,
48 PERROR("snprintf index path");
52 /* Create index directory if necessary. */
53 ret
= utils_mkdir(fullpath
, S_IRWXU
| S_IRWXG
, uid
, gid
);
55 if (errno
!= EEXIST
) {
56 PERROR("Index trace directory creation error");
62 * For tracefile rotation. We need to unlink the old
63 * file if present to synchronize with the tail of the
64 * live viewer which could be working on this same file.
65 * By doing so, any reference to the old index file
66 * stays valid even if we re-create a new file with the
67 * same name afterwards.
69 ret
= utils_unlink_stream_file(fullpath
, stream_name
, size
, count
, uid
,
70 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
71 if (ret
< 0 && errno
!= ENOENT
) {
74 ret
= utils_create_stream_file(fullpath
, stream_name
, size
, count
, uid
,
75 gid
, DEFAULT_INDEX_FILE_SUFFIX
);
81 hdr
.magic
= htobe32(CTF_INDEX_MAGIC
);
82 hdr
.index_major
= htobe32(CTF_INDEX_MAJOR
);
83 hdr
.index_minor
= htobe32(CTF_INDEX_MINOR
);
84 hdr
.packet_index_len
= htobe32(sizeof(struct ctf_packet_index
));
86 size_ret
= lttng_write(fd
, &hdr
, sizeof(hdr
));
87 if (size_ret
< sizeof(hdr
)) {
88 PERROR("write index header");
99 close_ret
= close(fd
);
101 PERROR("close index fd");
108 * Write index values to the given fd of size len.
110 * Return "len" on success or else < len on error. errno contains error
113 ssize_t
index_write(int fd
, struct ctf_packet_index
*index
, size_t len
)
124 ret
= lttng_write(fd
, index
, len
);
126 PERROR("writing index file");
134 * Open index file using a given path, channel name and tracefile count.
136 * Return read only FD on success or else a negative value.
138 int index_open(const char *path_name
, const char *channel_name
,
139 uint64_t tracefile_count
, uint64_t tracefile_count_current
)
143 char fullpath
[PATH_MAX
];
144 struct ctf_packet_index_file_hdr hdr
;
147 assert(channel_name
);
149 if (tracefile_count
> 0) {
150 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
151 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, path_name
,
152 channel_name
, tracefile_count_current
);
154 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
155 DEFAULT_INDEX_FILE_SUFFIX
, path_name
, channel_name
);
158 PERROR("snprintf index path");
162 DBG("Index opening file %s in read only", fullpath
);
163 read_fd
= open(fullpath
, O_RDONLY
);
165 if (errno
== ENOENT
) {
168 PERROR("opening index in read-only");
173 read_len
= lttng_read(read_fd
, &hdr
, sizeof(hdr
));
175 PERROR("Reading index header");
179 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
180 ERR("Invalid header magic");
183 if (be32toh(hdr
.index_major
) != CTF_INDEX_MAJOR
||
184 be32toh(hdr
.index_minor
) != CTF_INDEX_MINOR
) {
185 ERR("Invalid header version");
195 close_ret
= close(read_fd
);
197 PERROR("close read fd %d", read_fd
);