You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2014/11/01 15:11:05 UTC

svn commit: r1635950 - in /subversion/trunk/subversion/libsvn_fs_x: ./ cached_data.c index.c index.h low_level.c low_level.h rev_file.c rev_file.h transaction.c verify.c

Author: stefan2
Date: Sat Nov  1 14:11:05 2014
New Revision: 1635950

URL: http://svn.apache.org/r1635950
Log:
Sync FSX with FSFS:  Merge revisions
r1627966, r1628093, r1628159, r1631563, r1631567, r1631588, r1631598
and r1634920, resolved the usual conflicts and adapted the code to FSX.

This ports the latest indexing improvements (platform independence,
index checksumming, text markers) to FSX.

Modified:
    subversion/trunk/subversion/libsvn_fs_x/   (props changed)
    subversion/trunk/subversion/libsvn_fs_x/cached_data.c
    subversion/trunk/subversion/libsvn_fs_x/index.c
    subversion/trunk/subversion/libsvn_fs_x/index.h
    subversion/trunk/subversion/libsvn_fs_x/low_level.c
    subversion/trunk/subversion/libsvn_fs_x/low_level.h
    subversion/trunk/subversion/libsvn_fs_x/rev_file.c
    subversion/trunk/subversion/libsvn_fs_x/rev_file.h
    subversion/trunk/subversion/libsvn_fs_x/transaction.c
    subversion/trunk/subversion/libsvn_fs_x/verify.c

Propchange: subversion/trunk/subversion/libsvn_fs_x/
------------------------------------------------------------------------------
  Merged /subversion/trunk/subversion/libsvn_fs_fs:r1627966,1628093,1628159,1631563,1631567,1631588,1631598,1634920

Modified: subversion/trunk/subversion/libsvn_fs_x/cached_data.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/cached_data.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/cached_data.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/cached_data.c Sat Nov  1 14:11:05 2014
@@ -70,7 +70,7 @@ static svn_error_t *
 dgb__log_access(svn_fs_t *fs,
                 const svn_fs_x__id_part_t *id,
                 void *item,
-                int item_type,
+                apr_uint32_t item_type,
                 apr_pool_t *scratch_pool)
 {
   /* no-op if this macro is not defined */

Modified: subversion/trunk/subversion/libsvn_fs_x/index.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/index.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/index.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/index.c Sat Nov  1 14:11:05 2014
@@ -44,6 +44,33 @@
 /* maximum length of a uint64 in an 7/8b encoding */
 #define ENCODED_INT_LENGTH 10
 
+/* APR is missing an APR_OFF_T_MAX.  So, define one.  We will use it to
+ * limit file offsets stored in the indexes.
+ *
+ * We assume that everything shorter than 64 bits, it is at least 32 bits.
+ * We also assume that the type is always signed meaning we only have an
+ * effective positive range of 63 or 31 bits, respectively.
+ */
+static
+const apr_uint64_t off_t_max = (sizeof(apr_off_t) == sizeof(apr_int64_t))
+                             ? APR_INT64_MAX
+                             : APR_INT32_MAX;
+
+/* We store P2L proto-index entries as 6 values, 64 bits each on disk.
+ * See also svn_fs_fs__p2l_proto_index_add_entry().
+ */
+#define P2L_PROTO_INDEX_ENTRY_SIZE (6 * sizeof(apr_uint64_t))
+
+/* We put this string in front of the L2P index header. */
+#define L2P_STREAM_PREFIX "L2P-INDEX\n"
+
+/* We put this string in front of the P2L index header. */
+#define P2L_STREAM_PREFIX "P2L-INDEX\n"
+
+/* Size of the buffer that will fit the index header prefixes. */
+#define STREAM_PREFIX_LEN MAX(sizeof(L2P_STREAM_PREFIX), \
+                              sizeof(P2L_STREAM_PREFIX))
+
 /* Page tables in the log-to-phys index file exclusively contain entries
  * of this type to describe position and size of a given page.
  */
