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;
215 svn_fs_path_change3_t *
216 svn_fs__path_change_create_internal2(svn_fs_path_change_kind_t change_kind,
217 apr_pool_t *result_pool)
219 svn_fs_path_change3_t *change;
221 change = apr_pcalloc(result_pool, sizeof(*change));
222 change->path.data = "";
223 change->change_kind = change_kind;
224 change->mergeinfo_mod = svn_tristate_unknown;
225 change->copyfrom_rev = SVN_INVALID_REVNUM;
231 svn_fs__append_to_merged_froms(svn_mergeinfo_t *output,
232 svn_mergeinfo_t input,
233 const char *rel_path,
236 apr_hash_index_t *hi;
238 *output = apr_hash_make(pool);
239 for (hi = apr_hash_first(pool, input); hi; hi = apr_hash_next(hi))
241 const char *path = apr_hash_this_key(hi);
242 svn_rangelist_t *rangelist = apr_hash_this_val(hi);
244 svn_hash_sets(*output,
245 svn_fspath__join(path, rel_path, pool),
246 svn_rangelist_dup(rangelist, pool));
252 /* Set the version info in *VERSION to COMPAT_MAJOR and COMPAT_MINOR, if
253 the current value refers to a newer version than that.
256 add_compatility(svn_version_t *version,
260 if ( version->major > compat_major
261 || (version->major == compat_major && version->minor > compat_minor))
263 version->major = compat_major;
264 version->minor = compat_minor;
269 svn_fs__compatible_version(svn_version_t **compatible_version,
273 svn_version_t *version;
274 const char *compatible;
276 /* set compatible version according to generic option.
277 Make sure, we are always compatible to the current SVN version
279 compatible = svn_hash_gets(config, SVN_FS_CONFIG_COMPATIBLE_VERSION);
282 SVN_ERR(svn_version__parse_version_string(&version,
284 add_compatility(version,
285 svn_subr_version()->major,
286 svn_subr_version()->minor);
290 version = apr_pmemdup(pool, svn_subr_version(), sizeof(*version));
293 /* specific options take precedence.
294 Let the lowest version compatibility requirement win */
295 if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE))
296 add_compatility(version, 1, 3);
297 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE))
298 add_compatility(version, 1, 4);
299 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_6_COMPATIBLE))
300 add_compatility(version, 1, 5);
301 else if (svn_hash_gets(config, SVN_FS_CONFIG_PRE_1_8_COMPATIBLE))
302 add_compatility(version, 1, 7);
304 /* we ignored the patch level and tag so far.
305 * Give them a defined value. */
310 *compatible_version = version;
315 svn_fs__prop_lists_equal(apr_hash_t *a,
319 apr_hash_index_t *hi;
321 /* Quick checks and special cases. */
326 return apr_hash_count(b) == 0;
328 return apr_hash_count(a) == 0;
330 if (apr_hash_count(a) != apr_hash_count(b))
333 /* Compare prop by prop. */
334 for (hi = apr_hash_first(pool, a); hi; hi = apr_hash_next(hi))
338 svn_string_t *val_a, *val_b;
340 apr_hash_this(hi, (const void **)&key, &klen, (void **)&val_a);
341 val_b = apr_hash_get(b, key, klen);
343 if (!val_b || !svn_string_compare(val_a, val_b))
347 /* No difference found. */