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] == '/';
46 /* based on canonpath() from kern_pledge.c */
47 const struct got_error *
48 got_canonpath(const char *input, char *buf, size_t bufsize)
53 /* can't canon relative paths, don't bother */
54 if (!got_path_is_absolute(input)) {
55 if (strlcpy(buf, input, bufsize) >= bufsize)
56 return got_error(GOT_ERR_NO_SPACE);
62 while (*p && (q - buf < bufsize)) {
63 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
66 } else if (p[0] == '/' && p[1] == '.' &&
67 (p[2] == '/' || p[2] == '\0')) {
70 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
71 (p[3] == '/' || p[3] == '\0')) {
73 if (q != buf) /* "/../" at start of buf */
81 if ((*p == '\0') && (q - buf < bufsize)) {
85 return got_error(GOT_ERR_NO_SPACE);
88 const struct got_error *
89 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
92 const struct got_error *err = NULL;
93 size_t len_parent, len, bufsize;
97 len_parent = strlen(parent_abspath);
98 len = strlen(abspath);
99 if (len_parent >= len)
100 return got_error_path(abspath, GOT_ERR_BAD_PATH);
101 if (strncmp(parent_abspath, abspath, len_parent) != 0)
102 return got_error_path(abspath, GOT_ERR_BAD_PATH);
103 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
104 return got_error_path(abspath, GOT_ERR_BAD_PATH);
105 while (abspath[len_parent] == '/')
107 bufsize = len - len_parent + 1;
108 *child = malloc(bufsize);
110 return got_error_from_errno("malloc");
111 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
112 err = got_error_from_errno("strlcpy");
121 got_path_is_root_dir(const char *path)
125 return (*path == '\0');
129 got_path_is_current_dir(const char *path)
131 return (path[0] == '.' && path[1] == '\0');
135 got_path_is_child(const char *child, const char *parent, size_t parent_len)
137 if (parent_len == 0 || got_path_is_root_dir(parent))
140 if (strncmp(parent, child, parent_len) != 0)
142 if (child[parent_len] != '/')
149 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
154 /* Leading directory separators are insignificant. */
155 while (path1[0] == '/') {
159 while (path2[0] == '/') {
164 min_len = MIN(len1, len2);
166 /* Skip over common prefix. */
167 while (i < min_len && path1[i] == path2[i])
170 /* Are the paths exactly equal (besides path separators)? */
171 if (len1 == len2 && i >= min_len)
174 /* Skip over redundant trailing path seperators. */
175 while (path1[i] == '/' && path1[i + 1] == '/')
177 while (path2[i] == '/' && path2[i + 1] == '/')
180 /* Trailing path separators are insignificant. */
181 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
183 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
186 /* Order children in subdirectories directly after their parents. */
187 if (path1[i] == '/' && path2[i] == '\0')
189 if (path2[i] == '/' && path1[i] == '\0')
191 if (path1[i] == '/' && path2[i] != '\0')
193 if (path2[i] == '/' && path1[i] != '\0')
196 /* Next character following the common prefix determines order. */
197 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
200 const struct got_error *
201 got_pathlist_insert(struct got_pathlist_entry **inserted,
202 struct got_pathlist_head *pathlist, const char *path, void *data)
204 struct got_pathlist_entry *new, *pe;
209 new = malloc(sizeof(*new));
211 return got_error_from_errno("malloc");
213 new->path_len = strlen(path);
217 * Many callers will provide paths in a somewhat sorted order while
218 * constructing a path list from inputs such as tree objects or
219 * dirents. Iterating backwards from the tail of the list should
220 * be more efficient than traversing through the entire list each
221 * time an element is inserted.
223 pe = TAILQ_LAST(pathlist, got_pathlist_head);
225 int cmp = got_path_cmp(pe->path, new->path,
226 pe->path_len, new->path_len);
228 free(new); /* duplicate */
230 } else if (cmp < 0) {
231 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
236 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
239 TAILQ_INSERT_HEAD(pathlist, new, entry);
245 const struct got_error *
246 got_pathlist_append(struct got_pathlist_head *pathlist,
247 const char *path, void *data)
249 struct got_pathlist_entry *new;
251 new = malloc(sizeof(*new));
253 return got_error_from_errno("malloc");
255 new->path_len = strlen(path);
257 TAILQ_INSERT_TAIL(pathlist, new, entry);
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);
272 static const struct got_error *
273 make_parent_dirs(const char *abspath)
275 const struct got_error *err = NULL;
278 err = got_path_dirname(&parent, abspath);
282 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
283 if (errno == ENOENT) {
284 err = make_parent_dirs(parent);
287 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
288 err = got_error_from_errno2("mkdir", parent);
292 err = got_error_from_errno2("mkdir", parent);
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);
309 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
310 err = got_error_from_errno2("mkdir", abspath);
312 err = got_error_from_errno2("mkdir", abspath);
320 got_path_dir_is_empty(const char *dir)
330 while ((dent = readdir(d)) != NULL) {
331 if (strcmp(dent->d_name, ".") == 0 ||
332 strcmp(dent->d_name, "..") == 0)
343 const struct got_error *
344 got_path_dirname(char **parent, const char *path)
349 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
350 return got_error(GOT_ERR_NO_SPACE);
354 return got_error_from_errno2("dirname", path);
356 if (p[0] == '.' && p[1] == '\0')
357 return got_error_path(path, GOT_ERR_BAD_PATH);
361 return got_error_from_errno("strdup");
366 const struct got_error *
367 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
369 const struct got_error *err = NULL;
373 if (dent->d_type != DT_UNKNOWN) {
374 *type = dent->d_type;
381 * This is a fallback to accommodate filesystems which do not
382 * provide directory entry type information. DT_UNKNOWN directory
383 * entries occur on NFS mounts without "readdir plus" RPC.
386 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
387 return got_error_from_errno("asprintf");
389 if (lstat(path_child, &sb) == -1) {
390 err = got_error_from_errno2("lstat", path_child);
394 if (S_ISFIFO(sb.st_mode))
396 else if (S_ISCHR(sb.st_mode))
398 else if (S_ISDIR(sb.st_mode))
400 else if (S_ISBLK(sb.st_mode))
402 else if (S_ISLNK(sb.st_mode))
404 else if (S_ISREG(sb.st_mode))
406 else if (S_ISSOCK(sb.st_mode))
413 const struct got_error *
414 got_path_basename(char **s, const char *path)
419 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
420 return got_error(GOT_ERR_NO_SPACE);
422 base = basename(buf);
424 return got_error_from_errno2("basename", path);
428 return got_error_from_errno("strdup");
434 got_path_strip_trailing_slashes(char *path)
439 while (x-- > 0 && path[x] == '/')
443 /* based on findprog() from usr.sbin/which/which.c */
444 const struct got_error *
445 got_path_find_prog(char **filename, const char *prog)
447 const struct got_error *err = NULL;
451 char *path, *pathcpy;
455 path = getenv("PATH");
457 path = _PATH_DEFPATH;
459 /* Special case if prog contains '/' */
460 if (strchr(prog, '/')) {
461 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
462 access(prog, X_OK) == 0) {
463 *filename = strdup(prog);
464 if (*filename == NULL)
465 return got_error_from_errno("strdup");
470 if ((path = strdup(path)) == NULL)
471 return got_error_from_errno("strdup");
474 while ((p = strsep(&pathcpy, ":")) != NULL) {
479 while (len > 0 && p[len-1] == '/')
480 p[--len] = '\0'; /* strip trailing '/' */
482 if (asprintf(filename, "%s/%s", p, prog) == -1) {
483 err = got_error_from_errno("asprintf");
486 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
487 access(*filename, X_OK) == 0)
497 const struct got_error *
498 got_path_create_file(const char *path, const char *content)
500 const struct got_error *err = NULL;
503 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
504 GOT_DEFAULT_FILE_MODE);
506 err = got_error_from_errno2("open", path);
511 int len = dprintf(fd, "%s\n", content);
512 if (len != strlen(content) + 1) {
513 err = got_error_from_errno("dprintf");
519 if (fd != -1 && close(fd) == -1 && err == NULL)
520 err = got_error_from_errno("close");