@@ -321,22 +348,45 @@ packed_stream_read(svn_fs_x__packed_numb
 
 /* Create and open a packed number stream reading from offsets START to
  * END in FILE and return it in *STREAM.  Access the file in chunks of
- * BLOCK_SIZE bytes.  Allocate *STREAM in RESULT_POOL.
+ * BLOCK_SIZE bytes.  Expect the stream to be prefixed by STREAM_PREFIX.
+ * Allocate *STREAM in RESULT_POOL and use SCRATCH_POOL for temporaries.
  */
 static svn_error_t *
 packed_stream_open(svn_fs_x__packed_number_stream_t **stream,
                    apr_file_t *file,
                    apr_off_t start,
                    apr_off_t end,
+                   const char *stream_prefix,
                    apr_size_t block_size,
-                   apr_pool_t *result_pool)
+                   apr_pool_t *result_pool,
+                   apr_pool_t *scratch_pool)
 {
-  svn_fs_x__packed_number_stream_t *result
-    = apr_palloc(result_pool, sizeof(*result));
+  char buffer[STREAM_PREFIX_LEN + 1] = { 0 };
+  apr_size_t len = strlen(stream_prefix);
+  svn_fs_x__packed_number_stream_t *result;
+
+  /* If this is violated, we forgot to adjust STREAM_PREFIX_LEN after
+   * changing the index header prefixes. */
+  SVN_ERR_ASSERT(len < sizeof(buffer));
+
+  /* Read the header prefix and compare it with the expected prefix */
+  SVN_ERR(svn_io_file_aligned_seek(file, block_size, NULL, start,
+                                   scratch_pool));
+  SVN_ERR(svn_io_file_read_full2(file, buffer, len, NULL, NULL,
+                                 scratch_pool));
+
+  if (strncmp(buffer, stream_prefix, len))
+    return svn_error_createf(SVN_ERR_FS_INDEX_CORRUPTION, NULL,
+                             _("Index stream header prefix mismatch.\n"
+                               "  expected: %s"
+                               "  found: %s"), stream_prefix, buffer);
+
+  /* Construct the actual stream object. */
+  result = apr_palloc(result_pool, sizeof(*result));
 
   result->pool = result_pool;
   result->file = file;
-  result->stream_start = start;
+  result->stream_start = start + len;
   result->stream_end = end;
 
   result->used = 0;
@@ -417,6 +467,138 @@ packed_stream_offset(svn_fs_x__packed_nu
   return file_offset - stream->stream_start;
 }
 
+/* Write VALUE to the PROTO_INDEX file, using SCRATCH_POOL for temporary
+ * allocations.
+ *
+ * The point of this function is to ensure an architecture-independent
+ * proto-index file format.  All data is written as unsigned 64 bits ints
+ * in little endian byte order.  64 bits is the largest portable integer
+ * we have and unsigned values have well-defined conversions in C.
+ */
+static svn_error_t *
+write_uint64_to_proto_index(apr_file_t *proto_index,
+                            apr_uint64_t value,
+                            apr_pool_t *scratch_pool)
+{
+  apr_byte_t buffer[sizeof(value)];
+  int i;
+  apr_size_t written;
+
+  /* Split VALUE into 8 bytes using LE ordering. */
+  for (i = 0; i < sizeof(buffer); ++i)
+    {
+      /* Unsigned conversions are well-defined ... */
+      buffer[i] = (apr_byte_t)value;
+      value >>= CHAR_BIT;
+    }
+
+  /* Write it all to disk. */
+  SVN_ERR(svn_io_file_write_full(proto_index, buffer, sizeof(buffer),
+                                 &written, scratch_pool));
+  SVN_ERR_ASSERT(written == sizeof(buffer));
+
+  return SVN_NO_ERROR;
+}
+
+/* Read one unsigned 64 bit value from PROTO_INDEX file and return it in
+ * *VALUE_P.  If EOF is NULL, error out when trying to read beyond EOF.
+ * Use SCRATCH_POOL for temporary allocations.
+ *
+ * This function is the inverse to write_uint64_to_proto_index (see there),
+ * reading the external LE byte order and convert it into host byte order.
+ */
+static svn_error_t *
+read_uint64_from_proto_index(apr_file_t *proto_index,
+                             apr_uint64_t *value_p,
+                             svn_boolean_t *eof,
+                             apr_pool_t *scratch_pool)
+{
+  apr_byte_t buffer[sizeof(*value_p)];
+  apr_size_t read;
+
+  /* Read the full 8 bytes or our 64 bit value, unless we hit EOF.
+   * Assert that we never read partial values. */
+  SVN_ERR(svn_io_file_read_full2(proto_index, buffer, sizeof(buffer),
+                                 &read, eof, scratch_pool));
+  SVN_ERR_ASSERT((eof && *eof) || read == sizeof(buffer));
+
+  /* If we did not hit EOF, reconstruct the uint64 value and return it. */
+  if (!eof || !*eof)
+    {
+      int i;
+      apr_uint64_t value;
+
+      /* This could only overflow if CHAR_BIT had a value that is not
+       * a divisor of 64. */
+      value = 0;
+      for (i = sizeof(buffer) - 1; i >= 0; --i)
+        value = (value << CHAR_BIT) + buffer[i];
+
+      *value_p = value;
+    }
+
+  return SVN_NO_ERROR;
+}
+
+/* Convenience function similar to read_uint64_from_proto_index, but returns
+ * an uint32 value in VALUE_P.  Return an error if the value does not fit.
+ */
+static svn_error_t *
+read_uint32_from_proto_index(apr_file_t *proto_index,
+                             apr_uint32_t *value_p,
+                             svn_boolean_t *eof,
+                             apr_pool_t *scratch_pool)
+{
+  apr_uint64_t value;
+  SVN_ERR(read_uint64_from_proto_index(proto_index, &value, eof,
+                                       scratch_pool));
+  if (!eof || !*eof)
+    {
+      if (value > APR_UINT32_MAX)
+        return svn_error_createf(SVN_ERR_FS_INDEX_OVERFLOW, NULL,
+                                _("UINT32 0x%" APR_UINT64_T_HEX_FMT
+                                  " too large, max = 0x%"
+                                  APR_UINT64_T_HEX_FMT),
+                                value, (apr_uint64_t)APR_UINT32_MAX);
+
+      /* This conversion is not lossy because the value can be represented
+       * in the target type. */
+      *value_p = (apr_uint32_t)value;
+    }
+
+  return SVN_NO_ERROR;
+}
+
+/* Convenience function similar to read_uint64_from_proto_index, but returns
+ * an off_t value in VALUE_P.  Return an error if the value does not fit.
+ */
+static svn_error_t *
+read_off_t_from_proto_index(apr_file_t *proto_index,
+                            apr_off_t *value_p,
+                            svn_boolean_t *eof,
+                            apr_pool_t *scratch_pool)
+{
+  apr_uint64_t value;
+  SVN_ERR(read_uint64_from_proto_index(proto_index, &value, eof,
+                                       scratch_pool));
+  if (!eof || !*eof)
+    {
+      if (value > off_t_max)
+        return svn_error_createf(SVN_ERR_FS_INDEX_OVERFLOW, NULL,
+                                _("File offset 0x%" APR_UINT64_T_HEX_FMT
+                                  " too large, max = 0x%"
+                                  APR_UINT64_T_HEX_FMT),
+                                value, off_t_max);
+
+      /* Shortening conversion from unsigned to signed int is well-defined
+       * and not lossy in C because the value can be represented in the
+       * target type. */
+      *value_p = (apr_off_t)value;
+    }
+
+  return SVN_NO_ERROR;
+}
+
 /*
  * log-to-phys index
  */
@@ -432,18 +614,41 @@ svn_fs_x__l2p_proto_index_open(apr_file_
   return SVN_NO_ERROR;
 }
 
-/* Write ENTRY to log-to-phys PROTO_INDEX file and verify the results.
+/* Append ENTRY to log-to-phys PROTO_INDEX file.
  * Use SCRATCH_POOL for temporary allocations.
  */
 static svn_error_t *
-write_entry_to_proto_index(apr_file_t *proto_index,
-                           l2p_proto_entry_t entry,
-                           apr_pool_t *scratch_pool)
+write_l2p_entry_to_proto_index(apr_file_t *proto_index,
+                               l2p_proto_entry_t entry,
+                               apr_pool_t *scratch_pool)
 {
-  apr_size_t written = sizeof(entry);
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry.offset,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry.item_index,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry.sub_item,
+                                      scratch_pool));
 
-  SVN_ERR(svn_io_file_write(proto_index, &entry, &written, scratch_pool));
-  SVN_ERR_ASSERT(written == sizeof(entry));
+  return SVN_NO_ERROR;
+}
+
+/* Read *ENTRY from log-to-phys PROTO_INDEX file and indicate end-of-file
+ * in *EOF, or error out in that case if EOF is NULL.  *ENTRY is in an
+ * undefined state if an end-of-file occurred.
+ * Use SCRATCH_POOL for temporary allocations.
+ */
+static svn_error_t *
+read_l2p_entry_from_proto_index(apr_file_t *proto_index,
+                                l2p_proto_entry_t *entry,
+                                svn_boolean_t *eof,
+                                apr_pool_t *scratch_pool)
+{
+  SVN_ERR(read_uint64_from_proto_index(proto_index, &entry->offset, eof,
+                                       scratch_pool));
+  SVN_ERR(read_uint64_from_proto_index(proto_index, &entry->item_index, eof,
+                                       scratch_pool));
+  SVN_ERR(read_uint32_from_proto_index(proto_index, &entry->sub_item, eof,
+                                       scratch_pool));
 
   return SVN_NO_ERROR;
 }
