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(GOT_ERR_BAD_PATH);
116 if (strncmp(parent_abspath, abspath, len_parent) != 0)
117 return got_error(GOT_ERR_BAD_PATH);
118 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
119 return got_error(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)
138 return (path[0] == '/' && path[1] == '\0');
142 got_path_is_current_dir(const char *path)
144 return (path[0] == '.' && path[1] == '\0');
148 got_path_is_child(const char *child, const char *parent, size_t parent_len)
150 if (parent_len == 0 || got_path_is_root_dir(parent))
153 if (strncmp(parent, child, parent_len) != 0)
155 if (child[parent_len] != '/')
162 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
167 /* Leading directory separators are insignificant. */
168 while (path1[0] == '/') {
172 while (path2[0] == '/') {
177 min_len = MIN(len1, len2);
179 /* Skip over common prefix. */
180 while (i < min_len && path1[i] == path2[i])
183 /* Are the paths exactly equal (besides path separators)? */
184 if (len1 == len2 && i >= min_len)
187 /* Skip over redundant trailing path seperators. */
188 while (path1[i] == '/' && path1[i + 1] == '/')
190 while (path2[i] == '/' && path2[i + 1] == '/')
193 /* Trailing path separators are insignificant. */
194 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
196 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
199 /* Order children in subdirectories directly after their parents. */
200 if (path1[i] == '/' && path2[i] == '\0')
202 if (path2[i] == '/' && path1[i] == '\0')
204 if (path1[i] == '/' && path2[i] != '\0')
206 if (path2[i] == '/' && path1[i] != '\0')
209 /* Next character following the common prefix determines order. */
210 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
213 const struct got_error *
214 got_pathlist_insert(struct got_pathlist_entry **inserted,
215 struct got_pathlist_head *pathlist, const char *path, void *data)
217 struct got_pathlist_entry *new, *pe;
222 new = malloc(sizeof(*new));
224 return got_error_from_errno("malloc");
226 new->path_len = strlen(path);
230 * Many callers will provide paths in a somewhat sorted order while
231 * constructing a path list from inputs such as tree objects or
232 * dirents. Iterating backwards from the tail of the list should
233 * be more efficient than traversing through the entire list each
234 * time an element is inserted.
236 pe = TAILQ_LAST(pathlist, got_pathlist_head);
238 int cmp = got_path_cmp(pe->path, new->path,
239 pe->path_len, new->path_len);
241 free(new); /* duplicate */
243 } else if (cmp < 0) {
244 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
249 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
252 TAILQ_INSERT_HEAD(pathlist, new, entry);
258 const struct got_error *
259 got_pathlist_append(struct got_pathlist_head *pathlist,
260 const char *path, void *data)
262 struct got_pathlist_entry *new;
264 new = malloc(sizeof(*new));
266 return got_error_from_errno("malloc");
268 new->path_len = strlen(path);
270 TAILQ_INSERT_TAIL(pathlist, new, entry);
275 got_pathlist_free(struct got_pathlist_head *pathlist)
277 struct got_pathlist_entry *pe;
279 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
280 TAILQ_REMOVE(pathlist, pe, entry);
285 static const struct got_error *
286 make_parent_dirs(const char *abspath)
288 const struct got_error *err = NULL;
291 err = got_path_dirname(&parent, abspath);
295 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
296 if (errno == ENOENT) {
297 err = make_parent_dirs(parent);
300 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
301 err = got_error_from_errno2("mkdir", parent);
305 err = got_error_from_errno2("mkdir", parent);
312 const struct got_error *
313 got_path_mkdir(const char *abspath)
315 const struct got_error *err = NULL;
317 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
318 if (errno == ENOENT) {
319 err = make_parent_dirs(abspath);
322 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
323 err = got_error_from_errno2("mkdir", abspath);
325 err = got_error_from_errno2("mkdir", abspath);
333 got_path_dir_is_empty(const char *dir)
343 while ((dent = readdir(d)) != NULL) {
344 if (strcmp(dent->d_name, ".") == 0 ||
345 strcmp(dent->d_name, "..") == 0)
356 const struct got_error *
357 got_path_dirname(char **parent, const char *path)
363 return got_error_from_errno2("dirname", path);
365 if (p[0] == '.' && p[1] == '\0')
366 return got_error(GOT_ERR_BAD_PATH);
370 return got_error_from_errno("strdup");
375 const struct got_error *
376 got_path_basename(char **s, const char *path)
380 base = basename(path);
382 return got_error_from_errno2("basename", path);
386 return got_error_from_errno("strdup");
392 got_path_strip_trailing_slashes(char *path)
397 while (x-- > 0 && path[x] == '/')
401 /* based on findprog() from usr.sbin/which/which.c */
402 const struct got_error *
403 got_path_find_prog(char **filename, const char *prog)
405 const struct got_error *err = NULL;
409 char *path, *pathcpy;
413 path = getenv("PATH");
415 path = _PATH_DEFPATH;
417 /* Special case if prog contains '/' */
418 if (strchr(prog, '/')) {
419 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
420 access(prog, X_OK) == 0) {
421 *filename = strdup(prog);
422 if (*filename == NULL)
423 return got_error_from_errno("strdup");
428 if ((path = strdup(path)) == NULL)
429 return got_error_from_errno("strdup");
432 while ((p = strsep(&pathcpy, ":")) != NULL) {
437 while (len > 0 && p[len-1] == '/')
438 p[--len] = '\0'; /* strip trailing '/' */
440 if (asprintf(filename, "%s/%s", p, prog) == -1) {
441 err = got_error_from_errno("asprintf");
444 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
445 access(*filename, X_OK) == 0)
455 const struct got_error *
456 got_path_create_file(const char *path, const char *content)
458 const struct got_error *err = NULL;
461 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
462 GOT_DEFAULT_FILE_MODE);
464 err = got_error_from_errno2("open", path);
469 int len = dprintf(fd, "%s\n", content);
470 if (len != strlen(content) + 1) {
471 err = got_error_from_errno("dprintf");
477 if (fd != -1 && close(fd) == -1 && err == NULL)
478 err = got_error_from_errno("close");