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>
33 #include "got_error.h"
37 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 got_path_is_absolute(const char *path)
43 return path[0] == '/';
47 got_path_get_absolute(const char *relpath)
52 if (getcwd(cwd, sizeof(cwd)) == NULL)
55 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
61 /* based on canonpath() from kern_pledge.c */
62 const struct got_error *
63 got_canonpath(const char *input, char *buf, size_t bufsize)
68 /* can't canon relative paths, don't bother */
69 if (!got_path_is_absolute(input)) {
70 if (strlcpy(buf, input, bufsize) >= bufsize)
71 return got_error(GOT_ERR_NO_SPACE);
77 while (*p && (q - buf < bufsize)) {
78 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
81 } else if (p[0] == '/' && p[1] == '.' &&
82 (p[2] == '/' || p[2] == '\0')) {
85 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
86 (p[3] == '/' || p[3] == '\0')) {
88 if (q != buf) /* "/../" at start of buf */
96 if ((*p == '\0') && (q - buf < bufsize)) {
100 return got_error(GOT_ERR_NO_SPACE);
103 const struct got_error *
104 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
107 const struct got_error *err = NULL;
108 size_t len_parent, len, bufsize;
112 len_parent = strlen(parent_abspath);
113 len = strlen(abspath);
114 if (len_parent >= len)
115 return got_error_path(abspath, GOT_ERR_BAD_PATH);
116 if (strncmp(parent_abspath, abspath, len_parent) != 0)
117 return got_error_path(abspath, GOT_ERR_BAD_PATH);
118 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
119 return got_error_path(abspath, GOT_ERR_BAD_PATH);
120 while (abspath[len_parent] == '/')
122 bufsize = len - len_parent + 1;
123 *child = malloc(bufsize);
125 return got_error_from_errno("malloc");
126 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
127 err = got_error_from_errno("strlcpy");
136 got_path_is_root_dir(const char *path)
140 return (*path == '\0');
144 got_path_is_current_dir(const char *path)
146 return (path[0] == '.' && path[1] == '\0');
150 got_path_is_child(const char *child, const char *parent, size_t parent_len)
152 if (parent_len == 0 || got_path_is_root_dir(parent))
155 if (strncmp(parent, child, parent_len) != 0)
157 if (child[parent_len] != '/')
164 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
169 /* Leading directory separators are insignificant. */
170 while (path1[0] == '/') {
174 while (path2[0] == '/') {
179 min_len = MIN(len1, len2);
181 /* Skip over common prefix. */
182 while (i < min_len && path1[i] == path2[i])
185 /* Are the paths exactly equal (besides path separators)? */
186 if (len1 == len2 && i >= min_len)
189 /* Skip over redundant trailing path seperators. */
190 while (path1[i] == '/' && path1[i + 1] == '/')
192 while (path2[i] == '/' && path2[i + 1] == '/')
195 /* Trailing path separators are insignificant. */
196 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
198 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
201 /* Order children in subdirectories directly after their parents. */
202 if (path1[i] == '/' && path2[i] == '\0')
204 if (path2[i] == '/' && path1[i] == '\0')
206 if (path1[i] == '/' && path2[i] != '\0')
208 if (path2[i] == '/' && path1[i] != '\0')
211 /* Next character following the common prefix determines order. */
212 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
215 const struct got_error *
216 got_pathlist_insert(struct got_pathlist_entry **inserted,
217 struct got_pathlist_head *pathlist, const char *path, void *data)
219 struct got_pathlist_entry *new, *pe;
224 new = malloc(sizeof(*new));
226 return got_error_from_errno("malloc");
228 new->path_len = strlen(path);
232 * Many callers will provide paths in a somewhat sorted order while
233 * constructing a path list from inputs such as tree objects or
234 * dirents. Iterating backwards from the tail of the list should
235 * be more efficient than traversing through the entire list each
236 * time an element is inserted.
238 pe = TAILQ_LAST(pathlist, got_pathlist_head);
240 int cmp = got_path_cmp(pe->path, new->path,
241 pe->path_len, new->path_len);
243 free(new); /* duplicate */
245 } else if (cmp < 0) {
246 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
251 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
254 TAILQ_INSERT_HEAD(pathlist, new, entry);
260 const struct got_error *
261 got_pathlist_append(struct got_pathlist_head *pathlist,
262 const char *path, void *data)
264 struct got_pathlist_entry *new;
266 new = malloc(sizeof(*new));
268 return got_error_from_errno("malloc");
270 new->path_len = strlen(path);
272 TAILQ_INSERT_TAIL(pathlist, new, entry);
277 got_pathlist_free(struct got_pathlist_head *pathlist)
279 struct got_pathlist_entry *pe;
281 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
282 TAILQ_REMOVE(pathlist, pe, entry);
287 static const struct got_error *
288 make_parent_dirs(const char *abspath)
290 const struct got_error *err = NULL;
293 err = got_path_dirname(&parent, abspath);
297 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
298 if (errno == ENOENT) {
299 err = make_parent_dirs(parent);
302 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
303 err = got_error_from_errno2("mkdir", parent);
307 err = got_error_from_errno2("mkdir", parent);
314 const struct got_error *
315 got_path_mkdir(const char *abspath)
317 const struct got_error *err = NULL;
319 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
320 if (errno == ENOENT) {
321 err = make_parent_dirs(abspath);
324 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
325 err = got_error_from_errno2("mkdir", abspath);
327 err = got_error_from_errno2("mkdir", abspath);
335 got_path_dir_is_empty(const char *dir)
345 while ((dent = readdir(d)) != NULL) {
346 if (strcmp(dent->d_name, ".") == 0 ||
347 strcmp(dent->d_name, "..") == 0)
358 const struct got_error *
359 got_path_dirname(char **parent, const char *path)
365 return got_error_from_errno2("dirname", path);
367 if (p[0] == '.' && p[1] == '\0')
368 return got_error_path(path, GOT_ERR_BAD_PATH);
372 return got_error_from_errno("strdup");
377 const struct got_error *
378 got_path_basename(char **s, const char *path)
382 base = basename(path);
384 return got_error_from_errno2("basename", path);
388 return got_error_from_errno("strdup");
394 got_path_strip_trailing_slashes(char *path)
399 while (x-- > 0 && path[x] == '/')
403 /* based on findprog() from usr.sbin/which/which.c */
404 const struct got_error *
405 got_path_find_prog(char **filename, const char *prog)
407 const struct got_error *err = NULL;
411 char *path, *pathcpy;
415 path = getenv("PATH");
417 path = _PATH_DEFPATH;
419 /* Special case if prog contains '/' */
420 if (strchr(prog, '/')) {
421 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
422 access(prog, X_OK) == 0) {
423 *filename = strdup(prog);
424 if (*filename == NULL)
425 return got_error_from_errno("strdup");
430 if ((path = strdup(path)) == NULL)
431 return got_error_from_errno("strdup");
434 while ((p = strsep(&pathcpy, ":")) != NULL) {
439 while (len > 0 && p[len-1] == '/')
440 p[--len] = '\0'; /* strip trailing '/' */
442 if (asprintf(filename, "%s/%s", p, prog) == -1) {
443 err = got_error_from_errno("asprintf");
446 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
447 access(*filename, X_OK) == 0)
457 const struct got_error *
458 got_path_create_file(const char *path, const char *content)
460 const struct got_error *err = NULL;
463 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
464 GOT_DEFAULT_FILE_MODE);
466 err = got_error_from_errno2("open", path);
471 int len = dprintf(fd, "%s\n", content);
472 if (len != strlen(content) + 1) {
473 err = got_error_from_errno("dprintf");
479 if (fd != -1 && close(fd) == -1 && err == NULL)
480 err = got_error_from_errno("close");