@@ -453,8 +658,8 @@ svn_fs_x__l2p_proto_index_add_revision(a
                                        apr_pool_t *scratch_pool)
 {
   l2p_proto_entry_t entry = { 0 };
-  return svn_error_trace(write_entry_to_proto_index(proto_index, entry,
-                                                    scratch_pool));
+  return svn_error_trace(write_l2p_entry_to_proto_index(proto_index, entry,
+                                                        scratch_pool));
 }
 
 svn_error_t *
@@ -480,8 +685,8 @@ svn_fs_x__l2p_proto_index_add_entry(apr_
   /* no limits on the container sub-item index */
   entry.sub_item = sub_item;
 
-  return svn_error_trace(write_entry_to_proto_index(proto_index, entry,
-                                                    scratch_pool));
+  return svn_error_trace(write_l2p_entry_to_proto_index(proto_index, entry,
+                                                        scratch_pool));
 }
 
 /* Encode VALUE as 7/8b into P and return the number of bytes written.
@@ -512,6 +717,18 @@ encode_int(unsigned char *p, apr_int64_t
   return encode_uint(p, (apr_uint64_t)(value < 0 ? -1 - 2*value : 2*value));
 }
 
+/* Append VALUE to STREAM in 7/8b encoding.
+ */
+static svn_error_t *
+stream_write_encoded(svn_stream_t *stream,
+                     apr_uint64_t value)
+{
+  unsigned char encoded[ENCODED_INT_LENGTH];
+ 
+  apr_size_t len = encode_uint(encoded, value);
+  return svn_error_trace(svn_stream_write(stream, (char *)encoded, &len));
+}
+
 /* Run-length-encode the uint64 numbers in ARRAY starting at index START
  * up to but not including END.  All numbers must be > 0.
  * Return the number of remaining entries in ARRAY after START.
@@ -672,19 +889,21 @@ encode_l2p_page(apr_array_header_t *entr
 }
 
 svn_error_t *
-svn_fs_x__l2p_index_append(svn_fs_t *fs,
+svn_fs_x__l2p_index_append(svn_checksum_t **checksum,
+                           svn_fs_t *fs,
                            apr_file_t *index_file,
                            const char *proto_file_name,
                            svn_revnum_t revision,
+                           apr_pool_t * result_pool,
                            apr_pool_t *scratch_pool)
 {
   fs_x_data_t *ffd = fs->fsap_data;
   apr_file_t *proto_index = NULL;
+  svn_stream_t *stream;
   int i;
   int end;
   apr_uint64_t entry;
   svn_boolean_t eof = FALSE;
-  unsigned char encoded[ENCODED_INT_LENGTH];
 
   int last_page_count = 0;          /* total page count at the start of
                                        the current revision */
@@ -726,13 +945,10 @@ svn_fs_x__l2p_index_append(svn_fs_t *fs,
   for (entry = 0; !eof; ++entry)
     {
       l2p_proto_entry_t proto_entry;
-      apr_size_t read = 0;
 
       /* (attempt to) read the next entry from the source */
-      SVN_ERR(svn_io_file_read_full2(proto_index,
-                                     &proto_entry, sizeof(proto_entry),
-                                     &read, &eof, local_pool));
-      SVN_ERR_ASSERT(eof || read == sizeof(proto_entry));
+      SVN_ERR(read_l2p_entry_from_proto_index(proto_index, &proto_entry,
+                                              &eof, local_pool));
 
       /* handle new revision */
       if ((entry > 0 && proto_entry.offset == 0) || eof)
@@ -803,48 +1019,40 @@ svn_fs_x__l2p_index_append(svn_fs_t *fs,
                               " exceeds current limit of 2G pages"),
                             page_counts->nelts);
 
+  /* open target stream. */
+  stream = svn_stream_checksummed2(svn_stream_from_aprfile2(index_file, TRUE,
+                                                            local_pool),
+                                   NULL, checksum, svn_checksum_md5, FALSE,
+                                   result_pool);
+
+
   /* write header info */
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, revision),
-                                 NULL, local_pool));
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, page_counts->nelts),
-                                 NULL, local_pool));
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, ffd->l2p_page_size),
-                                 NULL, local_pool));
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, page_sizes->nelts),
-                                 NULL, local_pool));
+  SVN_ERR(svn_stream_puts(stream, L2P_STREAM_PREFIX));
+  SVN_ERR(stream_write_encoded(stream, revision));
+  SVN_ERR(stream_write_encoded(stream, page_counts->nelts));
+  SVN_ERR(stream_write_encoded(stream, ffd->l2p_page_size));
+  SVN_ERR(stream_write_encoded(stream, page_sizes->nelts));
 
   /* write the revision table */
   end = rle_array(page_counts, 0, page_counts->nelts);
   for (i = 0; i < end; ++i)
     {
       apr_uint64_t value = APR_ARRAY_IDX(page_counts, i, apr_uint64_t);
-      SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                     encode_uint(encoded, value),
-                                     NULL, local_pool));
+      SVN_ERR(stream_write_encoded(stream, value));
     }
-    
+
   /* write the page table */
   for (i = 0; i < page_sizes->nelts; ++i)
     {
       apr_uint64_t value = APR_ARRAY_IDX(page_sizes, i, apr_uint64_t);
-      SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                     encode_uint(encoded, value),
-                                     NULL, local_pool));
+      SVN_ERR(stream_write_encoded(stream, value));
       value = APR_ARRAY_IDX(entry_counts, i, apr_uint64_t);
-      SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                     encode_uint(encoded, value),
-                                     NULL, local_pool));
+      SVN_ERR(stream_write_encoded(stream, value));
     }
 
-  /* append page contents */
+  /* append page contents and implicitly close STREAM */
   SVN_ERR(svn_stream_copy3(svn_stream__from_spillbuf(buffer, local_pool),
-                           svn_stream_from_aprfile2(index_file, TRUE,
-                                                    local_pool),
-                           NULL, NULL, local_pool));
+                           stream, NULL, NULL, local_pool));
 
   svn_pool_destroy(local_pool);
 
@@ -1029,7 +1237,9 @@ auto_open_l2p_index(svn_fs_x__revision_f
                                  rev_file->file,
                                  rev_file->l2p_offset,
                                  rev_file->p2l_offset,
-                                 ffd->block_size,
+                                 L2P_STREAM_PREFIX,
+                                 (apr_size_t)ffd->block_size,
+                                 rev_file->pool,
                                  rev_file->pool));
     }
 
