1 /* fs-util.c : internal utility functions used by both FSFS and BDB back
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
26 #include <apr_pools.h>
27 #include <apr_strings.h>
29 #include "svn_private_config.h"
32 #include "svn_dirent_uri.h"
34 #include "svn_version.h"
36 #include "private/svn_fs_util.h"
37 #include "private/svn_fspath.h"
38 #include "private/svn_subr_private.h"
39 #include "../libsvn_fs/fs-loader.h"
43 svn_fs_util__version(void)
49 /* Return TRUE, if PATH of PATH_LEN > 0 chars starts with a '/' and does
50 * not end with a '/' and does not contain duplicate '/'.
53 is_canonical_abspath(const char *path, size_t path_len)
57 /* check for leading '/' */
61 /* check for trailing '/' */
64 if (path[path_len - 1] == '/')
68 end = path + path_len - 1;
69 for (; path != end; ++path)
70 if ((path[0] == '/') && (path[1] == '/'))
77 svn_fs__is_canonical_abspath(const char *path)
79 /* No PATH? No problem. */
83 /* Empty PATH? That's just "/". */
88 return is_canonical_abspath(path, strlen(path));
92 svn_fs__canonicalize_abspath(const char *path, apr_pool_t *pool)
96 size_t path_i = 0, newpath_i = 0;
97 svn_boolean_t eating_slashes = FALSE;
99 /* No PATH? No problem. */
103 /* Empty PATH? That's just "/". */
107 /* Non-trivial cases. Maybe, the path already is canonical after all? */
108 path_len = strlen(path);
109 if (is_canonical_abspath(path, path_len))
110 return apr_pstrmemdup(pool, path, path_len);
112 /* Now, the fun begins. Alloc enough room to hold PATH with an
113 added leading '/'. */
114 newpath = apr_palloc(pool, path_len + 2);
116 /* No leading slash? Fix that. */
119 newpath[newpath_i++] = '/';
122 for (path_i = 0; path_i < path_len; path_i++)
124 if (path[path_i] == '/')
126 /* The current character is a '/'. If we are eating up
127 extra '/' characters, skip this character. Else, note
128 that we are now eating slashes. */
131 eating_slashes = TRUE;
135 /* The current character is NOT a '/'. If we were eating
136 slashes, we need not do that any more. */
138 eating_slashes = FALSE;
141 /* Copy the current character into our new buffer. */
142 newpath[newpath_i++] = path[path_i];
145 /* Did we leave a '/' attached to the end of NEWPATH (other than in
146 the root directory case)? */
147 if ((newpath[newpath_i - 1] == '/') && (newpath_i > 1))
148 newpath[newpath_i - 1] = '\0';
150 newpath[newpath_i] = '\0';
156 svn_fs__check_fs(svn_fs_t *fs,
157 svn_boolean_t expect_open)
159 if ((expect_open && fs->fsap_data)
160 || ((! expect_open) && (! fs->fsap_data)))
163 return svn_error_create(SVN_ERR_FS_NOT_OPEN, 0,
164 _("Filesystem object has not been opened yet"));
166 return svn_error_create(SVN_ERR_FS_ALREADY_OPEN, 0,
167 _("Filesystem object already open"));
171 svn_fs__next_entry_name(const char **next_p,
177 /* Find the end of the current component. */
178 end = strchr(path, '/');
182 /* The path contains only one component, with no trailing
185 return apr_pstrdup(pool, path);
189 /* There's a slash after the first component. Skip over an arbitrary
190 number of slashes to find the next one. */
191 const char *next = end;
195 return apr_pstrndup(pool, path, end - path);
199 svn_fs_path_change2_t *
200 svn_fs__path_change_create_internal(const svn_fs_id_t *node_rev_id,
201 svn_fs_path_change_kind_t change_kind,
204 svn_fs_path_change2_t *change;
206 change = apr_pcalloc(pool, sizeof(*change));
207 change->node_rev_id = node_rev_id;
208 change->change_kind = change_kind;
209 change->mergeinfo_mod = svn_tristate_unknown;
210 change->copyfrom_rev = SVN_INVALID_REVNUM;
216 svn_fs__append_to_merged_froms(svn_mergeinfo_t *output,
217 svn_mergeinfo_t input,
218 const char *rel_path,
221 apr_hash_index_t *hi;
223 *output = apr_hash_make(pool);
224 for (hi = apr_hash_first(pool, input); hi; hi = apr_hash_next(hi))
226 const char *path = apr_hash_this_key(hi);
227 svn_rangelist_t *rangelist = apr_hash_this_val(hi);
229 svn_hash_sets(*output,
230 svn_fspath__join(path, rel_path, pool),
231 svn_rangelist_dup(rangelist, pool));
237 /* Set the version info in *VERSION to COMPAT_MAJOR and COMPAT_MINOR, if
238 the current value refers to a newer version than that.
241 add_compatility(svn_version_t *version,
245 if ( version->major > compat_major
246 || (version->major == compat_major && version->minor > compat_minor))
248 version->major = compat_major;
249 version->minor = compat_minor;
254 svn_fs__compatible_version(svn_version_t **compatible_version,
258 svn_version_t *version;
259 const char *compatible;
261 /* set compatible version according to generic option.
262 Make sure, we are always compatible to the current SVN version
264 compatible = svn_hash_gets(config, SVN_FS_CONFIG_COMPATIBLE_VERSION);
267 SVN_ERR(svn_version__parse_version_string(&version,
269 add_compatility(version,
270 svn_subr_version()->major,
271 svn_subr_version()->minor);
275 version = apr_pmemdup(pool, svn_subr_version(), sizeof(*version));
278 /* specific options take precedence.
279 Let the lowest version compatibility requirement win */
280 if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE))
281 add_compatility(version, 1, 3);
282 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE))
283 add_compatility(version, 1, 4);
284 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_6_COMPATIBLE))
285 add_compatility(version, 1, 5);
286 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_8_COMPATIBLE))
287 add_compatility(version, 1, 7);
289 /* we ignored the patch level and tag so far.
290 * Give them a defined value. */
295 *compatible_version = version;
300 svn_fs__prop_lists_equal(apr_hash_t *a,
304 apr_hash_index_t *hi;
306 /* Quick checks and special cases. */
311 return apr_hash_count(b) == 0;
313 return apr_hash_count(a) == 0;
315 if (apr_hash_count(a) != apr_hash_count(b))
318 /* Compare prop by prop. */
319 for (hi = apr_hash_first(pool, a); hi; hi = apr_hash_next(hi))
323 svn_string_t *val_a, *val_b;
325 apr_hash_this(hi, (const void **)&key, &klen, (void **)&val_a);
326 val_b = apr_hash_get(b, key, klen);
328 if (!val_b || !svn_string_compare(val_a, val_b))
332 /* No difference found. */