Fix: set the enable all event command type
[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>
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>
1c20f0e2 21#include <sys/stat.h>
309167d2
JD
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 */
34int 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;
6cd525e8 38 ssize_t size_ret;
309167d2 39 struct lttng_packet_index_file_hdr hdr;
1c20f0e2 40 char fullpath[PATH_MAX];
309167d2 41
1c20f0e2
JD
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,
309167d2
JD
59 gid, DEFAULT_INDEX_FILE_SUFFIX);
60 if (ret < 0) {
61 goto error;
62 }
63 fd = ret;
64
65 memcpy(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic));
66 hdr.index_major = htobe32(INDEX_MAJOR);
67 hdr.index_minor = htobe32(INDEX_MINOR);
68
6cd525e8
MD
69 size_ret = lttng_write(fd, &hdr, sizeof(hdr));
70 if (size_ret < sizeof(hdr)) {
309167d2 71 PERROR("write index header");
6cd525e8 72 ret = -1;
309167d2
JD
73 goto error;
74 }
75
76 return fd;
77
78error:
79 if (fd >= 0) {
80 int close_ret;
81
82 close_ret = close(fd);
83 if (close_ret < 0) {
84 PERROR("close index fd");
85 }
86 }
87 return ret;
88}
89
90/*
91 * Write index values to the given fd of size len.
92 *
6cd525e8
MD
93 * Return "len" on success or else < len on error. errno contains error
94 * details.
309167d2 95 */
6cd525e8 96ssize_t index_write(int fd, struct lttng_packet_index *index, size_t len)
309167d2 97{
6cd525e8 98 ssize_t ret;
309167d2
JD
99
100 assert(fd >= 0);
101 assert(index);
102
6cd525e8
MD
103 ret = lttng_write(fd, index, len);
104 if (ret < len) {
309167d2
JD
105 PERROR("writing index file");
106 }
107
108 return ret;
109}
This page took 0.026792 seconds and 4 git commands to generate.