1/* load-index-cmd.c -- implements the dump-index sub-command. 2 * 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 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 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 19 * under the License. 20 * ==================================================================== 21 */ 22 23#include "svn_pools.h" 24 25#include "private/svn_sorts_private.h" 26 27#include "fs_fs.h" 28#include "index.h" 29#include "util.h" 30#include "transaction.h" 31 32/* From the ENTRIES array of svn_fs_fs__p2l_entry_t*, sorted by offset, 33 * return the first offset behind the last item. */ 34static apr_off_t 35get_max_covered(apr_array_header_t *entries) 36{ 37 const svn_fs_fs__p2l_entry_t *entry; 38 if (entries->nelts == 0) 39 return -1; 40 41 entry = APR_ARRAY_IDX(entries, entries->nelts - 1, 42 const svn_fs_fs__p2l_entry_t *); 43 return entry->offset + entry->size; 44} 45 46/* Make sure that the svn_fs_fs__p2l_entry_t* in ENTRIES are consecutive 47 * and non-overlapping. Use SCRATCH_POOL for temporaries. */ 48static svn_error_t * 49check_all_covered(apr_array_header_t *entries, 50 apr_pool_t *scratch_pool) 51{ 52 int i; 53 apr_off_t expected = 0; 54 for (i = 0; i < entries->nelts; ++i) 55 { 56 const svn_fs_fs__p2l_entry_t *entry 57 = APR_ARRAY_IDX(entries, i, const svn_fs_fs__p2l_entry_t *); 58 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)); 65 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)); 72 73 expected = entry->offset + entry->size; 74 } 75 76 return SVN_NO_ERROR; 77} 78 79/* A svn_sort__array compatible comparator function, sorting the 80 * svn_fs_fs__p2l_entry_t** given in LHS, RHS by offset. */ 81static int 82compare_p2l_entry_revision(const void *lhs, 83 const void *rhs) 84{ 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; 89 90 if (lhs_entry->offset < rhs_entry->offset) 91 return -1; 92 93 return lhs_entry->offset == rhs_entry->offset ? 0 : 1; 94} 95 96svn_error_t * 97svn_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) 101{ 102 apr_pool_t *subpool = svn_pool_create(scratch_pool); 103 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); 107 108 /* P2L index must be written in offset order. 109 * Sort ENTRIES accordingly. */ 110 svn_sort__array(entries, compare_p2l_entry_revision); 111 112 /* Treat an empty array as a no-op instead error. */ 113 if (entries->nelts != 0) 114 { 115 const char *l2p_proto_index; 116 const char *p2l_proto_index; 117 svn_fs_fs__revision_file_t *rev_file; 118 svn_error_t *err; 119 apr_off_t max_covered = get_max_covered(entries); 120 121 /* Ensure that the index data is complete. */ 122 SVN_ERR(check_all_covered(entries, scratch_pool)); 123 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, 126 revision, subpool, 127 subpool)); 128 129 /* Remove the existing index info. */ 130 err = svn_fs_fs__auto_read_footer(rev_file); 131 if (err) 132 { 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, 137 subpool)); 138 } 139 else 140 { 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)); 151 152 SVN_ERR(svn_io_file_trunc(rev_file->file, rev_file->l2p_offset, 153 subpool)); 154 } 155 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, 159 rev_file, entries, 160 subpool, subpool)); 161 SVN_ERR(svn_fs_fs__l2p_index_from_p2l_entries(&l2p_proto_index, fs, 162 entries, subpool, 163 subpool)); 164 165 /* Combine rev data with new index data. */ 166 SVN_ERR(svn_fs_fs__add_index_data(fs, rev_file->file, l2p_proto_index, 167 p2l_proto_index, 168 rev_file->start_revision, subpool)); 169 } 170 171 svn_pool_destroy(subpool); 172 173 return SVN_NO_ERROR; 174} 175