| 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 | |
| 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 | ssize_t size_ret; |
| 39 | struct lttng_packet_index_file_hdr hdr; |
| 40 | char fullpath[PATH_MAX]; |
| 41 | |
| 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, |
| 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 | |
| 69 | size_ret = lttng_write(fd, &hdr, sizeof(hdr)); |
| 70 | if (size_ret < sizeof(hdr)) { |
| 71 | PERROR("write index header"); |
| 72 | ret = -1; |
| 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 "len" on success or else < len on error. errno contains error |
| 94 | * details. |
| 95 | */ |
| 96 | ssize_t index_write(int fd, struct lttng_packet_index *index, size_t len) |
| 97 | { |
| 98 | ssize_t ret; |
| 99 | |
| 100 | assert(fd >= 0); |
| 101 | assert(index); |
| 102 | |
| 103 | ret = lttng_write(fd, index, len); |
| 104 | if (ret < len) { |
| 105 | PERROR("writing index file"); |
| 106 | } |
| 107 | |
| 108 | return ret; |
| 109 | } |