Blob


1 /*
2 * Copyright (c) 2018, 2019 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 <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <libgen.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "got_error.h"
30 #include "got_path.h"
32 #ifndef MIN
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
34 #endif
36 int
37 got_path_is_absolute(const char *path)
38 {
39 return path[0] == '/';
40 }
42 char *
43 got_path_get_absolute(const char *relpath)
44 {
45 char cwd[PATH_MAX];
46 char *abspath;
48 if (getcwd(cwd, sizeof(cwd)) == NULL)
49 return NULL;
51 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
52 return NULL;
54 return abspath;
55 }
57 char *
58 got_path_normalize(const char *path)
59 {
60 char *resolved;
62 resolved = realpath(path, NULL);
63 if (resolved == NULL)
64 return NULL;
66 if (!got_path_is_absolute(resolved)) {
67 char *abspath = got_path_get_absolute(resolved);
68 free(resolved);
69 resolved = abspath;
70 }
72 return resolved;
73 }
75 /* based on canonpath() from kern_pledge.c */
76 const struct got_error *
77 got_canonpath(const char *input, char *buf, size_t bufsize)
78 {
79 const char *p;
80 char *q;
82 /* can't canon relative paths, don't bother */
83 if (!got_path_is_absolute(input)) {
84 if (strlcpy(buf, input, bufsize) >= bufsize)
85 return got_error(GOT_ERR_NO_SPACE);
86 return NULL;
87 }
89 p = input;
90 q = buf;
91 while (*p && (q - buf < bufsize)) {
92 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
93 p += 1;
95 } else if (p[0] == '/' && p[1] == '.' &&
96 (p[2] == '/' || p[2] == '\0')) {
97 p += 2;
99 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
100 (p[3] == '/' || p[3] == '\0')) {
101 p += 3;
102 if (q != buf) /* "/../" at start of buf */
103 while (*--q != '/')
104 continue;
106 } else {
107 *q++ = *p++;
110 if ((*p == '\0') && (q - buf < bufsize)) {
111 *q = 0;
112 return NULL;
113 } else
114 return got_error(GOT_ERR_NO_SPACE);
117 const struct got_error *
118 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
119 const char *abspath)
121 const struct got_error *err = NULL;
122 size_t len_parent, len, bufsize;
124 *child = NULL;
126 len_parent = strlen(parent_abspath);
127 len = strlen(abspath);
128 if (len_parent >= len)
129 return got_error(GOT_ERR_BAD_PATH);
130 if (strncmp(parent_abspath, abspath, len_parent) != 0)
131 return got_error(GOT_ERR_BAD_PATH);
132 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
133 return got_error(GOT_ERR_BAD_PATH);
134 while (abspath[len_parent] == '/')
135 abspath++;
136 bufsize = len - len_parent + 1;
137 *child = malloc(bufsize);
138 if (*child == NULL)
139 return got_error_prefix_errno("malloc");
140 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
141 err = got_error_prefix_errno("strlcpy");
142 free(*child);
143 *child = NULL;
144 return err;
146 return NULL;
149 int
150 got_path_is_root_dir(const char *path)
152 return (path[0] == '/' && path[1] == '\0');
155 int
156 got_path_is_current_dir(const char *path)
158 return (path[0] == '.' && path[1] == '\0');
161 int
162 got_path_is_child(const char *child, const char *parent, size_t parent_len)
164 if (parent_len == 0 || got_path_is_root_dir(parent))
165 return 1;
167 if (strncmp(parent, child, parent_len) != 0)
168 return 0;
169 if (child[parent_len] != '/')
170 return 0;
172 return 1;
175 int
176 got_path_cmp(const char *path1, const char *path2)
178 size_t len1 = strlen(path1);
179 size_t len2 = strlen(path2);
180 size_t min_len = MIN(len1, len2);
181 size_t i = 0;
183 /* Leading directory separators are insignificant. */
184 while (path1[0] == '/')
185 path1++;
186 while (path2[0] == '/')
187 path2++;
189 len1 = strlen(path1);
190 len2 = strlen(path2);
191 min_len = MIN(len1, len2);
193 /* Skip over common prefix. */
194 while (i < min_len && path1[i] == path2[i])
195 i++;
197 /* Are the paths exactly equal (besides path separators)? */
198 if (len1 == len2 && i >= min_len)
199 return 0;
201 /* Skip over redundant trailing path seperators. */
202 while (path1[i] == '/' && path1[i + 1] == '/')
203 path1++;
204 while (path2[i] == '/' && path2[i + 1] == '/')
205 path2++;
207 /* Trailing path separators are insignificant. */
208 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
209 return 0;
210 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
211 return 0;
213 /* Order children in subdirectories directly after their parents. */
214 if (path1[i] == '/' && path2[i] == '\0')
215 return 1;
216 if (path2[i] == '/' && path1[i] == '\0')
217 return -1;
218 if (path1[i] == '/' && path2[i] != '\0')
219 return -1;
220 if (path2[i] == '/' && path1[i] != '\0')
221 return 1;
223 /* Next character following the common prefix determines order. */
224 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
227 const struct got_error *
228 got_pathlist_insert(struct got_pathlist_entry **inserted,
229 struct got_pathlist_head *pathlist, const char *path, void *data)
231 struct got_pathlist_entry *new, *pe;
233 if (inserted)
234 *inserted = NULL;
236 new = malloc(sizeof(*new));
237 if (new == NULL)
238 return got_error_prefix_errno("malloc");
239 new->path = path;
240 new->data = data;
242 /*
243 * Many callers will provide paths in a somewhat sorted order while
244 * constructing a path list from inputs such as tree objects or
245 * dirents. Iterating backwards from the tail of the list should
246 * be more efficient than traversing through the entire list each
247 * time an element is inserted.
248 */
249 pe = TAILQ_LAST(pathlist, got_pathlist_head);
250 while (pe) {
251 int cmp = got_path_cmp(pe->path, path);
252 if (cmp == 0) {
253 free(new); /* duplicate */
254 return NULL;
255 } else if (cmp < 0) {
256 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
257 if (inserted)
258 *inserted = new;
259 return NULL;
261 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
264 TAILQ_INSERT_HEAD(pathlist, new, entry);
265 if (inserted)
266 *inserted = new;
267 return NULL;
270 void
271 got_pathlist_free(struct got_pathlist_head *pathlist)
273 struct got_pathlist_entry *pe;
275 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
276 TAILQ_REMOVE(pathlist, pe, entry);
277 free(pe);
281 static const struct got_error *
282 make_parent_dirs(const char *abspath)
284 const struct got_error *err = NULL;
285 char *parent;
287 err = got_path_dirname(&parent, abspath);
288 if (err)
289 return err;
291 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
292 if (errno == ENOENT) {
293 err = make_parent_dirs(parent);
294 if (err)
295 goto done;
296 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
297 err = got_error_prefix_errno2("mkdir", parent);
298 goto done;
300 } else
301 err = got_error_prefix_errno2("mkdir", parent);
303 done:
304 free(parent);
305 return err;
308 const struct got_error *
309 got_path_mkdir(const char *abspath)
311 const struct got_error *err = NULL;
313 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
314 if (errno == ENOENT) {
315 err = make_parent_dirs(abspath);
316 if (err)
317 goto done;
318 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
319 err = got_error_prefix_errno2("mkdir", abspath);
320 } else
321 err = got_error_prefix_errno2("mkdir", abspath);
324 done:
325 return err;
328 const struct got_error *
329 got_path_dirname(char **parent, const char *path)
331 char *p;
333 p = dirname(path);
334 if (p == NULL)
335 return got_error_prefix_errno2("dirname", path);
337 if (p[0] == '.' && p[1] == '\0')
338 return got_error(GOT_ERR_BAD_PATH);
340 *parent = strdup(p);
341 if (*parent == NULL)
342 return got_error_prefix_errno("strdup");
344 return NULL;
347 void
348 got_path_strip_trailing_slashes(char *path)
350 int x;
352 while (path[x = strlen(path) - 1] == '/')
353 path[x] = '\0';