1 /* rev_file.c --- revision file and index access functions
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"
28 #include "low_level.h"
31 #include "../libsvn_fs/fs-loader.h"
33 #include "private/svn_io_private.h"
34 #include "svn_private_config.h"
36 struct svn_fs_x__revision_file_t
38 /* the filesystem that this revision file belongs to */
41 /* Meta-data to FILE. */
42 svn_fs_x__rev_file_info_t file_info;
47 /* stream based on FILE and not NULL exactly when FILE is not NULL */
50 /* the opened P2L index stream or NULL. Always NULL for txns. */
51 svn_fs_x__packed_number_stream_t *p2l_stream;
53 /* the opened L2P index stream or NULL. Always NULL for txns. */
54 svn_fs_x__packed_number_stream_t *l2p_stream;
56 /* Copied from FS->FFD->BLOCK_SIZE upon creation. It allows us to
57 * use aligned seek() without having the FS handy. */
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;
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;
68 /* Pool used for all sub-structure allocations (file, streams etc.).
69 A sub-pool of OWNER. NULL until the lazily initilized. */
72 /* Pool that this structure got allocated in. */
76 /* Return a new revision file instance, allocated in RESULT_POOL, for
77 * filesystem FS. Set its pool member to the provided RESULT_POOL. */
78 static svn_fs_x__revision_file_t *
79 create_revision_file(svn_fs_t *fs,
80 apr_pool_t *result_pool)
82 svn_fs_x__data_t *ffd = fs->fsap_data;
84 svn_fs_x__revision_file_t *file = apr_palloc(result_pool, sizeof(*file));
86 file->file_info.is_packed = FALSE;
87 file->file_info.start_revision = SVN_INVALID_REVNUM;
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;
100 file->owner = result_pool;
105 /* Return a new revision file instance, allocated in RESULT_POOL, for
106 * REVISION in filesystem FS. Set its pool member to the provided
108 static svn_fs_x__revision_file_t *
109 init_revision_file(svn_fs_t *fs,
110 svn_revnum_t revision,
111 apr_pool_t *result_pool)
113 svn_fs_x__revision_file_t *file = create_revision_file(fs, result_pool);
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);
121 /* Baton type for set_read_only() */
122 typedef struct set_read_only_baton_t
124 /* File to set to read-only. */
125 const char *file_path;
127 /* Scratch pool sufficient life time.
128 * Ideally the pool that we registered the cleanup on. */
130 } set_read_only_baton_t;
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. */
135 set_read_only(void *baton)
137 set_read_only_baton_t *ro_baton = baton;
138 apr_status_t status = APR_SUCCESS;
141 err = svn_io_set_file_read_only(ro_baton->file_path, TRUE, ro_baton->pool);
144 status = err->apr_err;
145 svn_error_clear(err);
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. */
155 auto_make_writable(const char *path,
156 apr_pool_t *result_pool,
157 apr_pool_t *scratch_pool)
159 svn_boolean_t is_read_only;
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));
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,
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);
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));
184 /* Return the pool to be used for allocations with FILE.
185 Lazily created that pool upon the first call. */
187 get_file_pool(svn_fs_x__revision_file_t *file)
189 if (file->pool == NULL)
190 file->pool = svn_pool_create(file->owner);
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.
200 open_pack_or_rev_file(svn_fs_x__revision_file_t *file,
201 svn_boolean_t writable,
202 apr_pool_t *scratch_pool)
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);
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;
218 /* We may have to *temporarily* enable write access. */
219 err = writable ? auto_make_writable(path, file_pool, scratch_pool)
222 /* open the revision file in buffered r/o or r/w mode */
224 err = svn_io_file_open(&apr_file, path, flags, APR_OS_DEFAULT,
229 file->file = apr_file;
230 file->stream = svn_stream_from_aprfile2(apr_file, TRUE,
236 if (err && APR_STATUS_IS_ENOENT(err->apr_err))
238 /* Could not open the file. This may happen if the
239 * file once existed but got packed later. */
240 svn_error_clear(err);
242 /* if that was our 2nd attempt, leave it at that. */
244 return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
245 _("No such revision %ld"), rev);
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);
260 return svn_error_trace(err);
264 svn_fs_x__rev_file_init(svn_fs_x__revision_file_t **file,
267 apr_pool_t *result_pool)
269 *file = init_revision_file(fs, rev, result_pool);
274 svn_fs_x__rev_file_open_writable(svn_fs_x__revision_file_t** file,
277 apr_pool_t* result_pool,
278 apr_pool_t *scratch_pool)
280 *file = init_revision_file(fs, rev, result_pool);
281 return svn_error_trace(open_pack_or_rev_file(*file, TRUE, scratch_pool));
284 /* If the revision file in FILE has not been opened, yet, do it now. */
286 auto_open(svn_fs_x__revision_file_t *file)
288 if (file->file == NULL)
289 SVN_ERR(open_pack_or_rev_file(file, FALSE, get_file_pool(file)));
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.
300 auto_read_footer(svn_fs_x__revision_file_t *file)
302 if (file->l2p_info.start == -1)
304 apr_off_t filesize = 0;
305 unsigned char footer_length;
306 svn_stringbuf_t *footer;
308 /* Determine file size. */
309 SVN_ERR(auto_open(file));
310 SVN_ERR(svn_io_file_seek(file->file, APR_END, &filesize, file->pool));
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,
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,
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';
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;
343 svn_fs_x__rev_file_open_proto_rev(svn_fs_x__revision_file_t **file,
345 svn_fs_x__txn_id_t txn_id,
346 apr_pool_t* result_pool,
347 apr_pool_t *scratch_pool)
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,
353 APR_READ | APR_BUFFERED, APR_OS_DEFAULT,
356 return svn_error_trace(svn_fs_x__rev_file_wrap_temp(file, fs, apr_file,
361 svn_fs_x__rev_file_wrap_temp(svn_fs_x__revision_file_t **file,
363 apr_file_t *temp_file,
364 apr_pool_t *result_pool)
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);
374 svn_fs_x__rev_file_info(svn_fs_x__rev_file_info_t *info,
375 svn_fs_x__revision_file_t *file)
377 SVN_ERR(auto_open(file));
379 *info = file->file_info;
384 svn_fs_x__rev_file_name(const char **filename,
385 svn_fs_x__revision_file_t *file,
386 apr_pool_t *result_pool)
388 SVN_ERR(auto_open(file));
390 return svn_error_trace(svn_io_file_name_get(filename, file->file,
395 svn_fs_x__rev_file_stream(svn_stream_t **stream,
396 svn_fs_x__revision_file_t *file)
398 SVN_ERR(auto_open(file));
400 *stream = file->stream;
405 svn_fs_x__rev_file_get(apr_file_t **apr_file,
406 svn_fs_x__revision_file_t *file)
408 SVN_ERR(auto_open(file));
410 *apr_file = file->file;
415 svn_fs_x__rev_file_l2p_index(svn_fs_x__packed_number_stream_t **stream,
416 svn_fs_x__revision_file_t *file)
418 if (file->l2p_stream == NULL)
420 SVN_ERR(auto_read_footer(file));
421 SVN_ERR(svn_fs_x__packed_stream_open(&file->l2p_stream,
423 file->l2p_info.start,
425 SVN_FS_X__L2P_STREAM_PREFIX,
426 (apr_size_t)file->block_size,
431 *stream = file->l2p_stream;
436 svn_fs_x__rev_file_p2l_index(svn_fs_x__packed_number_stream_t **stream,
437 svn_fs_x__revision_file_t *file)
439 if (file->p2l_stream== NULL)
441 SVN_ERR(auto_read_footer(file));
442 SVN_ERR(svn_fs_x__packed_stream_open(&file->p2l_stream,
444 file->p2l_info.start,
446 SVN_FS_X__P2L_STREAM_PREFIX,
447 (apr_size_t)file->block_size,
452 *stream = file->p2l_stream;
457 svn_fs_x__rev_file_l2p_info(svn_fs_x__index_info_t *info,
458 svn_fs_x__revision_file_t *file)
460 SVN_ERR(auto_read_footer(file));
461 *info = file->l2p_info;
467 svn_fs_x__rev_file_p2l_info(svn_fs_x__index_info_t *info,
468 svn_fs_x__revision_file_t *file)
470 SVN_ERR(auto_read_footer(file));
471 *info = file->p2l_info;
477 svn_fs_x__rev_file_data_size(svn_filesize_t *size,
478 svn_fs_x__revision_file_t *file)
480 SVN_ERR(auto_read_footer(file));
481 *size = file->l2p_info.start;
487 svn_fs_x__rev_file_seek(svn_fs_x__revision_file_t *file,
488 apr_off_t *buffer_start,
491 SVN_ERR(auto_open(file));
492 return svn_error_trace(svn_io_file_aligned_seek(file->file,
494 buffer_start, offset,
499 svn_fs_x__rev_file_offset(apr_off_t *offset,
500 svn_fs_x__revision_file_t *file)
502 SVN_ERR(auto_open(file));
503 return svn_error_trace(svn_io_file_get_offset(offset, file->file,
508 svn_fs_x__rev_file_read(svn_fs_x__revision_file_t *file,
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));
518 svn_fs_x__close_revision_file(svn_fs_x__revision_file_t *file)
520 /* Close sub-objects properly */
522 SVN_ERR(svn_stream_close(file->stream));
524 SVN_ERR(svn_io_file_close(file->file, file->pool));
526 /* Release the memory. */
528 svn_pool_clear(file->pool);
530 /* Reset pointers to objects previously allocated from FILE->POOL. */
533 file->l2p_stream = NULL;
534 file->p2l_stream = NULL;
536 /* Cause any index data getters to re-read the footer. */
537 file->l2p_info.start = -1;