Rename LTTng index in CTF index
[lttng-tools.git] / src / common / index / index.c
index 3d22ca61cc1ad720e7e50270137053c6d5f5a787..2946f7bab3335e5456d9628d9e0a3c6707e0eca6 100644 (file)
@@ -18,6 +18,7 @@
 
 #define _GNU_SOURCE
 #include <assert.h>
+#include <sys/stat.h>
 
 #include <common/common.h>
 #include <common/defaults.h>
@@ -34,24 +35,42 @@ int index_create_file(char *path_name, char *stream_name, int uid, int gid,
                uint64_t size, uint64_t count)
 {
        int ret, fd = -1;
-       struct lttng_packet_index_file_hdr hdr;
+       ssize_t size_ret;
+       struct ctf_packet_index_file_hdr hdr;
+       char fullpath[PATH_MAX];
 
-       ret = utils_create_stream_file(path_name, stream_name, size, count, uid,
+       ret = snprintf(fullpath, sizeof(fullpath), "%s/" DEFAULT_INDEX_DIR,
+                       path_name);
+       if (ret < 0) {
+               PERROR("snprintf index path");
+               goto error;
+       }
+
+       /* Create index directory if necessary. */
+       ret = run_as_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid);
+       if (ret < 0) {
+               if (ret != -EEXIST) {
+                       ERR("Index trace directory creation error");
+                       goto error;
+               }
+       }
+
+       ret = utils_create_stream_file(fullpath, stream_name, size, count, uid,
                        gid, DEFAULT_INDEX_FILE_SUFFIX);
        if (ret < 0) {
                goto error;
        }
        fd = ret;
 
-       memcpy(hdr.magic, INDEX_MAGIC, sizeof(hdr.magic));
-       hdr.index_major = htobe32(INDEX_MAJOR);
-       hdr.index_minor = htobe32(INDEX_MINOR);
+       hdr.magic = htobe32(CTF_INDEX_MAGIC);
+       hdr.index_major = htobe32(CTF_INDEX_MAJOR);
+       hdr.index_minor = htobe32(CTF_INDEX_MINOR);
+       hdr.packet_index_len = sizeof(struct ctf_packet_index);
 
-       do {
-               ret = write(fd, &hdr, sizeof(hdr));
-       } while (ret < 0 && errno == EINTR);
-       if (ret < 0) {
+       size_ret = lttng_write(fd, &hdr, sizeof(hdr));
+       if (size_ret < sizeof(hdr)) {
                PERROR("write index header");
+               ret = -1;
                goto error;
        }
 
@@ -72,19 +91,18 @@ error:
 /*
  * Write index values to the given fd of size len.
  *
- * Return 0 on success or else a negative value on error.
+ * Return "len" on success or else < len on error. errno contains error
+ * details.
  */
-int index_write(int fd, struct lttng_packet_index *index, size_t len)
+ssize_t index_write(int fd, struct ctf_packet_index *index, size_t len)
 {
-       int ret;
+       ssize_t ret;
 
        assert(fd >= 0);
        assert(index);
 
-       do {
-               ret = write(fd, index, len);
-       } while (ret < 0 && errno == EINTR);
-       if (ret < 0) {
+       ret = lttng_write(fd, index, len);
+       if (ret < len) {
                PERROR("writing index file");
        }
 
This page took 0.023482 seconds and 4 git commands to generate.