Use O_RDWR for open write mode (for mmap)
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 May 2011 16:27:59 +0000 (12:27 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sat, 7 May 2011 16:27:59 +0000 (12:27 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
converter/babeltrace.c
formats/ctf-text/ctf-text.c
formats/ctf/ctf.c
formats/ctf/types/float.c
tests/test-dummytrace.c

index ce7664a834d7ddeb1ce01ac3677e7635e1ad7d26..64392d0c5aee66900cdca53db0dc190ddfcea6fa 100644 (file)
@@ -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 ? : "<none>");
index 17b7bc232f290031e350ad6b8683b907f7d4c75c..cc882f60b02d64ebbe777701c50c69755bf8007b 100644 (file)
@@ -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
index 625b1c097a9de02eab852e1d4d9e9c6273177fa3..aae7187284bfe5317d2b4e8e338cf7c858087454 100644 (file)
@@ -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:
index c494ee8202dec2ef9f9ac7d0f23b82c2fc377cf2..ebd7b7ae166c91a3be761b2ea9da6198e3cf9277 100644 (file)
@@ -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);
index 06bb695b6434266a160d2e2a97377f40162cacfc..49a650e46ac7ef897fd697e529c83294f0f67732 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <dirent.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <errno.h>
 #include <uuid/uuid.h>
 #include <string.h>
+#include <endian.h>
 
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/ctf/types.h>
 
+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);
 }
This page took 0.029366 seconds and 4 git commands to generate.