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