1 /* fs_id.c : FSX's implementation of svn_fs_id_t
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 "cached_data.h"
28 #include "../libsvn_fs/fs-loader.h"
29 #include "private/svn_string_private.h"
33 /* Structure holding everything needed to implement svn_fs_id_t for FSX.
35 typedef struct fs_x__id_t
38 The fsap_data member points to our svn_fs_x__id_context_t object. */
39 svn_fs_id_t generic_id;
42 This addresses the DAG node identified by this ID object.
43 If it refers to a TXN, it may become . */
44 svn_fs_x__id_t noderev_id;
50 /* The state machine behind this is as follows:
52 (A) FS passed in during context construction still open and uses a
53 different pool as the context (Usually the initial state). In that
54 case, FS_PATH is NULL and we watch for either pool's cleanup.
57 (B). Transition triggered by FS->POOL cleanup.
58 (D). Transition triggered by OWNER cleanup.
60 (B) FS has been closed but not the OWNER pool, i.e. the context is valid.
61 FS is NULL, FS_NAME has been set. No cleanup functions are registered.
64 (C). Transition triggered by successful access to the file system.
65 (D). Transition triggered by OWNER cleanup.
67 (C) FS is open, allocated in the context's OWNER pool (maybe the initial
68 state but that is atypical). No cleanup functions are registered.
71 (D). Transition triggered by OWNER cleanup.
73 (D) Destroyed. No access nor notification is allowed.
77 struct svn_fs_x__id_context_t
79 /* If this is NULL, FS_PATH points to the on-disk path to the file system
80 we need to re-open the FS. */
83 /* If FS is NULL, this points to the on-disk path to pass into svn_fs_open2
84 to reopen the filesystem. Allocated in OWNER. May only be NULL if FS
88 /* If FS is NULL, this points to svn_fs_open() as passed to the library. */
89 svn_error_t *(*svn_fs_open_)(svn_fs_t **,
95 /* Pool that this context struct got allocated in. */
98 /* A sub-pool of ONWER. We use this when querying data from FS. Gets
99 cleanup up immediately after usage. NULL until needed for the first
101 apr_pool_t *aux_pool;
104 /* Forward declaration. */
106 fs_cleanup(void *baton);
108 /* APR pool cleanup notification for the svn_fs_x__id_context_t given as
109 BATON. Sent at state (A)->(D) transition. */
111 owner_cleanup(void *baton)
113 svn_fs_x__id_context_t *context = baton;
115 /* Everything in CONTEXT gets cleaned up automatically.
116 However, we must prevent notifications from other pools. */
117 apr_pool_cleanup_kill(context->fs->pool, context, fs_cleanup);
122 /* APR pool cleanup notification for the svn_fs_x__id_context_t given as
123 BATON. Sent at state (A)->(B) transition. */
125 fs_cleanup(void *baton)
127 svn_fs_x__id_context_t *context = baton;
128 svn_fs_x__data_t *ffd = context->fs->fsap_data;
130 /* Remember the FS_PATH to potentially reopen and mark the FS as n/a. */
131 context->fs_path = apr_pstrdup(context->owner, context->fs->path);
132 context->svn_fs_open_ = ffd->svn_fs_open_;
136 /* No need for further notifications because from now on, everything is
137 allocated in OWNER. */
138 apr_pool_cleanup_kill(context->owner, context, owner_cleanup);
143 /* Return the filesystem provided by CONTEXT. Re-open it if necessary.
144 Returns NULL if the FS could not be opened. */
146 get_fs(svn_fs_x__id_context_t *context)
152 SVN_ERR_ASSERT_NO_RETURN(context->svn_fs_open_);
154 err = context->svn_fs_open_(&context->fs, context->fs_path, NULL,
155 context->owner, context->owner);
158 svn_error_clear(err);
166 /* Provide the auto-created auxiliary pool from ID's context object. */
168 get_aux_pool(const fs_x__id_t *id)
170 svn_fs_x__id_context_t *context = id->generic_id.fsap_data;
171 if (!context->aux_pool)
172 context->aux_pool = svn_pool_create(context->owner);
174 return context->aux_pool;
177 /* Return the noderev structure identified by ID. Returns NULL for invalid
178 IDs or inaccessible repositories. The caller should clear the auxiliary
179 pool before returning to its respective caller. */
180 static svn_fs_x__noderev_t *
181 get_noderev(const fs_x__id_t *id)
183 svn_fs_x__noderev_t *result = NULL;
185 svn_fs_x__id_context_t *context = id->generic_id.fsap_data;
186 svn_fs_t *fs = get_fs(context);
187 apr_pool_t *pool = get_aux_pool(id);
191 svn_error_t *err = svn_fs_x__get_node_revision(&result, fs,
196 svn_error_clear(err);
206 /*** Implement v-table functions ***/
208 /* Implement id_vtable_t.unparse */
209 static svn_string_t *
210 id_unparse(const svn_fs_id_t *fs_id,
211 apr_pool_t *result_pool)
213 const fs_x__id_t *id = (const fs_x__id_t *)fs_id;
214 return svn_fs_x__id_unparse(&id->noderev_id, result_pool);
217 /* Implement id_vtable_t.compare.
219 The result is efficiently computed for matching IDs. The far less
220 meaningful "common ancestor" relationship has a larger latency when
221 evaluated first for a given context object. Subsequent calls are
223 static svn_fs_node_relation_t
224 id_compare(const svn_fs_id_t *a,
225 const svn_fs_id_t *b)
227 const fs_x__id_t *id_a = (const fs_x__id_t *)a;
228 const fs_x__id_t *id_b = (const fs_x__id_t *)b;
229 svn_fs_x__noderev_t *noderev_a, *noderev_b;
230 svn_boolean_t same_node;
232 /* Quick check: same IDs? */
233 if (svn_fs_x__id_eq(&id_a->noderev_id, &id_b->noderev_id))
234 return svn_fs_node_unchanged;
236 /* Fetch the nodesrevs, compare the IDs of the nodes they belong to and
237 clean up any temporaries. If we can't find one of the noderevs, don't
238 get access to the FS etc., report the IDs as "unrelated" as only
239 valid / existing things may be related. */
240 noderev_a = get_noderev(id_a);
241 noderev_b = get_noderev(id_b);
243 if (noderev_a && noderev_b)
244 same_node = svn_fs_x__id_eq(&noderev_a->node_id, &noderev_b->node_id);
248 svn_pool_clear(get_aux_pool(id_a));
249 svn_pool_clear(get_aux_pool(id_b));
252 return same_node ? svn_fs_node_common_ancestor : svn_fs_node_unrelated;
258 static id_vtable_t id_vtable = {
263 svn_fs_x__id_context_t *
264 svn_fs_x__id_create_context(svn_fs_t *fs,
265 apr_pool_t *result_pool)
267 svn_fs_x__id_context_t *result = apr_pcalloc(result_pool, sizeof(*result));
269 result->owner = result_pool;
271 /* Check for a special case:
272 If the owner of the context also owns the FS, there will be no reason
273 to notify them of the respective other's cleanup. */
274 if (result_pool != fs->pool)
276 /* If the context's owner gets cleaned up before FS, we must disconnect
278 apr_pool_cleanup_register(result_pool,
281 apr_pool_cleanup_null);
283 /* If the FS gets cleaned up before the context's owner, disconnect
284 from the FS and remember its path on disk to be able to re-open it
285 later if necessary. */
286 apr_pool_cleanup_register(fs->pool,
289 apr_pool_cleanup_null);
296 svn_fs_x__id_create(svn_fs_x__id_context_t *context,
297 const svn_fs_x__id_t *noderev_id,
298 apr_pool_t *result_pool)
302 /* Special case: NULL IDs */
303 if (!svn_fs_x__id_used(noderev_id))
306 /* In theory, the CONTEXT might not be owned by POOL. It's FS might even
307 have been closed. Make sure we have a context owned by POOL. */
308 if (context->owner != result_pool)
309 context = svn_fs_x__id_create_context(get_fs(context), result_pool);
311 /* Finally, construct the ID object. */
312 id = apr_pcalloc(result_pool, sizeof(*id));
313 id->noderev_id = *noderev_id;
315 id->generic_id.vtable = &id_vtable;
316 id->generic_id.fsap_data = context;
318 return (svn_fs_id_t *)id;