@@ -1683,12 +1893,10 @@ l2p_proto_index_lookup(apr_off_t *offset
   while (!eof)
     {
       l2p_proto_entry_t entry;
-      apr_size_t read = 0;
 
       /* (attempt to) read the next entry from the source */
-      SVN_ERR(svn_io_file_read_full2(file, &entry, sizeof(entry),
-                                     &read, &eof, scratch_pool));
-      SVN_ERR_ASSERT(eof || read == sizeof(entry));
+      SVN_ERR(read_l2p_entry_from_proto_index(file, &entry, &eof,
+                                              scratch_pool));
 
       /* handle new revision */
       if (!eof && entry.item_index == item_index)
@@ -1801,33 +2009,138 @@ svn_fs_x__p2l_proto_index_open(apr_file_
 
 svn_error_t *
 svn_fs_x__p2l_proto_index_add_entry(apr_file_t *proto_index,
-                                    svn_fs_x__p2l_entry_t *entry,
+                                    const svn_fs_x__p2l_entry_t *entry,
                                     apr_pool_t *scratch_pool)
 {
-  apr_size_t written = sizeof(*entry);
-  apr_size_t written_total = 0;
+  apr_uint64_t revision;
+  apr_int32_t i;
 
-  /* Write main record. */
-  SVN_ERR(svn_io_file_write_full(proto_index, entry, sizeof(*entry),
-                                 &written, scratch_pool));
-  SVN_ERR_ASSERT(written == sizeof(*entry));
-  written_total += written;
+  /* Make sure all signed elements of ENTRY have non-negative values.
+   *
+   * For file offsets and sizes, this is a given as we use them to describe
+   * absolute positions and sizes.  For revisions, SVN_INVALID_REVNUM is
+   * valid, hence we have to shift it by 1.
+   */
+  SVN_ERR_ASSERT(entry->offset >= 0);
+  SVN_ERR_ASSERT(entry->size >= 0);
+
+  /* Now, all values will nicely convert to uint64. */
+  /* Make sure to keep P2L_PROTO_INDEX_ENTRY_SIZE consistent with this: */
+
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry->offset,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry->size,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry->type,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry->fnv1_checksum,
+                                      scratch_pool));
+  SVN_ERR(write_uint64_to_proto_index(proto_index, entry->item_count,
+                                      scratch_pool));
 
   /* Add sub-items. */
-  if (entry->item_count)
+  for (i = 0; i < entry->item_count; ++i)
     {
-      written = entry->item_count * sizeof(*entry->items);
-      SVN_ERR(svn_io_file_write_full(proto_index, entry->items, written,
-                                     &written, scratch_pool));
-      SVN_ERR_ASSERT(written == entry->item_count * sizeof(*entry->items));
-      written_total += written;
+      const svn_fs_x__id_part_t *sub_item = &entry->items[i];
+
+      /* Make sure all signed elements of ENTRY have non-negative values.
+       *
+       * For file offsets and sizes, this is a given as we use them to
+       * describe absolute positions and sizes.  For revisions,
+       * SVN_INVALID_REVNUM is valid, hence we have to shift it by 1.
+      */
+      SVN_ERR_ASSERT(   sub_item->change_set >= 0
+                     || sub_item->change_set == SVN_INVALID_REVNUM);
+
+      /* Write sub- record. */
+      revision = sub_item->change_set == SVN_INVALID_REVNUM
+               ? 0
+               : ((apr_uint64_t)sub_item->change_set + 1);
+
+      SVN_ERR(write_uint64_to_proto_index(proto_index, revision,
+                                          scratch_pool));
+      SVN_ERR(write_uint64_to_proto_index(proto_index, sub_item->number,
+                                          scratch_pool));
     }
 
-  /* Add trailer: number of bytes total in this entr.y */
-  written = sizeof(written_total);
-  SVN_ERR(svn_io_file_write_full(proto_index, &written_total, written,
-                                 &written, scratch_pool));
-  SVN_ERR_ASSERT(written == sizeof(written_total));
+  /* Add trailer: rev / pack file offset of the next item */
+  SVN_ERR(write_uint64_to_proto_index(proto_index,
+                                      entry->offset + entry->size,
+                                      scratch_pool));
+
+  return SVN_NO_ERROR;
+}
+
+/* Read *ENTRY from log-to-phys PROTO_INDEX file and indicate end-of-file
+ * in *EOF, or error out in that case if EOF is NULL.  *ENTRY is in an
+ * undefined state if an end-of-file occurred.
+ * Use SCRATCH_POOL for temporary allocations.
+ */
+static svn_error_t *
+read_p2l_entry_from_proto_index(apr_file_t *proto_index,
+                                svn_fs_x__p2l_entry_t *entry,
+                                svn_boolean_t *eof,
+                                apr_pool_t *scratch_pool)
+{
+  SVN_ERR(read_off_t_from_proto_index(proto_index, &entry->offset,
+                                      eof, scratch_pool));
+  SVN_ERR(read_off_t_from_proto_index(proto_index, &entry->size,
+                                      eof, scratch_pool));
+  SVN_ERR(read_uint32_from_proto_index(proto_index, &entry->type,
+                                       eof, scratch_pool));
+  SVN_ERR(read_uint32_from_proto_index(proto_index, &entry->fnv1_checksum,
+                                       eof, scratch_pool));
+  SVN_ERR(read_uint32_from_proto_index(proto_index, &entry->item_count,
+                                       eof, scratch_pool));
+
+  return SVN_NO_ERROR;
+}
+
+static svn_error_t *
+read_p2l_sub_items_from_proto_index(apr_file_t *proto_index,
+                                    svn_fs_x__p2l_entry_t *entry,
+                                    svn_boolean_t *eof,
+                                    apr_pool_t *scratch_pool)
+{
+  apr_int32_t i;
+  for (i = 0; i < entry->item_count; ++i)
+    {
+      apr_uint64_t revision;
+      svn_fs_x__id_part_t *sub_item = &entry->items[i];
+
+      SVN_ERR(read_uint64_from_proto_index(proto_index, &revision,
+                                           eof, scratch_pool));
+      SVN_ERR(read_uint64_from_proto_index(proto_index, &sub_item->number,
+                                           eof, scratch_pool));
+
+      /* Do the inverse REVSION number conversion (see
+       * svn_fs_x__p2l_proto_index_add_entry), if we actually read a
+       * complete record.
+      */
+      if (!eof || !*eof)
+        {
+          /* Be careful with the arithmetics here (overflows and wrap-around):
+           */
+          if (revision > 0 && revision - 1 > LONG_MAX)
+            return svn_error_createf(SVN_ERR_FS_INDEX_OVERFLOW, NULL,
+                                    _("Revision 0x%" APR_UINT64_T_HEX_FMT
+                                      " too large, max = 0x%"
+                                      APR_UINT64_T_HEX_FMT),
+                                    revision, (apr_uint64_t)LONG_MAX);
+
+          /* Shortening conversion from unsigned to signed int is well-
+           * defined and not lossy in C because the value can be represented
+           * in the target type.  Also, cast to 'long' instead of
+           * 'svn_revnum_t' here to provoke a compiler warning if those
+           * types should differ and we would need to change the overflow
+           * checking logic.
+           */
+          sub_item->change_set = revision == 0
+                               ? SVN_INVALID_REVNUM
+                               : (long)(revision - 1);
+        }
+
+    }
 
   return SVN_NO_ERROR;
 }
@@ -1847,39 +2160,29 @@ svn_fs_x__p2l_proto_index_next_offset(ap
     }
   else
     {
-      /* At least one entry.  Read last entry. */
-      apr_size_t size;
-      svn_fs_x__p2l_entry_t entry;
-
-      /* Read length of last entry. */
-      offset -= sizeof(size);
-      SVN_ERR(svn_io_file_seek(proto_index, APR_SET, &offset, scratch_pool));
-      SVN_ERR(svn_io_file_read_full2(proto_index, &size, sizeof(size),
-                                    NULL, NULL, scratch_pool));
-
-      /* Read last entry's main record. */
-      offset -= size;
+      /* The last 64 bits contain the value we are looking for. */
+      offset -= sizeof(apr_uint64_t);
       SVN_ERR(svn_io_file_seek(proto_index, APR_SET, &offset, scratch_pool));
-      SVN_ERR(svn_io_file_read_full2(proto_index, &entry, sizeof(entry),
-                                    NULL, NULL, scratch_pool));
-
-      /* Return next offset. */
-      *next_offset = entry.offset + entry.size;
+      SVN_ERR(read_off_t_from_proto_index(proto_index, next_offset, NULL,
+                                          scratch_pool));
     }
 
   return SVN_NO_ERROR;
 }
 
 svn_error_t *
