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>
4 0ee7065d 2019-05-13 stsp * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
6 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
7 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
8 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
10 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 e08cc72d 2019-02-05 stsp #include <sys/queue.h>
20 0cd1c46a 2019-03-11 stsp #include <sys/stat.h>
22 0cd1c46a 2019-03-11 stsp #include <errno.h>
23 2c7829a4 2019-06-17 stsp #include <fcntl.h>
24 4027f31a 2017-11-04 stsp #include <limits.h>
25 0cd1c46a 2019-03-11 stsp #include <libgen.h>
26 4027f31a 2017-11-04 stsp #include <stdlib.h>
27 4027f31a 2017-11-04 stsp #include <unistd.h>
28 4027f31a 2017-11-04 stsp #include <stdio.h>
29 4027f31a 2017-11-04 stsp #include <string.h>
30 3c45a30a 2019-05-12 jcs #include <dirent.h>
31 0ee7065d 2019-05-13 stsp #include <paths.h>
33 9d31a1d8 2018-03-11 stsp #include "got_error.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
37 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
43 4027f31a 2017-11-04 stsp return path[0] == '/';
47 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
49 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
50 4027f31a 2017-11-04 stsp char *abspath;
52 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
53 4027f31a 2017-11-04 stsp return NULL;
55 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
56 4027f31a 2017-11-04 stsp return NULL;
58 4027f31a 2017-11-04 stsp return abspath;
62 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
64 4027f31a 2017-11-04 stsp char *resolved;
66 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
67 4027f31a 2017-11-04 stsp if (resolved == NULL)
68 4027f31a 2017-11-04 stsp return NULL;
70 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
71 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
72 4027f31a 2017-11-04 stsp free(resolved);
73 4027f31a 2017-11-04 stsp resolved = abspath;
76 4027f31a 2017-11-04 stsp return resolved;
79 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
80 f7d20e89 2018-06-17 stsp const struct got_error *
81 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
83 e6eac3b8 2018-06-17 stsp const char *p;
86 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
87 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
88 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
89 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
90 f7d20e89 2018-06-17 stsp return NULL;
95 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
96 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
99 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
100 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
103 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
104 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
106 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
107 e6eac3b8 2018-06-17 stsp while (*--q != '/')
111 e6eac3b8 2018-06-17 stsp *q++ = *p++;
114 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
116 f7d20e89 2018-06-17 stsp return NULL;
118 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
121 04ca23f4 2018-07-16 stsp const struct got_error *
122 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
123 04ca23f4 2018-07-16 stsp const char *abspath)
125 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
126 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
128 db5ba8ed 2019-03-26 stsp *child = NULL;
130 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
131 04ca23f4 2018-07-16 stsp len = strlen(abspath);
132 04ca23f4 2018-07-16 stsp if (len_parent >= len)
133 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
134 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
135 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
136 eb4304b9 2019-05-09 stsp if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
137 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
138 5e3ce57a 2019-03-26 stsp while (abspath[len_parent] == '/')
140 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
141 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
142 04ca23f4 2018-07-16 stsp if (*child == NULL)
143 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
144 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
145 638f9024 2019-05-13 stsp err = got_error_from_errno("strlcpy");
146 04ca23f4 2018-07-16 stsp free(*child);
147 04ca23f4 2018-07-16 stsp *child = NULL;
148 04ca23f4 2018-07-16 stsp return err;
150 04ca23f4 2018-07-16 stsp return NULL;
154 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
156 31cedeaf 2018-09-15 stsp return (path[0] == '/' && path[1] == '\0');
160 a129376b 2019-03-28 stsp got_path_is_current_dir(const char *path)
162 a129376b 2019-03-28 stsp return (path[0] == '.' && path[1] == '\0');
166 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
168 eb4304b9 2019-05-09 stsp if (parent_len == 0 || got_path_is_root_dir(parent))
171 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
173 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
180 1beed999 2019-01-12 stsp got_path_cmp(const char *path1, const char *path2)
182 e0159033 2019-01-08 stsp size_t len1 = strlen(path1);
183 e0159033 2019-01-08 stsp size_t len2 = strlen(path2);
184 e0159033 2019-01-08 stsp size_t min_len = MIN(len1, len2);
185 e0159033 2019-01-08 stsp size_t i = 0;
187 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
188 e08cc72d 2019-02-05 stsp while (path1[0] == '/')
190 e08cc72d 2019-02-05 stsp while (path2[0] == '/')
193 e08cc72d 2019-02-05 stsp len1 = strlen(path1);
194 e08cc72d 2019-02-05 stsp len2 = strlen(path2);
195 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
197 e0159033 2019-01-08 stsp /* Skip over common prefix. */
198 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
201 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
202 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
205 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
206 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
208 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
211 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
212 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
214 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
217 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
218 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
220 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
222 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
224 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
227 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
228 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
231 e08cc72d 2019-02-05 stsp const struct got_error *
232 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
233 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
235 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
237 7e5c804b 2019-02-05 stsp if (inserted)
238 7e5c804b 2019-02-05 stsp *inserted = NULL;
240 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
241 e08cc72d 2019-02-05 stsp if (new == NULL)
242 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
243 e08cc72d 2019-02-05 stsp new->path = path;
244 3d8df59c 2019-02-05 stsp new->data = data;
247 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
248 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
249 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
250 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
251 e08cc72d 2019-02-05 stsp * time an element is inserted.
253 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
254 e08cc72d 2019-02-05 stsp while (pe) {
255 e08cc72d 2019-02-05 stsp int cmp = got_path_cmp(pe->path, path);
256 e08cc72d 2019-02-05 stsp if (cmp == 0) {
257 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
258 e08cc72d 2019-02-05 stsp return NULL;
259 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
260 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
261 7e5c804b 2019-02-05 stsp if (inserted)
262 7e5c804b 2019-02-05 stsp *inserted = new;
263 e08cc72d 2019-02-05 stsp return NULL;
265 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
268 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
269 7e5c804b 2019-02-05 stsp if (inserted)
270 7e5c804b 2019-02-05 stsp *inserted = new;
271 e08cc72d 2019-02-05 stsp return NULL;
275 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
277 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
279 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
280 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
285 0cd1c46a 2019-03-11 stsp static const struct got_error *
286 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
288 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
289 d1667f0d 2019-03-11 stsp char *parent;
291 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
293 d1667f0d 2019-03-11 stsp return err;
295 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
296 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
297 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
300 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
301 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
305 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
308 5e1c9f23 2019-03-11 stsp free(parent);
309 0cd1c46a 2019-03-11 stsp return err;
312 0cd1c46a 2019-03-11 stsp const struct got_error *
313 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
315 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
317 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
318 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
319 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
322 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
323 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
325 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
329 0cd1c46a 2019-03-11 stsp return err;
333 280f921b 2019-05-12 stsp got_path_dir_is_empty(const char *dir)
336 3c45a30a 2019-05-12 jcs struct dirent *dent;
337 3c45a30a 2019-05-12 jcs int empty = 1;
339 3c45a30a 2019-05-12 jcs d = opendir(dir);
340 3c45a30a 2019-05-12 jcs if (d == NULL)
343 3c45a30a 2019-05-12 jcs while ((dent = readdir(d)) != NULL) {
344 3c45a30a 2019-05-12 jcs if (strcmp(dent->d_name, ".") == 0 ||
345 3c45a30a 2019-05-12 jcs strcmp(dent->d_name, "..") == 0)
352 7f2a8dc2 2019-05-12 stsp closedir(d);
353 3c45a30a 2019-05-12 jcs return empty;
356 d1667f0d 2019-03-11 stsp const struct got_error *
357 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
361 d1667f0d 2019-03-11 stsp p = dirname(path);
362 d1667f0d 2019-03-11 stsp if (p == NULL)
363 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
365 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
366 d1667f0d 2019-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
368 d1667f0d 2019-03-11 stsp *parent = strdup(p);
369 d1667f0d 2019-03-11 stsp if (*parent == NULL)
370 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
372 d1667f0d 2019-03-11 stsp return NULL;
376 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(char *path)
380 72151b04 2019-05-11 stsp while (path[x = strlen(path) - 1] == '/')
381 72151b04 2019-05-11 stsp path[x] = '\0';
384 08680430 2019-05-13 stsp /* based on findprog() from usr.sbin/which/which.c */
385 0ee7065d 2019-05-13 stsp const struct got_error *
386 0ee7065d 2019-05-13 stsp got_path_find_prog(char **filename, const char *prog)
390 0ee7065d 2019-05-13 stsp struct stat sbuf;
391 0ee7065d 2019-05-13 stsp char *path, *pathcpy;
393 0ee7065d 2019-05-13 stsp *filename = NULL;
395 0ee7065d 2019-05-13 stsp path = getenv("PATH");
396 0ee7065d 2019-05-13 stsp if (path == NULL)
397 0ee7065d 2019-05-13 stsp path = _PATH_DEFPATH;
399 0ee7065d 2019-05-13 stsp /* Special case if prog contains '/' */
400 0ee7065d 2019-05-13 stsp if (strchr(prog, '/')) {
401 0ee7065d 2019-05-13 stsp if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
402 0ee7065d 2019-05-13 stsp access(prog, X_OK) == 0) {
403 0ee7065d 2019-05-13 stsp *filename = strdup(prog);
404 0ee7065d 2019-05-13 stsp if (*filename == NULL)
405 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
407 0ee7065d 2019-05-13 stsp return NULL;
410 0ee7065d 2019-05-13 stsp if ((path = strdup(path)) == NULL)
411 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
412 0ee7065d 2019-05-13 stsp pathcpy = path;
414 0ee7065d 2019-05-13 stsp while ((p = strsep(&pathcpy, ":")) != NULL) {
415 0ee7065d 2019-05-13 stsp if (*p == '\0')
418 0ee7065d 2019-05-13 stsp len = strlen(p);
419 0ee7065d 2019-05-13 stsp while (len > 0 && p[len-1] == '/')
420 0ee7065d 2019-05-13 stsp p[--len] = '\0'; /* strip trailing '/' */
422 0ee7065d 2019-05-13 stsp if (asprintf(filename, "%s/%s", p, prog) == -1) {
423 0ee7065d 2019-05-13 stsp free(path);
424 0ee7065d 2019-05-13 stsp return got_error_from_errno("asprintf");
426 0ee7065d 2019-05-13 stsp if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
427 0ee7065d 2019-05-13 stsp access(*filename, X_OK) == 0) {
428 0ee7065d 2019-05-13 stsp free(path);
429 0ee7065d 2019-05-13 stsp return NULL;
431 0ee7065d 2019-05-13 stsp free(*filename);
432 0ee7065d 2019-05-13 stsp *filename = NULL;
435 0ee7065d 2019-05-13 stsp free(path);
436 0ee7065d 2019-05-13 stsp return NULL;
439 2c7829a4 2019-06-17 stsp const struct got_error *
440 2c7829a4 2019-06-17 stsp got_path_create_file(const char *path, const char *content)
442 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
443 2c7829a4 2019-06-17 stsp int fd = -1;
445 2c7829a4 2019-06-17 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
446 2c7829a4 2019-06-17 stsp GOT_DEFAULT_FILE_MODE);
447 2c7829a4 2019-06-17 stsp if (fd == -1) {
448 2c7829a4 2019-06-17 stsp err = got_error_from_errno2("open", path);
452 2c7829a4 2019-06-17 stsp if (content) {
453 2c7829a4 2019-06-17 stsp int len = dprintf(fd, "%s\n", content);
454 2c7829a4 2019-06-17 stsp if (len != strlen(content) + 1) {
455 2c7829a4 2019-06-17 stsp err = got_error_from_errno("dprintf");
461 2c7829a4 2019-06-17 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
462 2c7829a4 2019-06-17 stsp err = got_error_from_errno("close");
463 2c7829a4 2019-06-17 stsp return err;