Use lttng_read/lttng_write wrappers
[lttng-tools.git] / src / common / index / index.c
index 3d22ca61cc1ad720e7e50270137053c6d5f5a787..cddc58c09de1726b8322816b4eb3f308dace03f3 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,9 +35,27 @@ int index_create_file(char *path_name, char *stream_name, int uid, int gid,
                uint64_t size, uint64_t count)
 {
        int ret, fd = -1;
+       ssize_t size_ret;
        struct lttng_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;
@@ -47,11 +66,10 @@ int index_create_file(char *path_name, char *stream_name, int uid, int gid,
        hdr.index_major = htobe32(INDEX_MAJOR);
        hdr.index_minor = htobe32(INDEX_MINOR);
 
-       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 +90,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 lttng_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.024746 seconds and 4 git commands to generate.