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 | |
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 | */ | |
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 | struct lttng_packet_index_file_hdr hdr; | |
1c20f0e2 | 39 | char fullpath[PATH_MAX]; |
309167d2 | 40 | |
1c20f0e2 JD |
41 | ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR, |
42 | path_name); | |
43 | if (ret < 0) { | |
44 | PERROR("snprintf index path"); | |
45 | goto error; | |
46 | } | |
47 | ||
48 | /* Create index directory if necessary. */ | |
49 | ret = run_as_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); | |
50 | if (ret < 0) { | |
51 | if (ret != -EEXIST) { | |
52 | ERR("Index trace directory creation error"); | |
53 | goto error; | |
54 | } | |
55 | } | |
56 | ||
57 | ret = utils_create_stream_file(fullpath, stream_name, size, count, uid, | |
309167d2 JD |
58 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
59 | if (ret < 0) { | |
60 | goto error; | |
61 | } | |
62 | fd = ret; | |
63 | ||
64 | memcpy(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic)); | |
65 | hdr.index_major = htobe32(INDEX_MAJOR); | |
66 | hdr.index_minor = htobe32(INDEX_MINOR); | |
67 | ||
68 | do { | |
69 | ret = write(fd, &hdr, sizeof(hdr)); | |
70 | } while (ret < 0 && errno == EINTR); | |
71 | if (ret < 0) { | |
72 | PERROR("write index header"); | |
73 | goto error; | |
74 | } | |
75 | ||
76 | return fd; | |
77 | ||
78 | error: | |
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 | * | |
93 | * Return 0 on success or else a negative value on error. | |
94 | */ | |
95 | int index_write(int fd, struct lttng_packet_index *index, size_t len) | |
96 | { | |
97 | int ret; | |
98 | ||
99 | assert(fd >= 0); | |
100 | assert(index); | |
101 | ||
102 | do { | |
103 | ret = write(fd, index, len); | |
104 | } while (ret < 0 && errno == EINTR); | |
105 | if (ret < 0) { | |
106 | PERROR("writing index file"); | |
107 | } | |
108 | ||
109 | return ret; | |
110 | } |