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 <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <libgen.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "got_error.h"
31 #include "got_lib_path.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 int
38 got_path_is_absolute(const char *path)
39 {
40 return path[0] == '/';
41 }
43 char *
44 got_path_get_absolute(const char *relpath)
45 {
46 char cwd[PATH_MAX];
47 char *abspath;
49 if (getcwd(cwd, sizeof(cwd)) == NULL)
50 return NULL;
52 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
53 return NULL;
55 return abspath;
56 }
58 char *
59 got_path_normalize(const char *path)
60 {
61 char *resolved;
63 resolved = realpath(path, NULL);
64 if (resolved == NULL)
65 return NULL;
67 if (!got_path_is_absolute(resolved)) {
68 char *abspath = got_path_get_absolute(resolved);
69 free(resolved);
70 resolved = abspath;
71 }
73 return resolved;
74 }
76 /* based on canonpath() from kern_pledge.c */
77 const struct got_error *
78 got_canonpath(const char *input, char *buf, size_t bufsize)
79 {
80 const char *p;
81 char *q;
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);
87 return NULL;
88 }
90 p = input;
91 q = buf;
92 while (*p && (q - buf < bufsize)) {
93 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
94 p += 1;
96 } else if (p[0] == '/' && p[1] == '.' &&
97 (p[2] == '/' || p[2] == '\0')) {
98 p += 2;
100 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
101 (p[3] == '/' || p[3] == '\0')) {
102 p += 3;
103 if (q != buf) /* "/../" at start of buf */
104 while (*--q != '/')
105 continue;
107 } else {
108 *q++ = *p++;
111 if ((*p == '\0') && (q - buf < bufsize)) {
112 *q = 0;
113 return NULL;
114 } else
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,
120 const char *abspath)
122 const struct got_error *err = NULL;
123 size_t len_parent, len, bufsize;
125 len_parent = strlen(parent_abspath);
126 len = strlen(abspath);
127 if (len_parent >= len)
128 return got_error(GOT_ERR_BAD_PATH);
129 if (strncmp(parent_abspath, abspath, len_parent) != 0)
130 return got_error(GOT_ERR_BAD_PATH);
131 if (abspath[len_parent] != '/')
132 return got_error(GOT_ERR_BAD_PATH);
133 bufsize = len - len_parent + 1;
134 *child = malloc(bufsize);
135 if (*child == NULL)
136 return got_error_from_errno();
137 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
138 err = got_error_from_errno();
139 free(*child);
140 *child = NULL;
141 return err;
143 return NULL;
146 int
147 got_path_is_root_dir(const char *path)
149 return (path[0] == '/' && path[1] == '\0');
152 int
153 got_path_is_child(const char *child, const char *parent, size_t parent_len)
155 if (parent_len == 0)
156 return 1;
158 if (strncmp(parent, child, parent_len) != 0)
159 return 0;
160 if (child[parent_len] != '/')
161 return 0;
163 return 1;
166 int
167 got_path_cmp(const char *path1, const char *path2)
169 size_t len1 = strlen(path1);
170 size_t len2 = strlen(path2);
171 size_t min_len = MIN(len1, len2);
172 size_t i = 0;
174 /* Leading directory separators are insignificant. */
175 while (path1[0] == '/')
176 path1++;
177 while (path2[0] == '/')
178 path2++;
180 len1 = strlen(path1);
181 len2 = strlen(path2);
182 min_len = MIN(len1, len2);
184 /* Skip over common prefix. */
185 while (i < min_len && path1[i] == path2[i])
186 i++;
188 /* Are the paths exactly equal (besides path separators)? */
189 if (len1 == len2 && i >= min_len)
190 return 0;
192 /* Skip over redundant trailing path seperators. */
193 while (path1[i] == '/' && path1[i + 1] == '/')
194 path1++;
195 while (path2[i] == '/' && path2[i + 1] == '/')
196 path2++;
198 /* Trailing path separators are insignificant. */
199 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
200 return 0;
201 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
202 return 0;
204 /* Order children in subdirectories directly after their parents. */
205 if (path1[i] == '/' && path2[i] == '\0')
206 return 1;
207 if (path2[i] == '/' && path1[i] == '\0')
208 return -1;
209 if (path1[i] == '/' && path2[i] != '\0')
210 return -1;
211 if (path2[i] == '/' && path1[i] != '\0')
212 return 1;
214 /* Next character following the common prefix determines order. */
215 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
218 const struct got_error *
219 got_pathlist_insert(struct got_pathlist_entry **inserted,
220 struct got_pathlist_head *pathlist, const char *path, void *data)
222 struct got_pathlist_entry *new, *pe;
224 if (inserted)
225 *inserted = NULL;
227 new = malloc(sizeof(*new));
228 if (new == NULL)
229 return got_error_from_errno();
230 new->path = path;
231 new->data = data;
233 /*
234 * Many callers will provide paths in a somewhat sorted order while
235 * constructing a path list from inputs such as tree objects or
236 * dirents. Iterating backwards from the tail of the list should
237 * be more efficient than traversing through the entire list each
238 * time an element is inserted.
239 */
240 pe = TAILQ_LAST(pathlist, got_pathlist_head);
241 while (pe) {
242 int cmp = got_path_cmp(pe->path, path);
243 if (cmp == 0) {
244 free(new); /* duplicate */
245 return NULL;
246 } else if (cmp < 0) {
247 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
248 if (inserted)
249 *inserted = new;
250 return NULL;
252 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
255 TAILQ_INSERT_HEAD(pathlist, new, entry);
256 if (inserted)
257 *inserted = new;
258 return NULL;
261 void
262 got_pathlist_free(struct got_pathlist_head *pathlist)
264 struct got_pathlist_entry *pe;
266 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
267 TAILQ_REMOVE(pathlist, pe, entry);
268 free(pe);
272 static const struct got_error *
273 make_parent_dirs(const char *abspath)
275 const struct got_error *err = NULL;
276 char *parent;
278 err = got_path_dirname(&parent, abspath);
279 if (err)
280 return err;
282 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
283 if (errno == ENOENT) {
284 err = make_parent_dirs(parent);
285 if (err)
286 goto done;
287 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
288 err = got_error_from_errno();
289 goto done;
291 } else
292 err = got_error_from_errno();
294 done:
295 free(parent);
296 return err;
299 const struct got_error *
300 got_path_mkdir(const char *abspath)
302 const struct got_error *err = NULL;
304 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
305 if (errno == ENOENT) {
306 err = make_parent_dirs(abspath);
307 if (err)
308 goto done;
309 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
310 err = got_error_from_errno();
311 } else
312 err = got_error_from_errno();
315 done:
316 return err;
319 const struct got_error *
320 got_path_dirname(char **parent, const char *path)
322 char *p;
324 p = dirname(path);
325 if (p == NULL)
326 return got_error_from_errno();
328 if (p[0] == '.' && p[1] == '\0')
329 return got_error(GOT_ERR_BAD_PATH);
331 *parent = strdup(p);
332 if (*parent == NULL)
333 return got_error_from_errno();
335 return NULL;