-svn_fs_x__p2l_index_append(svn_fs_t *fs,
+svn_fs_x__p2l_index_append(svn_checksum_t **checksum,
+                           svn_fs_t *fs,
                            apr_file_t *index_file,
                            const char *proto_file_name,
                            svn_revnum_t revision,
+                           apr_pool_t *result_pool,
                            apr_pool_t *scratch_pool)
 {
   fs_x_data_t *ffd = fs->fsap_data;
   apr_uint64_t page_size = ffd->p2l_page_size;
   apr_file_t *proto_index = NULL;
+  svn_stream_t *stream;
   int i;
   apr_uint32_t sub_item;
   svn_boolean_t eof = FALSE;
@@ -1913,8 +2216,6 @@ svn_fs_x__p2l_index_append(svn_fs_t *fs,
   while (!eof)
     {
       svn_fs_x__p2l_entry_t entry;
-      apr_size_t read = 0;
-      apr_size_t to_read;
       apr_uint64_t entry_end;
       svn_boolean_t new_page = svn_spillbuf__get_size(buffer) == 0;
       svn_revnum_t last_revision = revision;
@@ -1923,28 +2224,23 @@ svn_fs_x__p2l_index_append(svn_fs_t *fs,
       svn_pool_clear(iterpool);
 
       /* (attempt to) read the next entry from the source */
-      SVN_ERR(svn_io_file_read_full2(proto_index, &entry, sizeof(entry),
-                                     &read, &eof, iterpool));
-      SVN_ERR_ASSERT(eof || read == sizeof(entry));
+      SVN_ERR(read_p2l_entry_from_proto_index(proto_index, &entry,
+                                              &eof, iterpool));
 
       if (entry.item_count && !eof)
         {
-          to_read = entry.item_count * sizeof(*entry.items);
-          entry.items = apr_palloc(iterpool, to_read);
-
-          SVN_ERR(svn_io_file_read_full2(proto_index, entry.items, to_read,
-                                         &read, &eof, iterpool));
-          SVN_ERR_ASSERT(eof || read == to_read);
+          entry.items = apr_palloc(iterpool,
+                                   entry.item_count * sizeof(*entry.items));
+          SVN_ERR(read_p2l_sub_items_from_proto_index(proto_index, &entry,
+                                                      &eof, iterpool));
         }
 
       /* Read entry trailer. However, we won't need its content. */
       if (!eof)
         {
-          apr_size_t entry_size;
-          to_read = sizeof(entry_size);
-          SVN_ERR(svn_io_file_read_full2(proto_index, &entry_size, to_read,
-                                         &read, &eof, iterpool));
-          SVN_ERR_ASSERT(eof || read == to_read);
+          apr_uint64_t trailer;
+          SVN_ERR(read_uint64_from_proto_index(proto_index, &trailer, &eof,
+                                               scratch_pool));
         }
 
       /* "unused" (and usually non-existent) section to cover the offsets
@@ -2031,34 +2327,29 @@ svn_fs_x__p2l_index_append(svn_fs_t *fs,
   APR_ARRAY_PUSH(table_sizes, apr_uint64_t)
       = svn_spillbuf__get_size(buffer) - last_buffer_size;
 
+  /* Open target stream. */
+  stream = svn_stream_checksummed2(svn_stream_from_aprfile2(index_file, TRUE,
+                                                            local_pool),
+                                   NULL, checksum, svn_checksum_md5, FALSE,
+                                   result_pool);
+
   /* write the start revision, file size and page size */
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, revision),
-                                 NULL, local_pool));
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, file_size),
-                                 NULL, local_pool));
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, page_size),
-                                 NULL, local_pool));
+  SVN_ERR(svn_stream_puts(stream, P2L_STREAM_PREFIX));
+  SVN_ERR(stream_write_encoded(stream, revision));
+  SVN_ERR(stream_write_encoded(stream, file_size));
+  SVN_ERR(stream_write_encoded(stream, page_size));
 
   /* write the page table (actually, the sizes of each page description) */
-  SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                 encode_uint(encoded, table_sizes->nelts),
-                                 NULL, local_pool));
+  SVN_ERR(stream_write_encoded(stream, table_sizes->nelts));
   for (i = 0; i < table_sizes->nelts; ++i)
     {
       apr_uint64_t value = APR_ARRAY_IDX(table_sizes, i, apr_uint64_t);
-      SVN_ERR(svn_io_file_write_full(index_file, encoded,
-                                     encode_uint(encoded, value),
-                                     NULL, local_pool));
+      SVN_ERR(stream_write_encoded(stream, value));
     }
 
-  /* append page contents */
+  /* append page contents and implicitly close STREAM */
   SVN_ERR(svn_stream_copy3(svn_stream__from_spillbuf(buffer, local_pool),
-                           svn_stream_from_aprfile2(index_file, TRUE,
-                                                    local_pool),
-                           NULL, NULL, local_pool));
+                           stream, NULL, NULL, local_pool));
 
   svn_pool_destroy(iterpool);
   svn_pool_destroy(local_pool);
@@ -2083,7 +2374,10 @@ auto_open_p2l_index(svn_fs_x__revision_f
                                  rev_file->file,
                                  rev_file->p2l_offset,
                                  rev_file->footer_offset,
-                                 ffd->block_size, rev_file->pool));
+                                 P2L_STREAM_PREFIX,
+                                 (apr_size_t)ffd->block_size,
+                                 rev_file->pool,
+                                 rev_file->pool));
     }
 
   return SVN_NO_ERROR;

