| 1 | /* |
| 2 | Authors: |
| 3 | John Dennis <jdennis.redhat.com> |
| 4 | |
| 5 | Copyright (C) 2009 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU Lesser General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #ifndef PATH_UTILS_H |
| 22 | #define PATH_UTILS_H |
| 23 | |
| 24 | /*****************************************************************************/ |
| 25 | /******************************** Documentation ******************************/ |
| 26 | /*****************************************************************************/ |
| 27 | |
| 28 | /** @mainpage Pathname manipulation utilities |
| 29 | * |
| 30 | * This library contains a set of utilities designed to extract info from |
| 31 | * and manipulate path names. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | /*****************************************************************************/ |
| 36 | /******************************* Include Files *******************************/ |
| 37 | /*****************************************************************************/ |
| 38 | |
| 39 | #include <stdbool.h> |
| 40 | #include <libintl.h> |
| 41 | #include <sys/param.h> |
| 42 | #include <sys/stat.h> |
| 43 | |
| 44 | /*****************************************************************************/ |
| 45 | /*********************************** Defines *********************************/ |
| 46 | /*****************************************************************************/ |
| 47 | |
| 48 | /** |
| 49 | * @defgroup constants Constants |
| 50 | * @{ |
| 51 | */ |
| 52 | |
| 53 | #ifndef _ |
| 54 | #define _(String) gettext(String) |
| 55 | #endif |
| 56 | |
| 57 | /** |
| 58 | * @brief SUCCESS (=0) is returned by all functions in the path_utils |
| 59 | * library on success. |
| 60 | */ |
| 61 | #ifndef SUCCESS |
| 62 | #define SUCCESS 0 |
| 63 | #endif |
| 64 | |
| 65 | /** |
| 66 | * @} |
| 67 | */ |
| 68 | |
| 69 | /** |
| 70 | * @defgroup errors Error codes and macros |
| 71 | * @{ |
| 72 | */ |
| 73 | |
| 74 | #define PATH_UTILS_ERROR_BASE -3000 |
| 75 | #define PATH_UTILS_ERROR_LIMIT (PATH_UTILS_ERROR_BASE+20) |
| 76 | |
| 77 | /** |
| 78 | * @brief You can use this macro to check if an error code is one of |
| 79 | * the internal path_utils codes. |
| 80 | */ |
| 81 | #define IS_PATH_UTILS_ERROR(error) (((error) >= PATH_UTILS_ERROR_BASE) && ((error) < PATH_UTILS_ERROR_LIMIT)) |
| 82 | |
| 83 | /** @brief A path cannot be normalized due to too many parent links |
| 84 | * |
| 85 | * Returned when a relative path contains too many parent (\c "..") links. |
| 86 | * Please see the documentation of \c normalize_path() for full explanation. |
| 87 | */ |
| 88 | #define PATH_UTILS_ERROR_NOT_FULLY_NORMALIZED (PATH_UTILS_ERROR_BASE + 1) |
| 89 | |
| 90 | /** |
| 91 | * @} |
| 92 | */ |
| 93 | |
| 94 | /*****************************************************************************/ |
| 95 | /******************************* Type Definitions ****************************/ |
| 96 | /*****************************************************************************/ |
| 97 | |
| 98 | /*****************************************************************************/ |
| 99 | /************************* External Global Variables ***********************/ |
| 100 | /*****************************************************************************/ |
| 101 | |
| 102 | /*****************************************************************************/ |
| 103 | /**************************** Exported Functions ***************************/ |
| 104 | /*****************************************************************************/ |
| 105 | |
| 106 | /** |
| 107 | * @defgroup functions Functions |
| 108 | * @{ |
| 109 | */ |
| 110 | |
| 111 | /** @brief Given an error code return the string description. |
| 112 | * |
| 113 | * @param[in] error The error code |
| 114 | * |
| 115 | * @return Error string. If error code is not recognized \c NULL is returned. |
| 116 | */ |
| 117 | const char *path_utils_error_string(int error); |
| 118 | |
| 119 | /** @brief Get the basename component of a path |
| 120 | * |
| 121 | * Given a path, copy the basename component (in the usual case, the part |
| 122 | * following the final "/") into the buffer \c base_name |
| 123 | * whose length is \c base_name_size. If the path does not contain a slash, |
| 124 | * \c get_basename() returns a copy of path. |
| 125 | * |
| 126 | * @param[out] base_name The basename component |
| 127 | * @param[in] base_name_size The size of the base_name buffer |
| 128 | * @param[in] path The full path to parse |
| 129 | * |
| 130 | * @return \c SUCCESS if successful, non-zero error code otherwise. |
| 131 | * Possible errors: |
| 132 | * \li \c ENOBUFS if the buffer space is too small |
| 133 | * \li \c EINVAL The path was a NULL pointer |
| 134 | */ |
| 135 | int get_basename(char *base_name, size_t base_name_size, const char *path); |
| 136 | |
| 137 | /** @brief Copy the directory components of a path |
| 138 | * |
| 139 | * Given a path, copy the directory components (in the usual case, the path |
| 140 | * up to, but not including the final "/") into the buffer \c dir_path whose |
| 141 | * length is \c dir_path_size. If the path does not contain a slash, |
| 142 | * \c get_dirname() returns the current working directory. |
| 143 | * |
| 144 | * @param[out] dir_path The directory component |
| 145 | * @param[in] dir_path_size The size of the dir_path buffer |
| 146 | * @param[in] path The full path to parse |
| 147 | * |
| 148 | * @return \c SUCCESS if successful, non-zero error code otherwise. |
| 149 | * Possible errors: |
| 150 | * \li \c ENOBUFS If the buffer space is too small |
| 151 | * \li \c EACCES Permission to read or search a component of the filename was denied. |
| 152 | * \li \c ENAMETOOLONG The size of the null-terminated pathname exceeds PATH_MAX bytes. |
| 153 | * \li \c ENOENT The current working directory has been unlinked. |
| 154 | * \li \c EINVAL The path was a NULL pointer |
| 155 | */ |
| 156 | int get_dirname(char *dir_path, size_t dir_path_size, const char *path); |
| 157 | |
| 158 | /** @brief Get the basaname and directory components of a path |
| 159 | * |
| 160 | * Given a path, copy the directory components into the buffer \c dir_path whose |
| 161 | * length is \c dir_path_size and copy the basename component into the buffer |
| 162 | * \c base_name whose length is \c base_name_size. |
| 163 | * |
| 164 | * @param[out] base_name The basename component |
| 165 | * @param[in] base_name_size The size of the base_name buffer |
| 166 | * @param[out] dir_path The directory component |
| 167 | * @param[in] dir_path_size The size of the dir_path buffer |
| 168 | * @param[in] path The full path to parse |
| 169 | * |
| 170 | * @return \c SUCCESS if successful, non-zero error code otherwise. |
| 171 | * Possible errors: |
| 172 | * \li \c ENOBUFS If the buffer space is too small |
| 173 | * \li \c EACCES Permission to read or search a component of the filename was denied. |
| 174 | * \li \c ENAMETOOLONG The size of the null-terminated pathname exceeds PATH_MAX bytes. |
| 175 | * \li \c ENOENT The current working directory has been unlinked. |
| 176 | * \li \c EINVAL The path was a NULL pointer |
| 177 | */ |
| 178 | int get_directory_and_base_name(char *dir_path, size_t dir_path_size, |
| 179 | char *base_name, size_t base_name_size, |
| 180 | const char *path); |
| 181 | |
| 182 | /** @brief Tell if path is absolute or relative |
| 183 | * |
| 184 | * @param[in] path The path to check |
| 185 | * |
| 186 | * @return \c true if the path is absolute, \c false otherwise. |
| 187 | */ |
| 188 | bool is_absolute_path(const char *path); |
| 189 | |
| 190 | /** @brief Concatenate two components of a path |
| 191 | * |
| 192 | * Given two paths, \c head & \c tail, copy their concatenation into the |
| 193 | * buffer \c path whose length is \c path_size. |
| 194 | * |
| 195 | * @param[out] path The full path |
| 196 | * @param[in] path_size The size of the path buffer |
| 197 | * @param[in] head The first component of the path |
| 198 | * @param[in] tail The second component of the path |
| 199 | * |
| 200 | * @return \c SUCCESS if successful, non-zero error code otherwise. |
| 201 | * \li \c ENOBUFS If the buffer space is too small. In this case, |
| 202 | * path will be set to an empty string. |
| 203 | */ |
| 204 | int path_concat(char *path, size_t path_size, const char *head, const char *tail); |
| 205 | |
| 206 | /** @brief Convert a path into absolute |
| 207 | * |
| 208 | * Given a path make it absolute storing the absolute path in into the buffer |
| 209 | * \c absolute_path whose length is \c absolute_path_size. |
| 210 | * |
| 211 | * Returns \c SUCCESS if successful, non-zero error code otherwise. Possible errors: |
| 212 | * \li \c ENOBUFS If the buffer space is too small |
| 213 | * \li \c ENOMEM If user memory cannot be mapped |
| 214 | * \li \c ENOENT If directory does not exist (i.e. it has been deleted) |
| 215 | * \li \c EFAULT If memory access violation occurs while copying |
| 216 | * \li \c EINVAL The path was a NULL pointer |
| 217 | */ |
| 218 | int make_path_absolute(char *absolute_path, size_t absolute_path_size, const char *path); |
| 219 | |
| 220 | /** @brief Split a file system path into individual components. |
| 221 | * |
| 222 | * Split a file system path into individual components. Return a pointer to an |
| 223 | * array of char pointers, each array entry is a pointer to a copy of the |
| 224 | * component. As a special case if the path begins with / then the first |
| 225 | * component is "/" so the caller can identify the pah as absolute with the |
| 226 | * first component being the root. The last entry in the array is \c NULL serving |
| 227 | * as a termination sentinel. An optional integer count parameter can be |
| 228 | * provided, which if non-NULL will have the number of components written into |
| 229 | * it. Thus the caller can iterate on the array until it sees a \c NULL pointer or |
| 230 | * iterate count times indexing the array. |
| 231 | * |
| 232 | * The caller is responsible for calling \c free() on the returned array. This |
| 233 | * frees both the array of component pointers and the copies of each component |
| 234 | * in one operation because the copy of each component is stored in the same |
| 235 | * allocation block. |
| 236 | * |
| 237 | * The original path parameter is not modified. |
| 238 | * |
| 239 | * In the event of an error \c NULL is returned and count (if specified) will be -1. |
| 240 | * |
| 241 | * Examples: |
| 242 | * |
| 243 | * \code |
| 244 | * char **components, **component; |
| 245 | * int i; |
| 246 | * |
| 247 | * components = split_path(path, NULL); |
| 248 | * for (component = components; *component; component++) |
| 249 | * printf("\"%s\" ", *component); |
| 250 | * free(components); |
| 251 | * \endcode |
| 252 | * |
| 253 | * -OR- |
| 254 | * |
| 255 | * \code |
| 256 | * components = split_path(path, &count); |
| 257 | * for (i = 0; i < count; i++) |
| 258 | * printf("\"%s\" ", components[i]); |
| 259 | * free(components); |
| 260 | * \endcode |
| 261 | * |
| 262 | * @param[in] path The original path |
| 263 | * @param[out] count The number of components the path was split into |
| 264 | * |
| 265 | * @return An array of char pointers, each array entry is a pointer to a |
| 266 | * copy of the component or NULL on error. |
| 267 | */ |
| 268 | char **split_path(const char *path, int *count); |
| 269 | |
| 270 | /** @brief Normalizes a path |
| 271 | * |
| 272 | * Normalizes a path copying the resulting normalized path into the buffer |
| 273 | * \c normalized_path whose length is \c normalized_size. |
| 274 | * |
| 275 | * A path is normalized when: |
| 276 | * \li only 1 slash separates all path components |
| 277 | * \li there are no \c . path components (except if \c . is the only component) |
| 278 | * \li there are no \c .. path components |
| 279 | * |
| 280 | * The input path may either be an absolute path or a path fragment. |
| 281 | * |
| 282 | * As a special case if the input path is \c NULL, the empty string \c "", |
| 283 | * or \c "." the returned normalized path will be \c ".". |
| 284 | * |
| 285 | * \c ".." path components point to the parent directory which effectively |
| 286 | * means poping the parent off the path. But what happens when there are |
| 287 | * more \c ".." path components than ancestors in the path? The answer depends |
| 288 | * on whether the path is an absolute path or a path fragment. If the path is |
| 289 | * absolute then the extra \c ".." components which would move above the root |
| 290 | * (/) are simply ignored. This effectively limits the path to the root. |
| 291 | * However if the path is not absolute, rather it is a path fragment, and |
| 292 | * there are more \c ".." components than ancestors which can be "popped off" |
| 293 | * then as many \c ".." components will be popped off the fragement as |
| 294 | * possible without changing the meaning of the path fragment. In this case |
| 295 | * some extra \c ".." components will be left in the path and the function |
| 296 | * will return the error \c ERROR_COULD_NOT_NORMALIZE_PATH_FULLY. However the |
| 297 | * function will still normalize as much of the path fragment as is possible. |
| 298 | * The behavior of \c ".." components when the input path is a fragment is |
| 299 | * adopted because after normalizing a path fragment then the normalized path |
| 300 | * fragment if made absolute should reference the same file system name as if |
| 301 | * the unnormalized fragment were made absolute. Note this also means |
| 302 | * \c ERROR_COULD_NOT_NORMALIZE_PATH_FULLY will never be returned if the input |
| 303 | * path is absolute. |
| 304 | * |
| 305 | * @returns \c SUCCESS if successful, non-zero error code otherwise. Possible |
| 306 | * errors: |
| 307 | * \li \c ENOBUFS If the buffer space is too small |
| 308 | * \li \c ERROR_COULD_NOT_NORMALIZE_PATH_FULLY If not all \c ".." path components could be removed |
| 309 | */ |
| 310 | int normalize_path(char *normalized_path, size_t normalized_path_size, const char *path); |
| 311 | |
| 312 | /** @brief Find the common prefix between two paths |
| 313 | * |
| 314 | * Finds the common prefix between two paths, returns the common prefix and |
| 315 | * optionally the count of how many path components were common between the |
| 316 | * two paths (if \c common_count is non-NULL). Please note that for absolute |
| 317 | * paths, the \c "/" root prefix is treated as a common components, so the |
| 318 | * paths \c "/usr/lib" and \c "/usr/share" would have two common components - |
| 319 | * \c "/" and \c "/usr". |
| 320 | * |
| 321 | * Contrary to some other implementations, \c common_path_prefix() works on |
| 322 | * path components, not characters, which guarantees at least some level of |
| 323 | * sanity of the returned prefixes (for example, the common prefix of |
| 324 | * \c "/usr/share" and \c "/usr/src" would be \c "/usr") |
| 325 | * |
| 326 | * @returns \c SUCCESS if successful, non-zero error code otherwise. |
| 327 | * Possible errors: |
| 328 | * \li \c ENOBUFS if the buffer space is too small |
| 329 | */ |
| 330 | int common_path_prefix(char *common_path, |
| 331 | size_t common_path_size, |
| 332 | int *common_count, |
| 333 | const char *path1, const char *path2); |
| 334 | |
| 335 | |
| 336 | /** @brief Make the input path absolute if it's not already, then normalize it. |
| 337 | * |
| 338 | * @returns \c SUCCESS if successful, non-zero error code otherwise. |
| 339 | * Possible errors: |
| 340 | * \li \c ENOBUFS if the buffer space is too small |
| 341 | */ |
| 342 | int make_normalized_absolute_path(char *result_path, size_t result_path_size, const char *path); |
| 343 | |
| 344 | /** |
| 345 | * Find the first path component which is an existing directory by walking from |
| 346 | * the tail of the path to it's head, return the path of the existing directory. |
| 347 | * |
| 348 | * If the pathname is relative and does not contain a directory, the current |
| 349 | * directory is returned as parent. |
| 350 | * |
| 351 | * @returns \c SUCCESS if successful, non-zero error code otherwise. |
| 352 | * Possible errors: |
| 353 | * \li \c ENOBUFS if the buffer space is too small |
| 354 | * \li \c EACCES Search permission is denied for one of the directories. |
| 355 | * \li \c ELOOP Too many symbolic links encountered while traversing the path. |
| 356 | * \li \c ENAMETOOLONG File name too long. |
| 357 | * \li \c ENOMEM Out of memory (i.e., kernel memory). |
| 358 | */ |
| 359 | int find_existing_directory_ancestor(char *ancestor, size_t ancestor_size, const char *path); |
| 360 | |
| 361 | /** @brief callback for the \c directory_list() function |
| 362 | * |
| 363 | * Please see the description of \c directory_list() to see more |
| 364 | * details about this callback |
| 365 | * |
| 366 | * @param[in] directory Directory name of the visited path |
| 367 | * @param[in] base_name Base name of the visited path |
| 368 | * @param[in] path Full name of the visited path |
| 369 | * @param[in] info Info about the visited directory |
| 370 | * @param[in] user_data Callback data passed by caller |
| 371 | * |
| 372 | * @returns if \c false, do not recursively descend into the directory, |
| 373 | * descend if \c true |
| 374 | */ |
| 375 | typedef bool (*directory_list_callback_t)(const char *directory, const char *base_name, |
| 376 | const char *path, struct stat *info, |
| 377 | void *user_data); |
| 378 | /** @brief Walk a directory. |
| 379 | * |
| 380 | * Walk a directory. If \c recursive is \c true child directories will be |
| 381 | * descended into. The supplied callback is invoked for each entry in the |
| 382 | * directory. |
| 383 | * |
| 384 | * The callback is provided with the directory name, basename, the full |
| 385 | * pathname (i.e. directory name + basename) a stat structure for the path |
| 386 | * item and a pointer to any user supplied data specified in the \c user_data |
| 387 | * parameter. If the callback returns \c false for a directory the recursive |
| 388 | * descent into that directory does not occur thus effectively "pruning" |
| 389 | * the tree. |
| 390 | * |
| 391 | * @param[in] path The path to examine |
| 392 | * @param[in] recursive Whether to recursively examine entries in the directory |
| 393 | * @param[in] callback The callback to invoke for each entry |
| 394 | * @param[in] user_data The data to pass into the callback |
| 395 | * |
| 396 | * @returns SUCCESS if successfull, an error code if not. |
| 397 | */ |
| 398 | int directory_list(const char *path, bool recursive, |
| 399 | directory_list_callback_t callback, void *user_data); |
| 400 | |
| 401 | /** @brief Tell if one path is ancestor of another |
| 402 | * |
| 403 | * Test to see if the path passed in the \c ancestor parameter is an ancestor |
| 404 | * of the path passed in the path parameter returning true if it is, \c false |
| 405 | * otherwise. |
| 406 | * |
| 407 | * The test is "static" as such it is performed on the string components in |
| 408 | * each path. Live symbolic links in the file system are not taken into |
| 409 | * consideration. The test operates by splitting each path into it's individual |
| 410 | * components and then comparing each component pairwise for string |
| 411 | * equality. Both paths mush share a common root component for the test to be |
| 412 | * meaningful (e.g. don't attempt to compare an absolute path with a relative |
| 413 | * path). |
| 414 | * |
| 415 | * Example: |
| 416 | * \code |
| 417 | * is_ancestor_path("/a/b/c" "/a/b/c/d") => true |
| 418 | * is_ancestor_path("/a/b/c/d" "/a/b/c/d") => false // equal, not ancestor |
| 419 | * is_ancestor_path("/a/x/c" "/a/b/c/d") => false |
| 420 | * \endcode |
| 421 | * |
| 422 | * @returns \c true if \c ancestor is an ancestor of \c path |
| 423 | */ |
| 424 | bool is_ancestor_path(const char *ancestor, const char *path); |
| 425 | |
| 426 | /** |
| 427 | * @} |
| 428 | */ |
| 429 | |
| 430 | #endif /* PATH_UTILS_H */ |