803fb7
From 608259be892c532d0afaeb81de3a5ee578d7658a Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Mon, 2 Mar 2015 10:34:51 -0500
803fb7
Subject: [PATCH] journal-remote: fix saving of binary fields
803fb7
803fb7
Binary fields were not processed properly, and resulting journal files
803fb7
were non-conforming, resulting in an error ("Invalid field.") when reading.
803fb7
803fb7
https://bugs.freedesktop.org/show_bug.cgi?id=89391
803fb7
(cherry picked from commit 09d801a82a46df518dd752e40bf13ac404daa2ce)
803fb7
---
803fb7
 src/journal-remote/journal-remote-parse.c | 31 ++++++++++++++++++-------------
803fb7
 src/journal-remote/journal-remote-parse.h |  4 +++-
803fb7
 2 files changed, 21 insertions(+), 14 deletions(-)
803fb7
803fb7
diff --git a/src/journal-remote/journal-remote-parse.c b/src/journal-remote/journal-remote-parse.c
803fb7
index d9dea8deb..afded7e38 100644
803fb7
--- a/src/journal-remote/journal-remote-parse.c
803fb7
+++ b/src/journal-remote/journal-remote-parse.c
803fb7
@@ -344,22 +344,25 @@ int process_data(RemoteSource *source) {
803fb7
                    LLLLLLLL0011223344...\n
803fb7
                 */
803fb7
                 sep = memchr(line, '=', n);
803fb7
-                if (sep)
803fb7
+                if (sep) {
803fb7
                         /* chomp newline */
803fb7
                         n--;
803fb7
-                else
803fb7
+
803fb7
+                        r = iovw_put(&source->iovw, line, n);
803fb7
+                        if (r < 0)
803fb7
+                                return r;
803fb7
+                } else {
803fb7
                         /* replace \n with = */
803fb7
                         line[n-1] = '=';
803fb7
-                log_trace("Received: %.*s", (int) n, line);
803fb7
 
803fb7
-                r = iovw_put(&source->iovw, line, n);
803fb7
-                if (r < 0) {
803fb7
-                        log_error("Failed to put line in iovect");
803fb7
-                        return r;
803fb7
+                        source->field_len = n;
803fb7
+                        source->state = STATE_DATA_START;
803fb7
+
803fb7
+                        /* we cannot put the field in iovec until we have all data */
803fb7
                 }
803fb7
 
803fb7
-                if (!sep)
803fb7
-                        source->state = STATE_DATA_START;
803fb7
+                log_trace("Received: %.*s (%s)", (int) n, line, sep ? "text" : "binary");
803fb7
+
803fb7
                 return 0; /* continue */
803fb7
         }
803fb7
 
803fb7
@@ -382,6 +385,7 @@ int process_data(RemoteSource *source) {
803fb7
 
803fb7
         case STATE_DATA: {
803fb7
                 void *data;
803fb7
+                char *field;
803fb7
 
803fb7
                 assert(source->data_size > 0);
803fb7
 
803fb7
@@ -396,11 +400,12 @@ int process_data(RemoteSource *source) {
803fb7
 
803fb7
                 assert(data);
803fb7
 
803fb7
-                r = iovw_put(&source->iovw, data, source->data_size);
803fb7
-                if (r < 0) {
803fb7
-                        log_error("failed to put binary buffer in iovect");
803fb7
+                field = (char*) data - sizeof(uint64_t) - source->field_len;
803fb7
+                memmove(field + sizeof(uint64_t), field, source->field_len);
803fb7
+
803fb7
+                r = iovw_put(&source->iovw, field + sizeof(uint64_t), source->field_len + source->data_size);
803fb7
+                if (r < 0)
803fb7
                         return r;
803fb7
-                }
803fb7
 
803fb7
                 source->state = STATE_DATA_FINISH;
803fb7
 
803fb7
diff --git a/src/journal-remote/journal-remote-parse.h b/src/journal-remote/journal-remote-parse.h
803fb7
index 8499f4eb8..22db55091 100644
803fb7
--- a/src/journal-remote/journal-remote-parse.h
803fb7
+++ b/src/journal-remote/journal-remote-parse.h
803fb7
@@ -42,7 +42,9 @@ typedef struct RemoteSource {
803fb7
         size_t offset;     /* offset to the beginning of live data in the buffer */
803fb7
         size_t scanned;    /* number of bytes since the beginning of data without a newline */
803fb7
         size_t filled;     /* total number of bytes in the buffer */
803fb7
-        size_t data_size;  /* size of the binary data chunk being processed */
803fb7
+
803fb7
+        size_t field_len;  /* used for binary fields: the field name length */
803fb7
+        size_t data_size;  /* and the size of the binary data chunk being processed */
803fb7
 
803fb7
         struct iovec_wrapper iovw;
803fb7