Modified: subversion/trunk/subversion/libsvn_fs_x/index.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/index.h?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/index.h (original)
+++ subversion/trunk/subversion/libsvn_fs_x/index.h Sat Nov  1 14:11:05 2014
@@ -66,7 +66,7 @@ typedef struct svn_fs_x__p2l_entry_t
   apr_off_t size;
 
   /* type of the item (see SVN_FS_X__ITEM_TYPE_*) defines */
-  unsigned type;
+  apr_uint32_t type;
 
   /* modified FNV-1a checksum.  0 if unknown checksum */
   apr_uint32_t fnv1_checksum;
@@ -123,13 +123,18 @@ svn_fs_x__l2p_proto_index_add_entry(apr_
 /* Use the proto index file stored at PROTO_FILE_NAME, construct the final
  * log-to-phys index and append it to INDEX_FILE.  The first revision will
  * be REVISION, entries to the next revision will be assigned to REVISION+1
- * and so forth.  Use SCRATCH_POOL for temporary allocations.
+ * and so forth.  
+ *
+ * Return the MD5 checksum of the on-disk index data in *CHECKSUM, allocated
+ * in RESULT_POOL.  Use SCRATCH_POOL for temporary allocations.
  */
 svn_error_t *
-svn_fs_x__l2p_index_append(svn_fs_t *fs,
+svn_fs_x__l2p_index_append(svn_checksum_t **checksum,
+                           svn_fs_t *fs,
                            apr_file_t *index_file,
                            const char *proto_file_name,
                            svn_revnum_t revision,
+                           apr_pool_t *result_pool,
                            apr_pool_t *scratch_pool);
 
 /* Open / create a phys-to-log index file with the full file path name
@@ -148,7 +153,7 @@ svn_fs_x__p2l_proto_index_open(apr_file_
  */
 svn_error_t *
 svn_fs_x__p2l_proto_index_add_entry(apr_file_t *proto_index,
-                                    svn_fs_x__p2l_entry_t *entry,
+                                    const svn_fs_x__p2l_entry_t *entry,
                                     apr_pool_t *scratch_pool);
 
 /* Set *NEXT_OFFSET to the first offset behind the last entry in the
@@ -163,13 +168,17 @@ svn_fs_x__p2l_proto_index_next_offset(ap
 /* Use the proto index file stored at PROTO_FILE_NAME, construct the final
  * phys-to-log index and append it to INDEX_FILE.  Entries without a valid
  * revision will be assigned to the REVISION given here.
- * Use SCRATCH_POOL for temporary allocations.
+ *
+ * Return the MD5 checksum of the on-disk index data in *CHECKSUM, allocated
+ * in RESULT_POOL.  Use SCRATCH_POOL for temporary allocations.
  */
 svn_error_t *
-svn_fs_x__p2l_index_append(svn_fs_t *fs,
+svn_fs_x__p2l_index_append(svn_checksum_t **checksum,
+                           svn_fs_t *fs,
                            apr_file_t *index_file,
                            const char *proto_file_name,
                            svn_revnum_t revision,
+                           apr_pool_t *result_pool,
                            apr_pool_t *scratch_pool);
 
 /* Use the phys-to-log mapping files in FS to build a list of entries

Modified: subversion/trunk/subversion/libsvn_fs_x/low_level.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/low_level.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/low_level.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/low_level.c Sat Nov  1 14:11:05 2014
@@ -104,38 +104,71 @@ parse_revnum(svn_revnum_t *rev,
 
 svn_error_t *
 svn_fs_x__parse_footer(apr_off_t *l2p_offset,
+                       svn_checksum_t **l2p_checksum,
                        apr_off_t *p2l_offset,
+                       svn_checksum_t **p2l_checksum,
                        svn_stringbuf_t *footer,
-                       svn_revnum_t rev)
+                       svn_revnum_t rev,
+                       apr_pool_t *result_pool)
 {
   apr_int64_t val;
+  char *last_str = footer->data;
 
-  /* Split the footer into the 2 number strings. */
-  char *seperator = strchr(footer->data, ' ');
-  if (!seperator)
-    return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
-                             _("Revision file (r%ld) has corrupt footer"),
-                             rev);
-  *seperator = '\0';
+  /* Get the L2P offset. */
+  const char *str = svn_cstring_tokenize(" ", &last_str);
+  if (str == NULL)
+    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
+                            _("Invalid revision footer"));
 
-  /* Convert offset values. */
-  SVN_ERR(svn_cstring_atoi64(&val, footer->data));
+  SVN_ERR(svn_cstring_atoi64(&val, str));
   *l2p_offset = (apr_off_t)val;
-  SVN_ERR(svn_cstring_atoi64(&val, seperator + 1));
+
+  /* Get the L2P checksum. */
+  str = svn_cstring_tokenize(" ", &last_str);
+  if (str == NULL)
+    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
+                            _("Invalid revision footer"));
+
+  SVN_ERR(svn_checksum_parse_hex(l2p_checksum, svn_checksum_md5, str,
+                                 result_pool));
+
+  /* Get the P2L offset. */
+  str = svn_cstring_tokenize(" ", &last_str);
+  if (str == NULL)
+    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
+                            _("Invalid revision footer"));
+
+  SVN_ERR(svn_cstring_atoi64(&val, str));
   *p2l_offset = (apr_off_t)val;
 
+  /* Get the P2L checksum. */
+  str = svn_cstring_tokenize(" ", &last_str);
+  if (str == NULL)
+    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
+                            _("Invalid revision footer"));
+
+  SVN_ERR(svn_checksum_parse_hex(p2l_checksum, svn_checksum_md5, str,
+                                 result_pool));
+
   return SVN_NO_ERROR;
 }
 
 svn_stringbuf_t *
 svn_fs_x__unparse_footer(apr_off_t l2p_offset,
+                         svn_checksum_t *l2p_checksum,
                          apr_off_t p2l_offset,
-                         apr_pool_t *result_pool)
+                         svn_checksum_t *p2l_checksum,
+                         apr_pool_t *result_pool,
+                         apr_pool_t *scratch_pool)
 {
   return svn_stringbuf_createf(result_pool,
-                               "%" APR_OFF_T_FMT " %" APR_OFF_T_FMT,
+                               "%" APR_OFF_T_FMT " %s %" APR_OFF_T_FMT " %s",
                                l2p_offset,
-                               p2l_offset);
+                               svn_checksum_to_cstring(l2p_checksum,
+                                                       scratch_pool),
+                               p2l_offset,
+                               svn_checksum_to_cstring(p2l_checksum,
+                                                       scratch_pool));
 }
 
 /* Given a revision file FILE that has been pre-positioned at the

Modified: subversion/trunk/subversion/libsvn_fs_x/low_level.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/low_level.h?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/low_level.h (original)
+++ subversion/trunk/subversion/libsvn_fs_x/low_level.h Sat Nov  1 14:11:05 2014
@@ -47,25 +47,35 @@ extern "C" {
 
 /* Given the FSX revision / pack FOOTER, parse it destructively
  * and return the start offsets of the index data in *L2P_OFFSET and
- * *P2L_OFFSET, respectively.
+ * *P2L_OFFSET, respectively.  Also, return the expected checksums in
+ * in *L2P_CHECKSUM and *P2L_CHECKSUM.
  * 
  * Note that REV is only used to construct nicer error objects that
- * mention this revision.
+ * mention this revision.  Allocate the checksums in RESULT_POOL.
  */
 svn_error_t *
 svn_fs_x__parse_footer(apr_off_t *l2p_offset,
+                       svn_checksum_t **l2p_checksum,
                        apr_off_t *p2l_offset,
+                       svn_checksum_t **p2l_checksum,
                        svn_stringbuf_t *footer,
-                       svn_revnum_t rev);
+                       svn_revnum_t rev,
+                       apr_pool_t *result_pool);
 
-/* Given the offset of the L2P index data in L2P_OFFSET and the offset of
- * the P2L index data in P2L_OFFSET,  return the corresponding format 7+
- * revision / pack file footer.  Allocate it in RESULT_POOL.
+/* Given the offset of the L2P index data in L2P_OFFSET, the content
+ * checksum in L2P_CHECKSUM and the offset plus checksum of the P2L
+ * index data in P2L_OFFSET and P2L_CHECKSUM.
+ *
+ * Return the corresponding format 7+ revision / pack file footer.
+ * Allocate it in RESULT_POOL and use SCRATCH_POOL for temporary.
  */
 svn_stringbuf_t *
 svn_fs_x__unparse_footer(apr_off_t l2p_offset,
+                         svn_checksum_t *l2p_checksum,
                          apr_off_t p2l_offset,
-                         apr_pool_t *result_pool);
+                         svn_checksum_t *p2l_checksum,
+                         apr_pool_t *result_pool,
+                         apr_pool_t *scratch_pool);
 
 /* Parse the description of a representation from TEXT and store it
    into *REP_P.  TEXT will be invalidated by this call.  Allocate *REP_P in

Modified: subversion/trunk/subversion/libsvn_fs_x/rev_file.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/rev_file.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/rev_file.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/rev_file.c Sat Nov  1 14:11:05 2014
@@ -49,7 +49,9 @@ create_revision_file(svn_fs_t *fs,
   file->l2p_stream = NULL;
   file->block_size = ffd->block_size;
   file->l2p_offset = -1;
+  file->l2p_checksum = NULL;
   file->p2l_offset = -1;
+  file->p2l_checksum = NULL;
   file->footer_offset = -1;
   file->pool = result_pool;
 
@@ -258,8 +260,10 @@ svn_fs_x__auto_read_footer(svn_fs_x__rev
       footer->data[footer->len] = '\0';
 
       /* Extract index locations. */
-      SVN_ERR(svn_fs_x__parse_footer(&file->l2p_offset, &file->p2l_offset,
-                                     footer, file->start_revision));
+      SVN_ERR(svn_fs_x__parse_footer(&file->l2p_offset, &file->l2p_checksum,
+                                     &file->p2l_offset, &file->p2l_checksum,
+                                     footer, file->start_revision,
+                                     file->pool));
       file->footer_offset = filesize - footer_length - 1;
     }
 

Modified: subversion/trunk/subversion/libsvn_fs_x/rev_file.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/rev_file.h?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/rev_file.h (original)
+++ subversion/trunk/subversion/libsvn_fs_x/rev_file.h Sat Nov  1 14:11:05 2014
@@ -72,11 +72,19 @@ typedef struct svn_fs_x__revision_file_t
    * has not been called, yet. */
   apr_off_t l2p_offset;
 
+  /* MD5 checksum on the whole on-disk representation of the L2P index.
+   * NULL if svn_fs_fs__auto_read_footer has not been called, yet. */
+  svn_checksum_t *l2p_checksum;
+
   /* Offset within FILE at which the L2P index ends and the P2L index
    * data starts. Greater than L2P_OFFSET. -1 if svn_fs_fs__auto_read_footer
    * has not been called, yet. */
   apr_off_t p2l_offset;
 
+  /* MD5 checksum on the whole on-disk representation of the P2L index.
+   * NULL if svn_fs_fs__auto_read_footer has not been called, yet. */
+  svn_checksum_t *p2l_checksum;
+
   /* Offset within FILE at which the P2L index ends and the footer starts.
    * Greater than P2L_OFFSET. -1 if svn_fs_fs__auto_read_footer has not
    * been called, yet. */

