Writer: validating the packet header is not necessary
[babeltrace.git] / formats / ctf / ir / stream.c
index 16bb0adad187be70e590fc566c5b9f61235c8acc..c0b4e979a9ff9195b21e323bc7402110cafe335e 100644 (file)
@@ -873,6 +873,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
        struct bt_ctf_field *integer = NULL;
        struct ctf_stream_pos packet_context_pos;
        bool empty_packet;
+       uint64_t packet_size_bits;
 
        if (!stream || stream->pos.fd < 0) {
                /*
@@ -883,12 +884,18 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       empty_packet = (stream->events->len == 0);
-       ret = bt_ctf_field_validate(stream->packet_header);
-       if (ret) {
+       if (!stream->packet_context && stream->flushed_packet_count > 0) {
+               /*
+                * A stream without a packet context, and thus without
+                * content and packet size members, can't have more than
+                * one packet.
+                */
+               ret = -1;
                goto end;
        }
 
+       empty_packet = (stream->events->len == 0);
+
        /* mmap the next packet */
        ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
 
@@ -993,6 +1000,11 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                }
        }
 
+       /* Rounded-up in case content_size is not byte-aligned. */
+       packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) &
+               ~(CHAR_BIT - 1);
+       stream->pos.packet_size = packet_size_bits;
+
        if (stream->packet_context) {
                /*
                 * Update the packet total size and content size and overwrite
@@ -1008,7 +1020,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                }
 
                ret = set_structure_field_integer(stream->packet_context,
-                       "packet_size", stream->pos.packet_size);
+                       "packet_size", packet_size_bits);
                if (ret) {
                        goto end;
                }
@@ -1022,6 +1034,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 
        g_ptr_array_set_size(stream->events, 0);
        stream->flushed_packet_count++;
+       stream->size += packet_size_bits / CHAR_BIT;
 end:
        bt_put(integer);
        return ret;
@@ -1044,8 +1057,24 @@ void bt_ctf_stream_destroy(struct bt_object *obj)
 
        stream = container_of(obj, struct bt_ctf_stream, base);
        ctf_fini_pos(&stream->pos);
-       if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
-               perror("close");
+       if (stream->pos.fd >= 0) {
+               int ret;
+
+               /*
+                * Truncate the file's size to the minimum required to fit the
+                * last packet as we might have grown it too much on the last
+                * mmap.
+                */
+               do {
+                       ret = ftruncate(stream->pos.fd, stream->size);
+               } while (ret == -1 && errno == EINTR);
+               if (ret) {
+                       perror("ftruncate");
+               }
+
+               if (close(stream->pos.fd)) {
+                       perror("close");
+               }
        }
 
        if (stream->events) {
This page took 0.026278 seconds and 4 git commands to generate.