Blob


1 /*
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>
5 *
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.
9 *
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.
17 */
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <libgen.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <paths.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_path.h"
37 #ifndef MIN
38 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
39 #endif
41 int
42 got_path_is_absolute(const char *path)
43 {
44 return path[0] == '/';
45 }
47 /* based on canonpath() from kern_pledge.c */
48 const struct got_error *
49 got_canonpath(const char *input, char *buf, size_t bufsize)
50 {
51 const char *p;
52 char *q;
54 /* can't canon relative paths, don't bother */
55 if (!got_path_is_absolute(input)) {
56 if (strlcpy(buf, input, bufsize) >= bufsize)
57 return got_error(GOT_ERR_NO_SPACE);
58 return NULL;
59 }
61 p = input;
62 q = buf;
63 while (*p && (q - buf < bufsize)) {
64 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
65 p += 1;
67 } else if (p[0] == '/' && p[1] == '.' &&
68 (p[2] == '/' || p[2] == '\0')) {
69 p += 2;
71 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
72 (p[3] == '/' || p[3] == '\0')) {
73 p += 3;
74 if (q != buf) /* "/../" at start of buf */
75 while (*--q != '/')
76 continue;
78 } else {
79 *q++ = *p++;
80 }
81 }
82 if ((*p == '\0') && (q - buf < bufsize)) {
83 *q = 0;
84 return NULL;
85 } else
86 return got_error(GOT_ERR_NO_SPACE);
87 }
89 const struct got_error *
90 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
91 const char *abspath)
92 {
93 const struct got_error *err = NULL;
94 size_t len_parent, len, bufsize;
96 *child = NULL;
98 len_parent = strlen(parent_abspath);
99 len = strlen(abspath);
100 if (len_parent >= len)
101 return got_error_path(abspath, GOT_ERR_BAD_PATH);
102 if (strncmp(parent_abspath, abspath, len_parent) != 0)
103 return got_error_path(abspath, GOT_ERR_BAD_PATH);
104 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
105 return got_error_path(abspath, GOT_ERR_BAD_PATH);
106 while (abspath[len_parent] == '/')
107 abspath++;
108 bufsize = len - len_parent + 1;
109 *child = malloc(bufsize);
110 if (*child == NULL)
111 return got_error_from_errno("malloc");
112 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
113 err = got_error_from_errno("strlcpy");
114 free(*child);
115 *child = NULL;
116 return err;
118 return NULL;
121 int
122 got_path_is_root_dir(const char *path)
124 while (*path == '/')
125 path++;
126 return (*path == '\0');
129 int
130 got_path_is_current_dir(const char *path)
132 return (path[0] == '.' && path[1] == '\0');
135 int
136 got_path_is_child(const char *child, const char *parent, size_t parent_len)
138 if (parent_len == 0 || got_path_is_root_dir(parent))
139 return 1;
141 if (strncmp(parent, child, parent_len) != 0)
142 return 0;
143 if (child[parent_len] != '/')
144 return 0;
146 return 1;
149 int
150 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
152 size_t min_len;
153 size_t i = 0;
155 /* Leading directory separators are insignificant. */
156 while (path1[0] == '/') {
157 path1++;
158 len1--;
160 while (path2[0] == '/') {
161 path2++;
162 len2--;
165 min_len = MIN(len1, len2);
167 /* Skip over common prefix. */
168 while (i < min_len && path1[i] == path2[i])
169 i++;
171 /* Are the paths exactly equal (besides path separators)? */
172 if (len1 == len2 && i >= min_len)
173 return 0;
175 /* Skip over redundant trailing path seperators. */
176 while (path1[i] == '/' && path1[i + 1] == '/')
177 path1++;
178 while (path2[i] == '/' && path2[i + 1] == '/')
179 path2++;
181 /* Trailing path separators are insignificant. */
182 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
183 return 0;
184 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
185 return 0;
187 /* Order children in subdirectories directly after their parents. */
188 if (path1[i] == '/' && path2[i] == '\0')
189 return 1;
190 if (path2[i] == '/' && path1[i] == '\0')
191 return -1;
192 if (path1[i] == '/' && path2[i] != '\0')
193 return -1;
194 if (path2[i] == '/' && path1[i] != '\0')
195 return 1;
197 /* Next character following the common prefix determines order. */
198 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
201 const struct got_error *
202 got_pathlist_insert(struct got_pathlist_entry **inserted,
203 struct got_pathlist_head *pathlist, const char *path, void *data)
205 struct got_pathlist_entry *new, *pe;
207 if (inserted)
208 *inserted = NULL;
210 new = malloc(sizeof(*new));
211 if (new == NULL)
212 return got_error_from_errno("malloc");
213 new->path = path;
214 new->path_len = strlen(path);
215 new->data = data;
217 /*
218 * Many callers will provide paths in a somewhat sorted order while
219 * constructing a path list from inputs such as tree objects or
220 * dirents. Iterating backwards from the tail of the list should
221 * be more efficient than traversing through the entire list each
222 * time an element is inserted.
223 */
224 pe = TAILQ_LAST(pathlist, got_pathlist_head);
225 while (pe) {
226 int cmp = got_path_cmp(pe->path, new->path,
227 pe->path_len, new->path_len);
228 if (cmp == 0) {
229 free(new); /* duplicate */
230 return NULL;
231 } else if (cmp < 0) {
232 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
233 if (inserted)
234 *inserted = new;
235 return NULL;
237 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
240 TAILQ_INSERT_HEAD(pathlist, new, entry);
241 if (inserted)
242 *inserted = new;
243 return NULL;
246 const struct got_error *
247 got_pathlist_append(struct got_pathlist_head *pathlist,
248 const char *path, void *data)
250 struct got_pathlist_entry *new;
252 new = malloc(sizeof(*new));
253 if (new == NULL)
254 return got_error_from_errno("malloc");
255 new->path = path;
256 new->path_len = strlen(path);
257 new->data = data;
258 TAILQ_INSERT_TAIL(pathlist, new, entry);
259 return NULL;
262 void
263 got_pathlist_free(struct got_pathlist_head *pathlist)
265 struct got_pathlist_entry *pe;
267 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
268 TAILQ_REMOVE(pathlist, pe, entry);
269 free(pe);
273 static const struct got_error *
274 make_parent_dirs(const char *abspath)
276 const struct got_error *err = NULL;
277 char *parent;
279 err = got_path_dirname(&parent, abspath);
280 if (err)
281 return err;
283 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
284 if (errno == ENOENT) {
285 err = make_parent_dirs(parent);
286 if (err)
287 goto done;
288 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
289 err = got_error_from_errno2("mkdir", parent);
290 goto done;
292 } else
293 err = got_error_from_errno2("mkdir", parent);
295 done:
296 free(parent);
297 return err;
300 const struct got_error *
301 got_path_mkdir(const char *abspath)
303 const struct got_error *err = NULL;
305 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
306 if (errno == ENOENT) {
307 err = make_parent_dirs(abspath);
308 if (err)
309 goto done;
310 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
311 err = got_error_from_errno2("mkdir", abspath);
312 } else
313 err = got_error_from_errno2("mkdir", abspath);
316 done:
317 return err;
320 int
321 got_path_dir_is_empty(const char *dir)
323 DIR *d;
324 struct dirent *dent;
325 int empty = 1;
327 d = opendir(dir);
328 if (d == NULL)
329 return 1;
331 while ((dent = readdir(d)) != NULL) {
332 if (strcmp(dent->d_name, ".") == 0 ||
333 strcmp(dent->d_name, "..") == 0)
334 continue;
336 empty = 0;
337 break;
340 closedir(d);
341 return empty;
344 const struct got_error *
345 got_path_dirname(char **parent, const char *path)
347 char buf[PATH_MAX];
348 char *p;
350 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
351 return got_error(GOT_ERR_NO_SPACE);
353 p = dirname(buf);
354 if (p == NULL)
355 return got_error_from_errno2("dirname", path);
357 if (p[0] == '.' && p[1] == '\0')
358 return got_error_path(path, GOT_ERR_BAD_PATH);
360 *parent = strdup(p);
361 if (*parent == NULL)
362 return got_error_from_errno("strdup");
364 return NULL;
367 const struct got_error *
368 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
370 const struct got_error *err = NULL;
371 char *path_child;
372 struct stat sb;
374 if (dent->d_type != DT_UNKNOWN) {
375 *type = dent->d_type;
376 return NULL;
379 *type = DT_UNKNOWN;
381 /*
382 * This is a fallback to accommodate filesystems which do not
383 * provide directory entry type information. DT_UNKNOWN directory
384 * entries occur on NFS mounts without "readdir plus" RPC.
385 */
387 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
388 return got_error_from_errno("asprintf");
390 if (lstat(path_child, &sb) == -1) {
391 err = got_error_from_errno2("lstat", path_child);
392 goto done;
395 if (S_ISFIFO(sb.st_mode))
396 *type = DT_FIFO;
397 else if (S_ISCHR(sb.st_mode))
398 *type = DT_CHR;
399 else if (S_ISDIR(sb.st_mode))
400 *type = DT_DIR;
401 else if (S_ISBLK(sb.st_mode))
402 *type = DT_BLK;
403 else if (S_ISLNK(sb.st_mode))
404 *type = DT_LNK;
405 else if (S_ISREG(sb.st_mode))
406 *type = DT_REG;
407 else if (S_ISSOCK(sb.st_mode))
408 *type = DT_SOCK;
409 done:
410 free(path_child);
411 return err;
414 const struct got_error *
415 got_path_basename(char **s, const char *path)
417 char buf[PATH_MAX];
418 char *base;
420 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
421 return got_error(GOT_ERR_NO_SPACE);
423 base = basename(buf);
424 if (base == NULL)
425 return got_error_from_errno2("basename", path);
427 *s = strdup(base);
428 if (*s == NULL)
429 return got_error_from_errno("strdup");
431 return NULL;
434 void
435 got_path_strip_trailing_slashes(char *path)
437 size_t x;
439 x = strlen(path);
440 while (x-- > 0 && path[x] == '/')
441 path[x] = '\0';
444 /* based on findprog() from usr.sbin/which/which.c */
445 const struct got_error *
446 got_path_find_prog(char **filename, const char *prog)
448 const struct got_error *err = NULL;
449 char *p;
450 int len;
451 struct stat sbuf;
452 char *path, *pathcpy;
454 *filename = NULL;
456 path = getenv("PATH");
457 if (path == NULL)
458 path = _PATH_DEFPATH;
460 /* Special case if prog contains '/' */
461 if (strchr(prog, '/')) {
462 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
463 access(prog, X_OK) == 0) {
464 *filename = strdup(prog);
465 if (*filename == NULL)
466 return got_error_from_errno("strdup");
468 return NULL;
471 if ((path = strdup(path)) == NULL)
472 return got_error_from_errno("strdup");
473 pathcpy = path;
475 while ((p = strsep(&pathcpy, ":")) != NULL) {
476 if (*p == '\0')
477 p = ".";
479 len = strlen(p);
480 while (len > 0 && p[len-1] == '/')
481 p[--len] = '\0'; /* strip trailing '/' */
483 if (asprintf(filename, "%s/%s", p, prog) == -1) {
484 err = got_error_from_errno("asprintf");
485 break;
487 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
488 access(*filename, X_OK) == 0)
489 break;
490 free(*filename);
491 *filename = NULL;
492 continue;
494 free(path);
495 return err;
498 const struct got_error *
499 got_path_create_file(const char *path, const char *content)
501 const struct got_error *err = NULL;
502 int fd = -1;
504 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
505 GOT_DEFAULT_FILE_MODE);
506 if (fd == -1) {
507 err = got_error_from_errno2("open", path);
508 goto done;
511 if (content) {
512 int len = dprintf(fd, "%s\n", content);
513 if (len != strlen(content) + 1) {
514 err = got_error_from_errno("dprintf");
515 goto done;
519 done:
520 if (fd != -1 && close(fd) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 return err;