X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=formats%2Fctf%2Fctf.c;h=27c03cfa8c2e23d211f2a5ed16fe5582f2f6bc66;hb=343c801f4e026a36093f654bdf88749d25f75fb4;hp=93d13e8a5bc5bfcbfefe75529181fe5d41ab8fc0;hpb=b086c01a10fc706dacf1a5a8ea2c4a8acfa0ee24;p=babeltrace.git diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 93d13e8a..27c03cfa 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -57,7 +57,7 @@ extern int yydebug; static struct trace_descriptor *ctf_open_trace(const char *path, int flags, void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, - int whence)); + int whence), FILE *metadata_fp); static void ctf_close_trace(struct trace_descriptor *descriptor); @@ -127,8 +127,16 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) uint64_t id = 0; int ret; + /* We need to check for EOF here for empty files. */ + if (unlikely(pos->offset == EOF)) + return EOF; + ctf_pos_get_event(pos); + /* + * This is the EOF check after we've advanced the position in + * ctf_pos_get_event. + */ if (unlikely(pos->offset == EOF)) return EOF; assert(pos->offset < pos->content_size); @@ -408,6 +416,7 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) /* empty packet */ pos->offset = index->data_offset; offset = 0; + whence = SEEK_CUR; goto read_next_packet; } else { pos->offset = EOF; @@ -613,7 +622,7 @@ int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, static int ctf_open_trace_metadata_read(struct ctf_trace *td, void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, - int whence)) + int whence), FILE *metadata_fp) { struct ctf_scanner *scanner; struct ctf_file_stream *metadata_stream; @@ -631,23 +640,26 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td, goto end_stream; } - td->metadata = &metadata_stream->parent; - metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY); - if (metadata_stream->pos.fd < 0) { - fprintf(stdout, "Unable to open metadata.\n"); - return metadata_stream->pos.fd; - } + if (metadata_fp) { + fp = metadata_fp; + } else { + td->metadata = &metadata_stream->parent; + metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY); + if (metadata_stream->pos.fd < 0) { + fprintf(stdout, "Unable to open metadata.\n"); + return metadata_stream->pos.fd; + } + fp = fdopen(metadata_stream->pos.fd, "r"); + if (!fp) { + fprintf(stdout, "[error] Unable to open metadata stream.\n"); + ret = -errno; + goto end_stream; + } + } if (babeltrace_debug) yydebug = 1; - fp = fdopen(metadata_stream->pos.fd, "r"); - if (!fp) { - fprintf(stdout, "[error] Unable to open metadata stream.\n"); - ret = -errno; - goto end_stream; - } - if (packet_metadata(td, fp)) { ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf); if (ret) @@ -1142,7 +1154,7 @@ error: static int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags, void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, - int whence)) + int whence), FILE *metadata_fp) { int ret; struct dirent *dirent; @@ -1170,7 +1182,7 @@ int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags, * Keep the metadata file separate. */ - ret = ctf_open_trace_metadata_read(td, move_pos_slow); + ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp); if (ret) { goto error_metadata; } @@ -1222,7 +1234,7 @@ error: static struct trace_descriptor *ctf_open_trace(const char *path, int flags, void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, - int whence)) + int whence), FILE *metadata_fp) { struct ctf_trace *td; int ret; @@ -1235,7 +1247,7 @@ struct trace_descriptor *ctf_open_trace(const char *path, int flags, fprintf(stdout, "[error] Path missing for input CTF trace.\n"); goto error; } - ret = ctf_open_trace_read(td, path, flags, move_pos_slow); + ret = ctf_open_trace_read(td, path, flags, move_pos_slow, metadata_fp); if (ret) goto error; break;