Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "got_error.h"
26 #include "got_lib_path.h"
28 #ifndef MIN
29 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
30 #endif
32 int
33 got_path_is_absolute(const char *path)
34 {
35 return path[0] == '/';
36 }
38 char *
39 got_path_get_absolute(const char *relpath)
40 {
41 char cwd[PATH_MAX];
42 char *abspath;
44 if (getcwd(cwd, sizeof(cwd)) == NULL)
45 return NULL;
47 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
48 return NULL;
50 return abspath;
51 }
53 char *
54 got_path_normalize(const char *path)
55 {
56 char *resolved;
58 resolved = realpath(path, NULL);
59 if (resolved == NULL)
60 return NULL;
62 if (!got_path_is_absolute(resolved)) {
63 char *abspath = got_path_get_absolute(resolved);
64 free(resolved);
65 resolved = abspath;
66 }
68 return resolved;
69 }
71 /* based on canonpath() from kern_pledge.c */
72 const struct got_error *
73 got_canonpath(const char *input, char *buf, size_t bufsize)
74 {
75 const char *p;
76 char *q;
78 /* can't canon relative paths, don't bother */
79 if (!got_path_is_absolute(input)) {
80 if (strlcpy(buf, input, bufsize) >= bufsize)
81 return got_error(GOT_ERR_NO_SPACE);
82 return NULL;
83 }
85 p = input;
86 q = buf;
87 while (*p && (q - buf < bufsize)) {
88 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
89 p += 1;
91 } else if (p[0] == '/' && p[1] == '.' &&
92 (p[2] == '/' || p[2] == '\0')) {
93 p += 2;
95 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
96 (p[3] == '/' || p[3] == '\0')) {
97 p += 3;
98 if (q != buf) /* "/../" at start of buf */
99 while (*--q != '/')
100 continue;
102 } else {
103 *q++ = *p++;
106 if ((*p == '\0') && (q - buf < bufsize)) {
107 *q = 0;
108 return NULL;
109 } else
110 return got_error(GOT_ERR_NO_SPACE);
113 const struct got_error *
114 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
115 const char *abspath)
117 const struct got_error *err = NULL;
118 size_t len_parent, len, bufsize;
120 len_parent = strlen(parent_abspath);
121 len = strlen(abspath);
122 if (len_parent >= len)
123 return got_error(GOT_ERR_BAD_PATH);
124 if (strncmp(parent_abspath, abspath, len_parent) != 0)
125 return got_error(GOT_ERR_BAD_PATH);
126 if (abspath[len_parent] != '/')
127 return got_error(GOT_ERR_BAD_PATH);
128 bufsize = len - len_parent + 1;
129 *child = malloc(bufsize);
130 if (*child == NULL)
131 return got_error_from_errno();
132 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
133 err = got_error_from_errno();
134 free(*child);
135 *child = NULL;
136 return err;
138 return NULL;
141 int
142 got_path_is_root_dir(const char *path)
144 return (path[0] == '/' && path[1] == '\0');
147 int
148 got_path_is_child(const char *child, const char *parent, size_t parent_len)
150 if (parent_len == 0)
151 return 1;
153 if (strncmp(parent, child, parent_len) != 0)
154 return 0;
155 if (child[parent_len] != '/')
156 return 0;
158 return 1;
161 int
162 got_path_cmp(const char *path1, const char *path2)
164 size_t len1 = strlen(path1);
165 size_t len2 = strlen(path2);
166 size_t min_len = MIN(len1, len2);
167 size_t i = 0;
169 /* Skip over common prefix. */
170 while (i < min_len && path1[i] == path2[i])
171 i++;
173 /* Are the paths exactly equal? */
174 if (len1 == len2 && i >= min_len)
175 return 0;
177 /* Order children in subdirectories directly after their parents. */
178 if (path1[i] == '/' && path2[i] == '\0')
179 return 1;
180 if (path2[i] == '/' && path1[i] == '\0')
181 return -1;
182 if (path1[i] == '/')
183 return -1;
184 if (path2[i] == '/')
185 return 1;
187 /* Next character following the common prefix determines order. */
188 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;