2 324d37e7 2019-05-11 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 324d37e7 2019-05-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 324d37e7 2019-05-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 324d37e7 2019-05-11 stsp * copyright notice and this permission notice appear in all copies.
8 324d37e7 2019-05-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 324d37e7 2019-05-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 324d37e7 2019-05-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 324d37e7 2019-05-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 324d37e7 2019-05-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 324d37e7 2019-05-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 324d37e7 2019-05-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 324d37e7 2019-05-11 stsp /* Utilities for dealing with filesystem paths. */
19 324d37e7 2019-05-11 stsp #define GOT_DEFAULT_FILE_MODE (S_IRUSR|S_IWUSR | S_IRGRP | S_IROTH)
20 324d37e7 2019-05-11 stsp #define GOT_DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH)
22 324d37e7 2019-05-11 stsp /* Determine whether a path is an absolute path. */
23 324d37e7 2019-05-11 stsp int got_path_is_absolute(const char *);
26 324d37e7 2019-05-11 stsp * Return an absolute version of a relative path.
27 324d37e7 2019-05-11 stsp * The result is allocated with malloc(3).
29 324d37e7 2019-05-11 stsp char *got_path_get_absolute(const char *);
32 324d37e7 2019-05-11 stsp * Normalize a path for internal processing.
33 324d37e7 2019-05-11 stsp * The result is allocated with malloc(3).
35 324d37e7 2019-05-11 stsp char *got_path_normalize(const char *);
38 324d37e7 2019-05-11 stsp * Canonicalize absolute paths by removing redundant path separators
39 324d37e7 2019-05-11 stsp * and resolving references to parent directories ("/../").
40 324d37e7 2019-05-11 stsp * Relative paths are copied from input to buf as-is.
42 324d37e7 2019-05-11 stsp const struct got_error *got_canonpath(const char *, char *, size_t);
45 324d37e7 2019-05-11 stsp * Get child part of two absolute paths. The second path must equal the first
46 324d37e7 2019-05-11 stsp * path up to some path component, and must be longer than the first path.
47 324d37e7 2019-05-11 stsp * The result is allocated with malloc(3).
49 324d37e7 2019-05-11 stsp const struct got_error *got_path_skip_common_ancestor(char **, const char *,
50 324d37e7 2019-05-11 stsp const char *);
52 324d37e7 2019-05-11 stsp /* Determine whether a path points to the root directory "/" . */
53 324d37e7 2019-05-11 stsp int got_path_is_root_dir(const char *);
55 324d37e7 2019-05-11 stsp /* Determine whether a path is a path-wise child of another path. */
56 324d37e7 2019-05-11 stsp int got_path_is_child(const char *, const char *, size_t);
59 324d37e7 2019-05-11 stsp * Like strcmp() but orders children in subdirectories directly after
60 324d37e7 2019-05-11 stsp * their parents.
62 324d37e7 2019-05-11 stsp int got_path_cmp(const char *, const char *);
65 324d37e7 2019-05-11 stsp * Path lists allow for predictable concurrent iteration over multiple lists
66 324d37e7 2019-05-11 stsp * of paths obtained from disparate sources which don't all provide the same
67 324d37e7 2019-05-11 stsp * ordering guarantees (e.g. git trees, file index, and on-disk directories).
69 324d37e7 2019-05-11 stsp struct got_pathlist_entry {
70 324d37e7 2019-05-11 stsp TAILQ_ENTRY(got_pathlist_entry) entry;
71 324d37e7 2019-05-11 stsp const char *path;
72 324d37e7 2019-05-11 stsp void *data; /* data pointer provided to got_pathlist_insert() */
74 324d37e7 2019-05-11 stsp TAILQ_HEAD(got_pathlist_head, got_pathlist_entry);
77 324d37e7 2019-05-11 stsp * Insert a path into the list of paths in a predictable order.
78 324d37e7 2019-05-11 stsp * The caller should already have initialized the list head. This list stores
79 324d37e7 2019-05-11 stsp * the pointer to the path as-is, i.e. the path is not copied internally and
80 324d37e7 2019-05-11 stsp * must remain available until the list is freed with got_pathlist_free().
81 324d37e7 2019-05-11 stsp * If the first argument is not NULL, set it to a pointer to the newly inserted
82 324d37e7 2019-05-11 stsp * element, or to a NULL pointer in case the path was already on the list.
84 324d37e7 2019-05-11 stsp const struct got_error *got_pathlist_insert(struct got_pathlist_entry **,
85 324d37e7 2019-05-11 stsp struct got_pathlist_head *, const char *, void *);
87 324d37e7 2019-05-11 stsp /* Free resources allocated for a path list. */
88 324d37e7 2019-05-11 stsp void got_pathlist_free(struct got_pathlist_head *);
90 324d37e7 2019-05-11 stsp /* Attempt to create a directory at a given path. */
91 324d37e7 2019-05-11 stsp const struct got_error *got_path_mkdir(const char *);
93 3c45a30a 2019-05-12 jcs /* Determine whether a directory has no files or directories in it. */
94 280f921b 2019-05-12 stsp int got_path_dir_is_empty(const char *);
96 324d37e7 2019-05-11 stsp /* dirname(3) with error handling and dynamically allocated result. */
97 324d37e7 2019-05-11 stsp const struct got_error *got_path_dirname(char **, const char *);
99 72151b04 2019-05-11 stsp /* Strip trailing slashes from a path; path will be modified in-place. */
100 72151b04 2019-05-11 stsp void got_path_strip_trailing_slashes(char *);
102 0ee7065d 2019-05-13 stsp /* Look up the absolute path of a program in $PATH */
103 0ee7065d 2019-05-13 stsp const struct got_error *got_path_find_prog(char **, const char *);