1/* rev_file.c --- revision file and index access functions 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 "rev_file.h" 26#include "fs_x.h" 27#include "index.h" 28#include "low_level.h" 29#include "util.h" 30 31#include "../libsvn_fs/fs-loader.h" 32 33#include "private/svn_io_private.h" 34#include "svn_private_config.h" 35 36struct svn_fs_x__revision_file_t 37{ 38 /* the filesystem that this revision file belongs to */ 39 svn_fs_t *fs; 40 41 /* Meta-data to FILE. */ 42 svn_fs_x__rev_file_info_t file_info; 43 44 /* rev / pack file */ 45 apr_file_t *file; 46 47 /* stream based on FILE and not NULL exactly when FILE is not NULL */ 48 svn_stream_t *stream; 49 50 /* the opened P2L index stream or NULL. Always NULL for txns. */ 51 svn_fs_x__packed_number_stream_t *p2l_stream; 52 53 /* the opened L2P index stream or NULL. Always NULL for txns. */ 54 svn_fs_x__packed_number_stream_t *l2p_stream; 55 56 /* Copied from FS->FFD->BLOCK_SIZE upon creation. It allows us to 57 * use aligned seek() without having the FS handy. */ 58 apr_off_t block_size; 59 60 /* Info on the L2P index within FILE. 61 * Elements are -1 / NULL until svn_fs_x__auto_read_footer gets called. */ 62 svn_fs_x__index_info_t l2p_info; 63 64 /* Info on the P2L index within FILE. 65 * Elements are -1 / NULL until svn_fs_x__auto_read_footer gets called. */ 66 svn_fs_x__index_info_t p2l_info; 67 68 /* Pool used for all sub-structure allocations (file, streams etc.). 69 A sub-pool of OWNER. NULL until the lazily initilized. */ 70 apr_pool_t *pool; 71 72 /* Pool that this structure got allocated in. */ 73 apr_pool_t *owner; 74}; 75 76/* Return a new revision file instance, allocated in RESULT_POOL, for 77 * filesystem FS. Set its pool member to the provided RESULT_POOL. */ 78static svn_fs_x__revision_file_t * 79create_revision_file(svn_fs_t *fs, 80 apr_pool_t *result_pool) 81{ 82 svn_fs_x__data_t *ffd = fs->fsap_data; 83 84 svn_fs_x__revision_file_t *file = apr_palloc(result_pool, sizeof(*file)); 85 file->fs = fs; 86 file->file_info.is_packed = FALSE; 87 file->file_info.start_revision = SVN_INVALID_REVNUM; 88 file->file = NULL; 89 file->stream = NULL; 90 file->p2l_stream = NULL; 91 file->l2p_stream = NULL; 92 file->block_size = ffd->block_size; 93 file->l2p_info.start = -1; 94 file->l2p_info.end = -1; 95 file->l2p_info.checksum = NULL; 96 file->p2l_info.start = -1; 97 file->p2l_info.end = -1; 98 file->p2l_info.checksum = NULL; 99 file->pool = NULL; 100 file->owner = result_pool; 101 102 return file; 103} 104 105/* Return a new revision file instance, allocated in RESULT_POOL, for 106 * REVISION in filesystem FS. Set its pool member to the provided 107 * RESULT_POOL. */ 108static svn_fs_x__revision_file_t * 109init_revision_file(svn_fs_t *fs, 110 svn_revnum_t revision, 111 apr_pool_t *result_pool) 112{ 113 svn_fs_x__revision_file_t *file = create_revision_file(fs, result_pool); 114 115 file->file_info.is_packed = svn_fs_x__is_packed_rev(fs, revision); 116 file->file_info.start_revision = svn_fs_x__packed_base_rev(fs, revision); 117 118 return file; 119} 120 121/* Baton type for set_read_only() */ 122typedef struct set_read_only_baton_t 123{ 124 /* File to set to read-only. */ 125 const char *file_path; 126 127 /* Scratch pool sufficient life time. 128 * Ideally the pool that we registered the cleanup on. */ 129 apr_pool_t *pool; 130} set_read_only_baton_t; 131 132/* APR pool cleanup callback taking a set_read_only_baton_t baton and then 133 * (trying to) set the specified file to r/o mode. */ 134static apr_status_t 135set_read_only(void *baton) 136{ 137 set_read_only_baton_t *ro_baton = baton; 138 apr_status_t status = APR_SUCCESS; 139 svn_error_t *err; 140 141 err = svn_io_set_file_read_only(ro_baton->file_path, TRUE, ro_baton->pool); 142 if (err) 143 { 144 status = err->apr_err; 145 svn_error_clear(err); 146 } 147 148 return status; 149} 150 151/* If the file at PATH is read-only, attempt to make it writable. The 152 * original state will be restored with RESULT_POOL gets cleaned up. 153 * SCRATCH_POOL is for temporary allocations. */ 154static svn_error_t * 155auto_make_writable(const char *path, 156 apr_pool_t *result_pool, 157 apr_pool_t *scratch_pool) 158{ 159 svn_boolean_t is_read_only; 160 apr_finfo_t finfo; 161 162 SVN_ERR(svn_io_stat(&finfo, path, SVN__APR_FINFO_READONLY, scratch_pool)); 163 SVN_ERR(svn_io__is_finfo_read_only(&is_read_only, &finfo, scratch_pool)); 164 165 if (is_read_only) 166 { 167 /* Tell the pool to restore the r/o state upon cleanup 168 (assuming the file will still exist, failing silently otherwise). */ 169 set_read_only_baton_t *baton = apr_pcalloc(result_pool, 170 sizeof(*baton)); 171 baton->pool = result_pool; 172 baton->file_path = apr_pstrdup(result_pool, path); 173 apr_pool_cleanup_register(result_pool, baton, 174 set_read_only, apr_pool_cleanup_null); 175 176 /* Finally, allow write access (undoing it has already been scheduled 177 and is idempotent). */ 178 SVN_ERR(svn_io_set_file_read_write(path, FALSE, scratch_pool)); 179 } 180 181 return SVN_NO_ERROR; 182} 183 184/* Return the pool to be used for allocations with FILE. 185 Lazily created that pool upon the first call. */ 186static apr_pool_t * 187get_file_pool(svn_fs_x__revision_file_t *file) 188{ 189 if (file->pool == NULL) 190 file->pool = svn_pool_create(file->owner); 191 192 return file->pool; 193} 194 195/* Core implementation of svn_fs_x__open_pack_or_rev_file working on an 196 * existing, initialized FILE structure. If WRITABLE is TRUE, give write 197 * access to the file - temporarily resetting the r/o state if necessary. 198 */ 199static svn_error_t * 200open_pack_or_rev_file(svn_fs_x__revision_file_t *file, 201 svn_boolean_t writable, 202 apr_pool_t *scratch_pool) 203{ 204 svn_error_t *err; 205 svn_boolean_t retry = FALSE; 206 svn_fs_t *fs = file->fs; 207 svn_revnum_t rev = file->file_info.start_revision; 208 apr_pool_t *file_pool = get_file_pool(file); 209 210 do 211 { 212 const char *path = svn_fs_x__path_rev_absolute(fs, rev, scratch_pool); 213 apr_file_t *apr_file; 214 apr_int32_t flags = writable 215 ? APR_READ | APR_WRITE | APR_BUFFERED 216 : APR_READ | APR_BUFFERED; 217 218 /* We may have to *temporarily* enable write access. */ 219 err = writable ? auto_make_writable(path, file_pool, scratch_pool) 220 : SVN_NO_ERROR; 221 222 /* open the revision file in buffered r/o or r/w mode */ 223 if (!err) 224 err = svn_io_file_open(&apr_file, path, flags, APR_OS_DEFAULT, 225 file_pool); 226 227 if (!err) 228 { 229 file->file = apr_file; 230 file->stream = svn_stream_from_aprfile2(apr_file, TRUE, 231 file_pool); 232 233 return SVN_NO_ERROR; 234 } 235 236 if (err && APR_STATUS_IS_ENOENT(err->apr_err)) 237 { 238 /* Could not open the file. This may happen if the 239 * file once existed but got packed later. */ 240 svn_error_clear(err); 241 242 /* if that was our 2nd attempt, leave it at that. */ 243 if (retry) 244 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL, 245 _("No such revision %ld"), rev); 246 247 /* We failed for the first time. Refresh cache & retry. */ 248 SVN_ERR(svn_fs_x__update_min_unpacked_rev(fs, scratch_pool)); 249 file->file_info.start_revision = svn_fs_x__packed_base_rev(fs, rev); 250 251 retry = TRUE; 252 } 253 else 254 { 255 retry = FALSE; 256 } 257 } 258 while (retry); 259 260 return svn_error_trace(err); 261} 262 263svn_error_t * 264svn_fs_x__rev_file_init(svn_fs_x__revision_file_t **file, 265 svn_fs_t *fs, 266 svn_revnum_t rev, 267 apr_pool_t *result_pool) 268{ 269 *file = init_revision_file(fs, rev, result_pool); 270 return SVN_NO_ERROR; 271} 272 273svn_error_t * 274svn_fs_x__rev_file_open_writable(svn_fs_x__revision_file_t** file, 275 svn_fs_t* fs, 276 svn_revnum_t rev, 277 apr_pool_t* result_pool, 278 apr_pool_t *scratch_pool) 279{ 280 *file = init_revision_file(fs, rev, result_pool); 281 return svn_error_trace(open_pack_or_rev_file(*file, TRUE, scratch_pool)); 282} 283 284/* If the revision file in FILE has not been opened, yet, do it now. */ 285static svn_error_t * 286auto_open(svn_fs_x__revision_file_t *file) 287{ 288 if (file->file == NULL) 289 SVN_ERR(open_pack_or_rev_file(file, FALSE, get_file_pool(file))); 290 291 return SVN_NO_ERROR; 292} 293 294/* If the footer data in FILE has not been read, yet, do so now. 295 * Index locations will only be read upon request as we assume they get 296 * cached and the FILE is usually used for REP data access only. 297 * Hence, the separate step. 298 */ 299static svn_error_t * 300auto_read_footer(svn_fs_x__revision_file_t *file) 301{ 302 if (file->l2p_info.start == -1) 303 { 304 apr_off_t filesize = 0; 305 unsigned char footer_length; 306 svn_stringbuf_t *footer; 307 308 /* Determine file size. */ 309 SVN_ERR(auto_open(file)); 310 SVN_ERR(svn_io_file_seek(file->file, APR_END, &filesize, file->pool)); 311 312 /* Read last byte (containing the length of the footer). */ 313 SVN_ERR(svn_io_file_aligned_seek(file->file, file->block_size, NULL, 314 filesize - 1, file->pool)); 315 SVN_ERR(svn_io_file_read_full2(file->file, &footer_length, 316 sizeof(footer_length), NULL, NULL, 317 file->pool)); 318 319 /* Read footer. */ 320 footer = svn_stringbuf_create_ensure(footer_length, file->pool); 321 SVN_ERR(svn_io_file_aligned_seek(file->file, file->block_size, NULL, 322 filesize - 1 - footer_length, 323 file->pool)); 324 SVN_ERR(svn_io_file_read_full2(file->file, footer->data, footer_length, 325 &footer->len, NULL, file->pool)); 326 footer->data[footer->len] = '\0'; 327 328 /* Extract index locations. */ 329 SVN_ERR(svn_fs_x__parse_footer(&file->l2p_info.start, 330 &file->l2p_info.checksum, 331 &file->p2l_info.start, 332 &file->p2l_info.checksum, 333 footer, file->file_info.start_revision, 334 filesize - footer_length - 1, file->pool)); 335 file->l2p_info.end = file->p2l_info.start; 336 file->p2l_info.end = filesize - footer_length - 1; 337 } 338 339 return SVN_NO_ERROR; 340} 341 342svn_error_t * 343svn_fs_x__rev_file_open_proto_rev(svn_fs_x__revision_file_t **file, 344 svn_fs_t *fs, 345 svn_fs_x__txn_id_t txn_id, 346 apr_pool_t* result_pool, 347 apr_pool_t *scratch_pool) 348{ 349 apr_file_t *apr_file; 350 SVN_ERR(svn_io_file_open(&apr_file, 351 svn_fs_x__path_txn_proto_rev(fs, txn_id, 352 scratch_pool), 353 APR_READ | APR_BUFFERED, APR_OS_DEFAULT, 354 result_pool)); 355 356 return svn_error_trace(svn_fs_x__rev_file_wrap_temp(file, fs, apr_file, 357 result_pool)); 358} 359 360svn_error_t * 361svn_fs_x__rev_file_wrap_temp(svn_fs_x__revision_file_t **file, 362 svn_fs_t *fs, 363 apr_file_t *temp_file, 364 apr_pool_t *result_pool) 365{ 366 *file = create_revision_file(fs, result_pool); 367 (*file)->file = temp_file; 368 (*file)->stream = svn_stream_from_aprfile2(temp_file, TRUE, result_pool); 369 370 return SVN_NO_ERROR; 371} 372 373svn_error_t * 374svn_fs_x__rev_file_info(svn_fs_x__rev_file_info_t *info, 375 svn_fs_x__revision_file_t *file) 376{ 377 SVN_ERR(auto_open(file)); 378 379 *info = file->file_info; 380 return SVN_NO_ERROR; 381} 382 383svn_error_t * 384svn_fs_x__rev_file_name(const char **filename, 385 svn_fs_x__revision_file_t *file, 386 apr_pool_t *result_pool) 387{ 388 SVN_ERR(auto_open(file)); 389 390 return svn_error_trace(svn_io_file_name_get(filename, file->file, 391 result_pool)); 392} 393 394svn_error_t * 395svn_fs_x__rev_file_stream(svn_stream_t **stream, 396 svn_fs_x__revision_file_t *file) 397{ 398 SVN_ERR(auto_open(file)); 399 400 *stream = file->stream; 401 return SVN_NO_ERROR; 402} 403 404svn_error_t * 405svn_fs_x__rev_file_get(apr_file_t **apr_file, 406 svn_fs_x__revision_file_t *file) 407{ 408 SVN_ERR(auto_open(file)); 409 410 *apr_file = file->file; 411 return SVN_NO_ERROR; 412} 413 414svn_error_t * 415svn_fs_x__rev_file_l2p_index(svn_fs_x__packed_number_stream_t **stream, 416 svn_fs_x__revision_file_t *file) 417{ 418 if (file->l2p_stream == NULL) 419 { 420 SVN_ERR(auto_read_footer(file)); 421 SVN_ERR(svn_fs_x__packed_stream_open(&file->l2p_stream, 422 file->file, 423 file->l2p_info.start, 424 file->l2p_info.end, 425 SVN_FS_X__L2P_STREAM_PREFIX, 426 (apr_size_t)file->block_size, 427 file->pool, 428 file->pool)); 429 } 430 431 *stream = file->l2p_stream; 432 return SVN_NO_ERROR; 433} 434 435svn_error_t * 436svn_fs_x__rev_file_p2l_index(svn_fs_x__packed_number_stream_t **stream, 437 svn_fs_x__revision_file_t *file) 438{ 439 if (file->p2l_stream== NULL) 440 { 441 SVN_ERR(auto_read_footer(file)); 442 SVN_ERR(svn_fs_x__packed_stream_open(&file->p2l_stream, 443 file->file, 444 file->p2l_info.start, 445 file->p2l_info.end, 446 SVN_FS_X__P2L_STREAM_PREFIX, 447 (apr_size_t)file->block_size, 448 file->pool, 449 file->pool)); 450 } 451 452 *stream = file->p2l_stream; 453 return SVN_NO_ERROR; 454} 455 456svn_error_t * 457svn_fs_x__rev_file_l2p_info(svn_fs_x__index_info_t *info, 458 svn_fs_x__revision_file_t *file) 459{ 460 SVN_ERR(auto_read_footer(file)); 461 *info = file->l2p_info; 462 463 return SVN_NO_ERROR; 464} 465 466svn_error_t * 467svn_fs_x__rev_file_p2l_info(svn_fs_x__index_info_t *info, 468 svn_fs_x__revision_file_t *file) 469{ 470 SVN_ERR(auto_read_footer(file)); 471 *info = file->p2l_info; 472 473 return SVN_NO_ERROR; 474} 475 476svn_error_t * 477svn_fs_x__rev_file_data_size(svn_filesize_t *size, 478 svn_fs_x__revision_file_t *file) 479{ 480 SVN_ERR(auto_read_footer(file)); 481 *size = file->l2p_info.start; 482 483 return SVN_NO_ERROR; 484} 485 486svn_error_t * 487svn_fs_x__rev_file_seek(svn_fs_x__revision_file_t *file, 488 apr_off_t *buffer_start, 489 apr_off_t offset) 490{ 491 SVN_ERR(auto_open(file)); 492 return svn_error_trace(svn_io_file_aligned_seek(file->file, 493 file->block_size, 494 buffer_start, offset, 495 file->pool)); 496} 497 498svn_error_t * 499svn_fs_x__rev_file_offset(apr_off_t *offset, 500 svn_fs_x__revision_file_t *file) 501{ 502 SVN_ERR(auto_open(file)); 503 return svn_error_trace(svn_io_file_get_offset(offset, file->file, 504 file->pool)); 505} 506 507svn_error_t * 508svn_fs_x__rev_file_read(svn_fs_x__revision_file_t *file, 509 void *buf, 510 apr_size_t nbytes) 511{ 512 SVN_ERR(auto_open(file)); 513 return svn_error_trace(svn_io_file_read_full2(file->file, buf, nbytes, 514 NULL, NULL, file->pool)); 515} 516 517svn_error_t * 518svn_fs_x__close_revision_file(svn_fs_x__revision_file_t *file) 519{ 520 /* Close sub-objects properly */ 521 if (file->stream) 522 SVN_ERR(svn_stream_close(file->stream)); 523 if (file->file) 524 SVN_ERR(svn_io_file_close(file->file, file->pool)); 525 526 /* Release the memory. */ 527 if (file->pool) 528 svn_pool_clear(file->pool); 529 530 /* Reset pointers to objects previously allocated from FILE->POOL. */ 531 file->file = NULL; 532 file->stream = NULL; 533 file->l2p_stream = NULL; 534 file->p2l_stream = NULL; 535 536 /* Cause any index data getters to re-read the footer. */ 537 file->l2p_info.start = -1; 538 return SVN_NO_ERROR; 539} 540