]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/subversion/subversion/libsvn_fs_x/fs_id.c
MFC r275385 (by bapt):
[FreeBSD/stable/10.git] / contrib / subversion / subversion / libsvn_fs_x / fs_id.c
1 /* fs_id.c : FSX's implementation of svn_fs_id_t
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 "cached_data.h"
26 #include "fs_id.h"
27
28 #include "../libsvn_fs/fs-loader.h"
29 #include "private/svn_string_private.h"
30
31 \f
32
33 /* Structure holding everything needed to implement svn_fs_id_t for FSX.
34  */
35 typedef struct fs_x__id_t
36 {
37   /* API visible part.
38      The fsap_data member points to our svn_fs_x__id_context_t object. */
39   svn_fs_id_t generic_id;
40
41   /* Private members.
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;
45
46 } fs_x__id_t;
47
48 \f
49
50 /* The state machine behind this is as follows:
51
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.
55
56        Next states:
57        (B). Transition triggered by FS->POOL cleanup.
58        (D). Transition triggered by OWNER cleanup.
59
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.
62
63        Next states:
64        (C). Transition triggered by successful access to the file system.
65        (D). Transition triggered by OWNER cleanup.
66
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.
69
70        Next states:
71        (D). Transition triggered by OWNER cleanup.
72
73    (D) Destroyed.  No access nor notification is allowed.
74        Final state.
75
76  */
77 struct svn_fs_x__id_context_t
78 {
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. */
81   svn_fs_t *fs;
82
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
85      is not.*/
86   const char *fs_path;
87
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 **,
90       const char *,
91       apr_hash_t *,
92       apr_pool_t *,
93       apr_pool_t *);
94
95   /* Pool that this context struct got allocated in. */
96   apr_pool_t *owner;
97
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
100      time. */
101   apr_pool_t *aux_pool;
102 };
103
104 /* Forward declaration. */
105 static apr_status_t
106 fs_cleanup(void *baton);
107
108 /* APR pool cleanup notification for the svn_fs_x__id_context_t given as
109    BATON.  Sent at state (A)->(D) transition. */
110 static apr_status_t
111 owner_cleanup(void *baton)
112 {
113   svn_fs_x__id_context_t *context = baton;
114
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);
118
119   return  APR_SUCCESS;
120 }
121
122 /* APR pool cleanup notification for the svn_fs_x__id_context_t given as
123    BATON.  Sent at state (A)->(B) transition. */
124 static apr_status_t
125 fs_cleanup(void *baton)
126 {
127   svn_fs_x__id_context_t *context = baton;
128   svn_fs_x__data_t *ffd = context->fs->fsap_data;
129
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_;
133   context->fs = NULL;
134
135
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);
139
140   return  APR_SUCCESS;
141 }
142
143 /* Return the filesystem provided by CONTEXT.  Re-open it if necessary.
144    Returns NULL if the FS could not be opened. */
145 static svn_fs_t *
146 get_fs(svn_fs_x__id_context_t *context)
147 {
148   if (!context->fs)
149     {
150       svn_error_t *err;
151
152       SVN_ERR_ASSERT_NO_RETURN(context->svn_fs_open_);
153
154       err = context->svn_fs_open_(&context->fs, context->fs_path, NULL,
155                                   context->owner, context->owner);
156       if (err)
157         {
158           svn_error_clear(err);
159           context->fs = NULL;
160         }
161     }
162
163   return context->fs;
164 }
165
166 /* Provide the auto-created auxiliary pool from ID's context object. */
167 static apr_pool_t *
168 get_aux_pool(const fs_x__id_t *id)
169 {
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);
173
174   return context->aux_pool;
175 }
176
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)
182 {
183   svn_fs_x__noderev_t *result = NULL;
184
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);
188
189   if (fs)
190     {
191       svn_error_t *err = svn_fs_x__get_node_revision(&result, fs,
192                                                      &id->noderev_id,
193                                                      pool, pool);
194       if (err)
195         {
196           svn_error_clear(err);
197           result = NULL;
198         }
199     }
200
201   return result;
202 }
203
204 \f
205
206 /*** Implement v-table functions ***/
207
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)
212 {
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);
215 }
216
217 /* Implement id_vtable_t.compare.
218
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
222    moderately fast. */
223 static svn_fs_node_relation_t
224 id_compare(const svn_fs_id_t *a,
225            const svn_fs_id_t *b)
226 {
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;
231
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;
235
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);
242
243   if (noderev_a && noderev_b)
244     same_node = svn_fs_x__id_eq(&noderev_a->node_id, &noderev_b->node_id);
245   else
246     same_node = FALSE;
247
248   svn_pool_clear(get_aux_pool(id_a));
249   svn_pool_clear(get_aux_pool(id_b));
250
251   /* Return result. */
252   return same_node ? svn_fs_node_common_ancestor : svn_fs_node_unrelated;
253 }
254
255 \f
256 /* Creating ID's.  */
257
258 static id_vtable_t id_vtable = {
259   id_unparse,
260   id_compare
261 };
262
263 svn_fs_x__id_context_t *
264 svn_fs_x__id_create_context(svn_fs_t *fs,
265                             apr_pool_t *result_pool)
266 {
267   svn_fs_x__id_context_t *result = apr_pcalloc(result_pool, sizeof(*result));
268   result->fs = fs;
269   result->owner = result_pool;
270
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)
275     {
276       /* If the context's owner gets cleaned up before FS, we must disconnect
277          from the FS. */
278       apr_pool_cleanup_register(result_pool,
279                                 result,
280                                 owner_cleanup,
281                                 apr_pool_cleanup_null);
282
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,
287                                 result,
288                                 fs_cleanup,
289                                 apr_pool_cleanup_null);
290     }
291
292   return result;
293 }
294
295 svn_fs_id_t *
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)
299 {
300   fs_x__id_t *id;
301
302   /* Special case: NULL IDs */
303   if (!svn_fs_x__id_used(noderev_id))
304     return NULL;
305
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);
310
311   /* Finally, construct the ID object. */
312   id = apr_pcalloc(result_pool, sizeof(*id));
313   id->noderev_id = *noderev_id;
314
315   id->generic_id.vtable = &id_vtable;
316   id->generic_id.fsap_data = context;
317
318   return (svn_fs_id_t *)id;
319 }