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)
364 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
365 return got_error(GOT_ERR_NO_SPACE);
369 return got_error_from_errno2("dirname", path);
371 if (p[0] == '.' && p[1] == '\0')
372 return got_error_path(path, GOT_ERR_BAD_PATH);
376 return got_error_from_errno("strdup");
381 const struct got_error *
382 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
384 const struct got_error *err = NULL;
388 if (dent->d_type != DT_UNKNOWN) {
389 *type = dent->d_type;
396 * This is a fallback to accommodate filesystems which do not
397 * provide directory entry type information. DT_UNKNOWN directory
398 * entries occur on NFS mounts without "readdir plus" RPC.
401 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
402 return got_error_from_errno("asprintf");
404 if (lstat(path_child, &sb) == -1) {
405 err = got_error_from_errno2("lstat", path_child);
409 if (S_ISFIFO(sb.st_mode))
411 else if (S_ISCHR(sb.st_mode))
413 else if (S_ISDIR(sb.st_mode))
415 else if (S_ISBLK(sb.st_mode))
417 else if (S_ISLNK(sb.st_mode))
419 else if (S_ISREG(sb.st_mode))
421 else if (S_ISSOCK(sb.st_mode))
428 const struct got_error *
429 got_path_basename(char **s, const char *path)
434 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
435 return got_error(GOT_ERR_NO_SPACE);
437 base = basename(buf);
439 return got_error_from_errno2("basename", path);
443 return got_error_from_errno("strdup");
449 got_path_strip_trailing_slashes(char *path)
454 while (x-- > 0 && path[x] == '/')
458 /* based on findprog() from usr.sbin/which/which.c */
459 const struct got_error *
460 got_path_find_prog(char **filename, const char *prog)
462 const struct got_error *err = NULL;
466 char *path, *pathcpy;
470 path = getenv("PATH");
472 path = _PATH_DEFPATH;
474 /* Special case if prog contains '/' */
475 if (strchr(prog, '/')) {
476 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
477 access(prog, X_OK) == 0) {
478 *filename = strdup(prog);
479 if (*filename == NULL)
480 return got_error_from_errno("strdup");
485 if ((path = strdup(path)) == NULL)
486 return got_error_from_errno("strdup");
489 while ((p = strsep(&pathcpy, ":")) != NULL) {
494 while (len > 0 && p[len-1] == '/')
495 p[--len] = '\0'; /* strip trailing '/' */
497 if (asprintf(filename, "%s/%s", p, prog) == -1) {
498 err = got_error_from_errno("asprintf");
501 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
502 access(*filename, X_OK) == 0)
512 const struct got_error *
513 got_path_create_file(const char *path, const char *content)
515 const struct got_error *err = NULL;
518 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
519 GOT_DEFAULT_FILE_MODE);
521 err = got_error_from_errno2("open", path);
526 int len = dprintf(fd, "%s\n", content);
527 if (len != strlen(content) + 1) {
528 err = got_error_from_errno("dprintf");
534 if (fd != -1 && close(fd) == -1 && err == NULL)
535 err = got_error_from_errno("close");