00001 /** 00002 * @copyright 00003 * ==================================================================== 00004 * Copyright (c) 2000-2004 CollabNet. All rights reserved. 00005 * 00006 * This software is licensed as described in the file COPYING, which 00007 * you should have received as part of this distribution. The terms 00008 * are also available at http://subversion.tigris.org/license-1.html. 00009 * If newer versions of this license are posted there, you may use a 00010 * newer version instead, at your option. 00011 * 00012 * This software consists of voluntary contributions made by many 00013 * individuals. For exact contribution history, see the revision 00014 * history and logs, available at http://subversion.tigris.org/. 00015 * ==================================================================== 00016 * @endcopyright 00017 * 00018 * @file svn_path.h 00019 * @brief A path manipulation library 00020 * 00021 * All incoming and outgoing paths are non-null and in UTF-8, unless 00022 * otherwise documented. 00023 * 00024 * No result path ever ends with a separator, no matter whether the 00025 * path is a file or directory, because we always canonicalize() it. 00026 * 00027 * All paths passed to the @c svn_path_xxx functions, with the exceptions of 00028 * the @c svn_path_canonicalize and @c svn_path_internal_style functions, must 00029 * be in canonical form. 00030 * 00031 * todo: this library really needs a test suite! 00032 */ 00033 00034 #ifndef SVN_PATH_H 00035 #define SVN_PATH_H 00036 00037 00038 #include <apr_pools.h> 00039 #include <apr_tables.h> 00040 00041 #include "svn_string.h" 00042 #include "svn_error.h" 00043 00044 00045 #ifdef __cplusplus 00046 extern "C" { 00047 #endif /* __cplusplus */ 00048 00049 00050 00051 /** Convert @a path from the local style to the canonical internal style. */ 00052 const char *svn_path_internal_style (const char *path, apr_pool_t *pool); 00053 00054 /** Convert @a path from the canonical internal style to the local style. */ 00055 const char *svn_path_local_style (const char *path, apr_pool_t *pool); 00056 00057 00058 /** Join a base path (@a base) with a component (@a component), allocated in 00059 * @a pool. 00060 * 00061 * If either @a base or @a component is the empty path, then the other 00062 * argument will be copied and returned. If both are the empty path the 00063 * empty path is returned. 00064 * 00065 * If the @a component is an absolute path, then it is copied and returned. 00066 * Exactly one slash character ('/') is used to joined the components, 00067 * accounting for any trailing slash in @a base. 00068 * 00069 * Note that the contents of @a base are not examined, so it is possible to 00070 * use this function for constructing URLs, or for relative URLs or 00071 * repository paths. 00072 * 00073 * This function is NOT appropriate for native (local) file 00074 * paths. Only for "internal" canonicalized paths, since it uses '/' 00075 * for the separator. Further, an absolute path (for @a component) is 00076 * based on a leading '/' character. Thus, an "absolute URI" for the 00077 * @a component won't be detected. An absolute URI can only be used 00078 * for the base. 00079 */ 00080 char *svn_path_join (const char *base, 00081 const char *component, 00082 apr_pool_t *pool); 00083 00084 /** Join multiple components onto a @a base path, allocated in @a pool. The 00085 * components are terminated by a @c NULL. 00086 * 00087 * If any component is the empty string, it will be ignored. 00088 * 00089 * If any component is an absolute path, then it resets the base and 00090 * further components will be appended to it. 00091 * 00092 * See @c svn_path_join() for further notes about joining paths. 00093 */ 00094 char *svn_path_join_many (apr_pool_t *pool, const char *base, ...); 00095 00096 00097 /** Get the basename of the specified canonicalized @a path. The 00098 * basename is defined as the last component of the path (ignoring any 00099 * trailing slashes). If the @a path is root ("/"), then that is 00100 * returned. Otherwise, the returned value will have no slashes in 00101 * it. 00102 * 00103 * Example: svn_path_basename("/foo/bar") -> "bar" 00104 * 00105 * The returned basename will be allocated in @a pool. 00106 * 00107 * Note: if an empty string is passed, then an empty string will be returned. 00108 */ 00109 char *svn_path_basename (const char *path, apr_pool_t *pool); 00110 00111 /** Get the dirname of the specified canonicalized @a path, defined as 00112 * the path with its basename removed. 00113 * 00114 * Get the dirname of the specified @a path, defined as the path with its 00115 * basename removed. If @a path is root ("/"), it is returned unchanged. 00116 * 00117 * The returned dirname will be allocated in @a pool. 00118 */ 00119 char *svn_path_dirname (const char *path, apr_pool_t *pool); 00120 00121 /** Return the number of components in the canonicalized @a path. */ 00122 apr_size_t 00123 svn_path_component_count (const char *path); 00124 00125 /** Add a @a component (a null-terminated C-string) to the 00126 * canonicalized @a path. @a component is allowed to contain 00127 * directory separators. 00128 * 00129 * If @a path is non-empty, append the appropriate directory separator 00130 * character, and then @a component. If @a path is empty, simply set it to 00131 * @a component; don't add any separator character. 00132 * 00133 * If the result ends in a separator character, then remove the separator. 00134 */ 00135 void svn_path_add_component (svn_stringbuf_t *path, 00136 const char *component); 00137 00138 /** Remove one component off the end of the canonicalized @a path. */ 00139 void svn_path_remove_component (svn_stringbuf_t *path); 00140 00141 /** Remove @a n components off the end of the canonizalized @a path. 00142 * Equivalent to calling @c svn_remove_component @a n times. */ 00143 void svn_path_remove_components (svn_stringbuf_t *path, apr_size_t n); 00144 00145 /** Divide the canonicalized @a path into @a *dirpath and @a 00146 * *base_name, allocated in @a pool. 00147 * 00148 * If @a dirpath or @a base_name is null, then don't set that one. 00149 * 00150 * Either @a dirpath or @a base_name may be @a path's own address, but they 00151 * may not both be the same address, or the results are undefined. 00152 * 00153 * If @a path has two or more components, the separator between @a dirpath 00154 * and @a base_name is not included in either of the new names. 00155 * 00156 * examples: 00157 * - <pre>"/foo/bar/baz" ==> "/foo/bar" and "baz"</pre> 00158 * - <pre>"/bar" ==> "/" and "bar"</pre> 00159 * - <pre>"/" ==> "/" and "/"</pre> 00160 * - <pre>"bar" ==> "" and "bar"</pre> 00161 * - <pre>"" ==> "" and ""</pre> 00162 */ 00163 void svn_path_split (const char *path, 00164 const char **dirpath, 00165 const char **base_name, 00166 apr_pool_t *pool); 00167 00168 00169 /** Return non-zero iff @a path is empty ("") or represents the current 00170 * directory -- that is, if prepending it as a component to an existing 00171 * path would result in no meaningful change. 00172 */ 00173 int svn_path_is_empty (const char *path); 00174 00175 00176 /** Return a new path (or URL) like @a path, but transformed such that 00177 * some types of path specification redundancies are removed. 00178 * 00179 * This involves collapsing redundant "/./" and "/../" elements, 00180 * removing multiple adjacent separator characters, removing trailing 00181 * separator characters, and possibly other semantically inoperative 00182 * transformations. 00183 * 00184 * The returned path may be statically allocated, equal to @a path, or 00185 * allocated from @a pool. 00186 */ 00187 const char *svn_path_canonicalize (const char *path, apr_pool_t *pool); 00188 00189 00190 /** Return an integer greater than, equal to, or less than 0, according 00191 * as @a path1 is greater than, equal to, or less than @a path2. 00192 */ 00193 int svn_path_compare_paths (const char *path1, const char *path2); 00194 00195 00196 /** Return the longest common path shared by two canonicalized paths, 00197 * @a path1 and @a path2. If there's no common ancestor, return the 00198 * empty path. 00199 * 00200 * @a path1 and @a path2 may be URLs. In order for two URLs to have 00201 * a common ancestor, they must (a) have the same protocol (since two URLs 00202 * with the same path but different protocols may point at completely 00203 * different resources), and (b) share a common ancestor in their path 00204 * component, i.e. 'protocol://' is not a sufficient ancestor. 00205 */ 00206 char *svn_path_get_longest_ancestor (const char *path1, 00207 const char *path2, 00208 apr_pool_t *pool); 00209 00210 /** Convert @a relative canonicalized path to an absolute path and 00211 * return the results in @a *pabsolute, allocated in @a pool. 00212 * 00213 * @a relative may be a URL, in which case no attempt is made to convert it, 00214 * and a copy of the URL is returned. 00215 */ 00216 svn_error_t * 00217 svn_path_get_absolute (const char **pabsolute, 00218 const char *relative, 00219 apr_pool_t *pool); 00220 00221 /** Return the path part of the canonicalized @a path in @a 00222 * *pdirectory, and the file part in @a *pfile. If @a path is a 00223 * directory, set @a *pdirectory to @a path, and @a *pfile to the 00224 * empty string. If @a path does not exist it is treated as if it is 00225 * a file, since directories do not normally vanish. 00226 */ 00227 svn_error_t * 00228 svn_path_split_if_file(const char *path, 00229 const char **pdirectory, 00230 const char **pfile, 00231 apr_pool_t *pool); 00232 00233 /** Find the common prefix of the canonicalized paths in @a targets 00234 * (an array of @a const char *'s), and remove redundant paths if @a 00235 * remove_redundancies is true. 00236 * 00237 * - Set @a *pcommon to the absolute path of the path or URL common to 00238 * all of the targets. If the targets have no common prefix, or 00239 * are a mix of URLs and local paths, set @a *pcommon to the 00240 * empty string. 00241 * 00242 * - If @a pcondensed_targets is non-null, set @a *pcondensed_targets 00243 * to an array of targets relative to @a *pcommon, and if 00244 * @a remove_redundancies is true, omit any paths/URLs that are 00245 * descendants of another path/URL in @a targets. If *pcommon 00246 * is empty, @a *pcondensed_targets will contain full URLs and/or 00247 * absolute paths; redundancies can still be removed (from both URLs 00248 * and paths). If @a pcondensed_targets is null, leave it alone. 00249 * 00250 * Else if there is exactly one target, then 00251 * 00252 * - Set @a *pcommon to that target, and 00253 * 00254 * - If @a pcondensed_targets is non-null, set @a *pcondensed_targets 00255 * to an array containing zero elements. Else if 00256 * @a pcondensed_targets is null, leave it alone. 00257 * 00258 * If there are no items in @a targets, set @a *pcommon and (if 00259 * applicable) @a *pcondensed_targets to @c NULL. 00260 * 00261 * NOTE: There is no guarantee that @a *pcommon is within a working 00262 * copy. */ 00263 svn_error_t * 00264 svn_path_condense_targets (const char **pcommon, 00265 apr_array_header_t **pcondensed_targets, 00266 const apr_array_header_t *targets, 00267 svn_boolean_t remove_redundancies, 00268 apr_pool_t *pool); 00269 00270 00271 /** Copy a list of canonicalized @a targets, one at a time, into @a 00272 * pcondensed_targets, omitting any targets that are found earlier in 00273 * the list, or whose ancestor is found earlier in the list. Ordering 00274 * of targets in the original list is preserved in the condensed list 00275 * of targets. Use @a pool for any allocations. 00276 * 00277 * How does this differ in functionality from @c svn_path_condense_targets? 00278 * 00279 * Here's the short version: 00280 * 00281 * 1. Disclaimer: if you wish to debate the following, talk to Karl. :-) 00282 * Order matters for updates because a multi-arg update is not 00283 * atomic, and CVS users are used to, when doing 'cvs up targetA 00284 * targetB' seeing targetA get updated, then targetB. I think the 00285 * idea is that if you're in a time-sensitive or flaky-network 00286 * situation, a user can say, "I really *need* to update 00287 * wc/A/D/G/tau, but I might as well update my whole working copy if 00288 * I can." So that user will do 'svn up wc/A/D/G/tau wc', and if 00289 * something dies in the middles of the 'wc' update, at least the 00290 * user has 'tau' up-to-date. 00291 * 00292 * 2. Also, we have this notion of an anchor and a target for updates 00293 * (the anchor is where the update editor is rooted, the target is 00294 * the actual thing we want to update). I needed a function that 00295 * would NOT screw with my input paths so that I could tell the 00296 * difference between someone being in A/D and saying 'svn up G' and 00297 * being in A/D/G and saying 'svn up .' -- believe it or not, these 00298 * two things don't mean the same thing. @c svn_path_condense_targets 00299 * plays with absolute paths (which is fine, so does 00300 * @c svn_path_remove_redundancies), but the difference is that it 00301 * actually tweaks those targets to be relative to the "grandfather 00302 * path" common to all the targets. Updates don't require a 00303 * "grandfather path" at all, and even if it did, the whole 00304 * conversion to an absolute path drops the crucial difference 00305 * between saying "i'm in foo, update bar" and "i'm in foo/bar, 00306 * update '.'" 00307 */ 00308 svn_error_t * 00309 svn_path_remove_redundancies (apr_array_header_t **pcondensed_targets, 00310 const apr_array_header_t *targets, 00311 apr_pool_t *pool); 00312 00313 00314 /** Decompose the canonicalized @a path into an array of <tt>const 00315 * char *</tt> components, allocated in @a pool. If @a path is 00316 * absolute, the first component will be a lone dir separator (the 00317 * root directory). 00318 */ 00319 apr_array_header_t *svn_path_decompose (const char *path, 00320 apr_pool_t *pool); 00321 00322 00323 /** Test that @a name is a single path component, that is: 00324 * - not @c NULL or empty. 00325 * - not a `/'-separated directory path 00326 * - not empty or `..' 00327 */ 00328 svn_boolean_t svn_path_is_single_path_component (const char *name); 00329 00330 00331 /** 00332 * @since New in 1.1. 00333 * 00334 * Test to see if a backpath, i.e. '..', is present in @a path. 00335 * If not, return @c FALSE. 00336 * If so, return @c TRUE. 00337 */ 00338 svn_boolean_t svn_path_is_backpath_present (const char *path); 00339 00340 00341 /** Test if @a path2 is a child of @a path1. 00342 * If not, return @c NULL. 00343 * If so, return a copy of the remainder path, allocated in @a pool. 00344 * (The remainder is the component which, added to @a path1, yields 00345 * @a path2. The remainder does not begin with a dir separator.) 00346 * 00347 * Both paths must be in canonical form, and must either be absolute, 00348 * or contain no ".." components. 00349 * 00350 * ### todo: the ".." restriction is unfortunate, and would ideally 00351 * be lifted by making the implementation smarter. But this is not 00352 * trivial: if the path is "../foo", how do you know whether or not 00353 * the current directory is named "foo" in its parent? 00354 */ 00355 const char *svn_path_is_child (const char *path1, 00356 const char *path2, 00357 apr_pool_t *pool); 00358 00359 00360 /** URI/URL stuff 00361 * 00362 * @defgroup svn_path_uri_stuff URI/URL stuff 00363 * @{ 00364 */ 00365 00366 /** Return @c TRUE iff @a path looks like a valid URL, @c FALSE otherwise. */ 00367 svn_boolean_t svn_path_is_url (const char *path); 00368 00369 /** Return @c TRUE iff @a path is URI-safe, @c FALSE otherwise. */ 00370 svn_boolean_t svn_path_is_uri_safe (const char *path); 00371 00372 /** Return a URI-encoded copy of @a path, allocated in @a pool. */ 00373 const char *svn_path_uri_encode (const char *path, apr_pool_t *pool); 00374 00375 /** Return a URI-decoded copy of @a path, allocated in @a pool. */ 00376 const char *svn_path_uri_decode (const char *path, apr_pool_t *pool); 00377 00378 /** Extend @a url by a single @a component, URI-encoding that @a component 00379 * before adding it to the @a url. Return the new @a url, allocated in 00380 * @a pool. Notes: if @a component is @c NULL, just return a copy or @a url 00381 * allocated in @a pool; if @a component is already URI-encoded, calling 00382 * code should just use <tt>svn_path_join (url, component, pool)</tt>. @a url 00383 * does not need to be a canonical path, it may have trailing '/'. 00384 */ 00385 const char *svn_path_url_add_component (const char *url, 00386 const char *component, 00387 apr_pool_t *pool); 00388 00389 /** @since New in 1.1. 00390 * Convert @a iri (Internationalized URI) to an URI. 00391 * The return value may be the same as @a iri if it was already 00392 * a URI. Else, allocate the return value in @a pool. */ 00393 const char *svn_path_uri_from_iri (const char *iri, 00394 apr_pool_t *pool); 00395 00396 /** @since New in 1.1. 00397 * URI-encode certain characters in @a uri that are not valid in an URI, but 00398 * doesn't have any special meaning in @a uri at their positions. If no 00399 * characters need escaping, just return @a uri. 00400 * 00401 * NOTE: Currently, this function escapes <, >, ", space, {, }, |, \, ^, and `. 00402 * This may be extended in the future to do context-dependent escaping. 00403 */ 00404 const char *svn_path_uri_autoescape (const char *uri, 00405 apr_pool_t *pool); 00406 00407 /** @} */ 00408 00409 /** Charset conversion stuff 00410 * 00411 * @defgroup svn_path_charset_stuff Charset conversion stuff 00412 * @{ 00413 */ 00414 00415 /** Convert @a path_utf8 from UTF-8 to the internal encoding used by APR. */ 00416 svn_error_t *svn_path_cstring_from_utf8 (const char **path_apr, 00417 const char *path_utf8, 00418 apr_pool_t *pool); 00419 00420 /** Convert @a path_apr from the internal encoding used by APR to UTF-8. */ 00421 svn_error_t *svn_path_cstring_to_utf8 (const char **path_utf8, 00422 const char *path_apr, 00423 apr_pool_t *pool); 00424 00425 00426 /** @} */ 00427 00428 #ifdef __cplusplus 00429 } 00430 #endif /* __cplusplus */ 00431 00432 00433 #endif /* SVN_PATH_H */
1.3.5