2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 e6eac3b8 2018-06-17 stsp * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
5 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
9 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 e08cc72d 2019-02-05 stsp #include <sys/queue.h>
19 0cd1c46a 2019-03-11 stsp #include <sys/stat.h>
21 0cd1c46a 2019-03-11 stsp #include <errno.h>
22 4027f31a 2017-11-04 stsp #include <limits.h>
23 0cd1c46a 2019-03-11 stsp #include <libgen.h>
24 4027f31a 2017-11-04 stsp #include <stdlib.h>
25 4027f31a 2017-11-04 stsp #include <unistd.h>
26 4027f31a 2017-11-04 stsp #include <stdio.h>
27 4027f31a 2017-11-04 stsp #include <string.h>
29 9d31a1d8 2018-03-11 stsp #include "got_error.h"
31 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
34 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
40 4027f31a 2017-11-04 stsp return path[0] == '/';
44 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
46 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
47 4027f31a 2017-11-04 stsp char *abspath;
49 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
50 4027f31a 2017-11-04 stsp return NULL;
52 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
53 4027f31a 2017-11-04 stsp return NULL;
55 4027f31a 2017-11-04 stsp return abspath;
59 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
61 4027f31a 2017-11-04 stsp char *resolved;
63 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
64 4027f31a 2017-11-04 stsp if (resolved == NULL)
65 4027f31a 2017-11-04 stsp return NULL;
67 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
68 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
69 4027f31a 2017-11-04 stsp free(resolved);
70 4027f31a 2017-11-04 stsp resolved = abspath;
73 4027f31a 2017-11-04 stsp return resolved;
76 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
77 f7d20e89 2018-06-17 stsp const struct got_error *
78 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
80 e6eac3b8 2018-06-17 stsp const char *p;
83 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
84 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
85 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
86 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
87 f7d20e89 2018-06-17 stsp return NULL;
92 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
93 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
96 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
97 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
100 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
101 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
103 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
104 e6eac3b8 2018-06-17 stsp while (*--q != '/')
108 e6eac3b8 2018-06-17 stsp *q++ = *p++;
111 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
113 f7d20e89 2018-06-17 stsp return NULL;
115 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
118 04ca23f4 2018-07-16 stsp const struct got_error *
119 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
120 04ca23f4 2018-07-16 stsp const char *abspath)
122 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
123 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
125 db5ba8ed 2019-03-26 stsp *child = NULL;
127 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
128 04ca23f4 2018-07-16 stsp len = strlen(abspath);
129 04ca23f4 2018-07-16 stsp if (len_parent >= len)
130 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
131 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
132 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
133 04ca23f4 2018-07-16 stsp if (abspath[len_parent] != '/')
134 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
135 5e3ce57a 2019-03-26 stsp while (abspath[len_parent] == '/')
137 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
138 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
139 04ca23f4 2018-07-16 stsp if (*child == NULL)
140 04ca23f4 2018-07-16 stsp return got_error_from_errno();
141 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
142 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
143 04ca23f4 2018-07-16 stsp free(*child);
144 04ca23f4 2018-07-16 stsp *child = NULL;
145 04ca23f4 2018-07-16 stsp return err;
147 04ca23f4 2018-07-16 stsp return NULL;
151 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
153 31cedeaf 2018-09-15 stsp return (path[0] == '/' && path[1] == '\0');
157 a129376b 2019-03-28 stsp got_path_is_current_dir(const char *path)
159 a129376b 2019-03-28 stsp return (path[0] == '.' && path[1] == '\0');
163 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
165 8da9e5f4 2019-01-12 stsp if (parent_len == 0)
168 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
170 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
177 1beed999 2019-01-12 stsp got_path_cmp(const char *path1, const char *path2)
179 e0159033 2019-01-08 stsp size_t len1 = strlen(path1);
180 e0159033 2019-01-08 stsp size_t len2 = strlen(path2);
181 e0159033 2019-01-08 stsp size_t min_len = MIN(len1, len2);
182 e0159033 2019-01-08 stsp size_t i = 0;
184 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
185 e08cc72d 2019-02-05 stsp while (path1[0] == '/')
187 e08cc72d 2019-02-05 stsp while (path2[0] == '/')
190 e08cc72d 2019-02-05 stsp len1 = strlen(path1);
191 e08cc72d 2019-02-05 stsp len2 = strlen(path2);
192 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
194 e0159033 2019-01-08 stsp /* Skip over common prefix. */
195 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
198 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
199 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
202 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
203 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
205 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
208 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
209 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
211 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
214 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
215 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
217 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
219 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
221 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
224 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
225 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
228 e08cc72d 2019-02-05 stsp const struct got_error *
229 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
230 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
232 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
234 7e5c804b 2019-02-05 stsp if (inserted)
235 7e5c804b 2019-02-05 stsp *inserted = NULL;
237 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
238 e08cc72d 2019-02-05 stsp if (new == NULL)
239 e08cc72d 2019-02-05 stsp return got_error_from_errno();
240 e08cc72d 2019-02-05 stsp new->path = path;
241 3d8df59c 2019-02-05 stsp new->data = data;
244 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
245 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
246 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
247 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
248 e08cc72d 2019-02-05 stsp * time an element is inserted.
250 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
251 e08cc72d 2019-02-05 stsp while (pe) {
252 e08cc72d 2019-02-05 stsp int cmp = got_path_cmp(pe->path, path);
253 e08cc72d 2019-02-05 stsp if (cmp == 0) {
254 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
255 e08cc72d 2019-02-05 stsp return NULL;
256 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
257 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
258 7e5c804b 2019-02-05 stsp if (inserted)
259 7e5c804b 2019-02-05 stsp *inserted = new;
260 e08cc72d 2019-02-05 stsp return NULL;
262 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
265 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
266 7e5c804b 2019-02-05 stsp if (inserted)
267 7e5c804b 2019-02-05 stsp *inserted = new;
268 e08cc72d 2019-02-05 stsp return NULL;
272 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
274 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
276 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
277 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
282 0cd1c46a 2019-03-11 stsp static const struct got_error *
283 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
285 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
286 d1667f0d 2019-03-11 stsp char *parent;
288 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
290 d1667f0d 2019-03-11 stsp return err;
292 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
293 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
294 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
297 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
298 5e1c9f23 2019-03-11 stsp err = got_error_from_errno();
302 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
305 5e1c9f23 2019-03-11 stsp free(parent);
306 0cd1c46a 2019-03-11 stsp return err;
309 0cd1c46a 2019-03-11 stsp const struct got_error *
310 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
312 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
314 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
315 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
316 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
319 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
320 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
322 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
326 0cd1c46a 2019-03-11 stsp return err;
329 d1667f0d 2019-03-11 stsp const struct got_error *
330 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
334 d1667f0d 2019-03-11 stsp p = dirname(path);
335 d1667f0d 2019-03-11 stsp if (p == NULL)
336 d1667f0d 2019-03-11 stsp return got_error_from_errno();
338 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
339 d1667f0d 2019-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
341 d1667f0d 2019-03-11 stsp *parent = strdup(p);
342 d1667f0d 2019-03-11 stsp if (*parent == NULL)
343 d1667f0d 2019-03-11 stsp return got_error_from_errno();
345 d1667f0d 2019-03-11 stsp return NULL;