| 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 | |
| 22 | #include <common/common.h> |
| 23 | #include <common/defaults.h> |
| 24 | #include <common/utils.h> |
| 25 | |
| 26 | #include "index.h" |
| 27 | |
| 28 | /* |
| 29 | * Create the index file associated with a trace file. |
| 30 | * |
| 31 | * Return fd on success, a negative value on error. |
| 32 | */ |
| 33 | int index_create_file(char *path_name, char *stream_name, int uid, int gid, |
| 34 | uint64_t size, uint64_t count) |
| 35 | { |
| 36 | int ret, fd = -1; |
| 37 | struct lttng_packet_index_file_hdr hdr; |
| 38 | |
| 39 | ret = utils_create_stream_file(path_name, stream_name, size, count, uid, |
| 40 | gid, DEFAULT_INDEX_FILE_SUFFIX); |
| 41 | if (ret < 0) { |
| 42 | goto error; |
| 43 | } |
| 44 | fd = ret; |
| 45 | |
| 46 | memcpy(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic)); |
| 47 | hdr.index_major = htobe32(INDEX_MAJOR); |
| 48 | hdr.index_minor = htobe32(INDEX_MINOR); |
| 49 | |
| 50 | do { |
| 51 | ret = write(fd, &hdr, sizeof(hdr)); |
| 52 | } while (ret < 0 && errno == EINTR); |
| 53 | if (ret < 0) { |
| 54 | PERROR("write index header"); |
| 55 | goto error; |
| 56 | } |
| 57 | |
| 58 | return fd; |
| 59 | |
| 60 | error: |
| 61 | if (fd >= 0) { |
| 62 | int close_ret; |
| 63 | |
| 64 | close_ret = close(fd); |
| 65 | if (close_ret < 0) { |
| 66 | PERROR("close index fd"); |
| 67 | } |
| 68 | } |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Write index values to the given fd of size len. |
| 74 | * |
| 75 | * Return 0 on success or else a negative value on error. |
| 76 | */ |
| 77 | int index_write(int fd, struct lttng_packet_index *index, size_t len) |
| 78 | { |
| 79 | int ret; |
| 80 | |
| 81 | assert(fd >= 0); |
| 82 | assert(index); |
| 83 | |
| 84 | do { |
| 85 | ret = write(fd, index, len); |
| 86 | } while (ret < 0 && errno == EINTR); |
| 87 | if (ret < 0) { |
| 88 | PERROR("writing index file"); |
| 89 | } |
| 90 | |
| 91 | return ret; |
| 92 | } |