From f2b4a9e10729321d7fd90382cda28d450479b336 Mon Sep 17 00:00:00 2001
From: John Hiesey <john@hiesey.com>
Date: Wed, 7 Oct 2015 14:43:13 -0700
Subject: [PATCH] static-delta: Don't run bspatch when output object already
 exists

There is already a check that the destination object does not
exist in all other cases when processing an incoming static delta.
However, the bspatch case would still try to run and fail. Add
an analogous check to that case as well.

https://bugzilla.gnome.org/show_bug.cgi?id=756260
---
 .../ostree-repo-static-delta-processing.c     | 47 ++++++++++---------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/src/libostree/ostree-repo-static-delta-processing.c b/src/libostree/ostree-repo-static-delta-processing.c
index 18687f8a..f60ec7a1 100644
--- a/src/libostree/ostree-repo-static-delta-processing.c
+++ b/src/libostree/ostree-repo-static-delta-processing.c
@@ -530,32 +530,35 @@ dispatch_bspatch (OstreeRepo                 *repo,
   if (!read_varuint64 (state, &length, error))
     goto out;
 
-  input_mfile = g_mapped_file_new_from_fd (state->read_source_fd, FALSE, error);
-  if (!input_mfile)
-    goto out;
+  if (!state->have_obj)
+    {
+      input_mfile = g_mapped_file_new_from_fd (state->read_source_fd, FALSE, error);
+      if (!input_mfile)
+        goto out;
 
-  buf = g_malloc0 (state->content_size);
+      buf = g_malloc0 (state->content_size);
 
-  opaque.state = state;
-  opaque.offset = offset;
-  opaque.length = length;
-  stream.read = bspatch_read;
-  stream.opaque = &opaque;
-  if (bspatch ((const guint8*)g_mapped_file_get_contents (input_mfile),
-               g_mapped_file_get_length (input_mfile),
-               buf,
-               state->content_size,
-               &stream) < 0)
-    goto out;
+      opaque.state = state;
+      opaque.offset = offset;
+      opaque.length = length;
+      stream.read = bspatch_read;
+      stream.opaque = &opaque;
+      if (bspatch ((const guint8*)g_mapped_file_get_contents (input_mfile),
+                   g_mapped_file_get_length (input_mfile),
+                   buf,
+                   state->content_size,
+                   &stream) < 0)
+        goto out;
 
-  if (!g_output_stream_write_all (state->content_out,
-                                  buf,
-                                  state->content_size,
-                                  &bytes_written,
-                                  cancellable, error))
-    goto out;
+      if (!g_output_stream_write_all (state->content_out,
+                                      buf,
+                                      state->content_size,
+                                      &bytes_written,
+                                      cancellable, error))
+        goto out;
 
-  g_assert (bytes_written == state->content_size);
+      g_assert (bytes_written == state->content_size);
+    }
 
   ret = TRUE;
  out: