1 /* load-index-cmd.c -- implements the dump-index sub-command.
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
23 #include "svn_pools.h"
25 #include "private/svn_sorts_private.h"
30 #include "transaction.h"
32 /* From the ENTRIES array of svn_fs_fs__p2l_entry_t*, sorted by offset,
33 * return the first offset behind the last item. */
35 get_max_covered(apr_array_header_t *entries)
37 const svn_fs_fs__p2l_entry_t *entry;
38 if (entries->nelts == 0)
41 entry = APR_ARRAY_IDX(entries, entries->nelts - 1,
42 const svn_fs_fs__p2l_entry_t *);
43 return entry->offset + entry->size;
46 /* Make sure that the svn_fs_fs__p2l_entry_t* in ENTRIES are consecutive
47 * and non-overlapping. Use SCRATCH_POOL for temporaries. */
49 check_all_covered(apr_array_header_t *entries,
50 apr_pool_t *scratch_pool)
53 apr_off_t expected = 0;
54 for (i = 0; i < entries->nelts; ++i)
56 const svn_fs_fs__p2l_entry_t *entry
57 = APR_ARRAY_IDX(entries, i, const svn_fs_fs__p2l_entry_t *);
59 if (entry->offset < expected)
60 return svn_error_createf(SVN_ERR_INVALID_INPUT, NULL,
61 "Overlapping index data for offset %s",
62 apr_psprintf(scratch_pool,
63 "%" APR_UINT64_T_HEX_FMT,
64 (apr_uint64_t)expected));
66 if (entry->offset > expected)
67 return svn_error_createf(SVN_ERR_INVALID_INPUT, NULL,
68 "Missing index data for offset %s",
69 apr_psprintf(scratch_pool,
70 "%" APR_UINT64_T_HEX_FMT,
71 (apr_uint64_t)expected));
73 expected = entry->offset + entry->size;
79 /* A svn_sort__array compatible comparator function, sorting the
80 * svn_fs_fs__p2l_entry_t** given in LHS, RHS by offset. */
82 compare_p2l_entry_revision(const void *lhs,
85 const svn_fs_fs__p2l_entry_t *lhs_entry
86 =*(const svn_fs_fs__p2l_entry_t *const *)lhs;
87 const svn_fs_fs__p2l_entry_t *rhs_entry
88 =*(const svn_fs_fs__p2l_entry_t *const *)rhs;
90 if (lhs_entry->offset < rhs_entry->offset)
93 return lhs_entry->offset == rhs_entry->offset ? 0 : 1;
97 svn_fs_fs__load_index(svn_fs_t *fs,
98 svn_revnum_t revision,
99 apr_array_header_t *entries,
100 apr_pool_t *scratch_pool)
102 apr_pool_t *subpool = svn_pool_create(scratch_pool);
104 /* Check the FS format number. */
105 if (! svn_fs_fs__use_log_addressing(fs))
106 return svn_error_create(SVN_ERR_FS_UNSUPPORTED_FORMAT, NULL, NULL);
108 /* P2L index must be written in offset order.
109 * Sort ENTRIES accordingly. */
110 svn_sort__array(entries, compare_p2l_entry_revision);
112 /* Treat an empty array as a no-op instead error. */
113 if (entries->nelts != 0)
115 const char *l2p_proto_index;
116 const char *p2l_proto_index;
117 svn_fs_fs__revision_file_t *rev_file;
119 apr_off_t max_covered = get_max_covered(entries);
121 /* Ensure that the index data is complete. */
122 SVN_ERR(check_all_covered(entries, scratch_pool));
124 /* Open rev / pack file & trim indexes + footer off it. */
125 SVN_ERR(svn_fs_fs__open_pack_or_rev_file_writable(&rev_file, fs,
129 /* Remove the existing index info. */
130 err = svn_fs_fs__auto_read_footer(rev_file);
133 /* Even the index footer cannot be read, even less be trusted.
134 * Take the range of valid data from the new index data. */
135 svn_error_clear(err);
136 SVN_ERR(svn_io_file_trunc(rev_file->file, max_covered,
141 /* We assume that the new index data covers all contents.
142 * Error out if it doesn't. The user can always truncate
143 * the file themselves. */
144 if (max_covered != rev_file->l2p_offset)
145 return svn_error_createf(SVN_ERR_INVALID_INPUT, NULL,
146 "New index data ends at %s, old index ended at %s",
147 apr_psprintf(scratch_pool, "%" APR_UINT64_T_HEX_FMT,
148 (apr_uint64_t)max_covered),
149 apr_psprintf(scratch_pool, "%" APR_UINT64_T_HEX_FMT,
150 (apr_uint64_t) rev_file->l2p_offset));
152 SVN_ERR(svn_io_file_trunc(rev_file->file, rev_file->l2p_offset,
156 /* Create proto index files for the new index data
157 * (will be cleaned up automatically with iterpool). */
158 SVN_ERR(svn_fs_fs__p2l_index_from_p2l_entries(&p2l_proto_index, fs,
161 SVN_ERR(svn_fs_fs__l2p_index_from_p2l_entries(&l2p_proto_index, fs,
165 /* Combine rev data with new index data. */
166 SVN_ERR(svn_fs_fs__add_index_data(fs, rev_file->file, l2p_proto_index,
168 rev_file->start_revision, subpool));
171 svn_pool_destroy(subpool);