2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
29 #include "got_error.h"
31 #include "got_lib_path.h"
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 got_path_is_absolute(const char *path)
40 return path[0] == '/';
44 got_path_get_absolute(const char *relpath)
49 if (getcwd(cwd, sizeof(cwd)) == NULL)
52 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
59 got_path_normalize(const char *path)
63 resolved = realpath(path, NULL);
67 if (!got_path_is_absolute(resolved)) {
68 char *abspath = got_path_get_absolute(resolved);
76 /* based on canonpath() from kern_pledge.c */
77 const struct got_error *
78 got_canonpath(const char *input, char *buf, size_t bufsize)
83 /* can't canon relative paths, don't bother */
84 if (!got_path_is_absolute(input)) {
85 if (strlcpy(buf, input, bufsize) >= bufsize)
86 return got_error(GOT_ERR_NO_SPACE);
92 while (*p && (q - buf < bufsize)) {
93 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
96 } else if (p[0] == '/' && p[1] == '.' &&
97 (p[2] == '/' || p[2] == '\0')) {
100 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
101 (p[3] == '/' || p[3] == '\0')) {
103 if (q != buf) /* "/../" at start of buf */
111 if ((*p == '\0') && (q - buf < bufsize)) {
115 return got_error(GOT_ERR_NO_SPACE);
118 const struct got_error *
119 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
122 const struct got_error *err = NULL;
123 size_t len_parent, len, bufsize;
127 len_parent = strlen(parent_abspath);
128 len = strlen(abspath);
129 if (len_parent >= len)
130 return got_error(GOT_ERR_BAD_PATH);
131 if (strncmp(parent_abspath, abspath, len_parent) != 0)
132 return got_error(GOT_ERR_BAD_PATH);
133 if (abspath[len_parent] != '/')
134 return got_error(GOT_ERR_BAD_PATH);
135 bufsize = len - len_parent + 1;
136 *child = malloc(bufsize);
138 return got_error_from_errno();
139 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
140 err = got_error_from_errno();
149 got_path_is_root_dir(const char *path)
151 return (path[0] == '/' && path[1] == '\0');
155 got_path_is_child(const char *child, const char *parent, size_t parent_len)
160 if (strncmp(parent, child, parent_len) != 0)
162 if (child[parent_len] != '/')
169 got_path_cmp(const char *path1, const char *path2)
171 size_t len1 = strlen(path1);
172 size_t len2 = strlen(path2);
173 size_t min_len = MIN(len1, len2);
176 /* Leading directory separators are insignificant. */
177 while (path1[0] == '/')
179 while (path2[0] == '/')
182 len1 = strlen(path1);
183 len2 = strlen(path2);
184 min_len = MIN(len1, len2);
186 /* Skip over common prefix. */
187 while (i < min_len && path1[i] == path2[i])
190 /* Are the paths exactly equal (besides path separators)? */
191 if (len1 == len2 && i >= min_len)
194 /* Skip over redundant trailing path seperators. */
195 while (path1[i] == '/' && path1[i + 1] == '/')
197 while (path2[i] == '/' && path2[i + 1] == '/')
200 /* Trailing path separators are insignificant. */
201 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
203 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
206 /* Order children in subdirectories directly after their parents. */
207 if (path1[i] == '/' && path2[i] == '\0')
209 if (path2[i] == '/' && path1[i] == '\0')
211 if (path1[i] == '/' && path2[i] != '\0')
213 if (path2[i] == '/' && path1[i] != '\0')
216 /* Next character following the common prefix determines order. */
217 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
220 const struct got_error *
221 got_pathlist_insert(struct got_pathlist_entry **inserted,
222 struct got_pathlist_head *pathlist, const char *path, void *data)
224 struct got_pathlist_entry *new, *pe;
229 new = malloc(sizeof(*new));
231 return got_error_from_errno();
236 * Many callers will provide paths in a somewhat sorted order while
237 * constructing a path list from inputs such as tree objects or
238 * dirents. Iterating backwards from the tail of the list should
239 * be more efficient than traversing through the entire list each
240 * time an element is inserted.
242 pe = TAILQ_LAST(pathlist, got_pathlist_head);
244 int cmp = got_path_cmp(pe->path, path);
246 free(new); /* duplicate */
248 } else if (cmp < 0) {
249 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
254 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
257 TAILQ_INSERT_HEAD(pathlist, new, entry);
264 got_pathlist_free(struct got_pathlist_head *pathlist)
266 struct got_pathlist_entry *pe;
268 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
269 TAILQ_REMOVE(pathlist, pe, entry);
274 static const struct got_error *
275 make_parent_dirs(const char *abspath)
277 const struct got_error *err = NULL;
280 err = got_path_dirname(&parent, abspath);
284 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
285 if (errno == ENOENT) {
286 err = make_parent_dirs(parent);
289 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
290 err = got_error_from_errno();
294 err = got_error_from_errno();
301 const struct got_error *
302 got_path_mkdir(const char *abspath)
304 const struct got_error *err = NULL;
306 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
307 if (errno == ENOENT) {
308 err = make_parent_dirs(abspath);
311 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
312 err = got_error_from_errno();
314 err = got_error_from_errno();
321 const struct got_error *
322 got_path_dirname(char **parent, const char *path)
328 return got_error_from_errno();
330 if (p[0] == '.' && p[1] == '\0')
331 return got_error(GOT_ERR_BAD_PATH);
335 return got_error_from_errno();