Fix: import index containing only a header
[babeltrace.git] / formats / ctf / ctf.c
index eec3350f3ba73b2c40c27c479943f3d80b4c093c..afc2aefae97c3aab8f0e81d94cf982c09b4ff9da 100644 (file)
@@ -472,12 +472,16 @@ int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *str
        if (unlikely(pos->offset == EOF))
                return EOF;
 
-       if (pos->content_size == 0) {
-               /* Stream is inactive for now (live reading). */
+       /* Stream is inactive for now (live reading). */
+       if (unlikely(pos->content_size == 0))
                return EAGAIN;
-       }
-       /* Packet only contains headers */
-       if (pos->offset == pos->content_size)
+
+       /*
+        * Packet seeked to by ctf_pos_get_event() only contains
+        * headers, no event. Consider stream as inactive (live
+        * reading).
+        */
+       if (unlikely(pos->data_offset == pos->content_size))
                return EAGAIN;
 
        assert(pos->offset < pos->content_size);
@@ -957,9 +961,10 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                pos->content_size = packet_index->content_size;
                pos->packet_size = packet_index->packet_size;
                pos->mmap_offset = packet_index->offset;
-               if (packet_index->data_offset < packet_index->content_size) {
+               pos->data_offset = packet_index->data_offset;
+               if (pos->data_offset < packet_index->content_size) {
                        pos->offset = 0;        /* will read headers */
-               } else if (packet_index->data_offset == packet_index->content_size) {
+               } else if (pos->data_offset == packet_index->content_size) {
                        /* empty packet */
                        pos->offset = packet_index->data_offset;
                        whence = SEEK_CUR;
@@ -1809,7 +1814,6 @@ int import_stream_packet_index(struct ctf_trace *td,
        struct ctf_packet_index ctf_index;
        struct ctf_packet_index_file_hdr index_hdr;
        struct packet_index index;
-       int index_read;
        int ret = 0;
        int first_packet = 1;
        size_t len;
@@ -1843,8 +1847,8 @@ int import_stream_packet_index(struct ctf_trace *td,
                goto error;
        }
 
-       while ((index_read = fread(&ctf_index, index_hdr.packet_index_len, 1,
-                                       pos->index_fp)) == 1) {
+       while (fread(&ctf_index, index_hdr.packet_index_len, 1,
+                       pos->index_fp) == 1) {
                uint64_t stream_id;
 
                memset(&index, 0, sizeof(index));
@@ -1882,6 +1886,12 @@ int import_stream_packet_index(struct ctf_trace *td,
                g_array_append_val(file_stream->pos.packet_cycles_index, index);
        }
 
+       /* Index containing only the header. */
+       if (!file_stream->parent.stream_class) {
+               ret = -1;
+               goto error;
+       }
+
        ret = 0;
 
 error:
@@ -2189,6 +2199,7 @@ void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
        pos->flags = MAP_PRIVATE;
        pos->parent.rw_table = read_dispatch_table;
        pos->parent.event_cb = ctf_read_event;
+       pos->priv = mmap_info->priv;
 }
 
 static
@@ -2386,10 +2397,12 @@ int ctf_close_file_stream(struct ctf_file_stream *file_stream)
                fprintf(stderr, "Error on ctf_fini_pos\n");
                return -1;
        }
-       ret = close(file_stream->pos.fd);
-       if (ret) {
-               perror("Error closing file fd");
-               return -1;
+       if (file_stream->pos.fd >= 0) {
+               ret = close(file_stream->pos.fd);
+               if (ret) {
+                       perror("Error closing file fd");
+                       return -1;
+               }
        }
        return 0;
 }
@@ -2421,15 +2434,19 @@ int ctf_close_trace(struct bt_trace_descriptor *tdp)
                }
        }
        ctf_destroy_metadata(td);
-       ret = close(td->dirfd);
-       if (ret) {
-               perror("Error closing dirfd");
-               return ret;
+       if (td->dirfd >= 0) {
+               ret = close(td->dirfd);
+               if (ret) {
+                       perror("Error closing dirfd");
+                       return ret;
+               }
        }
-       ret = closedir(td->dir);
-       if (ret) {
-               perror("Error closedir");
-               return ret;
+       if (td->dir) {
+               ret = closedir(td->dir);
+               if (ret) {
+                       perror("Error closedir");
+                       return ret;
+               }
        }
        free(td->metadata_string);
        g_free(td);
This page took 0.036707 seconds and 4 git commands to generate.