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
* ====================================================================
26
#include "low_level.h"
29
#include "../libsvn_fs/fs-loader.h"
31
#include "private/svn_io_private.h"
32
#include "svn_private_config.h"
34
/* Return a new revision file instance, allocated in RESULT_POOL, for
35
* filesystem FS. Set its pool member to the provided RESULT_POOL. */
36
static svn_fs_x__revision_file_t *
37
create_revision_file(svn_fs_t *fs,
38
apr_pool_t *result_pool)
40
svn_fs_x__data_t *ffd = fs->fsap_data;
41
svn_fs_x__revision_file_t *file = apr_palloc(result_pool, sizeof(*file));
43
file->is_packed = FALSE;
44
file->start_revision = SVN_INVALID_REVNUM;
48
file->p2l_stream = NULL;
49
file->l2p_stream = NULL;
50
file->block_size = ffd->block_size;
51
file->l2p_offset = -1;
52
file->l2p_checksum = NULL;
53
file->p2l_offset = -1;
54
file->p2l_checksum = NULL;
55
file->footer_offset = -1;
56
file->pool = result_pool;
61
/* Return a new revision file instance, allocated in RESULT_POOL, for
62
* REVISION in filesystem FS. Set its pool member to the provided
64
static svn_fs_x__revision_file_t *
65
init_revision_file(svn_fs_t *fs,
66
svn_revnum_t revision,
67
apr_pool_t *result_pool)
69
svn_fs_x__revision_file_t *file = create_revision_file(fs, result_pool);
71
file->is_packed = svn_fs_x__is_packed_rev(fs, revision);
72
file->start_revision = svn_fs_x__packed_base_rev(fs, revision);
77
/* Baton type for set_read_only() */
78
typedef struct set_read_only_baton_t
80
/* File to set to read-only. */
81
const char *file_path;
83
/* Scratch pool sufficient life time.
84
* Ideally the pool that we registered the cleanup on. */
86
} set_read_only_baton_t;
88
/* APR pool cleanup callback taking a set_read_only_baton_t baton and then
89
* (trying to) set the specified file to r/o mode. */
91
set_read_only(void *baton)
93
set_read_only_baton_t *ro_baton = baton;
94
apr_status_t status = APR_SUCCESS;
97
err = svn_io_set_file_read_only(ro_baton->file_path, TRUE, ro_baton->pool);
100
status = err->apr_err;
101
svn_error_clear(err);
107
/* If the file at PATH is read-only, attempt to make it writable. The
108
* original state will be restored with RESULT_POOL gets cleaned up.
109
* SCRATCH_POOL is for temporary allocations. */
111
auto_make_writable(const char *path,
112
apr_pool_t *result_pool,
113
apr_pool_t *scratch_pool)
115
svn_boolean_t is_read_only;
118
SVN_ERR(svn_io_stat(&finfo, path, SVN__APR_FINFO_READONLY, scratch_pool));
119
SVN_ERR(svn_io__is_finfo_read_only(&is_read_only, &finfo, scratch_pool));
123
/* Tell the pool to restore the r/o state upon cleanup
124
(assuming the file will still exist, failing silently otherwise). */
125
set_read_only_baton_t *baton = apr_pcalloc(result_pool,
127
baton->pool = result_pool;
128
baton->file_path = apr_pstrdup(result_pool, path);
129
apr_pool_cleanup_register(result_pool, baton,
130
set_read_only, apr_pool_cleanup_null);
132
/* Finally, allow write access (undoing it has already been scheduled
133
and is idempotent). */
134
SVN_ERR(svn_io_set_file_read_write(path, FALSE, scratch_pool));
140
/* Core implementation of svn_fs_fs__open_pack_or_rev_file working on an
141
* existing, initialized FILE structure. If WRITABLE is TRUE, give write
142
* access to the file - temporarily resetting the r/o state if necessary.
145
open_pack_or_rev_file(svn_fs_x__revision_file_t *file,
148
svn_boolean_t writable,
149
apr_pool_t *result_pool,
150
apr_pool_t *scratch_pool)
153
svn_boolean_t retry = FALSE;
157
const char *path = svn_fs_x__path_rev_absolute(fs, rev, scratch_pool);
158
apr_file_t *apr_file;
159
apr_int32_t flags = writable
160
? APR_READ | APR_WRITE | APR_BUFFERED
161
: APR_READ | APR_BUFFERED;
163
/* We may have to *temporarily* enable write access. */
164
err = writable ? auto_make_writable(path, result_pool, scratch_pool)
167
/* open the revision file in buffered r/o or r/w mode */
169
err = svn_io_file_open(&apr_file, path, flags, APR_OS_DEFAULT,
174
file->file = apr_file;
175
file->stream = svn_stream_from_aprfile2(apr_file, TRUE,
181
if (err && APR_STATUS_IS_ENOENT(err->apr_err))
183
/* Could not open the file. This may happen if the
184
* file once existed but got packed later. */
185
svn_error_clear(err);
187
/* if that was our 2nd attempt, leave it at that. */
189
return svn_error_createf(SVN_ERR_FS_NO_SUCH_REVISION, NULL,
190
_("No such revision %ld"), rev);
192
/* We failed for the first time. Refresh cache & retry. */
193
SVN_ERR(svn_fs_x__update_min_unpacked_rev(fs, scratch_pool));
194
file->start_revision = svn_fs_x__packed_base_rev(fs, rev);
205
return svn_error_trace(err);
209
svn_fs_x__open_pack_or_rev_file(svn_fs_x__revision_file_t **file,
212
apr_pool_t *result_pool,
213
apr_pool_t *scratch_pool)
215
*file = init_revision_file(fs, rev, result_pool);
216
return svn_error_trace(open_pack_or_rev_file(*file, fs, rev, FALSE,
217
result_pool, scratch_pool));
221
svn_fs_x__open_pack_or_rev_file_writable(svn_fs_x__revision_file_t** file,
224
apr_pool_t* result_pool,
225
apr_pool_t *scratch_pool)
227
*file = init_revision_file(fs, rev, result_pool);
228
return svn_error_trace(open_pack_or_rev_file(*file, fs, rev, TRUE,
229
result_pool, scratch_pool));
233
svn_fs_x__auto_read_footer(svn_fs_x__revision_file_t *file)
235
if (file->l2p_offset == -1)
237
apr_off_t filesize = 0;
238
unsigned char footer_length;
239
svn_stringbuf_t *footer;
241
/* Determine file size. */
242
SVN_ERR(svn_io_file_seek(file->file, APR_END, &filesize, file->pool));
244
/* Read last byte (containing the length of the footer). */
245
SVN_ERR(svn_io_file_aligned_seek(file->file, file->block_size, NULL,
246
filesize - 1, file->pool));
247
SVN_ERR(svn_io_file_read_full2(file->file, &footer_length,
248
sizeof(footer_length), NULL, NULL,
252
footer = svn_stringbuf_create_ensure(footer_length, file->pool);
253
SVN_ERR(svn_io_file_aligned_seek(file->file, file->block_size, NULL,
254
filesize - 1 - footer_length,
256
SVN_ERR(svn_io_file_read_full2(file->file, footer->data, footer_length,
257
&footer->len, NULL, file->pool));
258
footer->data[footer->len] = '\0';
260
/* Extract index locations. */
261
SVN_ERR(svn_fs_x__parse_footer(&file->l2p_offset, &file->l2p_checksum,
262
&file->p2l_offset, &file->p2l_checksum,
263
footer, file->start_revision,
265
file->footer_offset = filesize - footer_length - 1;
272
svn_fs_x__open_proto_rev_file(svn_fs_x__revision_file_t **file,
274
svn_fs_x__txn_id_t txn_id,
275
apr_pool_t* result_pool,
276
apr_pool_t *scratch_pool)
278
apr_file_t *apr_file;
279
SVN_ERR(svn_io_file_open(&apr_file,
280
svn_fs_x__path_txn_proto_rev(fs, txn_id,
282
APR_READ | APR_BUFFERED, APR_OS_DEFAULT,
285
return svn_error_trace(svn_fs_x__wrap_temp_rev_file(file, fs, apr_file,
290
svn_fs_x__wrap_temp_rev_file(svn_fs_x__revision_file_t **file,
292
apr_file_t *temp_file,
293
apr_pool_t *result_pool)
295
*file = create_revision_file(fs, result_pool);
296
(*file)->file = temp_file;
297
(*file)->stream = svn_stream_from_aprfile2(temp_file, TRUE, result_pool);
303
svn_fs_x__close_revision_file(svn_fs_x__revision_file_t *file)
306
SVN_ERR(svn_stream_close(file->stream));
308
SVN_ERR(svn_io_file_close(file->file, file->pool));
312
file->l2p_stream = NULL;
313
file->p2l_stream = NULL;