2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
32 #include "got_error.h"
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 got_path_is_absolute(const char *path)
42 return path[0] == '/';
46 got_path_get_absolute(const char *relpath)
51 if (getcwd(cwd, sizeof(cwd)) == NULL)
54 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
61 got_path_normalize(const char *path)
65 resolved = realpath(path, NULL);
69 if (!got_path_is_absolute(resolved)) {
70 char *abspath = got_path_get_absolute(resolved);
78 /* based on canonpath() from kern_pledge.c */
79 const struct got_error *
80 got_canonpath(const char *input, char *buf, size_t bufsize)
85 /* can't canon relative paths, don't bother */
86 if (!got_path_is_absolute(input)) {
87 if (strlcpy(buf, input, bufsize) >= bufsize)
88 return got_error(GOT_ERR_NO_SPACE);
94 while (*p && (q - buf < bufsize)) {
95 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
98 } else if (p[0] == '/' && p[1] == '.' &&
99 (p[2] == '/' || p[2] == '\0')) {
102 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
103 (p[3] == '/' || p[3] == '\0')) {
105 if (q != buf) /* "/../" at start of buf */
113 if ((*p == '\0') && (q - buf < bufsize)) {
117 return got_error(GOT_ERR_NO_SPACE);
120 const struct got_error *
121 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
124 const struct got_error *err = NULL;
125 size_t len_parent, len, bufsize;
129 len_parent = strlen(parent_abspath);
130 len = strlen(abspath);
131 if (len_parent >= len)
132 return got_error(GOT_ERR_BAD_PATH);
133 if (strncmp(parent_abspath, abspath, len_parent) != 0)
134 return got_error(GOT_ERR_BAD_PATH);
135 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
136 return got_error(GOT_ERR_BAD_PATH);
137 while (abspath[len_parent] == '/')
139 bufsize = len - len_parent + 1;
140 *child = malloc(bufsize);
142 return got_error_from_errno("malloc");
143 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
144 err = got_error_from_errno("strlcpy");
153 got_path_is_root_dir(const char *path)
155 return (path[0] == '/' && path[1] == '\0');
159 got_path_is_current_dir(const char *path)
161 return (path[0] == '.' && path[1] == '\0');
165 got_path_is_child(const char *child, const char *parent, size_t parent_len)
167 if (parent_len == 0 || got_path_is_root_dir(parent))
170 if (strncmp(parent, child, parent_len) != 0)
172 if (child[parent_len] != '/')
179 got_path_cmp(const char *path1, const char *path2)
181 size_t len1 = strlen(path1);
182 size_t len2 = strlen(path2);
183 size_t min_len = MIN(len1, len2);
186 /* Leading directory separators are insignificant. */
187 while (path1[0] == '/')
189 while (path2[0] == '/')
192 len1 = strlen(path1);
193 len2 = strlen(path2);
194 min_len = MIN(len1, len2);
196 /* Skip over common prefix. */
197 while (i < min_len && path1[i] == path2[i])
200 /* Are the paths exactly equal (besides path separators)? */
201 if (len1 == len2 && i >= min_len)
204 /* Skip over redundant trailing path seperators. */
205 while (path1[i] == '/' && path1[i + 1] == '/')
207 while (path2[i] == '/' && path2[i + 1] == '/')
210 /* Trailing path separators are insignificant. */
211 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
213 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
216 /* Order children in subdirectories directly after their parents. */
217 if (path1[i] == '/' && path2[i] == '\0')
219 if (path2[i] == '/' && path1[i] == '\0')
221 if (path1[i] == '/' && path2[i] != '\0')
223 if (path2[i] == '/' && path1[i] != '\0')
226 /* Next character following the common prefix determines order. */
227 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
230 const struct got_error *
231 got_pathlist_insert(struct got_pathlist_entry **inserted,
232 struct got_pathlist_head *pathlist, const char *path, void *data)
234 struct got_pathlist_entry *new, *pe;
239 new = malloc(sizeof(*new));
241 return got_error_from_errno("malloc");
246 * Many callers will provide paths in a somewhat sorted order while
247 * constructing a path list from inputs such as tree objects or
248 * dirents. Iterating backwards from the tail of the list should
249 * be more efficient than traversing through the entire list each
250 * time an element is inserted.
252 pe = TAILQ_LAST(pathlist, got_pathlist_head);
254 int cmp = got_path_cmp(pe->path, path);
256 free(new); /* duplicate */
258 } else if (cmp < 0) {
259 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
264 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
267 TAILQ_INSERT_HEAD(pathlist, new, entry);
274 got_pathlist_free(struct got_pathlist_head *pathlist)
276 struct got_pathlist_entry *pe;
278 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
279 TAILQ_REMOVE(pathlist, pe, entry);
284 static const struct got_error *
285 make_parent_dirs(const char *abspath)
287 const struct got_error *err = NULL;
290 err = got_path_dirname(&parent, abspath);
294 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
295 if (errno == ENOENT) {
296 err = make_parent_dirs(parent);
299 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
300 err = got_error_from_errno2("mkdir", parent);
304 err = got_error_from_errno2("mkdir", parent);
311 const struct got_error *
312 got_path_mkdir(const char *abspath)
314 const struct got_error *err = NULL;
316 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
317 if (errno == ENOENT) {
318 err = make_parent_dirs(abspath);
321 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
322 err = got_error_from_errno2("mkdir", abspath);
324 err = got_error_from_errno2("mkdir", abspath);
332 got_path_dir_is_empty(const char *dir)
342 while ((dent = readdir(d)) != NULL) {
343 if (strcmp(dent->d_name, ".") == 0 ||
344 strcmp(dent->d_name, "..") == 0)
355 const struct got_error *
356 got_path_dirname(char **parent, const char *path)
362 return got_error_from_errno2("dirname", path);
364 if (p[0] == '.' && p[1] == '\0')
365 return got_error(GOT_ERR_BAD_PATH);
369 return got_error_from_errno("strdup");
375 got_path_strip_trailing_slashes(char *path)
379 while (path[x = strlen(path) - 1] == '/')
383 /* based on findprog() from usr.sbin/which/which.c */
384 const struct got_error *
385 got_path_find_prog(char **filename, const char *prog)
390 char *path, *pathcpy;
394 path = getenv("PATH");
396 path = _PATH_DEFPATH;
398 /* Special case if prog contains '/' */
399 if (strchr(prog, '/')) {
400 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
401 access(prog, X_OK) == 0) {
402 *filename = strdup(prog);
403 if (*filename == NULL)
404 return got_error_from_errno("strdup");
409 if ((path = strdup(path)) == NULL)
410 return got_error_from_errno("strdup");
413 while ((p = strsep(&pathcpy, ":")) != NULL) {
418 while (len > 0 && p[len-1] == '/')
419 p[--len] = '\0'; /* strip trailing '/' */
421 if (asprintf(filename, "%s/%s", p, prog) == -1) {
423 return got_error_from_errno("asprintf");
425 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
426 access(*filename, X_OK) == 0) {