[ostree/wip/packfile-rebase2] fsck pack files



commit 8631dd28811fc245ee1aaf950ae99527d083a879
Author: Colin Walters <walters verbum org>
Date:   Tue Mar 27 11:36:49 2012 -0400

    fsck pack files

 src/libostree/ostree-repo.c    |   20 +++++-----
 src/libostree/ostree-repo.h    |    6 +++
 src/ostree/ot-builtin-fsck.c   |   84 ++++++++++++++++++++++++++++++++++++++-
 src/ostree/ot-builtin-repack.c |    8 +---
 tests/t0001-archive.sh         |   10 ++++-
 5 files changed, 108 insertions(+), 20 deletions(-)
---
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index a12ba6f..6ee96d4 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -2476,9 +2476,9 @@ get_checksum_from_pack_name (const char *name)
   return g_strndup (dash + 1, 64);
 }
 
-static GFile *
-get_pack_index_path (OstreeRepo *self,
-                     const char *checksum)
+GFile *
+ostree_repo_get_pack_index_path (OstreeRepo *self,
+                                 const char *checksum)
 {
   char *name;
   GFile *ret;
@@ -2490,9 +2490,9 @@ get_pack_index_path (OstreeRepo *self,
   return ret;
 }
 
-static GFile *
-get_pack_data_path (OstreeRepo *self,
-                    const char *checksum)
+GFile *
+ostree_repo_get_pack_data_path (OstreeRepo *self,
+                                const char *checksum)
 {
   char *name;
   GFile *ret;
@@ -2524,7 +2524,7 @@ ostree_repo_load_pack_index (OstreeRepo    *self,
     }
   else
     {
-      path = get_pack_index_path (self, sha256);
+      path = ostree_repo_get_pack_index_path (self, sha256);
       if (!ot_util_variant_map (path,
                                 OSTREE_PACK_INDEX_VARIANT_FORMAT,
                                 &ret_variant, error))
@@ -2579,7 +2579,7 @@ ostree_repo_map_pack_file (OstreeRepo    *self,
   map = g_hash_table_lookup (priv->pack_data_mappings, sha256);
   if (map == NULL)
     {
-      path = get_pack_data_path (self, sha256);
+      path = ostree_repo_get_pack_data_path (self, sha256);
 
       map = g_mapped_file_new (ot_gfile_get_path_cached (path), FALSE, error);
       if (!map)
@@ -2923,7 +2923,7 @@ list_objects_in_index (OstreeRepo                     *self,
   guint32 objtype_u32;
   guint64 offset;
 
-  index_path = get_pack_index_path (self, pack_checksum);
+  index_path = ostree_repo_get_pack_index_path (self, pack_checksum);
 
   if (!ostree_repo_load_pack_index (self, pack_checksum, &index_variant, cancellable, error))
     goto out;
@@ -3077,7 +3077,7 @@ find_object_in_packs (OstreeRepo        *self,
       guint64 offset;
 
       g_clear_object (&index_path);
-      index_path = get_pack_index_path (self, pack_checksum);
+      index_path = ostree_repo_get_pack_index_path (self, pack_checksum);
 
       ot_clear_gvariant (&index_variant);
       if (!ostree_repo_load_pack_index (self, pack_checksum, &index_variant, cancellable, error))
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index a825d48..7c38176 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -280,6 +280,12 @@ gboolean ostree_repo_list_pack_indexes (OstreeRepo              *self,
                                         GCancellable            *cancellable,
                                         GError                 **error);
 
+GFile * ostree_repo_get_pack_index_path (OstreeRepo         *self,
+                                         const char         *checksum);
+
+GFile * ostree_repo_get_pack_data_path (OstreeRepo         *self,
+                                        const char         *checksum);
+
 G_END_DECLS
 
 #endif /* _OSTREE_REPO */
diff --git a/src/ostree/ot-builtin-fsck.c b/src/ostree/ot-builtin-fsck.c
index 6e5de25..121e233 100644
--- a/src/ostree/ot-builtin-fsck.c
+++ b/src/ostree/ot-builtin-fsck.c
@@ -26,6 +26,7 @@
 #include "ostree.h"
 
 #include <glib/gi18n.h>
+#include <glib/gprintf.h>
 
 static gboolean quiet;
 static gboolean delete;
@@ -123,6 +124,29 @@ checksum_archived_file (OtFsckData   *data,
   return ret;
 }
 
+static void
+encountered_fsck_error (OtFsckData  *data,
+                        const char  *fmt,
+                        ...) G_GNUC_PRINTF (2, 3);
+
+static void
+encountered_fsck_error (OtFsckData  *data,
+                        const char  *fmt,
+                        ...)
+{
+  va_list args;
+  char *msg;
+
+  va_start (args, fmt);
+
+  g_vasprintf (&msg, fmt, args);
+  g_printerr ("ERROR: %s\n", msg);
+  data->had_error = TRUE;
+
+  va_end (args);
+}
+                        
+
 static gboolean
 fsck_loose_object (OtFsckData    *data,
                    const char    *exp_checksum,
@@ -158,9 +182,9 @@ fsck_loose_object (OtFsckData    *data,
 
   if (real_checksum && strcmp (exp_checksum, g_checksum_get_string (real_checksum)) != 0)
     {
-      data->had_error = TRUE;
-      g_printerr ("ERROR: corrupted object '%s'; actual checksum: %s\n",
-                  ot_gfile_get_path_cached (objf), g_checksum_get_string (real_checksum));
+
+      encountered_fsck_error (data, "corrupted object '%s'; actual checksum: %s",
+                              ot_gfile_get_path_cached (objf), g_checksum_get_string (real_checksum));
       if (delete)
         (void) unlink (ot_gfile_get_path_cached (objf));
     }
@@ -173,6 +197,57 @@ fsck_loose_object (OtFsckData    *data,
   return ret;
 }
 
+static gboolean
+fsck_pack_files (OtFsckData  *data,
+                 GCancellable   *cancellable,
+                 GError        **error)
+{
+  gboolean ret = FALSE;
+  GPtrArray *pack_indexes = NULL;
+  GFile *pack_data_path = NULL;
+  GInputStream *input = NULL;
+  GChecksum *pack_content_checksum = NULL;
+  guint i;
+
+  if (!ostree_repo_list_pack_indexes (data->repo, &pack_indexes, cancellable, error))
+    goto out;
+
+  for (i = 0; i < pack_indexes->len; i++)
+    {
+      const char *checksum = pack_indexes->pdata[i];
+
+      g_clear_object (&pack_data_path);
+      pack_data_path = ostree_repo_get_pack_data_path (data->repo, checksum);
+      
+      g_clear_object (&input);
+      input = (GInputStream*)g_file_read (pack_data_path, cancellable, error);
+      if (!input)
+        goto out;
+     
+      if (pack_content_checksum)
+        g_checksum_free (pack_content_checksum);
+      if (!ot_gio_checksum_stream (input, &pack_content_checksum, cancellable, error))
+        goto out;
+
+      if (strcmp (g_checksum_get_string (pack_content_checksum), checksum) != 0)
+        {
+          encountered_fsck_error (data, "corrupted pack '%s', expected checksum %s",
+                                  checksum, g_checksum_get_string (pack_content_checksum));
+        }
+    }
+
+  ret = TRUE;
+ out:
+  if (pack_content_checksum)
+    g_checksum_free (pack_content_checksum);
+  if (pack_indexes)
+    g_ptr_array_unref (pack_indexes);
+  g_clear_object (&pack_data_path);
+  g_clear_object (&input);
+  return ret;
+}
+
+
 gboolean
 ostree_builtin_fsck (int argc, char **argv, GFile *repo_path, GError **error)
 {
@@ -224,6 +299,9 @@ ostree_builtin_fsck (int argc, char **argv, GFile *repo_path, GError **error)
         }
     }
 
+  if (!fsck_pack_files (&data, cancellable, error))
+    goto out;
+
   if (data.had_error)
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
diff --git a/src/ostree/ot-builtin-repack.c b/src/ostree/ot-builtin-repack.c
index 9bbbce6..bf596c2 100644
--- a/src/ostree/ot-builtin-repack.c
+++ b/src/ostree/ot-builtin-repack.c
@@ -353,12 +353,8 @@ create_pack_file (OtRepackData        *data,
         {
           if (!g_input_stream_read_all (read_object_in, buf, sizeof(buf), &bytes_read, cancellable, error))
             goto out;
-          if (bytes_read > 0)
-            {
-              g_checksum_update (pack_checksum, (guint8*)buf, bytes_read);
-              if (!g_output_stream_write_all ((GOutputStream*)object_data_stream, buf, bytes_read, &bytes_written, cancellable, error))
-                goto out;
-            }
+          if (!g_output_stream_write_all ((GOutputStream*)object_data_stream, buf, bytes_read, &bytes_written, cancellable, error))
+            goto out;
         }
       while (bytes_read > 0);
 
diff --git a/tests/t0001-archive.sh b/tests/t0001-archive.sh
index 61dec40..47efe53 100755
--- a/tests/t0001-archive.sh
+++ b/tests/t0001-archive.sh
@@ -21,7 +21,7 @@ set -e
 
 . libtest.sh
 
-echo '1..14'
+echo '1..16'
 
 setup_test_repository "archive"
 echo "ok setup"
@@ -72,6 +72,10 @@ cd ${test_tmpdir}
 $OSTREE repack --keep-loose
 echo "ok repack"
 
+cd ${test_tmpdir}
+$OSTREE fsck
+echo "ok fsck"
+
 $OSTREE checkout test2 checkout-test2-from-packed
 echo "ok checkout union 1"
 
@@ -79,5 +83,9 @@ cd ${test_tmpdir}
 $OSTREE repack
 echo "ok repack delete loose"
 
+cd ${test_tmpdir}
+$OSTREE fsck
+echo "ok fsck"
+
 $OSTREE repack --analyze-only
 echo "ok repack analyze"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]