From 989c73bc7635ba348a372649fa1ccb2d8250be63 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Sat, 7 May 2011 12:27:59 -0400 Subject: [PATCH] Use O_RDWR for open write mode (for mmap) Signed-off-by: Mathieu Desnoyers --- converter/babeltrace.c | 2 +- formats/ctf-text/ctf-text.c | 2 +- formats/ctf/ctf.c | 29 +++++----- formats/ctf/types/float.c | 2 +- tests/test-dummytrace.c | 106 +++++++++++++++++++++++++++++++----- 5 files changed, 109 insertions(+), 32 deletions(-) diff --git a/converter/babeltrace.c b/converter/babeltrace.c index ce7664a8..64392d0c 100644 --- a/converter/babeltrace.c +++ b/converter/babeltrace.c @@ -196,7 +196,7 @@ int main(int argc, char **argv) goto error_td_read; } - td_write = fmt_write->open_trace(opt_output_path, O_WRONLY); + td_write = fmt_write->open_trace(opt_output_path, O_RDWR); if (!td_write) { fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n", opt_output_path ? : ""); diff --git a/formats/ctf-text/ctf-text.c b/formats/ctf-text/ctf-text.c index 17b7bc23..cc882f60 100644 --- a/formats/ctf-text/ctf-text.c +++ b/formats/ctf-text/ctf-text.c @@ -60,7 +60,7 @@ struct trace_descriptor *ctf_text_open_trace(const char *path, int flags) pos = g_new0(struct ctf_text_stream_pos, 1); switch (flags & O_ACCMODE) { - case O_WRONLY: + case O_RDWR: if (!path) fp = stdout; else diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 625b1c09..aae71872 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -101,7 +101,6 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags) pos->flags = MAP_PRIVATE; pos->parent.rw_table = read_dispatch_table; break; - case O_WRONLY: case O_RDWR: pos->prot = PROT_WRITE; /* Write has priority */ pos->flags = MAP_SHARED; @@ -138,9 +137,6 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) off_t off; struct packet_index *index; - /* Only allow random seek in read mode */ - assert(pos->prot != PROT_WRITE || whence == SEEK_CUR); - if (pos->prot == PROT_WRITE && pos->content_size_loc) *pos->content_size_loc = pos->offset; @@ -160,18 +156,23 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) * except to get exactly at the beginning of the next packet. */ if (pos->prot == PROT_WRITE) { - /* The writer will add padding */ - assert(pos->offset + offset == pos->packet_size); - - /* - * Don't increment for initial stream move (only condition where - * pos->offset can be 0. - */ - if (pos->offset) + switch (whence) { + case SEEK_CUR: + /* The writer will add padding */ + assert(pos->offset + offset == pos->packet_size); pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT; + break; + case SEEK_SET: + assert(offset == 0); /* only seek supported for now */ + pos->cur_index = 0; + break; + default: + assert(0); + } pos->content_size = -1U; /* Unknown at this point */ pos->packet_size = WRITE_PACKET_LEN; - off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT); + off = posix_fallocate(pos->fd, pos->mmap_offset, + pos->packet_size / CHAR_BIT); assert(off >= 0); pos->offset = 0; } else { @@ -607,7 +608,7 @@ struct trace_descriptor *ctf_open_trace(const char *path, int flags) if (ret) goto error; break; - case O_WRONLY: + case O_RDWR: fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n"); goto error; default: diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index c494ee82..ebd7b7ae 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -148,7 +148,7 @@ int ctf_float_read(struct stream_pos *ppos, struct definition *definition) struct ctf_stream_pos destp; int ret; - ctf_init_pos(&destp, -1, O_WRONLY); + ctf_init_pos(&destp, -1, O_RDWR); destp.base = (char *) u.bits; ctf_align_pos(pos, float_declaration->p.alignment); diff --git a/tests/test-dummytrace.c b/tests/test-dummytrace.c index 06bb695b..49a650e4 100644 --- a/tests/test-dummytrace.c +++ b/tests/test-dummytrace.c @@ -22,16 +22,46 @@ #include #include #include +#include #include +#include #include #include #include #include #include +#include #include #include +static const char metadata[] = +"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" +"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n" +"\n" +"trace {\n" +" major = %s;\n" /* major (e.g. 0) */ +" minor = %s;\n" /* minor (e.g. 1) */ +" uuid = %s;\n" /* UUID */ +" byte_order = %s;\n" /* be or le */ +" packet.header := struct {\n" +" uint32_t magic;\n" +" uint8_t trace_uuid[16];\n" +" };\n" +"};\n" +"\n" +"stream {\n" +" packet.context := struct {\n" +" uint32_t content_size;\n" +" uint32_t packet_size;\n" +" };\n" +"};\n" +"\n" +"event {\n" +" name = string;\n" +" fields := struct { string str; };\n" +"};\n"; + int babeltrace_debug, babeltrace_verbose; static uuid_t s_uuid; @@ -126,7 +156,7 @@ void trace_text(FILE *input, int output) char *line = NULL, *nl; size_t linesize; - ctf_init_pos(&pos, output, O_WRONLY); + ctf_init_pos(&pos, output, O_RDWR); write_packet_header(&pos, s_uuid); write_packet_context(&pos); @@ -142,29 +172,75 @@ void trace_text(FILE *input, int output) ctf_fini_pos(&pos); } +static void usage(FILE *fp) +{ + fprintf(fp, "BabelTrace Log Converter %u.%u\n", + BABELTRACE_VERSION_MAJOR, + BABELTRACE_VERSION_MINOR); + fprintf(fp, "\n"); + fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); + fprintf(fp, "\n"); + fprintf(fp, "usage : babeltrace-log OUTPUT\n"); + fprintf(fp, "\n"); + fprintf(fp, " OUTPUT Output trace path\n"); + fprintf(fp, "\n"); +} + int main(int argc, char **argv) { int fd, ret; + char *outputname; + DIR *dir; + int dir_fd; - ret = unlink("dummystream"); - if (ret < 0) { - perror("unlink"); - return -1; - } - fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); - if (fd < 0) { - perror("open"); - return -1; + if (argc < 2) { + usage(stdout); + goto error; } + outputname = argv[1]; - ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid); + ret = mkdir(outputname, S_IRWXU|S_IRWXG); if (ret) { - printf("uuid parse error\n"); - close(fd); - return -1; + perror("mkdir"); + goto error; + } + + dir = opendir(outputname); + if (!dir) { + perror("opendir"); + goto error_rmdir; + } + dir_fd = dirfd(dir); + if (dir_fd < 0) { + perror("dirfd"); + goto error_closedir; + } + + fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT, + S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); + if (fd < 0) { + perror("openat"); + goto error_closedirfd; } trace_text(stdin, fd); + close(fd); - return 0; + exit(EXIT_SUCCESS); + + /* error handling */ +error_closedirfd: + ret = close(dir_fd); + if (ret) + perror("close"); +error_closedir: + ret = closedir(dir); + if (ret) + perror("closedir"); +error_rmdir: + ret = rmdir(outputname); + if (ret) + perror("rmdir"); +error: + exit(EXIT_FAILURE); } -- 2.34.1