Fix: babeltrace assert() triggered by directories within trace
[babeltrace.git] / formats / ctf / ctf.c
index 07966632e82caa99139f20458faf7f3d99f87449..b3f7699afc925bca91b6fbf0dce5c38aca091ce5 100644 (file)
@@ -147,6 +147,8 @@ uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
                struct ctf_stream_declaration *stream_class;
 
                stream_class = g_ptr_array_index(tin->streams, i);
+               if (!stream_class)
+                       continue;
                /* for each file_stream */
                for (j = 0; j < stream_class->streams->len; j++) {
                        struct ctf_stream_definition *stream;
@@ -190,6 +192,8 @@ uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
                struct ctf_stream_declaration *stream_class;
 
                stream_class = g_ptr_array_index(tin->streams, i);
+               if (!stream_class)
+                       continue;
                /* for each file_stream */
                for (j = 0; j < stream_class->streams->len; j++) {
                        struct ctf_stream_definition *stream;
@@ -491,7 +495,7 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
        pos->packet_size = 0;
        pos->content_size = 0;
        pos->content_size_loc = NULL;
-       pos->base = NULL;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
@@ -526,9 +530,9 @@ void ctf_fini_pos(struct ctf_stream_pos *pos)
 
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
@@ -555,15 +559,15 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
 
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
                        assert(0);
                }
-               pos->base = NULL;
+               pos->base_mma = NULL;
        }
 
        /*
@@ -686,9 +690,9 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                }
        }
        /* map new base. Need mapping length from header. */
-       pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
-                        pos->flags, pos->fd, pos->mmap_offset);
-       if (pos->base == MAP_FAILED) {
+       pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
+                       pos->flags, pos->fd, pos->mmap_offset);
+       if (pos->base_mma == MAP_FAILED) {
                fprintf(stderr, "[error] mmap error %s.\n",
                        strerror(errno));
                assert(0);
@@ -1145,19 +1149,20 @@ int create_stream_packet_index(struct ctf_trace *td,
        for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
                uint64_t stream_id = 0;
 
-               if (pos->base) {
+               if (pos->base_mma) {
                        /* unmap old base */
-                       ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+                       ret = munmap_align(pos->base_mma);
                        if (ret) {
                                fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                        strerror(errno));
                                return ret;
                        }
-                       pos->base = NULL;
+                       pos->base_mma = NULL;
                }
                /* map new base. Need mapping length from header. */
-               pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
+               pos->base_mma = mmap_align(MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
                                 MAP_PRIVATE, pos->fd, pos->mmap_offset);
+               assert(pos->base_mma != MAP_FAILED);
                pos->content_size = MAX_PACKET_HEADER_LEN;      /* Unknown at this point */
                pos->packet_size = MAX_PACKET_HEADER_LEN;       /* Unknown at this point */
                pos->offset = 0;        /* Position of the packet header */
@@ -1321,7 +1326,7 @@ int create_stream_packet_index(struct ctf_trace *td,
 
                if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
                        fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
-                               packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
+                               packet_index.packet_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
                        return -EINVAL;
                }
 
@@ -1373,14 +1378,29 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
                void (*packet_seek)(struct stream_pos *pos, size_t index,
                        int whence))
 {
-       int ret;
+       int ret, fd;
        struct ctf_file_stream *file_stream;
+       struct stat statbuf;
 
-       ret = openat(td->dirfd, path, flags);
-       if (ret < 0) {
+       fd = openat(td->dirfd, path, flags);
+       if (fd < 0) {
                perror("File stream openat()");
+               ret = fd;
                goto error;
        }
+
+       /* Don't try to mmap subdirectories. Skip them, return success. */
+       ret = fstat(fd, &statbuf);
+       if (ret) {
+               perror("File stream fstat()");
+               goto fstat_error;
+       }
+       if (S_ISDIR(statbuf.st_mode)) {
+               fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
+               ret = 0;
+               goto fd_is_dir_ok;
+       }
+
        file_stream = g_new0(struct ctf_file_stream, 1);
 
        if (packet_seek) {
@@ -1391,7 +1411,7 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
                goto error_def;
        }
 
-       ctf_init_pos(&file_stream->pos, ret, flags);
+       ctf_init_pos(&file_stream->pos, fd, flags);
        ret = create_trace_definitions(td, &file_stream->parent);
        if (ret)
                goto error_def;
@@ -1412,8 +1432,10 @@ error_index:
                definition_unref(&file_stream->parent.trace_packet_header->p);
 error_def:
        ctf_fini_pos(&file_stream->pos);
-       close(file_stream->pos.fd);
        g_free(file_stream);
+fd_is_dir_ok:
+fstat_error:
+       close(fd);
 error:
        return ret;
 }
@@ -1553,7 +1575,7 @@ void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
        pos->content_size = 0;
        pos->content_size_loc = NULL;
        pos->fd = mmap_info->fd;
-       pos->base = 0;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
This page took 0.026349 seconds and 4 git commands to generate.