Modified: subversion/trunk/subversion/libsvn_fs_x/transaction.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/transaction.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/transaction.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/transaction.c Sat Nov  1 14:11:05 2014
@@ -2589,7 +2589,7 @@ write_container_delta_rep(representation
                           svn_fs_x__txn_id_t txn_id,
                           node_revision_t *noderev,
                           apr_hash_t *reps_hash,
-                          int item_type,
+                          apr_uint32_t item_type,
                           svn_revnum_t final_revision,
                           apr_pool_t *scratch_pool)
 {
@@ -2901,9 +2901,9 @@ write_final_rev(const svn_fs_id_t **new_
       && svn_fs_x__is_txn(noderev->prop_rep->id.change_set))
     {
       apr_hash_t *proplist;
-      int item_type = noderev->kind == svn_node_dir
-                    ? SVN_FS_X__ITEM_TYPE_DIR_PROPS
-                    : SVN_FS_X__ITEM_TYPE_FILE_PROPS;
+      apr_uint32_t item_type = noderev->kind == svn_node_dir
+                             ? SVN_FS_X__ITEM_TYPE_DIR_PROPS
+                             : SVN_FS_X__ITEM_TYPE_FILE_PROPS;
       SVN_ERR(svn_fs_x__get_proplist(&proplist, fs, noderev, pool));
 
       noderev->prop_rep->id.change_set = change_set;
@@ -3239,20 +3239,25 @@ svn_fs_x__add_index_data(svn_fs_t *fs,
   apr_off_t p2l_offset;
   svn_stringbuf_t *footer;
   unsigned char footer_length;
+  svn_checksum_t *l2p_checksum;
+  svn_checksum_t *p2l_checksum;
 
   /* Append the actual index data to the pack file. */
   l2p_offset = 0;
   SVN_ERR(svn_io_file_seek(file, APR_END, &l2p_offset, pool));
-  SVN_ERR(svn_fs_x__l2p_index_append(fs, file, l2p_proto_index, revision,
-                                     pool));
+  SVN_ERR(svn_fs_x__l2p_index_append(&l2p_checksum, fs, file,
+                                     l2p_proto_index, revision,
+                                     pool, pool));
 
   p2l_offset = 0;
   SVN_ERR(svn_io_file_seek(file, APR_END, &p2l_offset, pool));
-  SVN_ERR(svn_fs_x__p2l_index_append(fs, file, p2l_proto_index, revision,
-                                     pool));
+  SVN_ERR(svn_fs_x__p2l_index_append(&p2l_checksum, fs, file,
+                                     p2l_proto_index, revision,
+                                     pool, pool));
 
   /* Append footer. */
-  footer = svn_fs_x__unparse_footer(l2p_offset, p2l_offset, pool);
+  footer = svn_fs_x__unparse_footer(l2p_offset, l2p_checksum, 
+                                    p2l_offset, p2l_checksum, pool, pool);
   SVN_ERR(svn_io_file_write_full(file, footer->data, footer->len, NULL,
                                  pool));
 

Modified: subversion/trunk/subversion/libsvn_fs_x/verify.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_fs_x/verify.c?rev=1635950&r1=1635949&r2=1635950&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_fs_x/verify.c (original)
+++ subversion/trunk/subversion/libsvn_fs_x/verify.c Sat Nov  1 14:11:05 2014
@@ -133,6 +133,93 @@ verify_rep_cache(svn_fs_t *fs,
   return SVN_NO_ERROR;
 }
 
+/* Verify that the MD5 checksum of the data between offsets START and END
+ * in FILE matches the EXPECTED checksum.  If there is a mismatch use the
+ * indedx NAME in the error message.  Supports cancellation with CANCEL_FUNC
+ * and CANCEL_BATON.  SCRATCH_POOL is for temporary allocations. */
+static svn_error_t *
+verify_index_checksum(apr_file_t *file,
+                      const char *name,
+                      apr_off_t start,
+                      apr_off_t end,
+                      svn_checksum_t *expected,
+                      svn_cancel_func_t cancel_func,
+                      void *cancel_baton,
+                      apr_pool_t *scratch_pool)
+{
+  unsigned char buffer[SVN__STREAM_CHUNK_SIZE];
+  apr_off_t size = end - start;
+  svn_checksum_t *actual;
+  svn_checksum_ctx_t *context
+    = svn_checksum_ctx_create(svn_checksum_md5, scratch_pool);
+
+  /* Calculate the index checksum. */
+  SVN_ERR(svn_io_file_seek(file, APR_SET, &start, scratch_pool));
+  while (size > 0)
+    {
+      apr_size_t to_read = size > sizeof(buffer)
+                         ? sizeof(buffer)
+                         : (apr_size_t)size;
+      SVN_ERR(svn_io_file_read_full2(file, buffer, to_read, NULL, NULL,
+                                     scratch_pool));
+      SVN_ERR(svn_checksum_update(context, buffer, to_read));
+      size -= to_read;
+
+      if (cancel_func)
+        SVN_ERR(cancel_func(cancel_baton));
+    }
+
+  SVN_ERR(svn_checksum_final(&actual, context, scratch_pool));
+
+  /* Verify that it matches the expected checksum. */
+  if (!svn_checksum_match(expected, actual))
+    {
+      const char *file_name;
+
+      SVN_ERR(svn_io_file_name_get(&file_name, file, scratch_pool));
+      SVN_ERR(svn_checksum_mismatch_err(expected, actual, scratch_pool, 
+                                        _("%s checksum mismatch in file %s"),
+                                        name, file_name));
+    }
+
+  return SVN_NO_ERROR;
+}
+
+/* Verify the MD5 checksums of the index data in the rev / pack file
+ * containing revision START in FS.  If given, invoke CANCEL_FUNC with
+ * CANCEL_BATON at regular intervals.  Use SCRATCH_POOL for temporary
+ * allocations.
+ */
+static svn_error_t *
+verify_index_checksums(svn_fs_t *fs,
+                       svn_revnum_t start,
+                       svn_cancel_func_t cancel_func,
+                       void *cancel_baton,
+                       apr_pool_t *scratch_pool)
+{
+  svn_fs_x__revision_file_t *rev_file;
+
+  /* Open the rev / pack file and read the footer */
+  SVN_ERR(svn_fs_x__open_pack_or_rev_file(&rev_file, fs, start,
+                                          scratch_pool, scratch_pool));
+  SVN_ERR(svn_fs_x__auto_read_footer(rev_file));
+
+  /* Verify the index contents against the checksum from the footer. */
+  SVN_ERR(verify_index_checksum(rev_file->file, "L2P index",
+                                rev_file->l2p_offset, rev_file->p2l_offset,
+                                rev_file->l2p_checksum,
+                                cancel_func, cancel_baton, scratch_pool));
+  SVN_ERR(verify_index_checksum(rev_file->file, "P2L index",
+                                rev_file->p2l_offset, rev_file->footer_offset,
+                                rev_file->p2l_checksum,
+                                cancel_func, cancel_baton, scratch_pool));
+
+  /* Done. */
+  SVN_ERR(svn_fs_x__close_revision_file(rev_file));
+
+  return SVN_NO_ERROR;
+}
+
 /* Verify that for all log-to-phys index entries for revisions START to
  * START + COUNT-1 in FS there is a consistent entry in the phys-to-log
  * index.  If given, invoke CANCEL_FUNC with CANCEL_BATON at regular
@@ -598,6 +685,7 @@ verify_index_consistency(svn_fs_t *fs,
                          void *cancel_baton,
                          apr_pool_t *pool)
 {
+  svn_error_t *err;
   fs_x_data_t *ffd = fs->fsap_data;
   svn_revnum_t revision, pack_start, pack_end;
   apr_pool_t *iterpool = svn_pool_create(pool);
@@ -610,11 +698,17 @@ verify_index_consistency(svn_fs_t *fs,
       if (notify_func && (pack_start % ffd->max_files_per_dir == 0))
         notify_func(pack_start, notify_baton, iterpool);
 
+      /* Check for external corruption to the indexes. */
+      err = verify_index_checksums(fs, pack_start, cancel_func,
+                                   cancel_baton, iterpool);
+ 
       /* two-way index check */
-      SVN_ERR(compare_l2p_to_p2l_index(fs, pack_start, pack_end - pack_start,
-                                       cancel_func, cancel_baton, iterpool));
-      SVN_ERR(compare_p2l_to_l2p_index(fs, pack_start, pack_end - pack_start,
-                                       cancel_func, cancel_baton, iterpool));
+      if (!err)
+        err = compare_l2p_to_p2l_index(fs, pack_start, pack_end - pack_start,
+                                       cancel_func, cancel_baton, iterpool);
+      if (!err)
+        err = compare_p2l_to_l2p_index(fs, pack_start, pack_end - pack_start,
+                                       cancel_func, cancel_baton, iterpool);
 
       /* verify in-index checksums and types vs. actual rev / pack files */
       SVN_ERR(compare_p2l_to_rev(fs, pack_start, pack_end - pack_start,