2 * Copyright (c) 2018 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.
24 #include "got_error.h"
26 #include "got_lib_path.h"
29 got_path_is_absolute(const char *path)
31 return path[0] == '/';
35 got_path_get_absolute(const char *relpath)
40 if (getcwd(cwd, sizeof(cwd)) == NULL)
43 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
50 got_path_normalize(const char *path)
54 resolved = realpath(path, NULL);
58 if (!got_path_is_absolute(resolved)) {
59 char *abspath = got_path_get_absolute(resolved);
67 /* based on canonpath() from kern_pledge.c */
68 const struct got_error *
69 got_canonpath(const char *input, char *buf, size_t bufsize)
74 /* can't canon relative paths, don't bother */
75 if (!got_path_is_absolute(input)) {
76 if (strlcpy(buf, input, bufsize) >= bufsize)
77 return got_error(GOT_ERR_NO_SPACE);
83 while (*p && (q - buf < bufsize)) {
84 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
87 } else if (p[0] == '/' && p[1] == '.' &&
88 (p[2] == '/' || p[2] == '\0')) {
91 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
92 (p[3] == '/' || p[3] == '\0')) {
94 if (q != buf) /* "/../" at start of buf */
102 if ((*p == '\0') && (q - buf < bufsize)) {
106 return got_error(GOT_ERR_NO_SPACE);
109 const struct got_error *
110 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
113 const struct got_error *err = NULL;
114 size_t len_parent, len, bufsize;
116 len_parent = strlen(parent_abspath);
117 len = strlen(abspath);
118 if (len_parent >= len)
119 return got_error(GOT_ERR_BAD_PATH);
120 if (strncmp(parent_abspath, abspath, len_parent) != 0)
121 return got_error(GOT_ERR_BAD_PATH);
122 if (abspath[len_parent] != '/')
123 return got_error(GOT_ERR_BAD_PATH);
124 bufsize = len - len_parent + 1;
125 *child = malloc(bufsize);
127 return got_error_from_errno();
128 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
129 err = got_error_from_errno();