Add _ostree_repo_open|commit_untrusted_content_bare
authorAlexander Larsson <alexl@redhat.com>
Thu, 15 Oct 2015 17:34:04 +0000 (19:34 +0200)
committerAlexander Larsson <alexl@redhat.com>
Tue, 10 Nov 2015 07:56:14 +0000 (08:56 +0100)
Also renames OstreeRepoTrustedContentBareCommit to
OstreeRepoContentBareCommit so that it can be used by both.

This will be needed when we introduce checksum verification of objects
in static deltas.

src/libostree/ostree-repo-commit.c
src/libostree/ostree-repo-private.h
src/libostree/ostree-repo-static-delta-processing.c

index 62da0e35d91eda8690b8d6b58a65dcbefc073d47..a761a8a9dee84478305269255ed7b3fc5845f609 100644 (file)
@@ -446,11 +446,106 @@ fallocate_stream (GFileDescriptorBased      *stream,
   return ret;
 }
 
+gboolean
+_ostree_repo_open_untrusted_content_bare (OstreeRepo          *self,
+                                          const char          *expected_checksum,
+                                          guint64              content_len,
+                                          OstreeRepoContentBareCommit *out_state,
+                                          GOutputStream      **out_stream,
+                                          gboolean            *out_have_object,
+                                          GCancellable        *cancellable,
+                                          GError             **error)
+{
+  /* The trusted codepath is fine here */
+  return _ostree_repo_open_trusted_content_bare (self,
+                                                 expected_checksum,
+                                                 content_len,
+                                                 out_state,
+                                                 out_stream,
+                                                 out_have_object,
+                                                 cancellable,
+                                                 error);
+}
+
+gboolean
+_ostree_repo_commit_untrusted_content_bare (OstreeRepo          *self,
+                                            const char          *expected_checksum,
+                                            OstreeRepoContentBareCommit *state,
+                                            guint32              uid,
+                                            guint32              gid,
+                                            guint32              mode,
+                                            GVariant            *xattrs,
+                                            GCancellable        *cancellable,
+                                            GError             **error)
+{
+  gboolean ret = FALSE;
+
+  if (state->fd != -1)
+    {
+      GMappedFile *mapped;
+      g_autoptr(GBytes) bytes = NULL;
+      g_autoptr(GInputStream) raw_in = NULL;
+      g_autoptr(GInputStream) in = NULL;
+      g_autoptr(GFileInfo) file_info = NULL;
+      g_autofree guchar *actual_csum = NULL;
+      g_autofree char *actual_checksum = NULL;
+      int fd;
+
+      fd = openat (self->tmp_dir_fd, state->temp_filename, O_RDONLY);
+      if (fd == -1)
+        {
+          gs_set_error_from_errno (error, errno);
+          goto out;
+        }
+
+      mapped = g_mapped_file_new_from_fd (fd, FALSE, error);
+
+      close (fd);
+      if (mapped == NULL)
+        goto out;
+
+      bytes = g_mapped_file_get_bytes (mapped);
+      g_mapped_file_unref (mapped);
+
+      raw_in = g_memory_input_stream_new_from_bytes (bytes);
+
+      file_info = _ostree_header_gfile_info_new (mode, uid, gid);
+
+      if (!ostree_raw_file_to_content_stream (raw_in, file_info, xattrs, &in, NULL, cancellable, error))
+        goto out;
+
+      if (!ot_gio_checksum_stream (in, &actual_csum, cancellable, error))
+        goto out;
+
+      actual_checksum = ostree_checksum_from_bytes (actual_csum);
+
+      if (strcmp (actual_checksum, expected_checksum) != 0)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Corrupted object %s (actual checksum is %s)",
+                       expected_checksum, actual_checksum);
+          goto out;
+        }
+
+      if (!commit_loose_object_trusted (self, expected_checksum, OSTREE_OBJECT_TYPE_FILE,
+                                        state->temp_filename,
+                                        FALSE, uid, gid, mode,
+                                        xattrs, state->fd,
+                                        cancellable, error))
+        goto out;
+    }
+
+  ret = TRUE;
+ out:
+  g_free (state->temp_filename);
+  return ret;
+}
+
 gboolean
 _ostree_repo_open_trusted_content_bare (OstreeRepo          *self,
                                         const char          *checksum,
                                         guint64              content_len,
-                                        OstreeRepoTrustedContentBareCommit *out_state,
+                                        OstreeRepoContentBareCommit *out_state,
                                         GOutputStream      **out_stream,
                                         gboolean            *out_have_object,
                                         GCancellable        *cancellable,
@@ -495,7 +590,7 @@ _ostree_repo_open_trusted_content_bare (OstreeRepo          *self,
 gboolean
 _ostree_repo_commit_trusted_content_bare (OstreeRepo          *self,
                                           const char          *checksum,
-                                          OstreeRepoTrustedContentBareCommit *state,
+                                          OstreeRepoContentBareCommit *state,
                                           guint32              uid,
                                           guint32              gid,
                                           guint32              mode,
index 8f86fec45ca3e345af764eff85d93a1da5ee8166..ccb648f8b4ebbd472352c57bf672ab8d6e535623 100644 (file)
@@ -245,13 +245,13 @@ _ostree_repo_commit_loose_final (OstreeRepo        *self,
 typedef struct {
   int fd;
   char *temp_filename;
-} OstreeRepoTrustedContentBareCommit;
+} OstreeRepoContentBareCommit;
 
 gboolean
 _ostree_repo_open_trusted_content_bare (OstreeRepo          *self,
                                         const char          *checksum,
                                         guint64              content_len,
-                                        OstreeRepoTrustedContentBareCommit *out_state,
+                                        OstreeRepoContentBareCommit *out_state,
                                         GOutputStream      **out_stream,
                                         gboolean            *out_have_object,
                                         GCancellable        *cancellable,
@@ -260,7 +260,7 @@ _ostree_repo_open_trusted_content_bare (OstreeRepo          *self,
 gboolean
 _ostree_repo_commit_trusted_content_bare (OstreeRepo          *self,
                                           const char          *checksum,
-                                          OstreeRepoTrustedContentBareCommit *state,
+                                          OstreeRepoContentBareCommit *state,
                                           guint32              uid,
                                           guint32              gid,
                                           guint32              mode,
@@ -268,6 +268,28 @@ _ostree_repo_commit_trusted_content_bare (OstreeRepo          *self,
                                           GCancellable        *cancellable,
                                           GError             **error);
 
+gboolean
+_ostree_repo_open_untrusted_content_bare (OstreeRepo          *self,
+                                          const char          *expected_checksum,
+                                          guint64              content_len,
+                                          OstreeRepoContentBareCommit *out_state,
+                                          GOutputStream      **out_stream,
+                                          gboolean            *out_have_object,
+                                          GCancellable        *cancellable,
+                                          GError             **error);
+
+gboolean
+_ostree_repo_commit_untrusted_content_bare (OstreeRepo          *self,
+                                            const char          *expected_checksum,
+                                            OstreeRepoContentBareCommit *state,
+                                            guint32              uid,
+                                            guint32              gid,
+                                            guint32              mode,
+                                            GVariant            *xattrs,
+                                            GCancellable        *cancellable,
+                                            GError             **error);
+
+
 gboolean
 _ostree_repo_read_bare_fd (OstreeRepo           *self,
                            const char           *checksum,
index f60ec7a1b8b6ff9e5e91d5b0d56c8b1928007ae5..496c9abbe24c632a248caf4d81bf190c56f0173b 100644 (file)
@@ -55,7 +55,7 @@ typedef struct {
   GError        **async_error;
 
   OstreeObjectType output_objtype;
-  OstreeRepoTrustedContentBareCommit barecommitstate;
+  OstreeRepoContentBareCommit barecommitstate;
   guint64          content_size;
   GOutputStream   *content_out;
   char             checksum[65];