Blob


1 /*
2 * Copyright (c) 2018 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 <limits.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "got_error.h"
26 #include "got_lib_path.h"
28 int
29 got_path_is_absolute(const char *path)
30 {
31 return path[0] == '/';
32 }
34 char *
35 got_path_get_absolute(const char *relpath)
36 {
37 char cwd[PATH_MAX];
38 char *abspath;
40 if (getcwd(cwd, sizeof(cwd)) == NULL)
41 return NULL;
43 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
44 return NULL;
46 return abspath;
47 }
49 char *
50 got_path_normalize(const char *path)
51 {
52 char *resolved;
54 resolved = realpath(path, NULL);
55 if (resolved == NULL)
56 return NULL;
58 if (!got_path_is_absolute(resolved)) {
59 char *abspath = got_path_get_absolute(resolved);
60 free(resolved);
61 resolved = abspath;
62 }
64 return resolved;
65 }
67 /* based on canonpath() from kern_pledge.c */
68 const struct got_error *
69 got_canonpath(const char *input, char *buf, size_t bufsize)
70 {
71 const char *p;
72 char *q;
74 /* can't canon relative paths, don't bother */
75 if (!got_path_is_absolute(input)) {
76 if (strlcpy(buf, input, bufsize) >= bufsize)
77 return got_error(GOT_ERR_NO_SPACE);
78 return NULL;
79 }
81 p = input;
82 q = buf;
83 while (*p && (q - buf < bufsize)) {
84 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
85 p += 1;
87 } else if (p[0] == '/' && p[1] == '.' &&
88 (p[2] == '/' || p[2] == '\0')) {
89 p += 2;
91 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
92 (p[3] == '/' || p[3] == '\0')) {
93 p += 3;
94 if (q != buf) /* "/../" at start of buf */
95 while (*--q != '/')
96 continue;
98 } else {
99 *q++ = *p++;
102 if ((*p == '\0') && (q - buf < bufsize)) {
103 *q = 0;
104 return NULL;
105 } else
106 return got_error(GOT_ERR_NO_SPACE);
109 const struct got_error *
110 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
111 const char *abspath)
113 const struct got_error *err = NULL;
114 size_t len_parent, len, bufsize;
116 len_parent = strlen(parent_abspath);
117 len = strlen(abspath);
118 if (len_parent >= len)
119 return got_error(GOT_ERR_BAD_PATH);
120 if (strncmp(parent_abspath, abspath, len_parent) != 0)
121 return got_error(GOT_ERR_BAD_PATH);
122 if (abspath[len_parent] != '/')
123 return got_error(GOT_ERR_BAD_PATH);
124 bufsize = len - len_parent + 1;
125 *child = malloc(bufsize);
126 if (*child == NULL)
127 return got_error_from_errno();
128 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
129 err = got_error_from_errno();
130 free(*child);
131 *child = NULL;
132 return err;
134 return NULL;
137 int
138 got_path_is_root_dir(const char *path)
140 return (path[0] == '/' && path[1] == '\0');